-
Notifications
You must be signed in to change notification settings - Fork 0
/
tridiag.cpp
51 lines (41 loc) · 1017 Bytes
/
tridiag.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
#include <armadillo>
using namespace arma;
vec trimul(double a, double b, double c, vec v)
{
//Function for multiplying a tridiagonal matrix with the
//diagonals filled with doubles a,b and c.
//Returns product
int n = v.n_elem;
vec u(n);
u(0) = b*v(0) + c*v(1);
for(int i = 1; i < n-1; i++)
{
u(i) = a*v(i-1) + b*v(i) + c*v(i+1);
}
u(n-1) = a*v(n-2) + b*v(n-1);
return u;
}
void trisolve(double a, double b, double c, vec &u, vec v)
{
//Function for solving equation Au = v for u.
//A is a tridiagonal matrix with
//diagonals filled with doubles a,b and c,
// v is known.
// u is modified in place
int n = v.n_elem;
vec bv(n);
bv.fill(b);
double ac = a*c;
//First: make the matrix upper diagonal:
for(int i=1; i<n; i++)
{
bv(i) -= ac/bv(i-1);
v(i) -= a*v(i-1)/bv(i-1);
}
//Backward substitution to obtain u
u(n-1) = v(n-1)/bv(n-1);
for(int i= (n-2); i>=0 ; i--)
{
u(i) = (v(i) - c*u(i+1))/bv(i);
}
}