Logo

SQL Server SVD Function

Updated 2024-02-13 02:14:29.837000

Description

Use the scalar function SVD to calculate the economy-sized singular value decomposition of an m-x-n matrix A*.* SVD returns an m-x-n orthogonal matrix U*,* an n-x-n orthogonal matrix V and an n-x-n diagonal matrix W such that,

    A = UWVT

SVD expects a string representation of the matrix, with columns separated by commas and rows separated by semi-colons.

Syntax

SELECT [westclintech].[wct].[SVD](
  <@Matrix, nvarchar(max),>
 ,<@MatrixName, nvarchar(4000),>)

Arguments

@Matrix

a string representation of the matrix.

@MatrixName

a string identifying the name of matrix to be returned; 'U', 'V' or 'W'.

Return Type

nvarchar(max)

Remarks

The string representations of @Matrix must only contain numbers, commas (to separate the columns), and semi-colons to separate the rows.

Consecutive commas will generate an error.

Consecutive semi-colons will generate an error.

Non-numeric data between commas will generate an error.

Non-number data between semi-colons will generate an error.

To convert non-normalized data to a string format, use the MATRIX2STRING or the MATRIX2STRING_q function.

To convert normalized data to a string format, us the NMATRIX2STRING or the NMATRIX2STRING_q function.

To convert the string result to a table, use the table-valued function MATRIX .

@MatrixName must be either 'U', 'V' or 'W' or an error message will be generated.

Examples

In this example, we return the U matrix from the singular value decomposition.

DECLARE @A as varchar(max)
    = wct.MATRIX2STRING_q('SELECT
       *
   FROM (
       VALUES
           (1,2,3,4)
          ,(2,3,4,5)
          ,(3,4,5,6)
          ,(4,5,6,7)
       )n(x1,x2,x3,x4)');
SELECT wct.SVD(@A, 'U') as U;

This produces the following result.

U
0.314721188083371,-0.775210019137647,-0.287437287650082,0.466240072997124;0.427472435994882,-0.342443158005239,0.730764549218788,-0.407410325844917;0.540223683906393,0.0903237031271687,-0.599217235487328,-0.583899567301538;0.652974931817905,0.523090564259578,0.155889973918623,0.525069820149331

To get the W matrix, we would have entered:

SELECT wct.SVD(@A, 'W') as W;

This produces the following result .

W
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
17.1651513899117,0,0,0;0,1.16515138991168,0,0;0,0,1.05167369421319E-16,0;0,0,0,1.01544874061251E-16

And, to get the V matrix we enter the following:

SELECT wct.SVD(@A, 'V') as V;

returning

V
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0.314721188083371,0.775210019137647,0.547722557505166,0;0.427472435994883,0.342443158005239,-0.730296743340221,-0.408248290463864;0.540223683906394,-0.0903237031271688,-0.182574185835056,0.816496580927726;0.652974931817905,-0.523090564259577,0.365148371670111,-0.408248290463863

In this example we calculate UWV' returning the original matrix.

SELECT [0],
       [1],
       [2],
       [3]
FROM
(
    SELECT *
    FROM wct.Matrix(wct.MATMULT(wct.MATMULT(wct.SVD(@A, 'U'), wct.SVD(@A, 'W')), 
              wct.TRANSPOSE(wct.SVD(@A, 'V'))))
) p
PIVOT
(
    SUM(ItemValue)
    For ColNum IN ([0], [1], [2], [3])
) as pvt
ORDER BY RowNum;

This produces the following result.

0123
1234
23.0000000000000145
34.0000000000000156
45.000000000000016.000000000000017.00000000000001

See Also

LU - LU factorization with partial pivoting

QR - QR decomposition

SVDECOMP - Calculate the economy-sized singular value decomposition of an m-x-n matrix A.