Skip to content

Commit 268d390

Browse files
QR: handle xtype/dtype returned from LibSuiteSparse that don't match matrix element type (#586)
* QR: handle xtype/dtype other than input Tv * Set types by SparseMatrixCSC{Tv, Ti} when possible * One less forced copy --------- Co-authored-by: Viral B. Shah <[email protected]>
1 parent 9731aef commit 268d390

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/solvers/cholmod.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,15 @@ Dense(A::StridedVecOrMatInclAdjAndTrans{T}) where
901901

902902
Dense(A::Sparse) = sparse_to_dense(A)
903903

904+
function Dense(ptr::Ptr{cholmod_dense})
905+
if ptr == C_NULL
906+
throw(ArgumentError("dense matrix construction failed for " *
907+
"unknown reasons. Please submit a bug report."))
908+
end
909+
s = unsafe_load(ptr)
910+
return Dense{jlxtype(s.xtype, s.dtype)}(ptr)
911+
end
912+
904913
function Base.convert(::Type{Dense{Tnew}}, A::Dense{T}) where {Tnew, T}
905914
GC.@preserve A begin
906915
Ap = unsafe_load(pointer(A))

src/solvers/spqr.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ function LinearAlgebra.qr(A::SparseMatrixCSC{Tv, Ti}; tol=_default_tol(A), order
204204
C_NULL, C_NULL, C_NULL, C_NULL,
205205
R, E, H, HPinv, HTau)
206206

207-
R_ = SparseMatrixCSC(Sparse(R[]))
208-
return QRSparse(SparseMatrixCSC(Sparse(H[])),
209-
vec(Array(CHOLMOD.Dense{Tv}(HTau[]))),
210-
SparseMatrixCSC(min(size(A)...),
207+
R_ = SparseMatrixCSC{Tv, Ti}(Sparse(R[]))
208+
return QRSparse(SparseMatrixCSC{Tv, Ti}(Sparse(H[])),
209+
vec(Array{Tv}(CHOLMOD.Dense(HTau[]))),
210+
SparseMatrixCSC{Tv, Ti}(min(size(A)...),
211211
size(R_, 2),
212212
getcolptr(R_),
213213
rowvals(R_),

test/spqr.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ end
110110
@test (F.Q*F.R)::SparseMatrixCSC == A[F.prow,F.pcol]
111111
end
112112

113+
@testset "Issue #585 for element type: $eltyA" for eltyA in (Float64, Float32)
114+
A = sparse(eltyA[1 0; 0 1])
115+
F = qr(A)
116+
@test eltype(F.Q) == eltype(F.R) == eltyA
117+
end
118+
113119
@testset "select ordering overdetermined" begin
114120
A = sparse([1:n; rand(1:m, nn - n)], [1:n; rand(1:n, nn - n)], randn(nn), m, n)
115121
b = randn(m)

0 commit comments

Comments
 (0)