Stream: helpdesk (published)

Topic: LinearAlgebra.svd() with identity matrix


view this post on Zulip Dale Black (May 30 2021 at 23:06):

I am trying to perform svd on the identity matrix I but it seems like whatever the type of I is, it's raising an error?

ii = svd(I(3))

error:

MethodError: no method matching LinearAlgebra.SVD(::SparseArrays.SparseMatrixCSC{Float64, Int64}, ::BitVector, ::SparseArrays.SparseMatrixCSC{Bool, Int64})

Closest candidates are:

LinearAlgebra.SVD(::AbstractArray{T, N} where N, !Matched::Vector{Tr}, !Matched::AbstractArray{T, N} where N) where {T, Tr} at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/svd.jl:68

svd(::LinearAlgebra.Diagonal{Bool, Vector{Bool}})@diagonal.jl:690
top-level scope@Local: 1[inlined]

Am I correct in this? If so, that seems odd that this isn't available out of the box

view this post on Zulip Júlio Hoffimann (May 30 2021 at 23:10):

The problem is that I doesn't have a size, it is a generic representation. If you type I(3) you get the 3x3 sparse identity. And then an error message will be thrown saying that for sparse matrices you could use Arpack.jl

view this post on Zulip Dale Black (May 30 2021 at 23:14):

Oh I meant to put I(3) so I just edited the message above. But that's weird those aren't compatible. How does Arpack.jl fix this?

view this post on Zulip Dale Black (May 30 2021 at 23:15):

Oh wait it looks like it has it's own version of svds. I will give that a try

view this post on Zulip Mason Protter (May 31 2021 at 00:01):

Could do svd(collect(I(3)))

view this post on Zulip Mason Protter (May 31 2021 at 00:01):

julia> ii = svd(I(3) |> collect)
SVD{Float64, Float64, Matrix{Float64}}
U factor:
3×3 Matrix{Float64}:
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0
singular values:
3-element Vector{Float64}:
 1.0
 1.0
 1.0
Vt factor:
3×3 Matrix{Float64}:
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0

view this post on Zulip Júlio Hoffimann (May 31 2021 at 00:35):

Alternatively, Matrix(I(3)) :smile:

view this post on Zulip Dale Black (May 31 2021 at 00:39):

Thank you, both of those are super easy solutions

view this post on Zulip Dominic Guri (May 31 2021 at 05:19):

svd(1.0I(3)) also works

view this post on Zulip Mason Protter (May 31 2021 at 05:29):

Oh interesting that works!


Last updated: Nov 22 2024 at 04:41 UTC