Member-only story

How to Calculate Determinant of a NxN Matrix via MS SQL Server?

DigNo Ape
3 min readDec 24, 2019

--

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:

  1. 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…

--

--

DigNo Ape
DigNo Ape

Written by DigNo Ape

我們秉持著從原人進化的精神,不斷追求智慧的累積和工具的運用來提升生產力。我們相信,每一個成員都擁有無限的潛力,透過學習和實踐,不斷成長和進步。

No responses yet