Skip to content

Commit 553c0f1

Browse files
committed
add constructor that inputs packed vector
1 parent ebe760b commit 553c0f1

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/SymmetricFormats.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import LinearAlgebra: mul!, BLAS, BlasFloat, generic_matvecmul!, MulAddMul
77

88
export SymmetricPacked, packedsize
99

10-
struct SymmetricPacked{T,S<:AbstractMatrix{<:T}} <: AbstractMatrix{T}
10+
struct SymmetricPacked{T,S<:AbstractVecOrMat{<:T}} <: AbstractMatrix{T}
1111
tri::Vector{T}
1212
n::Int
1313
uplo::Char
1414

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

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

7783
convert(::Type{SymmetricPacked{T,S}}, x::SymmetricPacked) where {T,S} = SymmetricPacked{T,S}(T.(x.tri), x.n, x.uplo)

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ end
2929
@test packedsize(AP) == 6
3030
end
3131

32+
@testset "vector constructor" begin
33+
AP = SymmetricPacked(A)
34+
BP = SymmetricPacked(AP.tri)
35+
@test AP == BP
36+
end
37+
3238
VERSION<v"1.8.0-DEV.1049" && include("blas.jl")

0 commit comments

Comments
 (0)