-
Notifications
You must be signed in to change notification settings - Fork 0
/
cholesky.cpp
63 lines (56 loc) · 1.41 KB
/
cholesky.cpp
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
#include <cstdio>
#include <cmath>
#include <vector>
std::vector<double> cholesky(std::vector<double>& A, const int n)
{
std::vector<double> L (n * n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < (i+1); j++)
{
double s = 0;
for (int k = 0; k < j; k++)
{
s += L[i * n + k] * L[j * n + k];
}
L[i * n + j] = (i == j) ?
sqrt(A[i * n + i] - s) :
(1.0 / L[j * n + j] * (A[i * n + j] - s));
}
}
return L;
}
void show_matrix(std::vector<double> &A, const int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
std::printf("%2.5f ", A[i * n + j]);
}
std::printf("\n");
}
}
int main()
{
int n = 3;
std::vector<double> m1 = {25, 15, -5,
15, 18, 0,
-5, 0, 11};
std::vector<double> c1 = cholesky(m1, n);
#ifdef DEBUG
show_matrix(c1, n);
std::printf("\n");
#endif
n = 4;
std::vector<double> m2 = {18, 22, 54, 42,
22, 70, 86, 62,
54, 86, 174, 134,
42, 62, 134, 106};
std::vector<double> c2 = cholesky(m2, n);
#ifdef DEBUG
show_matrix(c2, n);
std::printf("\n");
#endif
return 0;
}