]> matita.cs.unibo.it Git - pkg-cerco/acc.git/blob - tests/benchs/mat_det.c
Imported Upstream version 0.2
[pkg-cerco/acc.git] / tests / benchs / mat_det.c
1
2 /* function det: computes the determinant of a square matrice in exponential
3    complexity
4    ran 50 times on 3 matrices of dimension 3*3 */
5
6
7 int mat1[9] = {-5, 2, -2, -10, 7, -1, 4, -8, 5};
8 int mat2[9] = {7, -9, 4, 8, 8, 10, -8, 10, 1};
9
10 void lmatrice (int mat[], int lmat[], int n, int l) {
11   int i, j, line = 0, m = n-1;
12
13   for (i = 0; i < n ; i++) {
14     if (i != l) {
15       for (j = 1 ; j < n ; j++)
16         lmat[line*m + j-1] = mat[i*n + j];
17       line++;
18     }
19   }
20 }
21
22 int det (int mat[], int n) {
23   int res = 0, signe = 1, i;
24   int lmat[100];
25
26   if (n == 0) return 0;
27   if (n == 1) return mat[0];
28
29   for (i = 0 ; i < n ; i++) {
30     lmatrice(mat, lmat, n, i);
31     /* The real computation has been disable to avoid overflow, but the
32        complexity is the same. */
33     res = det(lmat, n-1);
34     // res += signe * mat[i] * det(lmat, n-1);
35     // signe = -signe;
36   }
37
38   return res;
39 }
40
41 int main () {
42   int i, res1, res2, res3;
43   int size = 9;
44
45   int mat3[9] = {7, -9, 7, 6, -10, -10, -5, -4, 1};
46
47   for (i = 0 ; i < 50 ; i++) {
48     res1 = det(mat1, size);
49     res2 = det(mat2, size);
50     res3 = det(mat3, size);
51   }
52
53   return 0;
54 }