2013년 4월 30일 화요일

// // 1 comment

Determinant of n x n Matrix and C source code



We can usually obtain the determinant of $2 \times 2$ matrix as follow;
$$
\begin{vmatrix}
a_{11} & a_{12} \\
a_{21} & a_{22}
\end{vmatrix}
=a_{11}a_{22} - a_{12}a_{21}.
\tag{1}
$$

We can also calculate $3 \times 3$ matrix as follow;
$$
\begin{vmatrix}
a_{11} & a_{12} & a_{13}\\
a_{21} & a_{22} & a_{23}\\
a_{31} & a_{32} & a_{33}
\end{vmatrix}
=a_{11}
\begin{vmatrix}
a_{22} & a_{23}\\
a_{32} & a_{33}
\end{vmatrix}
-
a_{12}
\begin{vmatrix}
a_{21} & a_{23}\\
a_{31} & a_{33}
\end{vmatrix}
+
a_{13}
\begin{vmatrix}
a_{21} & a_{22}\\
a_{31} & a_{32}
\end{vmatrix}
\tag{2}
$$

In this manner, the determinant of $ n \times n$ matrix is calculated by
\begin{align}
\begin{vmatrix}
a_{11} & a_{12} & \cdots & a_{1n}\\
a_{21} & a_{22} & \cdots & a_{2n}\\
\vdots & \vdots & \ddots & \vdots\\
a_{n1} & a_{n2} & \cdots & a_{nn}
\end{vmatrix}
&= a_{11}
\begin{vmatrix}
a_{22} & a_{23} & \cdots & a_{2n}\\
a_{32} & a_{33} & \cdots & a_{3n}\\
\vdots & \vdots & \ddots & \vdots\\
a_{n2} & a_{n3} & \cdots & a_{nn}
\end{vmatrix}
-
a_{12}
\begin{vmatrix}
a_{21} & a_{23} & \cdots & a_{2n}\\
a_{31} & a_{33} & \cdots & a_{3n}\\
\vdots & \vdots & \ddots & \vdots\\
a_{n1} & a_{n3} & \cdots & a_{nn}
\end{vmatrix}
+ \cdots \\
&+

a_{1n}
\begin{vmatrix}
a_{21} & a_{22} & \cdots & a_{2(n-1)}\\
a_{31} & a_{32} & \cdots & a_{3(n-1)}\\
\vdots & \vdots & \ddots & \vdots\\
a_{n1} & a_{n2} & \cdots & a_{n(n-1)}
\end{vmatrix}
\tag{3}

\end{align}

Note that the notation $a_{n(n-1)}$ in last term of Eq. (3) does not denote $n\times (n-1)$, but it denotes $n$th row and $(n-1)$th column for a brevity.
Also the sign of each term depends on the number of rows and columns, that is, the sum of rows and columns are even number, then the sign becomes $'+'$. Otherwise, the sign becomes $'-'$ as follows;
\begin{vmatrix}
+&-&+&-&\cdots\\
-&+&-&+&\cdots\\
\vdots&\vdots&\vdots&\vdots&\ddots
\end{vmatrix}

This is very tedious works to calculate more than 3 rows and columns.

Today, I am going to introduce the source code to calculate the determinant of $n \times n$ matrix.
The source code uses recursive function of itself.
And the recursive function returns the determinant when the column (or row) size of matrix reaches $2$.

Source File Download → Click Here


-->

댓글 1개: