# SVD solver for Sparse Matrices

I want to use singular value decomposition on large sparse matrices (18,000 x 8,000) and while the built-in LinearAlgebra function does the trick, it does not operate on the SparseMatrix type. My current solution is just converting it into a dense matrix and then calling the function, but as expected, it takes a long time.

Is there an existing package or option to use SVD with a sparse matrix or maybe only get the Kth largest Uk Sk V’k from an input?

You could use PROPACK.jl, it works if `A` is a SparseMatrixCSC or a linear operator.

1 Like

I’m not sure it provides a function to compute the SVD, however you may want to check SuiteSparse.

I downloaded the package and did the setup but I’m getting this strange error below. Not really sure why since I’m following the documentation steps.

``````(TextHW2) julia> train_mat.dtm |> typeof
SparseArrays.SparseMatrixCSC{Int64, Int64}

(TextHW2) julia> train_mat.dtm |> size
(18000, 7666)

(TextHW2) julia> tsvd(train_mat.dtm; k = 1)
ERROR: MethodError: no method matching eps(::Int64)
Closest candidates are:
eps() at float.jl:831
eps(::AbstractFloat) at float.jl:827
eps(::T) where T<:Dates.TimeType at C:\Users\aledo\.julia\juliaup\julia-1.8.1+0.x64\share\julia\stdlib\v1.8\Dates\src\types.jl:444
...
Stacktrace:
[1] tsvd(A::SparseArrays.SparseMatrixCSC{Int64, Int64}; kwargs::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:k,), Tuple{Int64}}})
@ PROPACK C:\Users\aledo\.julia\packages\PROPACK\0cIiC\src\PROPACK.jl:216
[2] top-level scope
@ REPL[102]:1

(TextHW2) julia>
``````

Your matrix has an integer data type. I don’t think SuiteSparse is capable of handling that.

1 Like

Should I convert its contents to floats (eg 1 → 1.0) then?

I would.

1 Like

That did the trick, thank you!