Logo

QR

Updated 2023-10-19 18:55:07.870000

Syntax

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

Description

Use the scalar function QR for decomposing a the string representation of an N x N matrix A into the product of an upper triangular matrix R and an orthogonal matrix Q, such that

A=QR.

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

Arguments

@MatrixName

The name of the matrix, Q or R, to be returned.

@Matrix

a string representation of an upper-triangular 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, use the NMatrix2String or the NMatrix2String_q function.

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

If @MatrixName is not 'Q' or 'R' an error message will be returned.

Examples

In this example we will return the Q matrix in string format.

DECLARE @A as varchar(max);

SET @A = '12,-51,4;6,167,-68;-4,24,-41';

SELECT wct.QR(@A, 'Q') as Q;

This produces the following result.

{"columns":[{"field":"Q"}],"rows":[{"Q":"-0.857142857142857,0.394285714285714,-0.331428571428571;-0.428571428571429,-0.902857142857142,0.0342857142857143;0.285714285714286,-0.171428571428571,-0.942857142857142"}]}

To get the R matrix, we could have entered:

SELECT wct.QR(@A, 'R') as R;

This produces the following result.

{"columns":[{"field":"R"}],"rows":[{"R":"-14,-21,14;0,-175,70;0,0,35"}]}

If we calculate Q * R we should get the original matrix, A, returned, within the limits of floating point arithmetic.

DECLARE @A as varchar(max);

SET @A = '12,-51,4;6,167,-68;-4,24,-41';

SELECT wct.MATMULT(wct.QR(@A, 'Q'), wct.QR(@A, 'R')) as [Q * R];

This produces the following result.

{"columns":[{"field":"Q * R"}],"rows":[{"Q * R":"12,-51,4;6.00000000000001,167,-67.9999999999999;-4,23.9999999999999,-40.9999999999999"}]}

We can use the table-valued function MATRIX to turn the results into third-normal form.

SELECT RowNum,

       ColNum,

       ROUND(ItemValue, 0) as ItemValue

FROM wct.MATRIX(wct.MATMULT(wct.QR(@A, 'Q'), wct.QR(@A, 'R')));

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":"12"},{"RowNum":"0","ColNum":"1","ItemValue":"-51"},{"RowNum":"0","ColNum":"2","ItemValue":"4"},{"RowNum":"1","ColNum":"0","ItemValue":"6"},{"RowNum":"1","ColNum":"1","ItemValue":"167"},{"RowNum":"1","ColNum":"2","ItemValue":"-68"},{"RowNum":"2","ColNum":"0","ItemValue":"-4"},{"RowNum":"2","ColNum":"1","ItemValue":"24"},{"RowNum":"2","ColNum":"2","ItemValue":"-41"}]}

We can also calculate QTQ, verifying that this will return the identity matrix.

DECLARE @A as varchar(max);

SET @A = '12,-51,4;6,167,-68;-4,24,-41';

SELECT RowNum,

       ColNum,

       ROUND(ItemValue, 0) as ItemValue

FROM wct.MATRIX(wct.MATMULT(wct.TRANSPOSE(wct.QR(@A, 'Q')), wct.QR(@A, 'Q')));

This returns 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":"1"},{"RowNum":"0","ColNum":"1","ItemValue":"0"},{"RowNum":"0","ColNum":"2","ItemValue":"0"},{"RowNum":"1","ColNum":"0","ItemValue":"0"},{"RowNum":"1","ColNum":"1","ItemValue":"1"},{"RowNum":"1","ColNum":"2","ItemValue":"0"},{"RowNum":"2","ColNum":"0","ItemValue":"0"},{"RowNum":"2","ColNum":"1","ItemValue":"0"},{"RowNum":"2","ColNum":"2","ItemValue":"1"}]}