-
Notifications
You must be signed in to change notification settings - Fork 5
/
tiny_matrix_x.h
473 lines (416 loc) · 14.9 KB
/
tiny_matrix_x.h
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
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TINY_MATRIXXXX_H
#define TINY_MATRIXXXX_H
#include <stdio.h>
#include "tiny_spatial_motion_vector.h"
#include "tiny_vector_x.h"
/**
* Represents a matrix with arbitrary number of columns and custom column type.
*/
template <typename TinyScalar, typename TinyConstants,
template <typename, typename> typename ColumnType>
class TinyMatrixXxX_ {
typedef ColumnType<TinyScalar, TinyConstants> ColumnVector;
// columns are stored as vectors
ColumnVector* m_columns{nullptr};
inline void allocate() {
m_columns = new ColumnVector[m_cols];
for (int i = 0; i < m_cols; ++i) m_columns[i] = ColumnVector(m_rows);
set_zero();
}
public:
int m_rows{0};
int m_cols{0};
TinyMatrixXxX_() = default;
TinyMatrixXxX_(int rows, int cols) : m_rows(rows), m_cols(cols) {
allocate();
}
inline TinyMatrixXxX_(const TinyMatrixXxX_& other)
: m_rows(other.m_rows), m_cols(other.m_cols) {
allocate();
for (int i = 0; i < m_cols; ++i) m_columns[i] = other.m_columns[i];
}
inline TinyMatrixXxX_& operator=(const TinyMatrixXxX_& other) {
m_rows = other.m_rows;
m_cols = other.m_cols;
allocate();
for (int i = 0; i < m_cols; ++i) m_columns[i] = other.m_columns[i];
return *this;
}
virtual ~TinyMatrixXxX_() { delete[] m_columns; }
void set_zero() {
for (int i = 0; i < m_cols; ++i) m_columns[i].set_zero();
}
inline const TinyScalar& operator()(int row, int col) const {
TinyConstants::FullAssert(0 <= row && row < m_rows);
TinyConstants::FullAssert(0 <= col && col < m_cols);
return m_columns[col][row];
}
inline TinyScalar& operator()(int row, int col) {
TinyConstants::FullAssert(0 <= row && row < m_rows);
TinyConstants::FullAssert(0 <= col && col < m_cols);
return m_columns[col][row];
}
inline const ColumnVector& operator[](int col) const {
TinyConstants::FullAssert(0 <= col && col < m_cols);
return m_columns[col];
}
inline ColumnVector& operator[](int col) {
TinyConstants::FullAssert(0 <= col && col < m_cols);
return m_columns[col];
}
void print(const char* txt) const {
printf("%s\n", txt);
for (int r = 0; r < m_rows; r++) {
for (int c = 0; c < m_cols; c++) {
const TinyScalar& val = (*this)(r, c);
double v = TinyConstants::getDouble(val);
printf("%f, ", v);
}
printf("\n");
}
}
TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX> transpose() const {
TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX> t(m_cols, m_rows);
for (int i = 0; i < m_cols; ++i) {
t.assign_vector_horizontal(i, 0, m_columns[i]);
}
return t;
}
TinyMatrixXxX_ operator-() {
TinyMatrixXxX_ res(m_rows, m_cols);
for (int j = 0; j < m_cols; ++j) {
res.m_columns[j] = -m_columns[j];
}
return res;
}
TinyMatrixXxX_& operator+=(const TinyMatrixXxX_& m) {
TinyConstants::FullAssert(m.m_cols == m_cols);
TinyConstants::FullAssert(m.m_rows == m_rows);
for (int j = 0; j < m_cols; ++j) {
m_columns[j] += m.m_columns[j];
}
return *this;
}
TinyMatrixXxX_ operator+(const TinyMatrixXxX_& m) {
TinyConstants::FullAssert(m.m_cols == m_cols);
TinyConstants::FullAssert(m.m_rows == m_rows);
TinyMatrixXxX_ res(m_rows, m_cols);
for (int j = 0; j < m_cols; ++j) {
res[j] = m_columns[j] + m.m_columns[j];
}
return res;
}
TinyMatrixXxX_ operator-(const TinyMatrixXxX_& m) {
TinyConstants::FullAssert(m.m_cols == m_cols);
TinyConstants::FullAssert(m.m_rows == m_rows);
TinyMatrixXxX_ res(m_rows, m_cols);
for (int j = 0; j < m_cols; ++j) {
res[j] = m_columns[j] - m.m_columns[j];
}
return res;
}
/**
* Multiples the LHS matrix times the RHS matrix.
*/
friend TinyMatrixXxX_ operator*(const TinyMatrixXxX_& lhs,
const TinyMatrixXxX_& rhs) {
TinyConstants::FullAssert(lhs.m_cols == rhs.m_rows);
TinyMatrixXxX_ res(lhs.m_rows, rhs.m_cols);
res.set_zero();
for (int i = 0; i < lhs.m_rows; ++i) {
for (int j = 0; j < rhs.m_cols; ++j) {
for (int k = 0; k < lhs.m_cols; ++k) {
res(i, j) = res(i, j) + lhs(i, k) * rhs(k, j);
}
}
}
return res;
}
/**
* Multiples the LHS matrix times the RHS vector.
*/
template <template <typename, typename> typename VectorType>
friend ColumnVector operator*(
const TinyMatrixXxX_& lhs,
const VectorType<TinyScalar, TinyConstants>& rhs) {
TinyConstants::FullAssert(lhs.m_cols == rhs.m_size);
ColumnVector res(lhs.m_rows);
res.set_zero();
for (int i = 0; i < lhs.m_rows; ++i) {
for (int j = 0; j < lhs.m_cols; ++j) {
res[i] = res[i] + lhs(i, j) * rhs[j];
}
}
return res;
}
/**
* Transposes the LHS matrix and multiplies it with the RHS vector.
* RHS should be of matrix column dimension.
*/
template <template <typename, typename> typename VectorType>
TinyVectorX<TinyScalar, TinyConstants> mul_transpose(
const VectorType<TinyScalar, TinyConstants>& rhs) {
TinyConstants::FullAssert(m_rows == rhs.m_size);
TinyVectorX<TinyScalar, TinyConstants> res(m_cols);
for (int i = 0; i < m_cols; ++i) {
res[i] = m_columns[i].dot(rhs);
}
return res;
}
// M(r,c) v(r) R(c)
template <template <typename, typename> typename VectorType>
void adj_mx_mul_transpose(
const TinyVectorX<TinyScalar, TinyConstants>& R,
const VectorType<TinyScalar, TinyConstants>& v,
TinyMatrixXxX_<TinyScalar, TinyConstants, VectorType>& Rm,
VectorType<TinyScalar, TinyConstants>& Rv) {
Rm += vvt(v, R);
Rv += (*this)*R;
}
template <template <typename, typename> typename VectorType>
void assign_vector_horizontal(
int start_row_index, int start_col_index,
const VectorType<TinyScalar, TinyConstants>& v) {
TinyConstants::FullAssert(0 <= start_col_index);
TinyConstants::FullAssert(start_col_index + v.m_size <= m_cols);
TinyConstants::FullAssert(0 <= start_row_index);
TinyConstants::FullAssert(start_row_index < m_rows);
for (int i = 0; i < v.m_size; ++i) {
(*this)(start_row_index, start_col_index + i) = v[i];
}
}
template <template <typename, typename> typename VectorType>
void assign_vector_horizontal_add(
int start_row_index, int start_col_index,
const VectorType<TinyScalar, TinyConstants>& v) {
TinyConstants::FullAssert(0 <= start_col_index);
TinyConstants::FullAssert(start_col_index + v.m_size <= m_cols);
TinyConstants::FullAssert(0 <= start_row_index);
TinyConstants::FullAssert(start_row_index < m_rows);
for (int i = 0; i < v.m_size; ++i) {
(*this)(start_row_index, start_col_index + i) += v[i];
}
}
TinyVectorX<TinyScalar, TinyConstants> get_vector_horizontal(
int start_row_index, int start_col_index, int length) const {
TinyConstants::FullAssert(0 <= start_col_index);
TinyConstants::FullAssert(start_col_index + length <= m_cols);
TinyConstants::FullAssert(0 <= start_row_index);
TinyConstants::FullAssert(start_row_index < m_rows);
TinyVectorX<TinyScalar, TinyConstants> v(length);
for (int i = 0; i < length; ++i) {
v[i] = (*this)(start_row_index, i + start_col_index);
}
return v;
}
TinyVectorX<TinyScalar, TinyConstants> get_vector_vertical(
int start_row_index, int start_col_index, int length) const {
assert(start_row_index >= 0);
assert(start_row_index + length <= m_rows);
assert(start_col_index >= 0);
assert(start_col_index < m_cols);
TinyVectorX<TinyScalar, TinyConstants> v(length);
for (int i = 0; i < length; ++i) {
v[i] = (*this)(start_row_index + i,start_col_index);
}
return v;
}
template <template <typename, typename> typename VectorType>
void assign_vector_vertical(int start_row_index, int start_col_index,
const VectorType<TinyScalar, TinyConstants>& v) {
TinyConstants::FullAssert(0 <= start_row_index);
TinyConstants::FullAssert(start_row_index + v.m_size <= m_rows);
TinyConstants::FullAssert(0 <= start_col_index);
TinyConstants::FullAssert(start_col_index < m_cols);
ColumnVector& column = m_columns[start_col_index];
for (int i = 0; i < v.m_size; ++i) {
column[i + start_row_index] = v[i];
}
}
template <typename MatrixType>
void assign_matrix(int start_row_index, int start_col_index,
const MatrixType& m) {
TinyConstants::FullAssert(0 <= start_row_index);
TinyConstants::FullAssert(start_row_index + m.m_rows <= m_rows);
TinyConstants::FullAssert(0 <= start_col_index);
TinyConstants::FullAssert(start_col_index + m.m_cols <= m_cols);
for (int j = 0; j < m.m_cols; ++j) {
ColumnVector& column = m_columns[start_col_index + j];
for (int i = 0; i < m.m_rows; ++i) {
column[start_row_index + i] = m(i, j);
}
}
}
template <typename MatrixType>
void assign_matrix_add(int start_row_index, int start_col_index,
const MatrixType& m) {
TinyConstants::FullAssert(0 <= start_row_index);
TinyConstants::FullAssert(start_row_index + m.m_rows <= m_rows);
TinyConstants::FullAssert(0 <= start_col_index);
TinyConstants::FullAssert(start_col_index + m.m_cols <= m_cols);
for (int j = 0; j < m.m_cols; ++j) {
ColumnVector& column = m_columns[start_col_index + j];
for (int i = 0; i < m.m_rows; ++i) {
column[start_row_index + i] += m(i, j);
}
}
}
TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX> block(
int start_row_index, int start_col_index, int rows, int cols) const {
assert(start_row_index >= 0);
assert(start_row_index + rows <= m_rows);
assert(start_col_index >= 0);
assert(start_col_index + cols <= m_cols);
TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX> m(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j)
m(i, j) = (*this)(i + start_row_index, j + start_col_index);
}
return m;
}
template <template <typename, typename> typename VectorType>
static TinyMatrixXxX_<TinyScalar, TinyConstants, VectorType> vvt(
const VectorType<TinyScalar, TinyConstants>& ma,
const TinyVectorX<TinyScalar, TinyConstants>& mb) {
TinyMatrixXxX_<TinyScalar, TinyConstants, VectorType>
m(ma.m_size, mb.m_size);
for (int i = 0; i < mb.m_size; i++) {
m[i] = ma * mb[i];
}
return m;
}
/**
* main method for Cholesky decomposition.
* input/output a Symmetric positive def. matrix
* output diagonal vector of resulting diag of a
* inspired by public domain https://math.nist.gov/javanumerics/jama
*/
bool cholesky_decomposition(
TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX>& a,
TinyVectorX<TinyScalar, TinyConstants>& diagonal) const {
int i, j, k;
TinyScalar sum;
int n = a.m_cols;
bool is_positive_definite = true;
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
sum = a[i][j];
for (k = i - 1; k >= 0; k--) {
sum = sum - a[i][k] * a[j][k];
}
if (i == j) {
if (TinyConstants::getBool(
sum <= TinyConstants::zero())) {
is_positive_definite = false;
break;
}
diagonal[i] = TinyConstants::sqrt1(sum);
} else {
a[j][i] = sum / diagonal[i];
}
}
}
return is_positive_definite;
}
/**
* Inverse of Cholesky decomposition.
*
* input A Symmetric positive def. matrix
* output a inverse of lower decomposed matrix
* uses cholesky_decomposition
*/
bool inverse_cholesky_decomposition(
const TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX>& A,
TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX>& a) const {
int i, j, k;
int n = A.m_rows;
TinyScalar sum;
TinyVectorX<TinyScalar, TinyConstants> diagonal(A.m_rows);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) a[i][j] = A[i][j];
bool is_positive_definite = cholesky_decomposition(a, diagonal);
if (is_positive_definite) {
for (i = 0; i < n; i++) {
a[i][i] = TinyConstants::one() / diagonal[i];
for (j = i + 1; j < n; j++) {
sum = TinyConstants::zero();
for (k = i; k < j; k++) {
sum = sum - a[j][k] * a[k][i];
}
a[j][i] = sum / diagonal[j];
}
}
}
return is_positive_definite;
}
/**
* Inverse of a matrix, using Cholesky decomposition.
*
* input A Symmetric positive def. matrix
* input a storage for the result
* output boolean is_positive_definite if operation succeeded
*/
bool inversed(
TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX>& a) const {
assert(m_cols == m_cols);
assert(a.m_cols == m_cols);
assert(a.m_rows == m_rows);
const TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX>& A = *this;
bool is_positive_definite = inverse_cholesky_decomposition(A, a);
if (is_positive_definite) {
int n = m_cols;
int i, j, k;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
a[i][j] = TinyConstants::zero();
}
}
for (i = 0; i < n; i++) {
a[i][i] = a[i][i] * a[i][i];
for (k = i + 1; k < n; k++) {
a[i][i] = a[i][i] + a[k][i] * a[k][i];
}
for (j = i + 1; j < n; j++) {
for (k = j; k < n; k++) {
a[i][j] = a[i][j] + a[k][i] * a[k][j];
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < i; j++) {
a[i][j] = a[j][i];
}
}
}
return is_positive_definite;
}
void adj_inversed(
const TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX>& Rinv,
const TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX>& b,
TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX>& R) const {
R += - b.transpose() * Rinv * b.transpose();
}
};
template <typename TinyScalar, typename TinyConstants>
using TinyMatrix6xX =
TinyMatrixXxX_<TinyScalar, TinyConstants, TinySpatialMotionVector>;
template <typename TinyScalar, typename TinyConstants>
using TinyMatrix3xX = TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVector3>;
template <typename TinyScalar, typename TinyConstants>
using TinyMatrixXxX = TinyMatrixXxX_<TinyScalar, TinyConstants, TinyVectorX>;
#endif // TINY_MATRIXXXX_H