I got something done, but it only works for 2*2 and 3*3 matrices. There must be a mistake. I used the code I found somewhere:
int determinant(int b[5][5],int m) //FUNCTION TO CALCULATE
DETERMINANT
{
int i,j,sum = 0,c[5][5];
if(m==2)
{ //BASE CONDITION
sum = b[0][0]*b[1][1] - b[0][1]*b[1][0];
return sum;
}
for(int p=0;p<m;p++)
{
int h = 0,k = 0;
for(i=1;i<m;i++)
{
for( j=0;j<m;j++)
{
if(j==p)
continue;
c[h][k] = b[i][j];
k++;
if(k == m-1)
{
h++;
k = 0;
}
}
}
sum = sum + b[0][p]*pow(-1,p)*determinant(c,m-1);
}
return sum;
}
I also used this:
function det( a, n )
if n = 2 then
d := a11·a22 - a12·a21
else
d := 0
for i := 1...n do
M1i <-- A with row 1 and column i deleted
d := d + (-1)(i-1)·a1i·det( M1i, n - 1 )
end for
end if
return d
end function
And got my awk code:
function det(a, n) {
if (n == 2) {
d = a[1,1] * a[2,2] - a[1,2] * a[2,1]
print d
}
else {
d=0
for(i = 1; i <= n; i++){
k=1
j=1
for (x = 2; x <= n; x++) {
for (z = 1; z <= n; z++){
if (z==i)
{z++}
M[k,j]= a[x,z]
j++}
j=1
k++
}
d= d + miinus1_astmel(i-1)*a[1,i]*det( M, n - 1)
}
return d
}
function miinus1_astmel(n){
vastus=1
for (x=1; x<=n; x++){
vastus=vastus*(-1)
}
return vastus
}
Looks the same to me, but somewhere is a mistake.