dispatch sparsify! on the type of matrix

This commit is contained in:
kalmar 2017-06-09 11:56:17 +02:00
parent 0eae65169e
commit d64ba34407

View File

@ -34,11 +34,23 @@ immutable Settings
tol::Float64 tol::Float64
end end
function sparsify!{T}(U::AbstractArray{T}, eps=eps(T)) function sparsify!{T}(U::Array{T}, eps=eps(T))
# n = rank(U) n = rank(U)
W = deepcopy(U)
W[abs.(W) .< eps] = zero(T)
if rank(W) != n
warn("Sparsification would decrease the rank!")
W = U
end
U = sparse(U)
dropzeros!(U)
return U
end
function sparsify!{T}(U::SparseMatrixCSC{T}, eps=eps(T))
U[abs.(U) .< eps] = zero(T) U[abs.(U) .< eps] = zero(T)
# @assert rank(U) == n dropzeros!(U)
return sparse(U) return U
end end
sparsify{T}(U::AbstractArray{T}, eps=eps(T)) = sparsify!(deepcopy(U), eps) sparsify{T}(U::AbstractArray{T}, eps=eps(T)) = sparsify!(deepcopy(U), eps)