2019年5月29日 星期三

Deteminant of a matrix 行列式值

Deteminant of a matrix 行列式值

源自於
https://www.bragitoff.com/2018/02/determinant-matrix-c-program/

/**************************************************
******DETERMINANT FROM GAUSS ELIMINATION***********
**************************************************/
#include<stdio.h>
#include<math.h>
/*******
Function that calculates the determinant of a square matrix using Gauss-Elimination :
Pass the square matrix as a parameter, and calculate and return the dete
Parameters: order(n),matrix[n][n]
********/
double determinant(int n, double a[n][n]){
double det=1;
int i,swapCount;
swapCount=gauEl(n,n,a);
for(i=0;i<n;i++){
det =det*a[i][i];
}
return det*pow(-1,swapCount);
}
/********
Function that perform Gauss Elimination
Pass the square matrix as a parameter, and calculate and store the upperTriangular(Gauss-Eliminated Matrix) in it
Parameters: rows(m),columns(n),matrix[m][n]
********/
int gauEl(int m, int p, double a[m][p]){
int i,j,k;
int swapCount=0;
for(i=0;i<m-1;i++){
//Partial Pivoting
for(k=i+1;k<m;k++){
//If diagonal element(absolute vallue) is smaller than any of the terms below it
if(fabs(a[i][i])<fabs(a[k][i])){
//Swap the rows
swapCount++;
for(j=0;j<p;j++){
double temp;
temp=a[i][j];
a[i][j]=a[k][j];
a[k][j]=temp;
}
}
}
//Begin Gauss Elimination
for(k=i+1;k<m;k++){
double  term=a[k][i]/ a[i][i];
for(j=0;j<p;j++){
a[k][j]=a[k][j]-term*a[i][j];
}
}
}
return swapCount;
}

/*******
Function that prints the elements of a matrix row-wise
Parameters: rows(m),columns(n),matrix[m][n]
*******/
void printMatrix(int m, int n, double matrix[m][n]){
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%0.2lf\t",matrix[i][j]);
}
printf("\n");
}
}
/*******
Function that copies the elements of a matrix to another matrix
Parameters: rows(m),columns(n),matrix1[m][n] , matrix2[m][n]
*******/
void copyMatrix(int m, int n, double matrix1[m][n], double matrix2[m][n]){
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
matrix2[i][j]=matrix1[i][j];
}
}
}

int main(){
int n,i,j;
n=4;
printf("Enter the order of the matrix:\n(No. of rows/columns (n))\n");
printf("%d",n);
//Declare a matrix to store the user given matrix
/*
double a[3][3]= {{6., 1, 1},
                     {4., -2, 5},
                     {2., 8, 7}};
    */
    double a[4][4] = { {1, 0, 2, -1},
                    {3, 0, 0, 5},
                    {2, 1, 4, -3},
                    {1, 0, 5, 0}};
   
    /*double a[n][n] = {{1, 2},
                     {3, 2} }; */



printf("\nEnter the elements of matrix:\n");
    printMatrix(n,n,a);
printf("\nThe determinant using Gauss Eliminiation is:\n\n%0.2lf\n",determinant(n,a));


}

輸出畫面
Enter the order of the matrix:
(No. of rows/columns (n))
4
Enter the elements of matrix:
1.00 0.00 2.00 -1.00
3.00 0.00 0.00 5.00
2.00 1.00 4.00 -3.00
1.00 0.00 5.00 0.00

The determinant using Gauss Eliminiation is:

30.00

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...