LU
Updated 2023-10-19 16:12:06.687000
Syntax
SELECT [westclintech].[wct].[LU](
<@Matrix, nvarchar(max),>
,<@MatrixName, nvarchar(4000),>)
Description
Use the scalar function LU to calculate the LU factorization of an N x N matrix A using partial pivoting. LU returns a lower triangular matrix L, an upper triangular matrix U, or a permutation matrix P such that,
LU = PA
This means that L has only zeroes above the diagonal and U has only zeroes below the diagonal.
LU expects a string representation of the matrix, with columns separated by commas and rows separated by semi-colons.
Arguments
@MatrixName
a string identifying the name of matrix to be returned; 'L' for the lower triangular matrix, 'U' for the upper triangular matrix, and 'LU' for the combined matrix.
@Matrix
a string representation of the matrix.
Return Type
nvarchar(max)
Remarks
The number of columns in @Matrix must be equal to the number of rows or an error will be returned.
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, us the table-valued function MATRIX.
@MatrixName must be either 'L', 'U', or 'P' or an error message will be generated.
Examples
In this example, we return the L matrix from the LU decomposition.
DECLARE @A as varchar(max) = '0.002,1.231,2.471;1.196,3.165,2.54;1.475,4.271,2.142';
SELECT wct.LU(@A, 'L') as L;
This produces the following result.
{"columns":[{"field":"L"}],"rows":[{"L":"1,0,0;0.00167224080267559,1,0;1.23327759197324,0.299970803835884,1"}]}
To get the U matrix, we would have entered:
SELECT wct.LU(@A, 'U') as U;
This produces the following result.
{"columns":[{"field":"U"}],"rows":[{"U":"1.196,3.165,2.54;0,1.22570735785953,2.4667525083612;0,0,-1.73047881640933"}]}
And, to get the P matrix we enter the following:
SELECT
wct.LU(@A,'P') as P;
returning
{"columns":[{"field":"P"}],"rows":[{"P":"0,1,0;1,0,0;0,0,1"}]}
In this example we calculate P'LU returning the original matrix.
SELECT wct.MATMULT(wct.MATMULT(wct.TRANSPOSE(wct.LU(@A, 'P')), wct.LU(@A, 'L')),
wct.LU(@A, 'U')) as A;
This produces the following result.
{"columns":[{"field":"A"}],"rows":[{"A":"0.00200000000000001,1.231,2.471;1.196,3.165,2.54;1.475,4.27099999999998,2.14199999999999"}]}
We can use the table-valued function MATRIX to convert the string into third-normal form.
SELECT *
FROM wct.MATRIX(wct.MATMULT(wct.MATMULT(wct.TRANSPOSE(wct.LU(@A, 'P')), wct.LU(@A,
'L')), wct.LU(@A, 'U')));
This produces the following result.
{"columns":[{"field":"RowNum","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"ColNum","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"ItemValue","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"RowNum":"0","ColNum":"0","ItemValue":"0.00200000000000001"},{"RowNum":"0","ColNum":"1","ItemValue":"1.231"},{"RowNum":"0","ColNum":"2","ItemValue":"2.471"},{"RowNum":"1","ColNum":"0","ItemValue":"1.196"},{"RowNum":"1","ColNum":"1","ItemValue":"3.165"},{"RowNum":"1","ColNum":"2","ItemValue":"2.54"},{"RowNum":"2","ColNum":"0","ItemValue":"1.475"},{"RowNum":"2","ColNum":"1","ItemValue":"4.27099999999998"},{"RowNum":"2","ColNum":"2","ItemValue":"2.14199999999999"}]}
See Also
LUDECOMP - Calculate the LU factorization of an N x N matrix using partial pivoting.
LUDECOMP_Q - Calculate the LU factorization of an N x N matrix using partial pivoting.
LUDECOMPN - Calculate the LU factorization of an N x N matrix using partial pivoting.
LUDECOMPN_Q - Calculate the LU factorization of an N x N matrix using partial pivoting.