Member-only story
How to Calculate Determinant of a NxN Matrix via MS SQL Server?
Purpose: Calculation of determinant, adjoint and inverse of a matrix is essential when conducting multivariate analysis. We first discuss how to leverage function in MS SQL Server to get the determinant value of a NxN matrix.
How to calculate Determinant of a Matrix
Steps:
- Define a table type for the variable stored in the form of table.
CREATE TYPE MatrixTableType AS TABLE
(
i int,
j int,
value float
)
Go
2. Create a function called [Matrix].[determinant_temp_function], which will be used in our main function. This function is to help us redefine the row and col number after selecting the first element of each row and multiply it by the decreasing matrix (removing the row and column where the selected first element) with alternative sign (starting from the positive).
CREATE FUNCTION [Matrix].[determinant_temp_function]
(
@x AS INT,
@y AS INT,
@input AS Matrix.MatrixTableType READONLY
)
RETURNS @output TABLE
(
i INT,
j INT,
value FLOAT
)
AS
BEGIN
INSERT @output
SELECT DENSE_RANK() OVER (ORDER BY A.i ASC) AS i, DENSE_RANK() OVER (ORDER BY A.j ASC) AS j, A.value
FROM @input AS A
WHERE A.i <> @x and A.j <> @y
RETURN
END
Note: After removing the row and column the first element, for example of a 3x3 matrix, removing the first row and column where X11 resides in, the remaining will be X22, X23, X32 and X33…