sigma = Matrices.singularValues(A); (sigma, U, VT) = Matrices.singularValues(A);
This function computes the singular values and optionally the singular vectors of matrix A. Basically the singular value decomposition of A is computed, i.e.,
A = U Σ VT = U*Sigma*VT
where U and V are orthogonal matrices (UUT=I,
VVT=I).
Σ = [diagonal(σi), zeros(n,m-n)], if n=size(A,1) ≤
m=size(A,2)) or [diagonal(σi); zeros(n-m,m)], if n >
m=size(A,2)). Σ has the same size as matrix A with
nonnegative diagonal elements in decreasing order and with all other elements zero
(σ1 is the largest element). The function
returns the singular values σi
in vector sigma
and the orthogonal matrices in
matrices U
and VT
.
A = [1, 2, 3, 4; 3, 4, 5, -2; -1, 2, -3, 5]; (sigma, U, VT) = singularValues(A); results in: sigma = {8.33, 6.94, 2.31}; i.e. Sigma = [8.33, 0, 0, 0; 0, 6.94, 0, 0; 0, 0, 2.31, 0]
function singularValues extends Modelica.Icons.Function; input Real A[:, :] "Matrix"; output Real sigma[min(size(A, 1), size(A, 2))] "Singular values"; output Real U[size(A, 1), size(A, 1)] = identity(size(A, 1)) "Left orthogonal matrix"; output Real VT[size(A, 2), size(A, 2)] = identity(size(A, 2)) "Transposed right orthogonal matrix"; end singularValues;