-
Notifications
You must be signed in to change notification settings - Fork 102
/
fixmatrix.c
534 lines (431 loc) · 15.4 KB
/
fixmatrix.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include "fixmatrix.h"
#include "fixarray.h"
/****************************
* Initialization functions *
****************************/
void mf16_fill(mf16 *dest, fix16_t value)
{
int row, column;
dest->errors = 0;
for (row = 0; row < dest->rows; row++)
{
for (column = 0; column < dest->columns; column++)
{
dest->data[row][column] = value;
}
}
}
void mf16_fill_diagonal(mf16 *dest, fix16_t value)
{
int row;
mf16_fill(dest, 0);
for (row = 0; row < dest->rows; row++)
{
dest->data[row][row] = value;
}
}
/*********************************
* Operations between 2 matrices *
*********************************/
void mf16_mul(mf16 *dest, const mf16 *a, const mf16 *b)
{
int row, column;
// If dest and input matrices alias, we have to use a temp matrix.
mf16 tmp;
fa16_unalias(dest, (void**)&a, (void**)&b, &tmp, sizeof(tmp));
dest->errors = a->errors | b->errors;
if (a->columns != b->rows)
dest->errors |= FIXMATRIX_DIMERR;
dest->rows = a->rows;
dest->columns = b->columns;
for (row = 0; row < dest->rows; row++)
{
for (column = 0; column < dest->columns; column++)
{
dest->data[row][column] = fa16_dot(
&a->data[row][0], 1,
&b->data[0][column], FIXMATRIX_MAX_SIZE,
a->columns);
if (dest->data[row][column] == fix16_overflow)
dest->errors |= FIXMATRIX_OVERFLOW;
}
}
}
// Multiply transpose of at with b
void mf16_mul_at(mf16 *dest, const mf16 *at, const mf16 *b)
{
int row, column;
// If dest and input matrices alias, we have to use a temp matrix.
mf16 tmp;
fa16_unalias(dest, (void**)&at, (void**)&b, &tmp, sizeof(tmp));
dest->errors = at->errors | b->errors;
if (at->rows != b->rows)
dest->errors |= FIXMATRIX_DIMERR;
dest->rows = at->columns;
dest->columns = b->columns;
for (row = 0; row < dest->rows; row++)
{
for (column = 0; column < dest->columns; column++)
{
dest->data[row][column] = fa16_dot(
&at->data[0][row], FIXMATRIX_MAX_SIZE,
&b->data[0][column], FIXMATRIX_MAX_SIZE,
at->rows);
if (dest->data[row][column] == fix16_overflow)
dest->errors |= FIXMATRIX_OVERFLOW;
}
}
}
void mf16_mul_bt(mf16 *dest, const mf16 *a, const mf16 *bt)
{
int row, column;
// If dest and input matrices alias, we have to use a temp matrix.
mf16 tmp;
fa16_unalias(dest, (void**)&a, (void**)&bt, &tmp, sizeof(tmp));
dest->errors = a->errors | bt->errors;
if (a->columns != bt->columns)
dest->errors |= FIXMATRIX_DIMERR;
dest->rows = a->rows;
dest->columns = bt->rows;
for (row = 0; row < dest->rows; row++)
{
for (column = 0; column < dest->columns; column++)
{
dest->data[row][column] = fa16_dot(
&a->data[row][0], 1,
&bt->data[column][0], 1,
a->columns);
if (dest->data[row][column] == fix16_overflow)
dest->errors |= FIXMATRIX_OVERFLOW;
}
}
}
static void mf16_addsub(mf16 *dest, const mf16 *a, const mf16 *b, uint8_t add)
{
int row, column;
dest->errors = a->errors | b->errors;
if (a->columns != b->columns || a->rows != b->rows)
dest->errors |= FIXMATRIX_DIMERR;
dest->rows = a->rows;
dest->columns = a->columns;
for (row = 0; row < dest->rows; row++)
{
for (column = 0; column < dest->columns; column++)
{
fix16_t sum;
if (add)
sum = fix16_add(a->data[row][column], b->data[row][column]);
else
sum = fix16_sub(a->data[row][column], b->data[row][column]);
if (sum == fix16_overflow)
dest->errors |= FIXMATRIX_OVERFLOW;
dest->data[row][column] = sum;
}
}
}
void mf16_add(mf16 *dest, const mf16 *a, const mf16 *b)
{
mf16_addsub(dest, a, b, 1);
}
void mf16_sub(mf16 *dest, const mf16 *a, const mf16 *b)
{
mf16_addsub(dest, a, b, 0);
}
/*********************************
* Operations on a single matrix *
*********************************/
void mf16_transpose(mf16 *dest, const mf16 *matrix)
{
int row, column;
// This code is a bit tricky in order to work
// in the situation when dest = matrix.
// Before writing a value in dest, we must copy
// the corresponding value from matrix to a temporary
// variable.
// We actually transpose a n by n square matrix, because
// that can be done in-place easily. Because mf16 always
// allocates a square area even if actual matrix is smaller,
// this is not a problem.
int n = matrix->rows;
if (matrix->columns > n) n = matrix->columns;
uint8_t rows = matrix->rows;
dest->rows = matrix->columns;
dest->columns = rows;
dest->errors = matrix->errors;
for (row = 0; row < n; row++)
{
for (column = 0; column < row; column++)
{
fix16_t temp = matrix->data[row][column];
dest->data[row][column] = matrix->data[column][row];
dest->data[column][row] = temp;
}
dest->data[row][row] = matrix->data[row][row];
}
}
/***************************************
* Operations of a matrix and a scalar *
***************************************/
static void mf16_divmul_s(mf16 *dest, const mf16 *matrix, fix16_t scalar, uint8_t mul)
{
int row, column;
dest->rows = matrix->rows;
dest->columns = matrix->columns;
dest->errors = matrix->errors;
for (row = 0; row < dest->rows; row++)
{
for (column = 0; column < dest->columns; column++)
{
fix16_t value = matrix->data[row][column];
if (mul)
value = fix16_mul(value, scalar);
else
value = fix16_div(value, scalar);
if (value == fix16_overflow)
dest->errors |= FIXMATRIX_OVERFLOW;
dest->data[row][column] = value;
}
}
}
void mf16_mul_s(mf16 *dest, const mf16 *matrix, fix16_t scalar)
{
mf16_divmul_s(dest, matrix, scalar, 1);
}
void mf16_div_s(mf16 *dest, const mf16 *matrix, fix16_t scalar)
{
mf16_divmul_s(dest, matrix, scalar, 0);
}
/***************************************************
* Solving linear equations using QR decomposition *
***************************************************/
// Takes two columns vectors, v and u, of size n.
// Performs v = v - dot(u, v) * u,
// where dot(u,v) has already been computed
// u is assumed to be an unit vector.
static void subtract_projection(fix16_t *v, const fix16_t *u, fix16_t dot, int n, uint8_t *errors)
{
while (n--)
{
// For unit vector u, u[i] <= 1
// Therefore this multiplication cannot overflow
fix16_t product = fix16_mul(dot, *u);
// Overflow here is rare, but possible.
fix16_t diff = fix16_sub(*v, product);
if (diff == fix16_overflow)
*errors |= FIXMATRIX_OVERFLOW;
*v = diff;
v += FIXMATRIX_MAX_SIZE;
u += FIXMATRIX_MAX_SIZE;
}
}
void mf16_qr_decomposition(mf16 *q, mf16 *r, const mf16 *matrix, int reorthogonalize)
{
int i, j, reorth;
fix16_t dot, norm;
uint8_t stride = FIXMATRIX_MAX_SIZE;
uint8_t n = matrix->rows;
// This uses the modified Gram-Schmidt algorithm.
// subtract_projection takes advantage of the fact that
// previous columns have already been normalized.
// We start with q = matrix
if (q != matrix)
{
*q = *matrix;
}
// R is initialized to have square size of cols(A) and zeroed.
r->columns = matrix->columns;
r->rows = matrix->columns;
r->errors = 0;
mf16_fill(r, 0);
// Now do the actual Gram-Schmidt for the rows.
for (j = 0; j < q->columns; j++)
{
for (reorth = 0; reorth <= reorthogonalize; reorth++)
{
for (i = 0; i < j; i++)
{
fix16_t *v = &q->data[0][j];
fix16_t *u = &q->data[0][i];
dot = fa16_dot(v, stride, u, stride, n);
subtract_projection(v, u, dot, n, &q->errors);
if (dot == fix16_overflow)
q->errors |= FIXMATRIX_OVERFLOW;
r->data[i][j] += dot;
}
}
// Normalize the row in q
norm = fa16_norm(&q->data[0][j], stride, n);
r->data[j][j] = norm;
if (norm == fix16_overflow)
q->errors |= FIXMATRIX_OVERFLOW;
if (norm < 5 && norm > -5)
{
// Nearly zero norm, which means that the row
// was linearly dependent.
q->errors |= FIXMATRIX_SINGULAR;
continue;
}
for (i = 0; i < n; i++)
{
// norm >= v[i] for all i, therefore this division
// doesn't overflow unless norm approaches 0.
q->data[i][j] = fix16_div(q->data[i][j], norm);
}
}
r->errors = q->errors;
}
void mf16_solve(mf16 *dest, const mf16 *q, const mf16 *r, const mf16 *matrix)
{
int row, column, variable;
if (r->columns != r->rows || r->columns != q->columns || r == dest)
{
dest->errors |= FIXMATRIX_USEERR;
return;
}
// Ax=b <=> QRx=b <=> Q'QRx=Q'b <=> Rx=Q'b
// Q'b is calculated directly and x is then solved row-by-row.
mf16_mul_at(dest, q, matrix);
for (column = 0; column < dest->columns; column++)
{
for (row = dest->rows - 1; row >= 0; row--)
{
fix16_t value = dest->data[row][column];
// Subtract any already solved variables
for (variable = row + 1; variable < r->columns; variable++)
{
fix16_t multiplier = r->data[row][variable];
fix16_t known_value = dest->data[variable][column];
fix16_t product = fix16_mul(multiplier, known_value);
value = fix16_sub(value, product);
if (product == fix16_overflow || value == fix16_overflow)
{
dest->errors |= FIXMATRIX_OVERFLOW;
}
}
// Now value = R_ij x_i <=> x_i = value / R_ij
fix16_t divider = r->data[row][row];
if (divider == 0)
{
dest->errors |= FIXMATRIX_SINGULAR;
dest->data[row][column] = 0;
continue;
}
fix16_t result = fix16_div(value, divider);
dest->data[row][column] = result;
if (result == fix16_overflow)
{
dest->errors |= FIXMATRIX_OVERFLOW;
}
}
}
}
/**************************
* Cholesky decomposition *
**************************/
void mf16_cholesky(mf16 *dest, const mf16 *matrix)
{
// This is the Cholesky–Banachiewicz algorithm.
// Refer to http://en.wikipedia.org/wiki/Cholesky_decomposition#The_Cholesky.E2.80.93Banachiewicz_and_Cholesky.E2.80.93Crout_algorithms
int row, column, k;
dest->errors = matrix->errors;
if (matrix->rows != matrix->columns)
dest->errors |= FIXMATRIX_DIMERR;
dest->rows = dest->columns = matrix->rows;
for (row = 0; row < dest->rows; row++)
{
for (column = 0; column < dest->columns; column++)
{
if (row == column)
{
// Value on the diagonal
// Ljj = sqrt(Ajj - sum(Ljk^2, k = 1..(j-1))
fix16_t value = matrix->data[row][column];
for (k = 0; k < column; k++)
{
fix16_t Ljk = dest->data[row][k];
Ljk = fix16_mul(Ljk, Ljk);
value = fix16_sub(value, Ljk);
if (value == fix16_overflow || Ljk == fix16_overflow)
dest->errors |= FIXMATRIX_OVERFLOW;
}
if (value < 0)
{
if (value < -65)
dest->errors |= FIXMATRIX_NEGATIVE;
value = 0;
}
dest->data[row][column] = fix16_sqrt(value);
}
else if (row < column)
{
// Value above diagonal
dest->data[row][column] = 0;
}
else
{
// Value below diagonal
// Lij = 1/Ljj (Aij - sum(Lik Ljk, k = 1..(j-1)))
fix16_t value = matrix->data[row][column];
for (k = 0; k < column; k++)
{
fix16_t Lik = dest->data[row][k];
fix16_t Ljk = dest->data[column][k];
fix16_t product = fix16_mul(Lik, Ljk);
value = fix16_sub(value, product);
if (value == fix16_overflow || product == fix16_overflow)
dest->errors |= FIXMATRIX_OVERFLOW;
}
fix16_t Ljj = dest->data[column][column];
value = fix16_div(value, Ljj);
dest->data[row][column] = value;
if (value == fix16_overflow)
dest->errors |= FIXMATRIX_OVERFLOW;
}
}
}
}
/***********************************
* Lower-triangular matrix inverse *
**********************************/
void mf16_invert_lt(mf16 *dest, const mf16 *matrix)
{
// This is port of the algorithm as found in the Efficient Java Matrix Library
// https://code.google.com/p/efficient-java-matrix-library
int_fast8_t i, j, k;
const uint_fast8_t n = matrix->rows;
// If dest and input matrices alias, we have to use a temp matrix.
mf16 tmp;
fa16_unalias(dest, (void**)&matrix, (void**)&matrix, &tmp, sizeof(tmp));
dest->errors = dest->errors | matrix->errors;
// TODO reorder these operations to avoid cache misses
// inverts the lower triangular system and saves the result
// in the upper triangle to minimize cache misses
for (i = 0; i < n; ++i)
{
const fix16_t el_ii = matrix->data[i][i];
for (j = 0; j <= i; ++j)
{
fix16_t sum = (i == j) ? fix16_one : 0;
for (k = i - 1; k >= j; --k)
{
sum = fix16_sub(sum, fix16_mul(matrix->data[i][k], dest->data[j][k]));
}
dest->data[j][i] = fix16_div(sum, el_ii);
}
}
// solve the system and handle the previous solution being in the upper triangle
// takes advantage of symmetry
for (i = n - 1; i >= 0; --i)
{
const fix16_t el_ii = matrix->data[i][i];
for (j = 0; j <= i; ++j)
{
fix16_t sum = (i < j) ? 0 : dest->data[j][i];
for (k = i + 1; k < n; ++k)
{
sum = fix16_sub(sum, fix16_mul(matrix->data[k][i], dest->data[j][k]));
}
dest->data[i][j] = dest->data[j][i] = fix16_div(sum, el_ii);
}
}
}