mirror of
https://github.com/kalmarek/PropertyT.jl.git
synced 2024-11-27 01:10:28 +01:00
eps-sparsify for both sparse and dense matrices
This commit is contained in:
parent
151a6ff6da
commit
7dbd11bebf
@ -57,20 +57,42 @@ include("OrbitDecomposition.jl")
|
|||||||
dens(M::SparseMatrixCSC) = length(M.nzval)/length(M)
|
dens(M::SparseMatrixCSC) = length(M.nzval)/length(M)
|
||||||
dens(M::AbstractArray) = length(findn(M)[1])/length(M)
|
dens(M::AbstractArray) = length(findn(M)[1])/length(M)
|
||||||
|
|
||||||
function sparsify{T}(U::AbstractArray{T}, check=true)
|
function sparsify!{Tv,Ti}(M::SparseMatrixCSC{Tv,Ti}, eps=eps(Tv))
|
||||||
W = deepcopy(U)
|
n = nnz(M)
|
||||||
W[abs.(W) .< eps(T)] .= zero(T)
|
for i in eachindex(M.nzval)
|
||||||
if check && rank(W) != rank(U)
|
if abs(M.nzval[i]) < eps
|
||||||
info("Sparsification would decrease the rank!")
|
M.nzval[i] = zero(Tv)
|
||||||
W = U
|
end
|
||||||
else
|
|
||||||
info("Sparsified:", rpad(dens(U), 10), "\t", rpad(dens(W),10))
|
|
||||||
end
|
end
|
||||||
W = sparse(W)
|
dropzeros!(M)
|
||||||
dropzeros!(W)
|
m = nnz(M)
|
||||||
return W
|
|
||||||
|
info("Sparsified density:", rpad(dens(U), 15), "→", rpad(dens(W),15))
|
||||||
|
|
||||||
|
return M
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function sparsify!{T}(U::AbstractArray{T}, eps=eps(T); check=true)
|
||||||
|
if check
|
||||||
|
W = deepcopy(U)
|
||||||
|
else
|
||||||
|
W = U
|
||||||
|
end
|
||||||
|
|
||||||
|
W[abs.(W) .< eps] .= zero(T)
|
||||||
|
|
||||||
|
if check
|
||||||
|
info("Sparsification would decrease the rank!")
|
||||||
|
W = U
|
||||||
|
else
|
||||||
|
info("Sparsified density:", rpad(dens(U), 15), "→", rpad(dens(W),15))
|
||||||
|
end
|
||||||
|
W = sparse(W)
|
||||||
|
return W
|
||||||
|
end
|
||||||
|
|
||||||
|
sparsify{T}(U::AbstractArray{T}, tol=eps(T)) = sparsify!(deepcopy(U), tol)
|
||||||
|
|
||||||
function init_orbit_data(logger, sett::Settings; radius=2)
|
function init_orbit_data(logger, sett::Settings; radius=2)
|
||||||
|
|
||||||
ex(fname) = isfile(joinpath(sett.name, fname))
|
ex(fname) = isfile(joinpath(sett.name, fname))
|
||||||
|
Loading…
Reference in New Issue
Block a user