-
Notifications
You must be signed in to change notification settings - Fork 23
/
matrixadv.c
193 lines (159 loc) · 5.77 KB
/
matrixadv.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "matrix.h"
#include <stdio.h>
/*===========================================================================
* LUdecomposition
* Given a matrix A and two more null values, the function uses Crout's
* algorithm (page 44 of Numerical Recipes in C) to perform the
* LU decomposition of matrix A.
*=========================================================================*/
void LUdecomposition(matrix* a, matrix** l, matrix** u) {
int i, j, k;
double* ptrA;
double* ptrL;
double* ptrU;
double sum;
assert(a->width == a->height, "Matrix A must be square");
assert(*l == NULL && *u == NULL, "Matricies L and U must be null");
*l = makeMatrix(a->width, a->height);
*u = makeMatrix(a->width, a->height);
// Step 1: Assign 1 to the diagonal of the lower matrix.
ptrL = (*l)->data;
for (i = 0; i < a->width; i++) {
*ptrL = 1.0;
ptrL += a->width + 1;
}
// Step 2
for (j = 0; j < a->width; j++) {
// Part A: Solve for the upper matrix.
for (i = 0; i <= j; i++) {
sum = 0.0;
for (k = 0; k < i; k++) {
sum += (*l)->data[i * a->width + k] * (*u)->data[k * a->width + j];
}
(*u)->data[i * a->width + j] = a->data[i * a->width + j] - sum;
}
// Part B: Solve fpr the lower matrix
for (i = j+1; i < a->width; i++) {
sum = 0.0;
for (k = 0; k < j; k++) {
sum += (*l)->data[i * a->width + k] * (*u)->data[k * a->width + j];
}
(*l)->data[i * a->width + j] = 1.0/(*u)->data[j * a->width + j] * ( a->data[i * a->width + j] - sum);
}
}
return;
}
/*===========================================================================
* determinantMatrix
* Given a matrix A, returns the determinant.
* Based on the formula on page 49 of Numerical Recipes in C.
*=========================================================================*/
double determinantMatrix(matrix* a) {
double product = 0.0;
matrix* l = NULL;
matrix* u = NULL;
int i;
assert(a->width == a->height, "Matrix A must be square.");
LUdecomposition(a, &l, &u);
// Get the product of upper matrix diagonal
// We don't need the lower matrix for this calculation.
for (i = 0; i < a->width; i++) {
product *= u->data[i * a->width + i];
}
freeMatrix(l);
freeMatrix(u);
return product;
}
/*===========================================================================
* solver
* Solves a system of liner equations using LU decomposition. This requires
* an A: n by n matrix and B: n by p matrix, where
* A * X = b
* The algorithm returns X, which will be a n by p matrix.
*
* This algorithm is described on page 121 of the book "Matrix Computations"
* by Golub and Loan.
*=========================================================================*/
matrix* solver(matrix* a, matrix* b) {
int i, j, k;
double sum;
matrix *l = NULL;
matrix *u = NULL;
matrix *y;
matrix *x;
double* row;
assert(a->height == b->height, "In the solver, both matrices should have the same height.");
LUdecomposition(a, &l, &u);
y = makeMatrix(1, a->height);
x = makeMatrix(b->width, b->height);
for (k = 0; k < b->width; k++) {
// Perform backward subsitituion with L
// L * y = B_k
for (i = 0; i < a->height; i++) {
row = l->data + i * a->width;
sum = 0;
for (j = 0; j < i; j++) {
sum += y->data[j] * (*row++);
}
y->data[i] = (b->data[i * b->width + k] - sum) / *row;
}
// Perform backward subsitituion again with U
// U * x = y
for (i = a->height - 1; i >= 0; i--) {
row = u->data + i * a->width + (a->width - 1);
sum = 0;
for (j = a->width - 1; j > i; j--) {
sum += x->data[j * b->width + k] * (*row--);
}
x->data[i * b->width + k] = (y->data[i] - sum) / *row;
}
}
freeMatrix(l);
freeMatrix(u);
freeMatrix(y);
return x;
}
/*===========================================================================
* inverseMatrix
* Given a matrix A, returns the determinant.
*
* This algorithm is described on page 121 of the book "Matrix Computations"
* by Golub and Loan.
*=========================================================================*/
matrix* inverseMatrix(matrix* a) {
matrix* eye;
matrix* inv;
assert(a->width == a->height, "Matrix A must be square.");
eye = eyeMatrix(a->width);
inv = solver(a, eye);
freeMatrix(eye);
return inv;
}
/*===========================================================================
* row_echelon_form
* Given a matrix A, returns the same matrix in row-echelon form
*=========================================================================*/
matrix* row_echelon_form(matrix* a) {
matrix* out = copyMatrix(a);
double* outPtr = out->data;
double* prevPtr;
int i, j;
// First, divide each element in the first row by the first element.
double scale = *outPtr;
for (i = 0; i < out->width; i++) {
*outPtr++ /= scale;
}
// Next, for each row beyond the first, we scale the previous
// row by the first non-zero value in the current row, then
// subtract that result from the current row. That should make
// The first non-zero value now zero. Then we divide each element
// on the current row by the next non-zero value.
for (j = 1; j < out->height; j++) {
outPtr = out->data + (j * out->width) + j;
prevPtr = out->data + (j-1 * out->width) + j;
scale = *outPtr;
for (i = j; i < out->width; i++) {
*outPtr = *outPtr - (*prevPtr * scale);
}
}
}