# SQL Server LU decomposition

LU

Updated: 19 January 2015

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.
Syntax
SELECT [wct].[LU](
<@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; ‘L’ for the lower triangular matrix, ‘U’ for the upper triangular matrix, and ‘LU’ for the combined matrix.
Return Types
[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.
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.
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
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.
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.
RowNum      ColNum      ItemValue
----------- ----------- ----------------------
0           0           0.00200000000000001
0           1           1.231
0           2           2.471
1           0           1.196
1           1           3.165
1           2           2.54
2           0           1.475
2           1           4.27099999999998
2           2           2.14199999999999