Skip to content

Commit

Permalink
Merge pull request #16426 from pabloferz/pz/cholmod
Browse files Browse the repository at this point in the history
Making `\` work with SparseMatrix.CHOLMOD.Factor and SubArrays
  • Loading branch information
andreasnoack committed May 19, 2016
2 parents 17bc2a4 + dc59cac commit 05366dc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -809,15 +809,15 @@ get_perm(FC::FactorComponent) = get_perm(Factor(FC))
#########################

# Convertion/construction
function convert{T<:VTypes}(::Type{Dense{T}}, A::VecOrMat)
function convert{T<:VTypes}(::Type{Dense{T}}, A::StridedVecOrMat)
d = allocate_dense(size(A, 1), size(A, 2), stride(A, 2), T)
s = unsafe_load(d.p)
for i in eachindex(A)
unsafe_store!(s.x, A[i], i)
end
d
end
function convert(::Type{Dense}, A::VecOrMat)
function convert(::Type{Dense}, A::StridedVecOrMat)
T = promote_type(eltype(A), Float64)
return convert(Dense{T}, A)
end
Expand Down Expand Up @@ -1464,8 +1464,8 @@ Ac_ldiv_B(L::FactorComponent, B) = ctranspose(L)\B

(\){T}(L::Factor{T}, B::Dense{T}) = solve(CHOLMOD_A, L, B)
(\)(L::Factor{Float64}, B::VecOrMat{Complex{Float64}}) = L\real(B) + L\imag(B)
(\)(L::Factor, b::Vector) = Vector(L\convert(Dense{eltype(L)}, b))
(\)(L::Factor, B::Matrix) = Matrix(L\convert(Dense{eltype(L)}, B))
(\)(L::Factor, b::StridedVector) = Vector(L\convert(Dense{eltype(L)}, b))
(\)(L::Factor, B::StridedMatrix) = Matrix(L\convert(Dense{eltype(L)}, B))
(\)(L::Factor, B::Sparse) = spsolve(CHOLMOD_A, L, B)
# When right hand side is sparse, we have to ensure that the rhs is not marked as symmetric.
(\)(L::Factor, B::SparseVecOrMat) = sparse(spsolve(CHOLMOD_A, L, Sparse(B, 0)))
Expand Down
7 changes: 7 additions & 0 deletions test/sparsedir/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,10 @@ Fnew = deserialize(b)
@test_throws MethodError cholfact(speye(BigFloat, 5))
@test_throws MethodError cholfact(Symmetric(speye(BigFloat, 5)))
@test_throws MethodError cholfact(Hermitian(speye(Complex{BigFloat}, 5)))

# test \ for Factor and StridedVecOrMat
let x = rand(5)
A = cholfact(sparse(diagm(x.\1)))
@test_approx_eq A\sub(ones(10),1:2:10) x
@test_approx_eq A\slice(eye(5,5),:,:) diagm(x)
end

0 comments on commit 05366dc

Please sign in to comment.