28
28
Factor,
29
29
Sparse
30
30
31
- import SparseArrays: AbstractSparseMatrix, SparseMatrixCSC, indtype, sparse, spzeros, nnz
31
+ import SparseArrays: AbstractSparseMatrix, SparseMatrixCSC, indtype, sparse, spzeros, nnz,
32
+ sparsevec
32
33
33
34
import .. increment, .. increment!, .. AdjType, .. TransType
34
35
@@ -864,7 +865,18 @@ function _trim_nz_builder!(m, n, colptr, rowval, nzval)
864
865
l = colptr[end ] - 1
865
866
resize! (rowval, l)
866
867
resize! (nzval, l)
867
- SparseMatrixCSC (m, n, colptr, rowval, nzval)
868
+ return (m, n, colptr, rowval, nzval)
869
+ end
870
+
871
+ function SparseVector {Tv,SuiteSparse_long} (A:: Sparse{Tv} ) where Tv
872
+ s = unsafe_load (pointer (A))
873
+ if s. stype != 0
874
+ throw (ArgumentError (" matrix has stype != 0. Convert to matrix " *
875
+ " with stype == 0 before converting to SparseVector" ))
876
+ end
877
+ args = _extract_args (s, Tv)
878
+ s. sorted == 0 && _sort_buffers! (args... );
879
+ return SparseVector (args[1 ], args[4 ], args[5 ])
868
880
end
869
881
870
882
function SparseMatrixCSC {Tv,SuiteSparse_long} (A:: Sparse{Tv} ) where Tv
@@ -875,15 +887,15 @@ function SparseMatrixCSC{Tv,SuiteSparse_long}(A::Sparse{Tv}) where Tv
875
887
end
876
888
args = _extract_args (s, Tv)
877
889
s. sorted == 0 && _sort_buffers! (args... );
878
- return _trim_nz_builder! (args... )
890
+ return SparseMatrixCSC ( _trim_nz_builder! (args... ) ... )
879
891
end
880
892
881
893
function Symmetric {Float64,SparseMatrixCSC{Float64,SuiteSparse_long}} (A:: Sparse{Float64} )
882
894
s = unsafe_load (pointer (A))
883
895
issymmetric (A) || throw (ArgumentError (" matrix is not symmetric" ))
884
896
args = _extract_args (s, Float64)
885
897
s. sorted == 0 && _sort_buffers! (args... )
886
- Symmetric (_trim_nz_builder! (args... ), s. stype > 0 ? :U : :L )
898
+ Symmetric (SparseMatrixCSC ( _trim_nz_builder! (args... ) ... ), s. stype > 0 ? :U : :L )
887
899
end
888
900
convert (T:: Type{Symmetric{Float64,SparseMatrixCSC{Float64,SuiteSparse_long}}} , A:: Sparse{Float64} ) = T (A)
889
901
@@ -892,10 +904,16 @@ function Hermitian{Tv,SparseMatrixCSC{Tv,SuiteSparse_long}}(A::Sparse{Tv}) where
892
904
ishermitian (A) || throw (ArgumentError (" matrix is not Hermitian" ))
893
905
args = _extract_args (s, Tv)
894
906
s. sorted == 0 && _sort_buffers! (args... )
895
- Hermitian (_trim_nz_builder! (args... ), s. stype > 0 ? :U : :L )
907
+ Hermitian (SparseMatrixCSC ( _trim_nz_builder! (args... ) ... ), s. stype > 0 ? :U : :L )
896
908
end
897
909
convert (T:: Type{Hermitian{Tv,SparseMatrixCSC{Tv,SuiteSparse_long}}} , A:: Sparse{Tv} ) where {Tv<: VTypes } = T (A)
898
910
911
+ function sparsevec (A:: Sparse{Tv} ) where {Tv}
912
+ s = unsafe_load (pointer (A))
913
+ @assert s. stype == 0
914
+ return SparseVector {Tv,SuiteSparse_long} (A)
915
+ end
916
+
899
917
function sparse (A:: Sparse{Float64} ) # Notice! Cannot be type stable because of stype
900
918
s = unsafe_load (pointer (A))
901
919
if s. stype == 0
@@ -1527,7 +1545,10 @@ end
1527
1545
function (\ )(L:: FactorComponent , B:: Matrix )
1528
1546
Matrix (L\ Dense (B))
1529
1547
end
1530
- function (\ )(L:: FactorComponent , B:: SparseVecOrMat )
1548
+ function (\ )(L:: FactorComponent , B:: SparseVector )
1549
+ sparsevec (L\ Sparse (B))
1550
+ end
1551
+ function (\ )(L:: FactorComponent , B:: SparseMatrixCSC )
1531
1552
sparse (L\ Sparse (B,0 ))
1532
1553
end
1533
1554
(\ )(L:: FactorComponent , B:: Adjoint{<:Any,<:SparseMatrixCSC} ) = L \ copy (B)
@@ -1553,7 +1574,7 @@ end
1553
1574
(\ )(L:: Factor , B:: SparseMatrixCSC ) = sparse (spsolve (CHOLMOD_A, L, Sparse (B, 0 )))
1554
1575
(\ )(L:: Factor , B:: Adjoint{<:Any,<:SparseMatrixCSC} ) = L \ copy (B)
1555
1576
(\ )(L:: Factor , B:: Transpose{<:Any,<:SparseMatrixCSC} ) = L \ copy (B)
1556
- (\ )(L:: Factor , B:: SparseVector ) = sparse (spsolve (CHOLMOD_A, L, Sparse (B)))
1577
+ (\ )(L:: Factor , B:: SparseVector ) = sparsevec (spsolve (CHOLMOD_A, L, Sparse (B)))
1557
1578
1558
1579
\ (adjL:: AdjType{<:Any,<:Factor} , B:: Dense ) = (L = adjL. parent; solve (CHOLMOD_A, L, B))
1559
1580
\ (adjL:: AdjType{<:Any,<:Factor} , B:: Sparse ) = (L = adjL. parent; spsolve (CHOLMOD_A, L, B))
0 commit comments