-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeterminant.c
98 lines (79 loc) · 2.13 KB
/
determinant.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
void createMinor(double **A, double **minor, int rows, int cols, int skipRow, int skipCol)
{
int minorRow = 0, minorCol = 0;
for (int i = 0; i < rows; i++)
{
if (i == skipRow)
continue;
minorCol = 0;
for (int j = 0; j < cols; j++)
{
if (j == skipCol)
continue;
minor[minorRow][minorCol] = A[i][j];
minorCol++;
}
minorRow++;
}
}
double calculateDeterminant(double **A, int rows, int cols)
{
if (rows != cols)
{
printf("Error: Matrix must be square to calculate determinant.\n");
return 0;
}
// Base case for 2x2 matrix
if (rows == 2)
{
return A[0][0] * A[1][1] - A[0][1] * A[1][0];
}
double det = 0;
int sign = 1;
double **minor = allocateMatrix(rows - 1, cols - 1);
for (int j = 0; j < cols; j++)
{
createMinor(A, minor, rows, cols, 0, j);
// Print minor
printf("\nMinor matrix for element A[0][%d]:\n", j);
printMatrix(minor, rows - 1, cols - 1, 2);
det += sign * A[0][j] * calculateDeterminant(minor, rows - 1, cols - 1);
sign = -sign;
}
// Free the allocated memory for minor matrix
for (int i = 0; i < rows - 1; i++)
{
free(minor[i]);
}
free(minor);
return det;
}
int main()
{
// Declaration of rows and cols in A matrix
int rows = 4, cols = 4;
// Declaration of determinant result
double determinant;
// Init matrix
double **A;
A = allocateMatrix(rows, cols);
// Fill matrix
double values[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
A[i][j] = values[i * cols + j];
}
};
// Print matrix
printf("Matrix A with %d rows and %d cols.\n", rows, cols);
printMatrix(A, rows, cols, 0);
// Calculate dererminant
determinant = calculateDeterminant(A, rows, cols);
printf("Determinant of matrix is %f\n", determinant);
return 0;
}