GCC Code Coverage Report


Directory: ./
File: s21_determinant.c
Date: 2025-08-15 23:01:21
Exec Total Coverage
Lines: 56 56 100.0%
Branches: 34 34 100.0%

Line Branch Exec Source
1 #include "s21_matrix.h"
2
3 int s21_process_lu_decomposition(matrix_t *A, matrix_t *temp);
4 double s21_calc_determinant(matrix_t *temp);
5 int s21_check_for_swap(matrix_t *temp, int k);
6 void s21_swap_rows(matrix_t *temp, int row1, int row2);
7 int s21_process_row_in_temp(matrix_t *temp, int k, int i, int *multiplier);
8
9 188 int s21_determinant(matrix_t *A, double *result) {
10
4/4
✓ Branch 1 taken 186 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 184 times.
188 if (s21_check_matrix_is_invalid(A) || result == NULL) {
11 4 return ERROR;
12 }
13
14 184 int code = OK;
15
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 4 times.
184 if (A->columns == A->rows) {
16 matrix_t temp;
17 180 s21_create_matrix(A->columns, A->columns, &temp);
18
19 180 int multiplier = s21_process_lu_decomposition(A, &temp);
20
21 180 *result = s21_calc_determinant(&temp) * multiplier;
22
23 180 s21_remove_matrix(&temp);
24 } else {
25 4 code = CALC_ERROR;
26 }
27
28 184 return code;
29 }
30
31 180 int s21_process_lu_decomposition(matrix_t *A, matrix_t *temp) {
32 180 int n = A->columns;
33
34
2/2
✓ Branch 0 taken 428 times.
✓ Branch 1 taken 180 times.
608 for (int i = 0; i < n; i++) {
35
2/2
✓ Branch 0 taken 1108 times.
✓ Branch 1 taken 428 times.
1536 for (int j = 0; j < n; j++) {
36 1108 temp->matrix[i][j] = A->matrix[i][j];
37 }
38 }
39
40 180 int end = 0;
41 180 int multiplier = 1;
42
4/4
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 178 times.
✓ Branch 2 taken 246 times.
✓ Branch 3 taken 2 times.
426 for (int k = 1; k < n && !end; k++) {
43
4/4
✓ Branch 0 taken 338 times.
✓ Branch 1 taken 244 times.
✓ Branch 2 taken 336 times.
✓ Branch 3 taken 2 times.
582 for (int i = k; i < n && !end; i++) {
44 336 end = s21_process_row_in_temp(temp, k, i, &multiplier);
45 }
46 }
47
48 180 return multiplier;
49 }
50
51 336 int s21_process_row_in_temp(matrix_t *temp, int k, int i, int *multiplier) {
52 336 (*multiplier) *= s21_check_for_swap(temp, k);
53
54 336 int end = 0;
55
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 4 times.
336 if (temp->matrix[k - 1][k - 1] != 0) {
56 332 temp->matrix[i][k - 1] =
57 332 temp->matrix[i][k - 1] / temp->matrix[k - 1][k - 1];
58
2/2
✓ Branch 0 taken 520 times.
✓ Branch 1 taken 332 times.
852 for (int j = k; j < temp->columns; j++) {
59 520 temp->matrix[i][j] =
60 520 temp->matrix[i][j] - temp->matrix[i][k - 1] * temp->matrix[k - 1][j];
61 }
62 } else {
63 4 end = 1;
64 }
65
66 336 return end;
67 }
68
69 336 int s21_check_for_swap(matrix_t *temp, int k) {
70 336 int multiplier = 1;
71 336 int n = temp->columns;
72
73
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 292 times.
336 if (temp->matrix[k - 1][k - 1] == 0) {
74 44 int i = k;
75
4/4
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 40 times.
50 while (i < n && temp->matrix[i][k - 1] == 0) {
76 6 i++;
77 }
78
79
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 4 times.
44 if (i < n) {
80 40 s21_swap_rows(temp, k - 1, i);
81 40 multiplier = -1;
82 } else {
83 4 multiplier = 0;
84 }
85 }
86
87 336 return multiplier;
88 }
89
90 40 void s21_swap_rows(matrix_t *temp, int row1, int row2) {
91
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 40 times.
128 for (int j = 0; j < temp->columns; j++) {
92 88 double t = temp->matrix[row1][j];
93 88 temp->matrix[row1][j] = temp->matrix[row2][j];
94 88 temp->matrix[row2][j] = t;
95 }
96 40 }
97
98 180 double s21_calc_determinant(matrix_t *temp) {
99 180 double result = 1.0;
100
101
2/2
✓ Branch 0 taken 428 times.
✓ Branch 1 taken 180 times.
608 for (int i = 0; i < temp->columns; i++) {
102 428 result *= temp->matrix[i][i];
103 }
104
105 180 return result;
106 }
107