Skip to content

svd return value doesn't match Vectorz's svd #71

@mars0i

Description

@mars0i

Vectorz's svd returns a full singular-value decomposition, while the Clatrix core.matrix svd returns reduced singular-value decomposition. The raw Clatrix interface allows you to get the full SVD with optional keyword arguments (:type :full). mikera suggested changing the core.matrix version of Clatrix's svd to return the full version. Would it be OK to add :type :full at line 1756?

Example:

user=> (run! pm (vals (svd (matrix :vectorz [[1 2 3][4 5 6]]))))
[[0.386  0.922]
 [0.922 -0.386]]
[9.508 0.773]
[[ 0.429  0.566 0.704]
 [-0.806 -0.112 0.581]
 [ 0.408 -0.816 0.408]]
nil
user=> (run! pm (vals (svd (matrix :clatrix [[1 2 3][4 5 6]]))))
[[-0.386 -0.922]
 [-0.922  0.386]]
[9.508 0.773]
[[-0.429 -0.566 -0.704]
 [ 0.806  0.112 -0.581]]
nil
;; Notice that the V* matrices differ above.
user=> (require '[clatrix.core :as cx])
nil
user=> (cx/svd (matrix :clatrix [[1 2 3][4 5 6]]))
{:left  A 2x2 matrix
 -------------
-3.86e-01 -9.22e-01
-9.22e-01  3.86e-01
, :right  A 3x2 matrix
 -------------
-4.29e-01  8.06e-01
-5.66e-01  1.12e-01
-7.04e-01 -5.81e-01
, :values (9.508032000695724 0.7728696356734847), :rank 2}
user=> (cx/svd (matrix :clatrix [[1 2 3][4 5 6]]) :type :full)
{:left  A 2x2 matrix
 -------------
-3.86e-01 -9.22e-01
-9.22e-01  3.86e-01
, :right  A 3x3 matrix
 -------------
-4.29e-01  8.06e-01  4.08e-01
-5.66e-01  1.12e-01 -8.16e-01
-7.04e-01 -5.81e-01  4.08e-01
, :values (9.508032000695724 0.7728696356734847), :rank 2}
user=> (run! pm (vals (cx/svd (matrix :clatrix [[1 2 3][4 5 6]]) :type :full)))
[[-0.386 -0.922]
 [-0.922  0.386]]
[[-0.429  0.806  0.408]
 [-0.566  0.112 -0.816]
 [-0.704 -0.581  0.408]]
[9.508 0.773]

I gather that it's OK for the U and V* matrices to be negated--i.e. the vectorz versions are -1 times the clatrix versions.

(Just noticed a typo in the docstring at line 1204. :values is missing the final "s".)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions