-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdets.h
130 lines (98 loc) · 2.83 KB
/
dets.h
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright (c) 2016 The Caroline authors. All rights reserved.
# Use of this source file is governed by a MIT license that can be found in the
# LICENSE file.
# Author: Glazachev Vladimir <[email protected]>
#ifndef DETS_H
#define DETS_H
#include "apost_statical.h"
#include "gauss.h"
#include "interval.h"
#include "matrix.h"
namespace interval {
// Computes the determenant of matrix using Gauss
// elimination (without pivoting).
template<class IntervalT>
IntervalT det(Matrix<IntervalT> matrix) {
size_t n = matrix.nrow();
gauss_elimination(matrix);
IntervalT d = IntervalT(1);
for (size_t i = 0; i < n; ++i)
d = d * matrix.at(i, i);
return d;
}
// Computes the determenant of matrix using Gauss
// elimination (with pivoting).
template<class IntervalT>
IntervalT det_pivot(Matrix<IntervalT> matrix) {
size_t n = matrix.nrow();
int sign = gauss_elimination_pivot(matrix);
IntervalT d = IntervalT(1);
for (size_t i = 0; i < n; ++i) {
d = d * matrix.at(i, i);
}
d = IntervalT(sign) * d;
return d;
}
// Computes the determenant of matrix using Gaussian
// elimination (without pivoting) and statical implementation
// of aposteriori method.
ArbInterval det_inv(Matrix<ArbInterval> M) {
Matrix<ArbInterval> init = M;
size_t n = M.nrow();
if (n == 0) {
return ArbInterval(0);
}
if (n == 1) {
return M.at(0, 0);
}
gauss_elimination(M);
ArbInterval f = 1;
for (size_t i = 0; i < n; ++i) {
f = f * M.at(i, i);
}
ArbInterval det = f;
Matrix<ArbInterval> dM(n, n, 0);
ArbInterval df = 1;
// inverse step
for (int i = n - 1; i >= 0; --i) {
f /= M.at(i, i);
dM.at(i, i) += df * f;
df *= M.at(i, i);
}
GaussInverse(M, dM);
det = ArbInterval(det.val(), ComputeError(init, dM));
return det;
}
// Computes the determenant of matrix using Gaussian
// elimination (with pivoting) and statical implementation
// of aposteriori method.
ArbInterval det_inv_pivot(Matrix<ArbInterval> M) {
Matrix<ArbInterval> init = M;
Matrix<ArbInterval> i2 = M;
size_t n = M.nrow();
if (n == 0) {
return ArbInterval(0);
}
if (n == 1) {
return M.at(0, 0);
}
int sign = gauss_elimination_pivot(M);
ArbInterval f = 1;
for (size_t i = 0; i < n; ++i) {
f = f * M.at(i, i);
}
ArbInterval det = ArbInterval(sign) * f;
Matrix<ArbInterval> dM(n, n, 0);
ArbInterval df = sign;
// inverse step
for (int i = n - 1; i >= 0; --i) {
f /= M.at(i, i);
dM.at(i, i) += df * f;
df *= M.at(i, i);
}
GaussInverse(M, dM);
det = ArbInterval(det.val(), ComputeError(init, dM));
return det;
}
} // namespace interval
#endif // DETS_H