Purpose: Calculation of determinant, adjoint and inverse of a matrix is essential when conducting multivariate analysis. We already discussed how to calculate the determinant value of a NxN matrix previously (click here). For this session, we will discuss how to leverage function in MS SQL Server to get the adjoint of a NxN matrix.
How to calculate Adjoint of a Matrix
Steps:
1. We will need the determinant function ([Matrix].[determinant]) we built previously (click here) to calculate each elements of adjoint matrix and function named [Matrix].[determinant_temp_function] to redefine the row and col number after selecting the first element of each row.
2. Create a function called [Matrix].[Adjoint] to have
- Input: Matrix as table type (MatrixTableType)
- Output: Adjoint Matrix as table type (MatrixTableType)
- Define i, j, iMax, jMax, and sign to control loop to calculate each element of decreasing matrix and transpose part.
How to calculate Adjoint of a Matrix
Steps:
1. We will need the determinant function ([Matrix].[determinant]) we built previously (click here) to calculate each elements of adjoint matrix and function named [Matrix].[determinant_temp_function] to redefine the row and col number after selecting the first element of each row.
2. Create a function called [Matrix].[Adjoint] to have
- Input: Matrix as table type (MatrixTableType)
- Output: Adjoint Matrix as table type (MatrixTableType)
- Define i, j, iMax, jMax, and sign to control loop to calculate each element of decreasing matrix and transpose part.
CREATE FUNCTION [Matrix].[Adjoint]
(
@input AS Matrix.MatrixTableType READONLY
)
RETURNS @output TABLE
(
i INT,
j INT,
value FLOAT
)
AS
BEGIN
DECLARE @i INT
DECLARE @j INT
DECLARE @iMAX INT
DECLARE @jMAX INT
DECLARE @sign INT
DECLARE @value_Adj FLOAT
DECLARE @table_cofactor Matrix.MatrixTableType
SET @i = 1
SET @j = 1
SET @sign = 1
SET @iMAX = (select max(i) from @input)
SET @jMAX = (select max(j) from @input)
IF (@iMAX = 1 AND @jMAX = 1)
BEGIN
Delete @output
INSERT INTO @output SELECT * from @input
END
WHILE (@i <= @iMAX )
BEGIN
WHILE (@j <= @jMAX )
BEGIN
SET @value_Adj = 0
Delete @table_cofactor
INSERT INTO @table_cofactor (i,j,value) SELECT i,j,value FROM Matrix.[determinant_temp_function] (@i, @j, @input)
SEt @value_Adj = (SELECT [Matrix].[determinant] ( @table_cofactor))
SET @sign = iif((@i+@j) %2 = 0, @sign, -@sign)
INSERT INTO @output (i,j,value) SELECT @j, @i, @value_Adj * @sign
SET @sign = 1
SET @j = @j + 1
END
Set @j = 1
SET @i = @i + 1
END
RETURN
END;
3. Test part (Input & Output)
DELETE Matrix.[Determinant_MainTable] INSERT INTO Matrix.[Determinant_MainTable] VALUES (1,1,5) INSERT INTO Matrix.[Determinant_MainTable] VALUES (1,2,-2) INSERT INTO Matrix.[Determinant_MainTable] VALUES (1,3,2) INSERT INTO Matrix.[Determinant_MainTable] VALUES (1,4,7) INSERT INTO Matrix.[Determinant_MainTable] VALUES (2,1,1) INSERT INTO Matrix.[Determinant_MainTable] VALUES (2,2,0) INSERT INTO Matrix.[Determinant_MainTable] VALUES (2,3,0) INSERT INTO Matrix.[Determinant_MainTable] VALUES (2,4,3) INSERT INTO Matrix.[Determinant_MainTable] VALUES (3,1,-3) INSERT INTO Matrix.[Determinant_MainTable] VALUES (3,2,1) INSERT INTO Matrix.[Determinant_MainTable] VALUES (3,3,5) INSERT INTO Matrix.[Determinant_MainTable] VALUES (3,4,0) INSERT INTO Matrix.[Determinant_MainTable] VALUES (4,1,3) INSERT INTO Matrix.[Determinant_MainTable] VALUES (4,2,-1) INSERT INTO Matrix.[Determinant_MainTable] VALUES (4,3,-9) INSERT INTO Matrix.[Determinant_MainTable] VALUES (4,4,4) DECLARE @tableinput Matrix.MatrixTableType INSERT INTO @tableinput SELECT * FROM Matrix.[Determinant_MainTable] SELECT * FROM [Matrix].[Adjoint] (@tableinput) order by i,j;
Thank you and enjoy it! If you want to support Informula, you can buy us a coffee here :)
Comments
Post a Comment