Numerical Methods: Determinant of nxn matrix using C
Source Code:
#include<stdio.h> int main(){ float matrix[10][10], ratio, det; int i, j, k, n; printf("Enter order of matrix: "); scanf("%d", &n); printf("Enter the matrix: \n"); for(i = 0; i < n; i++){ for(j = 0; j < n; j++){ scanf("%f", &matrix[i][j]); } } /* Conversion of matrix to upper triangular */ for(i = 0; i < n; i++){ for(j = 0; j < n; j++){ if(j>i){ ratio = matrix[j][i]/matrix[i][i]; for(k = 0; k < n; k++){ matrix[j][k] -= ratio * matrix[i][k]; } } } } det = 1; //storage for determinant for(i = 0; i < n; i++) det *= matrix[i][i]; printf("The determinant of matrix is: %.2f\n\n", det); return 0; }
if diagonal i.e matrix[1][1]=0 then ratio will be infinity ?
This code fails in this case, you must perform partial or complete pivoting
can you explain that 'raio' is what to do?
Ratio actually prevents the division by zero error. If you put matrix[j][i]/matrix[i][i] instead of ratio inside third nested for loop, it throws division by zero run time error.
This is how you reduce the matrix to an upper triangular, therefore the determinant is just the multiplication of diagonal elements.
matrix[i][j] = matrix[i][j] – matrix[k][j]*ratio
//this reduces rows using the previous row, until matrix is diagonal.
You can always check matrix[1][1]==0, if so, add a whole column to matrix[i][1].
exactly
/* u can simplify ur alghorithme by the following code*/
#include
int main(){
float matrix[10][10];
int i, j, k, n;
printf("Enter order of matrix: ");
scanf("%d", &n);
printf("Enter the matrix: n");
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
scanf("%f", &matrix[i][j]);
}
}
/* Calculate directly the determinante */
for(k = 1; k < n; k++)
for(i = k+1; i <= n; i++)
for(j = k+1; j <= n; j++){
matrix[i][j] = matrix[i][j]matrix[k][k]-matrix[i][k]matrix[k][j];
if(k>=2)
matrix[i][j]=matrix[i][j]/matrix[k][k];
}
printf("The determinant of matrix is: %.2fnn", matrix[n][n]);
return 0;
}
Or you could optimize the algorithm and get this:
#include
int main(){
int i, j, n;
printf("Enter order of matrix: ");
scanf("%d", &n);
float*m=(float*)malloc(n*n*sizeof(float));
printf("Enter the matrix: n");
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
scanf("%f", &m[i*n+j]);
}
}
float ary[n-1];
float* subc=&ary[-1];
float det=m[0];
for(i=1;i<n;++i){
subc[i]=m[i]/det;
}
for(i=1;i<n-1;++i){
float dc=m[i*(n+1)]-subc[i];
for(j=i+1;j<n;++j){
subc[j]+=(m[j*n+i]-subc[j])/rc;
}
det*=dc;
}
det*=(m[i*(n+1)]-subc[i]);
free(m);
printf("The determinant of matrix is: %.2fnn", det);
return 0;
}
this programme has some error in upper triangulr
Above source code is working or not?
not working for 0 matrix…
What is rc in the line "subc[j]+=(m[j*n+i]-subc[j])/rc;?"
You just need to declare rc at the main
Similar program, but can apply for degenerate matrix:
// Gauss-Jordan elimination with full pivoting.
// Computing determinants of square matrices
//
// Running time: O(n^3)
// INPUT: a[][] = an nxn matrix
// OUTPUT: determinant of a[][]
#include
#include
#include
using namespace std;
const double EPS = 1e-16;
double a[100][100];
double DetGJ(int n, double a[100][100]) {
int i, j, jmax;
double det=1.0, s;
for (int i=0;i> n;
cout << "n = " << n << "n" << "MATRIX A IS n";
for (int i=0; i> a[i][j];
cout << a[i][j] << " ";
}
cout << "n";
}
double det = DetGJ(n, a);
cout << "Determinant: " << det << endl;
return 0;
}