-
Notifications
You must be signed in to change notification settings - Fork 57
/
matrix.hh
517 lines (442 loc) · 15 KB
/
matrix.hh
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
// -*- c++ -*-
// Copyright (C) 2009 Tom Drummond (twd20@cam.ac.uk),
// Ed Rosten (er258@cam.ac.uk)
//
// This file is part of the TooN Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
namespace TooN {
/**
A matrix.
Support is provided for all the usual matrix operations:
- the (a,b) notation can be used to access an element directly
- the [] operator can be used to yield a vector from a matrix (which can be used
as an l-value)
- they can be added and subtracted
- they can be multiplied (on either side) or divided by a scalar on the right:
- they can be multiplied by matrices or vectors
- submatrices can be extracted using the templated slice() member function
- they can be transposed (and the transpose used as an l-value)
- inverse is \e not supported. Use one of the @link gDecomps matrix
decompositions @endlink instead
See individual member function documentation for examples of usage.
\par Statically-sized matrices
The library provides classes for statically and dynamically sized matrices. As
with @link Vector Vectors@endlink, statically sized matrices are more efficient,
since their size is determined at compile-time, not run-time.
To create a \f$3\times4\f$ matrix, use:
@code
Matrix<3,4> M;
@endcode
or replace 3 and 4 with the dimensions of your choice. If the matrix is square,
it can be declared as:
@code
Matrix<3> M;
@endcode
which just is a synonym for <code>Matrix<3,3></code>. Matrices can also be
constructed from pointers or static 1D or 2D arrays of doubles:
@code
Matrix<2,3, Reference::RowMajor> M2 = Data(1,2,3,4,5,6);
@endcode
\par Dynamically-sized matrices
To create a dynamically sized matrix, use:
@code
Matrix<> M(num_rows, num_cols);
@endcode
where \a num_rows and \a num_cols are integers which will be evaluated at run
time.
Half-dynamic matriced can be constructed in either dimension:
@code
Matrix<Dynamic, 2> M(num_rows, 2);
@endcode
note that the static dimension must be provided, but it is ignored.
@endcode
<code>Matrix<></code> is a synonym for <code> Matrix<Dynamic, Dynamic> </code>.
\par Row-major and column-major
The library supports both row major (the default - but you can change this if
you prefer) and column major layout ordering. Row major implies that the matrix
is laid out in memory in raster scan order:
\f[\begin{matrix}\text{Row major} & \text {Column major}\\
\begin{bmatrix}1&2&3\\4&5&6\\7&8&9\end{bmatrix} &
\begin{bmatrix}1&4&7\\2&5&8\\3&6&9\end{bmatrix} \end{matrix}\f]
You can override the default for a specific matrix by specifying the layout when
you construct it:
@code
Matrix<3,3,double,ColMajor> M1;
Matrix<Dynamic,Dynamic,double,RowMajor> M2(nrows, ncols);
@endcode
In this case the precision template argument must be given as it precedes the layout argument
@ingroup gLinAlg
**/
template <int Rows=Dynamic, int Cols=Rows, class Precision=DefaultPrecision, class Layout = RowMajor>
struct Matrix : public Layout::template MLayout<Rows, Cols, Precision>
{
public:
using Layout::template MLayout<Rows, Cols, Precision>::my_data;
using Layout::template MLayout<Rows, Cols, Precision>::num_rows;
using Layout::template MLayout<Rows, Cols, Precision>::num_cols;
//Use Tom's sneaky constructor hack...
///@name Construction and destruction
///@{
///Construction of static matrices. Values are not initialized.
Matrix(){}
///Construction of dynamic matrices. Values are not initialized.
Matrix(int rows, int cols) :
Layout::template MLayout<Rows,Cols,Precision>(rows, cols)
{}
///Construction of statically sized slice matrices
Matrix(Precision* p) :
Layout::template MLayout<Rows, Cols, Precision>(p)
{}
///Construction of dynamically sized slice matrices
Matrix(Precision* p, int r, int c) :
Layout::template MLayout<Rows, Cols, Precision>(p, r, c)
{}
/// Advanced construction of dynamically sized slice matrices.
/// Internal constructor used by GenericMBase::slice(...).
Matrix(Precision* data, int rows, int cols, int rowstride, int colstride, Internal::Slicing)
:Layout::template MLayout<Rows, Cols, Precision>(data, rows, cols, rowstride, colstride){}
//See vector.hh and allocator.hh for details about why the
//copy constructor should be default.
///Construction from an operator.
template <class Op>
inline Matrix(const Operator<Op>& op)
:Layout::template MLayout<Rows,Cols,Precision>(op)
{
op.eval(*this);
}
/// constructor from arbitrary matrix
template<int Rows2, int Cols2, typename Precision2, typename Base2>
inline Matrix(const Matrix<Rows2, Cols2,Precision2,Base2>& from)
:Layout::template MLayout<Rows,Cols,Precision>(from.num_rows(), from.num_cols())
{
operator=(from);
}
///@}
///@name Assignment
///@{
/// operator = from copy
inline Matrix& operator= (const Matrix& from)
{
SizeMismatch<Rows, Rows>::test(num_rows(), from.num_rows());
SizeMismatch<Cols, Cols>::test(num_cols(), from.num_cols());
for(int r=0; r < num_rows(); r++)
for(int c=0; c < num_cols(); c++)
(*this)[r][c] = from[r][c];
return *this;
}
// operator = 0-ary operator
template<class Op> inline Matrix& operator= (const Operator<Op>& op)
{
op.eval(*this);
return *this;
}
// operator =
template<int Rows2, int Cols2, typename Precision2, typename Base2>
Matrix& operator= (const Matrix<Rows2, Cols2, Precision2, Base2>& from)
{
SizeMismatch<Rows, Rows2>::test(num_rows(), from.num_rows());
SizeMismatch<Cols, Cols2>::test(num_cols(), from.num_cols());
for(int r=0; r < num_rows(); r++)
for(int c=0; c < num_cols(); c++)
(*this)[r][c] = from[r][c];
return *this;
}
///@}
///@name operations on the matrix
///@{
Matrix& operator*=(const Precision& rhs)
{
for(int r=0; r < num_rows(); r++)
for(int c=0; c < num_cols(); c++)
(*this)[r][c] *= rhs;
return *this;
}
Matrix& operator/=(const Precision& rhs)
{
for(int r=0; r < num_rows(); r++)
for(int c=0; c < num_cols(); c++)
(*this)[r][c] /= rhs;
return *this;
}
template<int Rows2, int Cols2, typename Precision2, typename Base2>
Matrix& operator+= (const Matrix<Rows2, Cols2, Precision2, Base2>& from)
{
SizeMismatch<Rows, Rows2>::test(num_rows(), from.num_rows());
SizeMismatch<Cols, Cols2>::test(num_cols(), from.num_cols());
for(int r=0; r < num_rows(); r++)
for(int c=0; c < num_cols(); c++)
(*this)[r][c] += from[r][c];
return *this;
}
template<class Op>
Matrix& operator+=(const Operator<Op>& op)
{
op.plusequals(*this);
return *this;
}
template<class Op>
Matrix& operator-=(const Operator<Op>& op)
{
op.minusequals(*this);
return *this;
}
template<int Rows2, int Cols2, typename Precision2, typename Base2>
Matrix& operator-= (const Matrix<Rows2, Cols2, Precision2, Base2>& from)
{
SizeMismatch<Rows, Rows2>::test(num_rows(), from.num_rows());
SizeMismatch<Cols, Cols2>::test(num_cols(), from.num_cols());
for(int r=0; r < num_rows(); r++)
for(int c=0; c < num_cols(); c++)
(*this)[r][c] -= from[r][c];
return *this;
}
template<int Rows2, int Cols2, typename Precision2, typename Base2>
bool operator== (const Matrix<Rows2, Cols2, Precision2, Base2>& rhs) const
{
SizeMismatch<Rows, Rows2>::test(num_rows(), rhs.num_rows());
SizeMismatch<Cols, Cols2>::test(num_cols(), rhs.num_cols());
for(int r=0; r < num_rows(); r++)
for(int c=0; c < num_cols(); c++)
if((*this)[r][c] != rhs[r][c])
return 0;
return 1;
}
template<int Rows2, int Cols2, typename Precision2, typename Base2>
bool operator!= (const Matrix<Rows2, Cols2, Precision2, Base2>& rhs) const
{
SizeMismatch<Rows, Rows2>::test(num_rows(), rhs.num_rows());
SizeMismatch<Cols, Cols2>::test(num_cols(), rhs.num_cols());
for(int r=0; r < num_rows(); r++)
for(int c=0; c < num_cols(); c++)
if((*this)[r][c] != rhs[r][c])
return 1;
return 0;
}
template<class Op>
bool operator!=(const Operator<Op>& op)
{
return op.notequal(*this);
}
///@}
/// @name Misc
/// @{
/// return me as a non const reference - useful for temporaries
Matrix& ref()
{
return *this;
}
///@}
#ifdef DOXYGEN_INCLUDE_ONLY_FOR_DOCS
/**
Access an element from the matrix.
The index starts at zero, i.e. the top-left element is m(0, 0).
@code
Matrix<2,3> m(Data(
1,2,3
4,5,6));
double e = m(1,2); // now e = 6.0;
@endcode
@internal
This method is not defined by Matrix: it is inherited.
*/
const double& operator() (int r, int c) const;
/**
Access an element from the matrix.
@param row_col <code>row_col.first</code> holds the row, <code>row_col.second</code> holds the column.
@internal
This method is not defined by Matrix: it is inherited.
*/
const double& operator[](const std::pair<int,int>& row_col) const;
/**
@overload
*/
double& operator[](const std::pair<int,int>& row_col);
/**
Access an element from the matrix.
This can be used as either an r-value or an l-value. The index starts at zero,
i.e. the top-left element is m(0, 0).
@code
Matrix<2,3> m(Data(
1,2,3
4,5,6));
Matrix<2,3> m(d);
m(1,2) = 8; // now d = [1 2 3]
// [4 5 8]
@endcode
@internal
This method is not defined by Matrix: it is inherited.
*/
double& operator() (int r, int c);
/**
Access a row from the matrix.
This can be used either as an r-value or an l-value. The index starts at zero,
i.e. the first row is m[0]. To extract a column from a matrix, apply [] to the
transpose of the matrix (see example). This can be used either as an r-value
or an l-value. The index starts at zero, i.e. the first row (or column) is
m[0].
@code
Matrix<2,3> m(Data(
1,2,3
4,5,6));
Matrix<2,3> m(d);
Vector<3> v = m[1]; // now v = [4 5 6];
Vector<2> v2 = m.T()[0]; // now v2 = [1 4];
@endcode
@internal
This method is not defined by Matrix: it is inherited.
*/
const Vector& operator[] (int r) const;
/**
Access a row from the matrix.
This can be used either as an r-value or an l-value. The index starts at zero,
i.e. the first row is m[0]. To extract a column from a matrix, apply [] to the
transpose of the matrix (see example). This can be used either as an r-value
or an l-value. The index starts at zero, i.e. the first row (or column) is
m[0].
@code
Matrix<2,3> m(Data(
1,2,3
4,5,6));
Matrix<2,3> m(d);
Zero(m[0]); // set the first row to zero
Vector<2> v = 8,9;
m.T()[1] = v; // now m = [0 8 0]
// [4 9 6]
@endcode
@internal
This method is not defined by Matrix: it is inherited.
*/
Vector& operator[] (int r);
/// How many rows does this matrix have?
/// @internal
/// This method is not defined by Matrix: it is inherited.
int num_rows() const;
/// How many columns does this matrix have?
/// @internal
/// This method is not defined by Matrix: it is inherited.
int num_cols() const;
/// @name Transpose and sub-matrices
//@{
/**
The transpose of the matrix. This is a very fast operation--it simply
reinterprets a row-major matrix as column-major or vice-versa. This can be
used as an l-value.
@code
Matrix<2,3> m(Data(
1,2,3
4,5,6));
Matrix<2,3> m(d);
Zero(m[0]); // set the first row to zero
Vector<2> v = 8,9;
m.T()[1] = v; // now m = [0 8 0]
// [4 9 6]
@endcode
@internal
This method is not defined by Matrix: it is inherited.
*/
const Matrix<Cols, Rows>& T() const;
/**
The transpose of the matrix. This is a very fast operation--it simply
reinterprets a row-major matrix as column-major or vice-versa. The result can
be used as an l-value.
@code
Matrix<2,3> m(Data(
1,2,3
4,5,6));
Matrix<2,3> m(d);
Vector<2> v = 8,9;
// Set the first column to v
m.T()[0] = v; // now m = [8 2 3]
// [9 5 6]
@endcode
<b>This means that the semantics of <code>M=M.T()</code> are broken</b>. In
general, it is not necessary to say <code>M=M.T()</code>, since you can use
M.T() for free whenever you need the transpose, but if you do need to, you
have to use the Tranpose() function defined in <code>helpers.h</code>.
@internal
This method is not defined by Matrix: it is inherited.
*/
Matrix<Cols, Rows>& T();
/**
Extract a sub-matrix. The matrix extracted will be begin at element
(Rstart, Cstart) and will contain the next Rsize by Csize elements.
@code
Matrix<2,3> m(Data(
1,2,3
4,5,6
7,8,9));
Matrix<3> m(d);
Extract the top-left 2x2 matrix
Matrix<2> b = m.slice<0,0,2,2>(); // b = [1 2]
// [4 5]
@endcode
@internal
This method is not defined by Matrix: it is inherited.
*/
template<Rstart, Cstart, Rsize, Csize>
const Matrix<Rsize, Csize>& slice() const;
/**
Extract a sub-matrix. The matrix extracted will be begin at element (Rstart,
Cstart) and will contain the next Rsize by Csize elements. This can be used as
either an r-value or an l-value.
@code
Matrix<2,3> m(Data(
1,2,3
4,5,6));
Matrix<2,3> m(d);
Zero(m.slice<0,2,2,1>()); // b = [1 2 0]
// [4 5 0]
@endcode
@internal
This method is not defined by Matrix: it is inherited.
*/
template<Rstart, Cstart, Rsize, Csize>
Matrix<Rsize, Csize>& slice();
/**
Extract a sub-matrix with runtime location and size. The matrix extracted will
begin at element (rstart, cstart) and will
contain the next rsize by csize elements.
@code
Matrix<> m(3,3);
Extract the top-left 2x2 matrix
Matrix<2> b = m.slice(0,0,2,2);
@endcode
@internal
This method is not defined by Matrix: it is inherited.
*/
const Matrix<>& slice(int rstart, int cstart, int rsize, int csize) const;
/**
Extract a sub-matrix with runtime location and size, which can be used as
an l-value. The matrix extracted will be begin at element (rstart, cstart) and
will contain the next rsize by csize elements.
@code
Matrix<> m(3,3);
Zero(m.slice(0,0,2,2));
@endcode
@internal
This method is not defined by Matrix: it is inherited.
*/
Matrix<>& slice(int rstart, int cstart, int rsize, int csize);
//@}
#endif
};
}