Login     Register

        Contact Us     Search

XLeratorDB/math Documentation

SQL Server MINVERSE function


MINVERSE_q
 
Updated: 17 August 2010
Use MINVERSE_q to calculate the matrix inverse of a square (N x N) array.
 
For matrices in normalized form, use the MINVERSEN_q function.
Syntax
SELECT * FROM [wctMath].[wct].[MINVERSE_q] (
   <@Matrix_RangeQuery, nvarchar(max),>)
Arguments
 
@Matrix_RangeQuery
the SELECT statement, as text, used to determine the square (N x N) matrix to be used in this function. The SELECT statement specifies the column names from the table or VIEW or can be used to enter the matrix values directly. Data returned from the @Matrix_RangeQuery select must be of the type float or of a type that implicitly converts to float.
Return Types
TABLE (
      [RowNum] [int] NULL,
      [ColNum] [int] NULL,
      [ItemValue] [float] NULL
Remarks
·         The number of columns in the array must be equal to the number of rows or an error will be returned.
·         If the matrix determinant is zero, an error will be returned.
·         Use the MINVERSE function for simpler queries.
·         Use MINVERSEN_q for a table in normal form.
·         If the array contains NULL, then NULL will be returned.
·         The function returns an error if the array contains a non-numeric value.
·         The table returned from MINVERSE_q multiplied by the input matrix returns the identity matrix
Examples
In this example, we calculate the matrix inverse directly from the SELECT statement.
SELECT C.rownum
,C.ColNum
,ROUND(C.ItemValue, 8)
FROM wct.MINVERSE_q(
      'SELECT 1,-2,3,-4 UNION ALL
      SELECT 5,5,7,8 UNION ALL
      SELECT 9,10,11,12 UNION ALL
      SELECT 13,14,15,16'
) C

This produces the following result

SQL Server Matrix Inverse

If we wanted to SELECT data from a TABLE or a VIEW, the SELECT statement would look like this.

CREATE TABLE #i (
      MatrixID    nvarchar(5),
      rowno       int,
      Col00       float,
      Col01       float,
      Col02       float,
      Col03       float,
      Col04       float,
      Col05       float,
      Col06       float,
      Col07       float,
      Col08       float,
      Col09       float
)
 
INSERT INTO #i (Matrixid, rowno, Col00, col01, col02, col03) VALUES ('1A',0,1.0,-2.0,3.0,-4.0)
INSERT INTO #i (Matrixid, rowno, Col00, col01, col02, col03) VALUES ('1A',0,5.0,5.0,7.0,8.0)
INSERT INTO #i (Matrixid, rowno, Col00, col01, col02, col03) VALUES ('1A',0,9.0,10.0,11.0,12.0)
INSERT INTO #i (Matrixid, rowno, Col00, col01, col02, col03) VALUES ('1A',0,13.0,14.0,15.0,16.0)
 
SELECT C.rownum
,C.ColNum
,ROUND(C.ItemValue, 8) as ItemValue
FROM wct.MINVERSE_q(
      'SELECT col00
      ,col01
      ,col02
      ,col03
      FROM #i'
) C

This produces the following result.
 SQL Server Matrix Inverse
 
In this example, we will use the MINVERSE_q function and the MMULT_q function to return the identity matrix.


DECLARE     @MString as varchar(4000),
            @IString as varchar(4000)
SET @Mstring = 'SELECT 1,-2,3,-4 UNION ALL
SELECT 5,5,7,8 UNION ALL
SELECT 9,10,11,12 UNION ALL
SELECT 13,14,15,16'
 
SET @IString = 'SELECT [0],[1],[2],[3]
FROM (
      SELECT C.*
      FROM wct.MINVERSE_q(''' +
      @MString + '''
      ) C
) M PIVOT (
    MAX(ItemValue)
    FOR colnum IN ([0],[1],[2],[3])
) AS pvt'
 
SELECT [0],[1],[2],[3]
FROM (
      SELECT C.rownum
      ,c.colnum
      ,round(c.ItemValue, 4) as ItemValue
      FROM wct.MMULT_q(
      @Mstring
      ,@IString
      ) C
) M PIVOT (
    MAX(ItemValue)
    FOR colnum IN ([0],[1],[2],[3])
) AS pvt

This produces the following result.

SQL Server Matrix Inverse
 

See Also

·         Blog Post - SQL Server Matrix Functions


Copyright 2008-2024 Westclintech LLC         Privacy Policy        Terms of Service