# Truncated Singular Value Decomposition

**URL:** <https://discourse.julialang.org/t/truncated-singular-value-decomposition/42124>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 26, 2020, 7:59pm UTC](https://discourse.julialang.org/t/truncated-singular-value-decomposition/42124 "2020-06-26T19:59:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vasily\_Ilin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_ilin/32/7836_2.png) [@Vasily\_Ilin](https://discourse.julialang.org/u/Vasily_Ilin)\
**Post date:** [June 26, 2020, 7:59pm UTC](https://discourse.julialang.org/t/truncated-singular-value-decomposition/42124/1 "2020-06-26T19:59:25Z")

</div>

Hi. Given a matrix M I would like to compute its SVD truncated to rank k. I think this is possible without doing the full SVD. For example, Python has this: [sklearn.decomposition.TruncatedSVD — scikit-learn 1.1.2 documentation](https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.TruncatedSVD.html). The code I am currently using to do this is given below. The problem is that it computes SVD first, and then throws out the extra rows/columns, which can be quite costly if k is much smaller than rank of M. Is there a function in Julia to do this?

```julia
using LinearAlgebra
"return U, S, Vt truncated to rank k"
function truncated_svd(M, k)
    @assert k <= min(size(M)...)
    F = svd(M)
    SVD(F.U[:,1:k], F.S[1:k], F.Vt[1:k,:])
end

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 27, 2020, 3:54am UTC](https://discourse.julialang.org/t/truncated-singular-value-decomposition/42124/2 "2020-06-27T03:54:55Z")

</div>

TSVD.jl

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 27, 2020, 4:03am UTC](https://discourse.julialang.org/t/truncated-singular-value-decomposition/42124/3 "2020-06-27T04:03:42Z")

</div>

There’s also LowRankApprox.jl, but yeah I would stick with TSVD.jl.
