13 template<
class Real>
class Svd;
16 template<
class Sub
class>
26 typedef typename Real_::DoubleType
Double_;
32 using Storage::assertSize;
33 using Storage::assertIndex;
36 using Storage::s_rows;
37 using Storage::s_cols;
38 using Storage::s_size;
39 using Storage::options;
40 using typename Storage::Alloc;
42 using Storage::operator[];
43 using Storage::operator();
53 MatrixS&
fromArray(
const Num* a,
bool rowMajor =
true)
56 if (!rowMajor)
return subc().fromColMajor(a);
64 if (!rowMajor)
return subc().fromColMajor(a);
79 for (
sdt i = 0; i < n; ++i)
m(i,i) = 1;
102 "Can only assign to matrix of the same size");
104 resize(rhs.rows(), rhs.cols());
110 template<
class Matrix>
123 assert(
size() == rhs.size(),
"Can't compare different sized matrices");
131 bool operator< (const MatrixBase<T>& rhs)
const {
return find(subc(), rhs.subc(), [](
Real e,
Real rhs) {
return e >= rhs; }) ==
end(); }
135 bool operator<=(const MatrixBase<T>& rhs)
const {
return find(subc(), rhs.subc(), [](
Real e,
Real rhs) {
return e > rhs; }) ==
end(); }
140 template<
class T,
class Res>
141 Res&&
add(
const T& rhs, Res&& res)
const {
return map(subc(), rhs.subc(), forward<Res>(res.resize(rows(),cols())), [](
Real e,
Real rhs) {
return e + rhs; }); }
150 template<
class T,
class Res>
151 Res&&
sub(
const T& rhs, Res&& res)
const {
return map(subc(), rhs.subc(), forward<Res>(res.resize(rows(),cols())), [](
Real e,
Real rhs) {
return e - rhs; }); }
160 template<
class T,
class Res>
161 Res&&
mul(
const T& rhs, Res&& res)
const
164 s_cols == T::s_rows,
"Concatenation invalid with rhs dimensions");
165 assert(cols() == rhs.rows(),
"Concatenation invalid with rhs dimensions");
167 res.resize(rows(), rhs.cols()).
fromZero();
168 const sdt rows = this->rows(), cols = this->cols(), cols2 = rhs.cols();
169 for (
sdt i = 0; i < rows; ++i)
170 for (
sdt j = 0; j < cols2; ++j)
171 for (
sdt k = 0; k < cols; ++k)
172 res(i,j) +=
m(i,k) * rhs(k,j);
173 return forward<Res>(res);
250 template<sdt Rows, sdt Cols>
254 template<sdt Rows, sdt Cols>
277 MatrixS&
resize(
sdt rows,
sdt cols) { Storage::resize(rows, cols);
return subc(); }
297 Num*
toArray(Num* a,
bool rowMajor =
true)
const
300 if (!rowMajor)
return subc().toColMajor(a);
307 if (!rowMajor)
return subc().toColMajor(a);
316 res.resize(cols(), rows());
317 const sdt rows = this->rows(), cols = this->cols();
318 for (
sdt i = 0; i < rows; ++i)
319 for (
sdt j = 0; j < cols; ++j)
321 return forward<Res>(res);
330 static_assert(s_size ==
matrix::dynamic || s_rows == s_cols,
"This matrix must be square");
331 assert(rows() == cols(),
"This matrix must be square");
333 const sdt rows = this->rows();
334 for (
sdt i = 0; i < rows-1; ++i)
335 for (
sdt j = i+1; j < rows; ++j)
340 template<
class T,
class Res>
344 s_rows == T::s_rows,
"Concatenation invalid with rhs dimensions");
345 assert(rows() == rhs.rows(),
"Concatenation invalid with rhs dimensions");
347 res.resize(cols(), rhs.cols()).
fromZero();
348 const sdt rows = this->rows(), cols = this->cols(), cols2 = rhs.cols();
349 for (
sdt i = 0; i < cols; ++i)
350 for (
sdt j = 0; j < cols2; ++j)
351 for (
sdt k = 0; k < rows; ++k)
352 res(i,j) +=
m(k,i) * rhs(k,j);
353 return forward<Res>(res);
361 template<
class T,
class Res>
365 s_cols == T::s_cols,
"Concatenation invalid with rhs dimensions");
366 assert(cols() == rhs.cols(),
"Concatenation invalid with rhs dimensions");
368 res.resize(rows(), rhs.rows()).
fromZero();
369 const sdt rows = this->rows(), cols = this->cols(), rows2 = rhs.rows();
370 for (
sdt i = 0; i < rows; ++i)
371 for (
sdt j = 0; j < rows2; ++j)
372 for (
sdt k = 0; k < cols; ++k)
373 res(i,j) +=
m(i,k) * rhs(j,k);
374 return forward<Res>(res);
382 template<
class T,
class Res>
386 s_rows == T::s_cols,
"Concatenation invalid with rhs dimensions");
387 assert(rows() == rhs.cols(),
"Concatenation invalid with rhs dimensions");
389 res.resize(cols(), rhs.rows()).
fromZero();
390 const sdt rows = this->rows(), cols = this->cols(), rows2 = rhs.rows();
391 for (
sdt i = 0; i < cols; ++i)
392 for (
sdt j = 0; j < rows2; ++j)
393 for (
sdt k = 0; k < rows; ++k)
394 res(i,j) +=
m(k,i) * rhs(j,k);
395 return forward<Res>(res);
406 (s_cols > 0) ? s_cols-1 : s_cols,
Real>
410 (s_cols > 0) ? s_cols-1 : s_cols,
Real>
411 ().
resize(rows()-1, cols()-1);
412 const sdt rows = this->rows()-
row-1, cols = this->cols()-
col-1;
425 if (rows() == cols() && rows() <= 3)
437 return res.fromZero();
450 return res.fromZero();
462 Real d = determinant3(res);
467 return res.fromZero();
491 if (rows() == cols())
496 case 1: res =
m(0,0);
break;
497 case 2: res =
m(0,0)*
m(1,1) -
m(1,0)*
m(0,1);
break;
498 case 3: { MatrixS tmp(*
this); res = determinant3(tmp); }
break;
501 for (
sdt j = 0; j < cols(); j++)
503 auto minor_ =
minor(0, j);
504 res += (1 - j%2 - j%2) *
m(0,j) * minor_.determinant();
525 auto bounds =
reduce(svd.calcValues(subc()).w(), make_tuple(Real_::inf, -Real_::inf),
527 return get<1>(bounds) / get<0>(bounds);
534 auto oslen = os.tellp();
535 for (
auto j :
range(mat.cols()))
538 for (
auto i :
range(mat.rows()))
540 int len = (int)((os << mat(i,j)).tellp() - oslen);
544 colLen.push_back(lenMax);
550 for (
auto i :
range(mat.rows()))
552 if (i != 0) os <<
",";
554 for (
auto j :
range(mat.cols()))
556 if (j != 0) os <<
", ";
557 os << std::setw(colLen[j]) << mat(i,j);
576 const sdt rows = this->rows(), cols = this->cols();
577 for (
sdt i = 0; i < rows; ++i)
578 for (
sdt j = 0; j < cols; ++j)
579 m(i*cols+j) = (
Real)a[j*rows+i];
586 const sdt rows = this->rows(), cols = this->cols();
587 for (
sdt i = 0; i < rows; ++i)
588 for (
sdt j = 0; j < cols; ++j)
589 a[j*rows+i] = (Num)
m(i*cols+j);
599 tmp(0,0) =
m(1,1)*
m(2,2)-
m(1,2)*
m(2,1);
600 tmp(0,1) =
m(0,2)*
m(2,1)-
m(0,1)*
m(2,2);
601 tmp(0,2) =
m(0,1)*
m(1,2)-
m(0,2)*
m(1,1);
602 tmp(1,0) =
m(1,2)*
m(2,0)-
m(1,0)*
m(2,2);
603 tmp(1,1) =
m(0,0)*
m(2,2)-
m(0,2)*
m(2,0);
604 tmp(1,2) =
m(0,2)*
m(1,0)-
m(0,0)*
m(1,2);
605 tmp(2,0) =
m(1,0)*
m(2,1)-
m(1,1)*
m(2,0);
606 tmp(2,1) =
m(0,1)*
m(2,0)-
m(0,0)*
m(2,1);
607 tmp(2,2) =
m(0,0)*
m(1,1)-
m(0,1)*
m(1,0);
608 return m(0,0)*tmp(0,0) +
m(0,1)*tmp(1,0) +
m(0,2)*tmp(2,0);
MatrixS & fromScalar(Real f)
Initialize with scalar in every element.
Definition: Base.h:72
(m x n)-dimensional matrix
Definition: Matrix.h:24
Real determinant() const
Get the determinant. Returns the pseudo-determinant if this matrix is not square. ...
Definition: Base.h:487
static const sdt dynamic
Definition: Traits.h:23
void storageFillZero(StorageBlock< T > &store)
Fill block storage with zeros.
Definition: Block.h:136
bool isZero() const
Check if each element is exactly zero.
Definition: Base.h:223
Real_::DoubleType Double_
Definition: Base.h:26
Algebra.
Definition: Alge.h:13
matrix::Iter< const MatrixS > iter(sdt row, sdt col) const
Definition: Base.h:293
bool operator>=(const MatrixBase< T > &rhs) const
Definition: Base.h:137
matrix::Block< MatrixS > block(sdt row, sdt col, sdt rows, sdt cols)
Get dynamic block at offset (row,col) with size (rows, cols)
Definition: Base.h:260
Matrix comma initializer.
Definition: Builder.h:11
Subclass MatrixS
Definition: Base.h:22
Matrix< s_cols, T::s_cols, Real > transposeMul(const T &rhs) const
Returns new matrix.
Definition: Base.h:358
MatrixS & fromColMajor(const Num *a)
Definition: Base.h:574
bool operator==(const MatrixBase< T > &rhs) const
Definition: Base.h:119
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
const Real & m(sdt i) const
Access matrix element at index. Wrapper for convenience only, more readable than (*this)(i) ...
Definition: Base.h:567
bool operator!=(const MatrixBase< T > &rhs) const
Definition: Base.h:129
const Real & m(sdt row, sdt col) const
Access matrix element with (row, column)
Definition: Base.h:570
matrix::Block< const MatrixS > block(sdt row, sdt col, sdt rows, sdt cols) const
Definition: Base.h:263
Matrix< s_cols, T::s_rows, Real > transposeMulTranspose(const T &rhs) const
Returns new matrix.
Definition: Base.h:400
matrix::Block< MatrixS, Rows, Cols > block(sdt row, sdt col, sdt rows=-1, sdt cols=-1)
Get block at offset (row,col) with size (Rows, Cols). If Rows or Cols is fixed then rows or cols may ...
Definition: Base.h:252
matrix::Block< const MatrixS, s_rows, 1 > col(sdt col) const
Definition: Base.h:271
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
MatrixS elemSub(Real rhs) const
Subtract rhs from each element. Returns a new matrix with the results.
Definition: Base.h:194
matrix::Iter< MatrixS > iter(sdt i)
Get an iterator to the element at index.
Definition: Base.h:288
Numeral< Real >::Real_ Real_
Definition: Base.h:25
MatrixS & operator+=(const MatrixBase< T > &rhs)
Definition: Base.h:147
Iter find(Range &&, Seqs &&..., Func &&pred)
Find an element in a series of sequences.
bool storageEqual(const StorageBlock< T > &lhs, const StorageBlock< T2 > &rhs)
Test between block storages.
Definition: Block.h:157
MatrixS elemMin(const MatrixBase< T > &rhs) const
Get the min of each element and its corresponding element in rhs. Returns a new matrix with the resul...
Definition: Base.h:217
bool isNearZero(Real tol=Real_::zeroTol) const
Check if each element is close to zero.
Definition: Base.h:225
Res && transposeMul(const T &rhs, Res &&res) const
Stores result in and returns res.
Definition: Base.h:341
matrix::Block< MatrixS, s_rows, 1 > col(sdt col)
Get column as a column vector.
Definition: Base.h:270
Real prod() const
Get the product of all elements.
Definition: Base.h:234
MatrixS operator-() const
Definition: Base.h:153
matrix::Block< MatrixS, 1, s_cols > row(sdt row)
Get row as a row vector.
Definition: Base.h:266
matrix::Block< const MatrixS, Rows, Cols > block(sdt row, sdt col, sdt rows=-1, sdt cols=-1) const
Definition: Base.h:256
Accum reduce(Range &&, Seqs &&..., Accum &&initVal, Func &&)
Accumulate a series of sequences into an output.
Matrix< s_cols, s_rows, Real > transpose() const
Returns new matrix.
Definition: Base.h:325
Double_::Real Double
Definition: Base.h:27
matrix::Iter< MatrixS > iter(sdt row, sdt col)
Get an iterator to the element at (row, col)
Definition: Base.h:292
std::enable_if< mt::isIterator< Iter1 >::value, Range_< Iter1, Iter2 > >::type range(Iter1 &&first, Iter2 &&last)
Range from iterators [first, last)
Definition: Range.h:116
MatrixS & fromIdentity()
Make matrix identity. For non-square matrices the identity is in the upper-left square block...
Definition: Base.h:75
Alge_< Real > Alge
Definition: Base.h:28
Matrix block view.
Definition: Block.h:188
Res && transposeMulTranspose(const T &rhs, Res &&res) const
Stores result in and returns res.
Definition: Base.h:383
static std::common_type< Num, Num2 >::type max(Num a, Num2 b)
Get the maximum of two numbers.
Definition: Alge.h:94
MatrixS & fromArray(const Real *a, bool rowMajor=true)
Definition: Base.h:61
MatrixS & elemDivEq(const MatrixBase< T > &rhs)
Divide each element by its corresponding element in rhs.
Definition: Base.h:208
matrix::Iter< const MatrixS > begin() const
Definition: Base.h:281
Real * toArray(Real *a, bool rowMajor=true) const
Definition: Base.h:304
Real & m(sdt row, sdt col)
Definition: Base.h:571
Res && mul(const T &rhs, Res &&res) const
Multiply with another matrix. This mat's column size must match rhs' row size. Stores result in and r...
Definition: Base.h:161
MatrixS elemMul(const MatrixBase< T > &rhs) const
Multiply each element with its corresponding element in rhs. Returns a new matrix with the results...
Definition: Base.h:199
static Int abs(Int x)
Get absolute value of signed integer.
Definition: Alge.h:21
Matrix element iterator.
Definition: Iter.h:9
static std::common_type< Num, Num2 >::type min(Num a, Num2 b)
Get the minimum of two numbers.
Definition: Alge.h:89
Matrix cast()
Convert to a matrix of another real type.
Definition: Base.h:111
MatrixS operator/(Real rhs) const
Definition: Base.h:184
static std::common_type< Num, Num2, Num3 >::type clamp(Num val, Num2 min, Num3 max)
Ensure that a number is within a range.
Definition: Alge.h:99
MatrixS operator+() const
Definition: Base.h:143
void storageTransform(const StorageBlock< Src > &src, StorageBlock< Dst > &dst, Func &&f)
Transform between block and dense storages.
Definition: Block.h:120
ostream & indentInc(ostream &os)
Increase stream indent level by 1.
Definition: Stream.h:32
matrix::Iter< MatrixS > begin()
Get an iterator over the elements in row-major order.
Definition: Base.h:280
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
Alge_< Double > Alge_d
Definition: Base.h:29
Num * toColMajor(Num *a) const
Definition: Base.h:584
N-dimensional vector.
Definition: Traits.h:12
ostream & indentDec(ostream &os)
Decrease stream indent level by 1.
Definition: Stream.h:34
Real mean() const
Get the mean of all elements.
Definition: Base.h:236
Singular Value Decomposition. Can be used to calculate the pseudo-inverse of any matrix or solve leas...
Definition: Base.h:13
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
MatrixS & resize(sdt rows, sdt cols)
Sets number of rows/columns and reallocates only if the size has changed (rows*cols). All previous data is lost on reallocation. Returns self.
Definition: Base.h:277
Enables any type to be optional so it can exist in an uninitialized null state.
Definition: Optional.h:52
static bool isNearZero(Real val, Real tol=Real_::zeroTol)
Check whether a number is close to zero.
Definition: Alge.h:108
Res && add(const T &rhs, Res &&res) const
Add another matrix. Stores result in and returns res. res may reference the same matrix as this or rh...
Definition: Base.h:141
MatrixS & fromZero()
Zero all elements.
Definition: Base.h:70
Real cond() const
Get the condition value. A high value means the matrix is ill-conditioned and close to singular...
Definition: Base.h:521
Matrix< s_rows, T::s_rows, Real > mulTranspose(const T &rhs) const
Returns new matrix.
Definition: Base.h:379
matrix::Iter< const MatrixS > end() const
Definition: Base.h:285
friend MatrixS operator*(Real lhs, const MatrixBase &rhs)
Definition: Base.h:187
double Real
Definition: Real.h:14
Svd< Real > Svd
Definition: Base.h:31
void storageFill(StorageBlock< T > &store, typename StorageBlock< T >::Real f)
Fill block storage with scalar.
Definition: Block.h:128
MatrixS & operator*=(const MatrixBase< T > &rhs)
Definition: Base.h:181
Real & m(sdt i)
Definition: Base.h:568
MatrixS & elemAddEq(Real rhs)
Add rhs to each element.
Definition: Base.h:192
MatrixS elemAbs() const
Get the absolute value of each element. Returns a new matrix with the results.
Definition: Base.h:210
Matrix< s_rows, T::s_cols, Real > operator*(const T &rhs) const
Multiply with another matrix. Returns a new matrix.
Definition: Base.h:178
int size(const StdContainer &cont)
Safely get the size of a std container as a signed integer.
Definition: StdUtil.h:19
MatrixBase & operator=(const MatrixBase< T > &rhs)
Assign to matrix of any size. Asserts that any fixed dimensions in this matrix match those in rhs...
Definition: Base.h:97
MatrixS clamp(const MatrixBase< T > &min, const MatrixBase< T > &max) const
Clamp each element between its corresponding elements in min and max. Returns a new matrix with the r...
Definition: Base.h:228
MatrixS & elemMulEq(const MatrixBase< T > &rhs)
Multiply each element with its corresponding element in rhs.
Definition: Base.h:202
MatrixS & operator/=(Real rhs)
Definition: Base.h:185
MatrixS elemMax(const MatrixBase< T > &rhs) const
Get the max of each element and its corresponding element in rhs. Returns a new matrix with the resul...
Definition: Base.h:220
MatrixS & elemSubEq(Real rhs)
Subtract rhs from each element.
Definition: Base.h:196
Real max() const
Get the maximum element.
Definition: Base.h:240
Matrix< s_cols, s_rows, Real > inverse(optional< Real & > det=optnull) const
Get the pseudo-inverse of this matrix. The pseudo-determinant will be returned in det if specified...
Definition: Base.h:421
MatrixS elemAdd(Real rhs) const
Add rhs to each element. Returns a new matrix with the results.
Definition: Base.h:190
matrix::Builder< MatrixS > operator<<(T &&val)
Initialize matrix elements from scalars and other matrices using the comma operator: Matrix() << 1...
Definition: Base.h:93
auto minor(sdt row, sdt col) const -> Matrix< (s_rows > 0)?s_rows-1:s_rows, (s_cols > 0)?s_cols-1:s_cols, Real >
Returns a matrix without the selected row and column.
Definition: Base.h:404
MatrixS elemSqr() const
Square each element. Returns a new matrix with the results.
Definition: Base.h:212
MatrixS elemInverse() const
Inverse each element. Returns a new matrix with the results.
Definition: Base.h:214
Vec< s_rows, Real > VecCol
Definition: Base.h:48
matrix::Block< const MatrixS, 1, s_cols > row(sdt row) const
Definition: Base.h:267
friend ostream & operator<<(ostream &os, const MatrixBase &mat)
Definition: Base.h:530
matrix::priv::Traits< Subclass >::Storage Storage
Definition: Base.h:20
void storageCopy(const StorageBlock< Src > &src, StorageBlock< Dst > &dst)
Copy between block and dense storages.
Definition: Block.h:84
MatrixS elemDiv(const MatrixBase< T > &rhs) const
Divide each element by its corresponding element in rhs. Returns a new matrix with the results...
Definition: Base.h:205
Real sum() const
Get the sum of all elements.
Definition: Base.h:232
Res && sub(const T &rhs, Res &&res) const
Subtract another matrix. Stores result in and returns res. res may reference the same matrix as this ...
Definition: Base.h:151
Matrix base class.
Definition: Base.h:17
void transposeInPlace()
transpose and store in this matrix, only valid for square matrices
Definition: Base.h:328
Real min() const
Get the minimum element.
Definition: Base.h:238
MatrixS & operator*=(Real rhs)
Definition: Base.h:182
MatrixS & operator-=(const MatrixBase< T > &rhs)
Definition: Base.h:157
MatrixS & fromArray(const Num *a, bool rowMajor=true)
Initialize from array. If the array is in row-major format set rowMajor to true, otherwise set to fal...
Definition: Base.h:53
bool operator>(const MatrixBase< T > &rhs) const
Definition: Base.h:133
ostream & endl(ostream &os)
End line and apply any indentation to the next line.
Definition: Stream.h:40
Global Honeycomb namespace.
Vec< s_cols, Real, matrix::Option::vecRow > VecRow
Definition: Base.h:49
Trig_< Real > Trig
Definition: Base.h:30
matrix::Iter< MatrixS > end()
Get an iterator to the end of the iteration provided by begin()
Definition: Base.h:284
Res && transpose(Res &&res) const
transpose and store result in res. Returns res.
Definition: Base.h:314
Num * toArray(Num *a, bool rowMajor=true) const
Copy matrix into array. If the array is in row-major format set rowMajor to true, otherwise set to fa...
Definition: Base.h:297
Trigonometry.
Definition: Trig.h:52
Res && mulTranspose(const T &rhs, Res &&res) const
Stores result in and returns res.
Definition: Base.h:362
matrix::Iter< const MatrixS > iter(sdt i) const
Definition: Base.h:289
OutSeq && map(Range &&, Seqs &&..., OutSeq &&, Func &&)
Transform a series of sequences into an output.