forked from madrury/linalg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linsolve.c
73 lines (60 loc) · 2.5 KB
/
linsolve.c
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
#include <assert.h>
#include "vector.h"
#include "matrix.h"
#include "linsolve.h"
/* Solve a general linear equation Mx = v using the QR decomposition of M.
If QR is the decomposion of M, then using the fact that Q is orthogonal
QRx = v => Rx = transpose(Q)v
The matrix product transpose(Q)v is easy to compute, so the decomposition
reduces the problem to solving a linear equation Rx = y for an upper
triangular matrix R.
*/
struct vector* linsolve_qr(struct matrix* M, struct vector* v) {
assert(M->n_row == v->length);
struct qr_decomp* qr = matrix_qr_decomposition(M);
struct vector* solution = linsolve_from_qr(qr, v);
qr_decomp_free(qr);
return solution;
}
/* Solve from the qr decomposition itself. Useful if multiple equations
with the same left hand side need to be solved.
*/
struct vector* linsolve_from_qr(struct qr_decomp* qr, struct vector* v) {
struct vector* rhs = matrix_vector_multiply_Mtv(qr->q, v);
struct vector* solution = linsolve_upper_triangular(qr->r, rhs);
vector_free(rhs);
return solution;
}
/* Solve a linear equation Rx = v, where R is an upper triangular matrix.
This type of equation is easy to solve by back substitution. We work *up*
the rows of R solving for the components of x backwards. For example, the
final row in R gives the equation
r_{l,l} x_l = v_l
whose solution is simply x_l = v_l / r_{l,l}. The second to final row gives
the equation
r_(l-1,l-1} x_{l-1} + r_{l-1,l} x_{l} = v_l
which can be solved by substituting in the value of x_l already found, and
then solving the resulting equation for x_{l-1}. Continuing in this way
solves the entire system.
*/
struct vector* linsolve_upper_triangular(struct matrix* R, struct vector* v) {
assert(R->n_col == v->length);
assert(R->n_row = R->n_col);
// TODO: Check upper triangular.
int n_eq = v->length;
struct vector* solution = vector_new(n_eq);
/* back_substitute:
Tracks the part of the current equation (row) that reduces to a constant
after substituting in the values for the already solved for varaiables.
*/
float back_substitute;
for(int i = n_eq - 1; i >= 0; i--) {
back_substitute = 0;
for(int j = i+1; j <= n_eq - 1; j++) {
back_substitute += VECTOR_IDX_INTO(solution, j) * MATRIX_IDX_INTO(R, i, j);
}
VECTOR_IDX_INTO(solution, i) =
(VECTOR_IDX_INTO(v, i) - back_substitute) / MATRIX_IDX_INTO(R, i, i);
}
return solution;
}