From fa6433d48920fce43087ff1842ce749080dc56b3 Mon Sep 17 00:00:00 2001 From: Michael Klamkin Date: Sat, 14 Nov 2026 22:05:23 +0000 Subject: [PATCH 1/2] sparse: symmetric and Hermitian matrices from one triangle AASparseMatrix(Symmetric(S, uplo)) and AASparseMatrix(Hermitian(S, uplo)) store only that triangle, with the symmetric (Hermitian) attributes. factor(A) picks LDLT for a complex symmetric (not Hermitian) matrix stored as one triangle, as libSparse's LU does not read that storage correctly. Assisted-by: Claude Code:claude-opus-5-5 --- docs/src/sparse.md | 20 ++++++++++++++ src/sparse.jl | 40 ++++++++++++++++++++++++++- test/sparse_tests.jl | 65 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) diff --git a/docs/src/sparse.md b/docs/src/sparse.md index f5efcb98..b39ed3ed 100644 --- a/docs/src/sparse.md +++ b/docs/src/sparse.md @@ -76,6 +76,26 @@ triangular structure and sets the appropriate Apple Accelerate attributes. The C constructor uses Accelerate's `SparseConvertFromCoordinate` and accepts `Float32`/`Float64` and (macOS 15.5+) `ComplexF32`/`ComplexF64` values. +To give a symmetric or Hermitian matrix by one triangle, wrap it in `Symmetric` or +`Hermitian`: `AASparseMatrix(Symmetric(S, :L))` (or `:U`) stores only that triangle +of `S`, with the symmetric (Hermitian) attributes, without testing `S` for symmetry, +so the lower triangle of a symmetric matrix can be wrapped as is. As in `Symmetric`, +the other triangle of `S` is ignored, and as in `Hermitian`, so is the imaginary part +of a Hermitian matrix's diagonal. + +```@example sparse +using LinearAlgebra + +L = tril(sprandn(100, 100, 0.05) + 10I) # the lower triangle of a symmetric matrix +A = AASparseMatrix(Symmetric(L, :L)) +F = AppleAccelerate.factor(A, :ldlt) +nothing # hide +``` + +!!! note + libSparse's LU does not read one-triangle symmetric storage correctly, so use + Cholesky or LDLᵀ for such matrices; `factor(A)` picks one of them. + ### Matrix operations | Function | Description | diff --git a/src/sparse.jl b/src/sparse.jl index de248425..80990e80 100644 --- a/src/sparse.jl +++ b/src/sparse.jl @@ -945,6 +945,39 @@ function AASparseMatrix(sparseM::SparseMatrixCSC{Tv, Ti}, return AASparseMatrix(SparseMatrixCSC{_aa_eltype(Tv), Ti}(sparseM), attributes) end +""" + AASparseMatrix(S::Symmetric{<:Number, <:SparseMatrixCSC}) + AASparseMatrix(H::Hermitian{<:Number, <:SparseMatrixCSC}) + +Wrap a symmetric (or Hermitian) matrix given by one triangle. Only the triangle that +`S` refers to (`:L` or `:U`) is stored, with the symmetric (Hermitian) attributes, +as libSparse expects for Cholesky and LDLᵀ factorizations; the other triangle of the +parent matrix is ignored, as in `Symmetric`. As in `Hermitian`, so is the imaginary +part of a Hermitian matrix's diagonal. A real `Hermitian` matrix is symmetric. + +Unlike `AASparseMatrix(::SparseMatrixCSC)`, this does not test the matrix for +symmetry, so it also serves to wrap the lower triangle of a symmetric matrix as is. +""" +function AASparseMatrix(S::Union{Symmetric{<:Number, <:SparseMatrixCSC}, + Hermitian{<:Number, <:SparseMatrixCSC}}) + P = parent(S) + Tv = _aa_eltype(eltype(P)) + # the stored triangle, copied once into the libSparse element and index types + stored = S.uplo == 'L' ? tril(P) : triu(P) + vals = eltype(stored) === Tv ? nonzeros(stored) : Vector{Tv}(nonzeros(stored)) + hermitian = S isa Hermitian && Tv <: Complex + if hermitian + # as in `Hermitian`, the diagonal of a Hermitian matrix is real + for j in 1:size(stored, 2), k in nzrange(stored, j) + rowvals(stored)[k] == j && (vals[k] = real(vals[k])) + end + end + kind = hermitian ? ATT_HERMITIAN : ATT_SYMMETRIC + triangle = S.uplo == 'L' ? ATT_LOWER_TRIANGLE : ATT_UPPER_TRIANGLE + return AASparseMatrix(size(stored)..., Clong.(stored.colptr .- 1), + Cint.(rowvals(stored) .- 1), vals, kind | triangle) +end + """ AASparseMatrix(I, J, V, m, n, attributes = ATT_ORDINARY) @@ -1269,7 +1302,9 @@ computed. !!! note "Complex symmetric (not Hermitian)" When the user leaves the factorization type unspecified and passes - a complex symmetric (but not Hermitian) matrix, we default to LU factorization. + a complex symmetric (but not Hermitian) matrix, we default to LU factorization, + unless the matrix is stored as one triangle (`AASparseMatrix(Symmetric(S))`): + libSparse's LU does not read that storage correctly, so it gets `LDLᵀ`. On older versions of MacOS, Apple's `SparseFactorizationLDLT` errors on complex symmetric (non-Hermitian) matrices, reporting "Cannot @@ -1286,7 +1321,10 @@ function factor!(aa_fact::AAFactorization{T}, # non-Hermitian (macOS 15.5+); QR otherwise. nrow, ncol = size(aa_fact.matrixObj) lu_ok = something(_macos_version[], v"0.0.0") >= v"15.5" + # A complex symmetric (not Hermitian) matrix stored as one triangle goes + # to LDLᵀ: libSparse's LU does not read the symmetric storage correctly. kind = ishermitian(aa_fact.matrixObj) ? SparseFactorizationCholesky : + issymmetric(aa_fact.matrixObj) ? SparseFactorizationLDLT : (nrow == ncol && lu_ok) ? SparseFactorizationLU : SparseFactorizationQR end diff --git a/test/sparse_tests.jl b/test/sparse_tests.jl index e18bd0f8..0cdc8c00 100644 --- a/test/sparse_tests.jl +++ b/test/sparse_tests.jl @@ -1596,4 +1596,69 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac @test mul!(copy(yc), M, xc, 2 - im, 0.5im) ≈ (2 - im) * (H * xc) + 0.5im * yc end end + + @testset "symmetric from one triangle $T" for T in (Float32, Float64) + AA = AppleAccelerate + n = 40 + K = spdiagm(-1 => ones(n - 1), 0 => [(-1)^i * (2 + i / n) for i in 1:n], 1 => ones(n - 1), + -7 => fill(0.25, n - 7), 7 => fill(0.25, n - 7)) + K = SparseMatrixCSC{T,Int64}(K) + b = rand(T, n) + tol = T == Float32 ? 1e-3 : 1e-8 + half = (positive = n ÷ 2, zero = 0, negative = n ÷ 2) + for (S, stored) in ((Symmetric(tril(K), :L), tril(K)), (Symmetric(K, :U), triu(K)), + (Hermitian(K, :L), tril(K)), (Symmetric(SparseMatrixCSC{T,Int32}(K), :L), tril(K))) + A = AASparseMatrix(S) + @test issymmetric(A) + @test SparseMatrixCSC(A) == K # the logical matrix + @test A._nzval == nonzeros(stored) # one stored triangle + @test A * b ≈ K * b + f = AA.factor(A, :ldlt) + @test AA.inertia(f) == half + @test Matrix(K) * AA.solve(f, b) ≈ b rtol = tol + end + # other element types are converted + @test SparseMatrixCSC(AASparseMatrix(Symmetric(SparseMatrixCSC{Int,Int64}(round.(Int, 4 * K)), :L))) == + round.(Int, 4 * K) + # the other triangle of the parent is ignored, as in Symmetric + J = copy(K); J[1, n] = 99 + @test SparseMatrixCSC(AASparseMatrix(Symmetric(J, :L))) == K + end + + something(AppleAccelerate.get_macos_version(), v"0.0.0") >= v"15.5" && + @testset "Hermitian from one triangle $T" for T in (ComplexF32, ComplexF64) + AA = AppleAccelerate + n = 20 + H = SparseMatrixCSC{T,Int64}(spdiagm(0 => fill(4.0, n)) + spdiagm(1 => fill(im, n - 1)) - spdiagm(-1 => fill(im, n - 1))) + b = rand(T, n) + tol = real(T) == Float32 ? 1e-3 : 1e-8 + A = AASparseMatrix(Hermitian(H, :L)) + @test SparseMatrixCSC(A) == H + @test ishermitian(A) + f = AA.factor(A) # :auto: Hermitian, so Cholesky + @test Matrix(H) * AA.solve(f, b) ≈ b rtol = tol + @test AA.inertia(AA.factor(A, :ldlt)) == (positive = n, zero = 0, negative = 0) + # as in Hermitian, the imaginary part of the stored diagonal is ignored + G = copy(H); for i in 1:n; G[i, i] += 0.5im; end + A = AASparseMatrix(Hermitian(G, :L)) + @test SparseMatrixCSC(A) == sparse(Hermitian(G, :L)) + @test A * b ≈ Hermitian(Matrix(G), :L) * b + @test Matrix(H) * AA.solve(AA.factor(A, :ldlt), b) ≈ b rtol = tol + end + + something(AppleAccelerate.get_macos_version(), v"0.0.0") >= v"26" && + @testset "complex symmetric (not Hermitian) from one triangle $T" for T in (ComplexF32, ComplexF64) + AA = AppleAccelerate + n = 20 + C = SparseMatrixCSC{T,Int64}(spdiagm(0 => fill(4.0 + 1im, n), 1 => fill(1.0im, n - 1), -1 => fill(1.0im, n - 1))) + @test issymmetric(C) && !ishermitian(C) + b = rand(T, n) + tol = real(T) == Float32 ? 1e-3 : 1e-8 + A = AASparseMatrix(Symmetric(C, :L)) + @test SparseMatrixCSC(A) == C + f = AA.factor(A) # :auto: LDLᵀ, not LU + @test f._factorization.symbolicFactorization.type in (AA.SparseFactorizationLDLT, AA.SparseFactorizationLDLTTPP) + @test Matrix(C) * AA.solve(f, b) ≈ b rtol = tol + @test Matrix(C) * AA.solve(AA.factor(A, :ldlt), b) ≈ b rtol = tol + end end From c57ba0653deb06ed0500b640bf3dca42f89cd753 Mon Sep 17 00:00:00 2001 From: Michael Klamkin Date: Sun, 15 Nov 2026 21:09:11 +0000 Subject: [PATCH 2/2] sparse: AASparseMatrix(::SparseMatrixCSC) stores the matrix as it is The constructor no longer tests the matrix for symmetric or triangular structure. Symmetric storage comes from Symmetric/Hermitian or the attributes. Like factorize, the SparseMatrixCSC conveniences (factor, solve, AAFactorization, refactor!) still test the matrix, and store a Hermitian (or real symmetric) one as its lower triangle for Cholesky, LDLT and :auto only, so LU and QR get the full storage. Breaking, so the version is 0.9.0. Assisted-by: Claude Code:claude-opus-5-5 --- Project.toml | 2 +- docs/src/index.md | 2 +- docs/src/sparse.md | 33 ++++++++++++-------- src/sparse.jl | 68 +++++++++++++++++++++++++++++------------- test/sparse_tests.jl | 71 ++++++++++++++++++++++++++++++++++---------- 5 files changed, 126 insertions(+), 50 deletions(-) diff --git a/Project.toml b/Project.toml index 4fde5089..e83167ce 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "AppleAccelerate" uuid = "13e28ba4-7ad8-5781-acae-3021b1ed3924" -version = "0.8.0" +version = "0.9.0" [deps] Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" diff --git a/docs/src/index.md b/docs/src/index.md index b5296a43..fedb12e1 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -87,7 +87,7 @@ nothing # hide ```@example quickstart-sparse using AppleAccelerate, LinearAlgebra, SparseArrays S = sprandn(500, 500, 0.01); S = S*S' + 500I # symmetric positive-definite -As = AppleAccelerate.AASparseMatrix(SparseMatrixCSC{Float64,Int64}(S)) +As = AppleAccelerate.AASparseMatrix(Symmetric(SparseMatrixCSC{Float64,Int64}(S), :L)) xs = AppleAccelerate.solve(AppleAccelerate.cholesky(As), randn(500)) nothing # hide ``` diff --git a/docs/src/sparse.md b/docs/src/sparse.md index b39ed3ed..94157c2e 100644 --- a/docs/src/sparse.md +++ b/docs/src/sparse.md @@ -25,8 +25,10 @@ g = AppleAccelerate.factor(S, :ldlt) # pick the kind: :cholesky, AppleAccelerate.inertia(g) # pivot signs of the LDLᵀ ``` -`factor(A)` picks Cholesky for a Hermitian matrix, LU for a square one (macOS -15.5+) and QR otherwise. Element types other than `Float32`/`Float64`/`ComplexF32`/ +`factor(S)` picks Cholesky for a Hermitian matrix, LU for a square one (macOS +15.5+) and QR otherwise. Like `factorize`, `factor` and `solve` test a +`SparseMatrixCSC` for symmetry, and for Cholesky and LDLᵀ store a symmetric +(Hermitian) one as its lower triangle. Element types other than `Float32`/`Float64`/`ComplexF32`/ `ComplexF64` are converted to `Float64`/`ComplexF64`, and `solve` promotes the matrix and right-hand side to a common type. @@ -71,15 +73,16 @@ julia> SparseMatrixCSC(B) == sparse(rows, cols, vals, 3, 3) true ``` -The `SparseMatrixCSC` constructor automatically detects symmetric/Hermitian and -triangular structure and sets the appropriate Apple Accelerate attributes. The COO +The `SparseMatrixCSC` constructor stores the matrix as it is, without testing it +for structure; pass `attributes` (e.g. `ATT_TRI_LOWER`) to set it. The COO constructor uses Accelerate's `SparseConvertFromCoordinate` and accepts `Float32`/`Float64` and (macOS 15.5+) `ComplexF32`/`ComplexF64` values. -To give a symmetric or Hermitian matrix by one triangle, wrap it in `Symmetric` or -`Hermitian`: `AASparseMatrix(Symmetric(S, :L))` (or `:U`) stores only that triangle -of `S`, with the symmetric (Hermitian) attributes, without testing `S` for symmetry, -so the lower triangle of a symmetric matrix can be wrapped as is. As in `Symmetric`, +A symmetric or Hermitian matrix, which Cholesky and LDLᵀ need, is given by one +triangle, wrapped in `Symmetric` or `Hermitian`: `AASparseMatrix(Symmetric(S, :L))` +(or `:U`) stores only that triangle of `S`, with the symmetric (Hermitian) +attributes, without testing `S` for symmetry, so the lower triangle of a symmetric +matrix can be wrapped as is. As in `Symmetric`, the other triangle of `S` is ignored, and as in `Hermitian`, so is the imaginary part of a Hermitian matrix's diagonal. @@ -96,6 +99,11 @@ nothing # hide libSparse's LU does not read one-triangle symmetric storage correctly, so use Cholesky or LDLᵀ for such matrices; `factor(A)` picks one of them. +!!! compat "AppleAccelerate 0.9" + Until 0.9, `AASparseMatrix(::SparseMatrixCSC)` tested the matrix and stored a + symmetric (Hermitian) one as its lower triangle, and tagged a triangular one. + Wrap it in `Symmetric` or `Hermitian` instead. + ### Matrix operations | Function | Description | @@ -147,8 +155,8 @@ Complex matrices differ from real ones in two ways: - **Cholesky and CG use the Hermitian path.** For a complex matrix, `cholesky(A)` and `solve(A, b; method = :cg)` require `A` to be **Hermitian** positive-definite (`A == A'`), and dispatch to libSparse's Hermitian factorization — not the - (complex-)symmetric one. A `SparseMatrixCSC{Complex}` that satisfies `ishermitian` - is auto-tagged Hermitian when wrapped. + (complex-)symmetric one. Wrap a `SparseMatrixCSC{Complex}` in `Hermitian` to store + it as Hermitian: `AASparseMatrix(Hermitian(S, :L))`. - **`adjoint` (`A'`) and `transpose` are distinct.** `adjoint` conjugates as well as transposes; both are attribute-flag views that share the underlying CSC data. @@ -157,7 +165,7 @@ using AppleAccelerate, SparseArrays, LinearAlgebra import AppleAccelerate: AASparseMatrix, cholesky, solve M = sprandn(ComplexF64, 60, 60, 0.05) -H = AASparseMatrix(SparseMatrixCSC(M * M' + 60I)) # Hermitian positive-definite +H = AASparseMatrix(Hermitian(M * M' + 60I, :L)) # Hermitian positive-definite b = randn(ComplexF64, 60) x = solve(cholesky(H), b) # Hermitian Cholesky @assert x ≈ Matrix(H) \ b @@ -229,7 +237,8 @@ G = qr(A) # QR (also works for rectangular / least-squares) nothing # hide ``` -`cholesky(A)` and `ldlt(A)` are available for symmetric/Hermitian matrices. +`cholesky(A)` and `ldlt(A)` are available for matrices stored as symmetric (Hermitian), +e.g. `cholesky(AASparseMatrix(Symmetric(S, :L)))`. !!! note "No type piracy" These methods dispatch on `AppleAccelerate`'s own `AASparseMatrix`, **not** on diff --git a/src/sparse.jl b/src/sparse.jl index 80990e80..03579dba 100644 --- a/src/sparse.jl +++ b/src/sparse.jl @@ -913,14 +913,6 @@ end function AASparseMatrix(sparseM::SparseMatrixCSC{T, <:Integer}, attributes::att_type = ATT_ORDINARY) where T<:vTypes - if attributes == ATT_ORDINARY - if ishermitian(sparseM) - kind = T <: Complex ? ATT_HERMITIAN : ATT_SYMMETRIC - return AASparseMatrix(tril(sparseM), kind | ATT_LOWER_TRIANGLE) - elseif istril(sparseM) || istriu(sparseM) - attributes = istril(sparseM) ? ATT_TRI_LOWER : ATT_TRI_UPPER - end - end # NOTE: we deliberately do NOT auto-tag unit-triangular matrices. libSparse # treats ATT_UNIT_TRIANGULAR as an *implicit* unit diagonal, but we keep the # stored diagonal in the CSC data, so tagging it would double-count the @@ -955,8 +947,9 @@ as libSparse expects for Cholesky and LDLᵀ factorizations; the other triangle parent matrix is ignored, as in `Symmetric`. As in `Hermitian`, so is the imaginary part of a Hermitian matrix's diagonal. A real `Hermitian` matrix is symmetric. -Unlike `AASparseMatrix(::SparseMatrixCSC)`, this does not test the matrix for -symmetry, so it also serves to wrap the lower triangle of a symmetric matrix as is. +This is how to get symmetric storage, which Cholesky and LDLᵀ factorizations need: +`AASparseMatrix(::SparseMatrixCSC)` stores the matrix as it is. The matrix is not +tested for symmetry, so the lower triangle of a symmetric matrix can be wrapped as is. """ function AASparseMatrix(S::Union{Symmetric{<:Number, <:SparseMatrixCSC}, Hermitian{<:Number, <:SparseMatrixCSC}}) @@ -1190,8 +1183,10 @@ Create via `f = AAFactorization(A::SparseMatrixCSC{T, Int64})`. Calls to `solve` `ldiv`, and their in-place versions require explicitly passing in the factorization object as the first argument. On construction, the struct stores a placeholder yet-to-be-factored object: the factorization is computed upon the first call -to `solve`, or by explicitly calling `factor!`. If the matrix is symmetric, it defaults to -a Cholesky factorization; otherwise, it defaults to QR. +to `solve`, or by explicitly calling `factor!`. If the matrix is stored as symmetric +(Hermitian), it defaults to a Cholesky factorization; otherwise, to LU for a square +matrix (macOS 15.5+) and QR for others. `AAFactorization(::SparseMatrixCSC)` stores a +Hermitian (or real symmetric) matrix so, as [`factor`](@ref) does. """ mutable struct AAFactorization{T<:vTypes} <: LinearAlgebra.Factorization{T} matrixObj::AASparseMatrix{T} @@ -1214,7 +1209,22 @@ end LinearAlgebra.factorize(A::AASparseMatrix{T}) where T<:vTypes = AAFactorization(A) # julia's LinearAlgebra module doesn't provide similar constructors. -AAFactorization(M::SparseMatrixCSC) = AAFactorization(AASparseMatrix(M)) +AAFactorization(M::SparseMatrixCSC) = AAFactorization(_detect_structure(M, SparseFactorizationTBD)) + +# The `AASparseMatrix` that the `SparseMatrixCSC` conveniences (`factor`, `solve`, +# `AAFactorization`, `refactor!`) factor with `kind`: like `factorize`, they test the +# matrix, and store a Hermitian (or real symmetric) matrix as its lower triangle for the +# factorizations that use it: Cholesky and LDLᵀ, and `:auto`, which then picks Cholesky. +# A complex symmetric (not Hermitian) matrix is stored so only for an explicit LDLᵀ. +function _detect_structure(S::SparseMatrixCSC, kind::SparseFactorization_t) + symmetric_kind = kind == SparseFactorizationTBD || _refactor_family(kind) == :Symmetric + if symmetric_kind && ishermitian(S) + return AASparseMatrix(eltype(S) <: Complex ? Hermitian(S, :L) : Symmetric(S, :L)) + elseif symmetric_kind && kind != SparseFactorizationTBD && issymmetric(S) + return AASparseMatrix(Symmetric(S, :L)) + end + return AASparseMatrix(S) +end # ============================================================ # Low-level numeric refactorization kernels @@ -1491,8 +1501,16 @@ function refactor!(aa_fact::AAFactorization{T}, A::AASparseMatrix{T}) where T<:v return aa_fact end -refactor!(aa_fact::AAFactorization{T}, A::SparseMatrixCSC{T, Int64}) where T<:vTypes = - refactor!(aa_fact, AASparseMatrix(A)) +# stored as the matrix that `aa_fact` holds: one triangle of a symmetric (Hermitian) matrix, +# or both +function refactor!(aa_fact::AAFactorization{T}, A::SparseMatrixCSC{T, Int64}) where T<:vTypes + old = aa_fact.matrixObj + uplo = (old.matrix.structure.attributes & ATT_TRIANGLE_MASK) == ATT_UPPER_TRIANGLE ? :U : :L + B = ishermitian(old) && T <: Complex ? AASparseMatrix(Hermitian(A, uplo)) : + issymmetric(old) ? AASparseMatrix(Symmetric(A, uplo)) : + AASparseMatrix(A) + return refactor!(aa_fact, B) +end # LinearAlgebra-style spellings of `refactor!`, matching how SparseArrays exposes # symbolic-factorization reuse (`lu!(F, A)` for UMFPACK, `cholesky!(F, A)` / @@ -1878,7 +1896,11 @@ end function solve(A::SparseMatrixCSC{Tv,Ti}, b::AbstractVecOrMat{<:Number}; kw...) where {Tv<:Number, Ti<:Integer} T = _aa_eltype(promote_type(Tv, eltype(b))) - AA = AASparseMatrix(Tv === T ? A : SparseMatrixCSC{T,Ti}(A)) + S = Tv === T ? A : SparseMatrixCSC{T,Ti}(A) + # the storage of the factorization for a direct solve; for an iterative one, as for `:auto` + kind = get(kw, :method, :direct) === :direct ? _factor_kind(get(kw, :kind, :auto)) : + SparseFactorizationTBD + AA = _detect_structure(S, kind) return solve(AA, b isa StridedVecOrMat{T} ? b : Array{T}(b); kw...) end @@ -2288,8 +2310,11 @@ completed [`AAFactorization`](@ref), ready for [`solve`](@ref), `\\`, `ldiv!`, `A` is an [`AASparseMatrix`](@ref) or a plain `SparseMatrixCSC` — any index type, and any numeric element type (values other than `Float32`/`Float64`/`ComplexF32`/ -`ComplexF64` are converted to `Float64`/`ComplexF64`). Symmetric/Hermitian and -triangular structure of a `SparseMatrixCSC` is detected automatically. +`ComplexF64` are converted to `Float64`/`ComplexF64`). Like `factorize`, `factor` +tests a `SparseMatrixCSC` for symmetry: for Cholesky, LDLᵀ and `:auto`, it stores a +Hermitian (or real symmetric) matrix as its lower triangle, as +`AASparseMatrix(Hermitian(S, :L))` does. An `AASparseMatrix` is factored as it is +stored. `kind` is `:auto` (Cholesky if Hermitian, else LU if square on macOS 15.5+, else QR), `:cholesky`, `:ldlt`, `:lu` (macOS 15.5+), `:qr`, or any @@ -2305,8 +2330,11 @@ function factor(A::AASparseMatrix, kind::Union{Symbol,SparseFactorization_t} = : factor!(f, _factor_kind(kind)) return f end -factor(A::SparseMatrixCSC, kind::Union{Symbol,SparseFactorization_t} = :auto) = - factor(AASparseMatrix(A), kind) +function factor(A::SparseMatrixCSC, kind::Union{Symbol,SparseFactorization_t} = :auto) + T = _aa_eltype(eltype(A)) + S = eltype(A) === T ? A : SparseMatrixCSC{T}(A) + return factor(_detect_structure(S, _factor_kind(kind)), kind) +end # ============================================================ # In-place multiply (`mul!`) on AASparseMatrix diff --git a/test/sparse_tests.jl b/test/sparse_tests.jl index 0cdc8c00..552b0db4 100644 --- a/test/sparse_tests.jl +++ b/test/sparse_tests.jl @@ -327,13 +327,18 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac symmetricM = sparse(sparseM + sparseM') # verify that the julia matrix is has the desired properties. @assert issymmetric(symmetricM) && !istril(symmetricM) && !istriu(symmetricM) - symmetric = AASparseMatrix(symmetricM) + # the structure is not detected: it comes from Symmetric/Hermitian or the attributes + @test !issymmetric(AASparseMatrix(symmetricM)) + symmetric = AASparseMatrix(Symmetric(symmetricM, :L)) @test issymmetric(symmetric) && !istril(symmetric) && !istriu(symmetric) upper_tri, lower_tri = sparse(I, N, N), sparse(I, N, N) upper_tri[1, 3] = 1.0 lower_tri[3, 1] = 1.0 - @test !issymmetric(upper_tri) && !istril(upper_tri) && istriu(upper_tri) - @test !issymmetric(lower_tri) && istril(lower_tri) && !istriu(lower_tri) + @test !istriu(AASparseMatrix(upper_tri)) + upper = AASparseMatrix(upper_tri, AppleAccelerate.ATT_TRI_UPPER) + lower = AASparseMatrix(lower_tri, AppleAccelerate.ATT_TRI_LOWER) + @test !issymmetric(upper) && !istril(upper) && istriu(upper) + @test !issymmetric(lower) && istril(lower) && !istriu(lower) end end @@ -651,7 +656,7 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac temp = sprand(T, N, N, 0.3) H = sparse(temp * adjoint(temp) + N * I) @test ishermitian(H) && !issymmetric(H) - aaH = AASparseMatrix(H) + aaH = AASparseMatrix(Hermitian(H, :L)) @test ishermitian(aaH) @test !issymmetric(aaH) # factor! default for a Hermitian matrix should pick Cholesky. @@ -788,14 +793,14 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac # Cholesky / LDLᵀ on a symmetric positive-definite matrix. M = sprandn(20, 20, 0.3); S = SparseMatrixCSC(M * M' + 20I) S2 = copy(S); S2.nzval .*= 1.1 - fc = cholesky(AASparseMatrix(S)) + fc = cholesky(AASparseMatrix(Symmetric(S, :L))) cholesky!(fc, S2) # CSC overload @test isapprox(solve(fc, b), Matrix(S2) \ b; rtol = 1e-7) @test_throws ArgumentError lu!(fc, S2) # wrong kind - fl = AAFactorization(AASparseMatrix(S)) + fl = AAFactorization(AASparseMatrix(Symmetric(S, :L))) factor!(fl, AppleAccelerate.SparseFactorizationLDLT) - ldlt!(fl, AASparseMatrix(S2)) + ldlt!(fl, AASparseMatrix(Symmetric(S2, :L))) @test isapprox(solve(fl, b), Matrix(S2) \ b; rtol = 1e-7) end @@ -821,10 +826,10 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac b = rand(T, N) @testset "Hermitian Cholesky refactor" begin - f = cholesky(AASparseMatrix(H1)) - cholesky!(f, AASparseMatrix(H2)) + f = cholesky(AASparseMatrix(Hermitian(H1, :L))) + cholesky!(f, AASparseMatrix(Hermitian(H2, :L))) xref = solve(f, b) - xfresh = solve(cholesky(AASparseMatrix(H2)), b) + xfresh = solve(cholesky(AASparseMatrix(Hermitian(H2, :L))), b) @test isapprox(xref, xfresh; rtol = rtol) @test isapprox(Matrix(H2) * xref, b; rtol = 10 * rtol) end @@ -872,9 +877,9 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac H1 = sparse(Hermitian(B1 + B1' + N*I)) H2 = copy(H1); H2.nzval .*= T(1.3) b = rand(T, N) - f = AAFactorization(AASparseMatrix(H1)) + f = AAFactorization(AASparseMatrix(Hermitian(H1, :L))) factor!(f, AppleAccelerate.SparseFactorizationLDLT) - ldlt!(f, AASparseMatrix(H2)) # Hermitian LDLT refactor + ldlt!(f, AASparseMatrix(Hermitian(H2, :L))) # Hermitian LDLT refactor xref = solve(f, b) ok = isapprox(Matrix(H2) * xref, b; rtol = 1e-6) exit(ok ? 0 : 1) @@ -903,6 +908,7 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac S = sparse(Symmetric(sprandn(8, 8, 0.3))) @test issymmetric(S) @test SparseMatrixCSC(AASparseMatrix(S)) == S + @test SparseMatrixCSC(AASparseMatrix(Symmetric(S, :L))) == S end @testset "Hermitian" begin @@ -910,6 +916,7 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac H = sparse(Hermitian(B + 8I)) @test ishermitian(H) @test SparseMatrixCSC(AASparseMatrix(H)) == H + @test SparseMatrixCSC(AASparseMatrix(Hermitian(H, :L))) == H end @testset "triangular $T" for T in (Float64, ComplexF64) @@ -936,7 +943,7 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac B = sprandn(15, 15, 0.3) A = sparse(B * B' + 15I) # symmetric positive definite b = rand(15) - f = cholesky(AASparseMatrix(A)) + f = cholesky(AASparseMatrix(Symmetric(A, :L))) @test isapprox(solve(f, b), Matrix(A) \ b; rtol = 1e-7) end @@ -944,7 +951,7 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac B = sprandn(12, 12, 0.3) A = sparse(B + B' + 12I) # symmetric b = rand(12) - f = ldlt(AASparseMatrix(A)) + f = ldlt(AASparseMatrix(Symmetric(A, :L))) @test isapprox(solve(f, b), Matrix(A) \ b; rtol = 1e-7) end @@ -1324,7 +1331,7 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac rtol = T == ComplexF32 ? 1e-3 : 1e-8 n = 24 Acsc = hpd(T, n) - Aaa = AASparseMatrix(Acsc) + Aaa = AASparseMatrix(Hermitian(Acsc, :L)) @test ishermitian(Aaa) b = randn(T, n) xref = Matrix(Acsc) \ b @@ -1511,7 +1518,7 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac Rm = SparseMatrixCSC{T,Ti}(Arect); br = rand(T, n + 15) @test solve(AA.factor(Rm), br) ≈ Matrix(Rm) \ br rtol = 10tol # an already-wrapped matrix works too - @test solve(AA.factor(AASparseMatrix(S), :cholesky), b) ≈ Matrix(S) \ b rtol = tol + @test solve(AA.factor(AASparseMatrix(Symmetric(S, :L)), :cholesky), b) ≈ Matrix(S) \ b rtol = tol end @testset "factor guards" begin @@ -1623,6 +1630,28 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac # the other triangle of the parent is ignored, as in Symmetric J = copy(K); J[1, n] = 99 @test SparseMatrixCSC(AASparseMatrix(Symmetric(J, :L))) == K + # AASparseMatrix(::SparseMatrixCSC) stores the matrix as it is + A = AASparseMatrix(K) + @test !issymmetric(A) && A._nzval == nonzeros(K) + # the SparseMatrixCSC conveniences store it as one triangle for Cholesky and LDLᵀ + f = AA.factor(K, :ldlt) + @test issymmetric(f.matrixObj) && f.matrixObj._nzval == nonzeros(tril(K)) + @test AA.inertia(f) == half + @test issymmetric(AA.factor(K + 2n * I).matrixObj) # :auto: Cholesky + @test issymmetric(AAFactorization(K).matrixObj) + @test Matrix(K) * AA.solve(K, b; kind = :ldlt) ≈ b rtol = tol + # and as it is for LU and QR + f = AA.factor(K, :lu) + @test !issymmetric(f.matrixObj) + @test Matrix(K) * AA.solve(f, b) ≈ b rtol = tol + @test Matrix(K) * AA.solve(K, b; kind = :lu) ≈ b rtol = tol + @test Matrix(K) * AA.solve(AA.factor(K, :qr), b) ≈ b rtol = tol + # refactor! with a SparseMatrixCSC stores it as the factored matrix is stored + for (f, K2) in ((AA.factor(K, :ldlt), 2K), (AA.factor(AASparseMatrix(Symmetric(K, :U)), :ldlt), 2K), + (AA.factor(K, :lu), 2K)) + refactor!(f, K2) + @test Matrix(K2) * AA.solve(f, b) ≈ b rtol = tol + end end something(AppleAccelerate.get_macos_version(), v"0.0.0") >= v"15.5" && @@ -1638,6 +1667,8 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac f = AA.factor(A) # :auto: Hermitian, so Cholesky @test Matrix(H) * AA.solve(f, b) ≈ b rtol = tol @test AA.inertia(AA.factor(A, :ldlt)) == (positive = n, zero = 0, negative = 0) + @test ishermitian(AA.factor(H).matrixObj) # stored as one triangle + @test Matrix(H) * AA.solve(H, b) ≈ b rtol = tol # as in Hermitian, the imaginary part of the stored diagonal is ignored G = copy(H); for i in 1:n; G[i, i] += 0.5im; end A = AASparseMatrix(Hermitian(G, :L)) @@ -1660,5 +1691,13 @@ import AppleAccelerate: AAFactorization, AASparseMatrix, factor!, muladd!, refac @test f._factorization.symbolicFactorization.type in (AA.SparseFactorizationLDLT, AA.SparseFactorizationLDLTTPP) @test Matrix(C) * AA.solve(f, b) ≈ b rtol = tol @test Matrix(C) * AA.solve(AA.factor(A, :ldlt), b) ≈ b rtol = tol + # through the SparseMatrixCSC conveniences: LU on the full storage for :auto, + # one triangle for LDLᵀ + f = AA.factor(C) + @test !issymmetric(f.matrixObj) + @test Matrix(C) * AA.solve(f, b) ≈ b rtol = tol + f = AA.factor(C, :ldlt) + @test issymmetric(f.matrixObj) + @test Matrix(C) * AA.solve(f, b) ≈ b rtol = tol end end