Skip to content

Commit

Permalink
add constructor that inputs packed vector
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarthur committed Feb 6, 2022
1 parent ebe760b commit 553c0f1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/SymmetricFormats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import LinearAlgebra: mul!, BLAS, BlasFloat, generic_matvecmul!, MulAddMul

export SymmetricPacked, packedsize

struct SymmetricPacked{T,S<:AbstractMatrix{<:T}} <: AbstractMatrix{T}
struct SymmetricPacked{T,S<:AbstractVecOrMat{<:T}} <: AbstractMatrix{T}
tri::Vector{T}
n::Int
uplo::Char

function SymmetricPacked{T,S}(tri, n, uplo) where {T,S<:AbstractMatrix{<:T}}
function SymmetricPacked{T,S}(tri, n, uplo) where {T,S<:AbstractVecOrMat{<:T}}
require_one_based_indexing(tri)
uplo=='U' || uplo=='L' || throw(ArgumentError("uplo must be either 'U' (upper) or 'L' (lower)"))
new{T,S}(tri, n, uplo)
Expand Down Expand Up @@ -72,6 +72,12 @@ function SymmetricPacked(x::SymmetricPacked{T,S}) where{T,S}
SymmetricPacked{T,S}(T.(x.tri), x.n, x.uplo)
end

function SymmetricPacked(V::AbstractVector{T}, uplo::Symbol=:U) where {T}
n = (sqrt(1+8*length(V))-1)/2
isinteger(n) || throw(DimensionMismatch("length of vector does not corresond to the number of elements in the triangle of a square matrix"))
SymmetricPacked{T,typeof(V)}(V, round(Int, n), char_uplo(uplo))
end

checksquare(x::SymmetricPacked) = x.n

convert(::Type{SymmetricPacked{T,S}}, x::SymmetricPacked) where {T,S} = SymmetricPacked{T,S}(T.(x.tri), x.n, x.uplo)
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ end
@test packedsize(AP) == 6
end

@testset "vector constructor" begin
AP = SymmetricPacked(A)
BP = SymmetricPacked(AP.tri)
@test AP == BP
end

VERSION<v"1.8.0-DEV.1049" && include("blas.jl")

0 comments on commit 553c0f1

Please sign in to comment.