-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
50 lines (40 loc) · 1.11 KB
/
matrix.cpp
File metadata and controls
50 lines (40 loc) · 1.11 KB
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
// tridiagonal matrix solver
//https://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
#include <iostream>
#include <cmath>
#include <cassert>
#include "matrix.h"
#include "cn_solver.h"
// a is lower / upper diagonal vector as matrix is symmetrix. real, only (n-1)
// b is diagonal. complex
// d is output vector
vector<complex<double>> tridiag_solve(vector<double> a, vector<complex<double>> b, vector<complex<double>> d) {
assert(b.size() == d.size());
assert(a.size() == d.size() - 1);
int n = d.size();
vector<complex<double>> x(n); //output vec
for (int j=1; j < n; j++) {
complex<double> w = a[j-1] / b[j-1];
b[j] = b[j] - w * a[j-1];
d[j] = d[j] - w * d[j-1];
}
x[n-1] = d[n-1] / b[n-1];
for (int j=n-2; j >= 0; j--) {
x[j] = (d[j] - a[j] * x[j+1]) / b[j];
}
return x;
}
void test_ex() {
vector<double> a(1);
vector<complex<double>> b(2);
vector<complex<double>> d(2);
a[0] = 1.0;
b[0] = {1.0, 1.0};
b[1] = {1.0, -1.0};
d[0] = {0.0, 2.0};
d[1] = {1.0, 1.0};
vector<complex<double>> x = tridiag_solve(a, b, d);
cout << "hello world" << endl;
cout << x[0] << endl;
cout << x[1];
}