|
|
@ -9,6 +9,42 @@ |
|
|
|
#include <cstring> |
|
|
|
#include <cstring> |
|
|
|
#include <cassert> |
|
|
|
#include <cassert> |
|
|
|
#include <type_traits> |
|
|
|
#include <type_traits> |
|
|
|
|
|
|
|
#include <limits> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace math { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename TYPE> |
|
|
|
|
|
|
|
bool almostEqual(TYPE a, TYPE b) { |
|
|
|
|
|
|
|
if (a<0) |
|
|
|
|
|
|
|
if (b<0) { |
|
|
|
|
|
|
|
a = -a; |
|
|
|
|
|
|
|
b = -b; |
|
|
|
|
|
|
|
} else return a==b; // different signs
|
|
|
|
|
|
|
|
else if (b<0) return a==b; // different signs
|
|
|
|
|
|
|
|
TYPE diff(abs(a-b)); |
|
|
|
|
|
|
|
a = abs(a); |
|
|
|
|
|
|
|
b = abs(b); |
|
|
|
|
|
|
|
TYPE max(a>b?a:b); |
|
|
|
|
|
|
|
if (max<1) return diff<=std::numeric_limits<TYPE>::epsilon(); |
|
|
|
|
|
|
|
return diff<=max*std::numeric_limits<TYPE>::epsilon(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename TYPE> |
|
|
|
|
|
|
|
bool equal(const TYPE& a, const TYPE& b) { |
|
|
|
|
|
|
|
return a==b; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<> |
|
|
|
|
|
|
|
bool equal(const double& a, const double& b) { |
|
|
|
|
|
|
|
return almostEqual(a, b); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<> |
|
|
|
|
|
|
|
bool equal(const float& a, const float& b) { |
|
|
|
|
|
|
|
return almostEqual(a, b); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template<typename TYPE, typename ARRAY=TYPE*> class MatrixBase { |
|
|
|
template<typename TYPE, typename ARRAY=TYPE*> class MatrixBase { |
|
|
|
|
|
|
|
|
|
|
@ -137,7 +173,7 @@ template<typename TYPE, typename ARRAY=TYPE*> class MatrixBase { |
|
|
|
bool operator==(const MatrixBase& o) const { |
|
|
|
bool operator==(const MatrixBase& o) const { |
|
|
|
if (!check(o)) return false; |
|
|
|
if (!check(o)) return false; |
|
|
|
TYPE *to((TYPE*)(_c)+SIZE), *from((TYPE*)(o._c)+SIZE); |
|
|
|
TYPE *to((TYPE*)(_c)+SIZE), *from((TYPE*)(o._c)+SIZE); |
|
|
|
while (to>(TYPE*)(_c)) if (*--to != *--from) return false; |
|
|
|
while (to>(TYPE*)(_c)) if (!math::equal(*--to, *--from)) return false; |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -176,6 +212,13 @@ template<typename TYPE, typename ARRAY=TYPE*> class MatrixBase { |
|
|
|
/// @name special operations
|
|
|
|
/// @name special operations
|
|
|
|
///{
|
|
|
|
///{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool similar(const MatrixBase& o, const TYPE& diff) const { |
|
|
|
|
|
|
|
if (!check(o)) return false; |
|
|
|
|
|
|
|
TYPE *to((TYPE*)(_c)+SIZE), *from((TYPE*)(o._c)+SIZE); |
|
|
|
|
|
|
|
while (to>(TYPE*)(_c)) if (abs(*--to - *--from)>diff) return false; |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TYPE det() { |
|
|
|
TYPE det() { |
|
|
|
TYPE res(gauss()); |
|
|
|
TYPE res(gauss()); |
|
|
|
for (TYPE *p((TYPE*)(_c)+SIZE); --p>=(TYPE*)(_c); p-=COLUMNS) res *= *p; |
|
|
|
for (TYPE *p((TYPE*)(_c)+SIZE); --p>=(TYPE*)(_c); p-=COLUMNS) res *= *p; |
|
|
@ -302,27 +345,45 @@ template<typename TYPE, size_t TROWS=0, size_t TCOLUMNS=0> class Matrix: |
|
|
|
Matrix o(*this); // left side
|
|
|
|
Matrix o(*this); // left side
|
|
|
|
*this = i(); // right side
|
|
|
|
*this = i(); // right side
|
|
|
|
/// 1. lower left part
|
|
|
|
/// 1. lower left part
|
|
|
|
for (size_t row(0); row<this->ROWS; ++row) |
|
|
|
for (size_t column(0); column<this->COLUMNS; ++column) { |
|
|
|
if (rows<this->COLUMNS) { |
|
|
|
if (column<this->ROWS) { |
|
|
|
/// 2. normalize first line to first value
|
|
|
|
/// 2. normalize pivot to one
|
|
|
|
TYPE pivot(o(row, row)); |
|
|
|
TYPE pivot(o(column, column)); |
|
|
|
if (pivot!=1) { |
|
|
|
if (pivot!=1) { |
|
|
|
o(row, row) = 1; |
|
|
|
o(column, column) = 1; |
|
|
|
for (size_t column(row+1); column<this->COLUMNS; ++column) o(row, column)/=pivot; |
|
|
|
for (size_t pos(column+1); pos<this->COLUMNS; ++pos) |
|
|
|
for (size_t column(row); column<this->COLUMNS; ++column) this->at(row, column)/=pivot; |
|
|
|
o(column, pos)/=pivot; |
|
|
|
|
|
|
|
for (size_t pos(0); pos<this->COLUMNS; ++pos) |
|
|
|
|
|
|
|
this->at(column, pos)/=pivot; |
|
|
|
} |
|
|
|
} |
|
|
|
/// 3. nullify lower triangle
|
|
|
|
/// 3. nullify lower triangle
|
|
|
|
for (size_t row(column+1); row<this->ROWS; ++row) { |
|
|
|
for (size_t row(column+1); row<this->ROWS; ++row) { |
|
|
|
TYPE pivot(o(row, column)); |
|
|
|
TYPE pivot(o(row, column)); |
|
|
|
if (pivot!=0) { |
|
|
|
if (pivot!=0) { |
|
|
|
o(row, column) = 0; |
|
|
|
o(row, column) = 0; |
|
|
|
for (size_t pos(column+1); pos<this->COLUMNS; ++pos) o(row, pos) -= pivot*o(0, pos); |
|
|
|
for (size_t pos(column+1); pos<this->COLUMNS; ++pos) |
|
|
|
for (size_t pos(column); pos<this->COLUMNS; ++pos) this->at(row, pos) -= pivot*this->at(0, pos); |
|
|
|
o(row, pos) -= pivot*o(column, pos); |
|
|
|
|
|
|
|
for (size_t pos(0); pos<this->COLUMNS; ++pos) |
|
|
|
|
|
|
|
this->at(row, pos) -= pivot*this->at(column, pos); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
/// 4. nullify the upper triangle
|
|
|
|
/// 4. nullify the upper triangle
|
|
|
|
|
|
|
|
const size_t LASTCOL(this->COLUMNS-1); |
|
|
|
|
|
|
|
const size_t LASTROW(this->ROWS-1); |
|
|
|
|
|
|
|
for (size_t column(1); column<this->COLUMNS; ++column) { |
|
|
|
|
|
|
|
for (size_t row(0); row<column && row<LASTROW; ++row) { |
|
|
|
|
|
|
|
TYPE pivot(o(row, column)); |
|
|
|
|
|
|
|
if (pivot!=0) { |
|
|
|
|
|
|
|
o(row, column) = 0; |
|
|
|
|
|
|
|
for (size_t pos(column+1); pos<this->COLUMNS; ++pos) |
|
|
|
|
|
|
|
o(row, pos) -= pivot*o(column, pos); |
|
|
|
|
|
|
|
for (size_t pos(0); pos<this->COLUMNS; ++pos) |
|
|
|
|
|
|
|
this->at(row, pos) -= pivot*this->at(column, pos); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -379,18 +440,6 @@ template<typename TYPE> class Matrix<TYPE, 0, 0>: public MatrixBase<TYPE> { |
|
|
|
/// @name operators
|
|
|
|
/// @name operators
|
|
|
|
///{
|
|
|
|
///{
|
|
|
|
|
|
|
|
|
|
|
|
Matrix& operator=(const Matrix& o) { |
|
|
|
|
|
|
|
if (o.ROWS!=Parent::ROWS&&o.COLUMNS!=Parent::COLUMNS) { |
|
|
|
|
|
|
|
delete[] Parent::_c; |
|
|
|
|
|
|
|
Parent::ROWS = o.ROWS; |
|
|
|
|
|
|
|
Parent::COLUMNS = o.COLUMNS; |
|
|
|
|
|
|
|
Parent::SIZE = o.SIZE; |
|
|
|
|
|
|
|
Parent::MEM_SIZE = o.MEM_SIZE; |
|
|
|
|
|
|
|
Parent::_c = new TYPE[Parent::SIZE]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return Parent::operator=(o); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Matrix operator-() const { |
|
|
|
Matrix operator-() const { |
|
|
|
Matrix res(Parent::COLUMNS, Parent::ROWS); |
|
|
|
Matrix res(Parent::COLUMNS, Parent::ROWS); |
|
|
|
for (TYPE *to((TYPE*)(res._c)+Parent::SIZE); to>(TYPE*)(Parent::_c); --to) |
|
|
|
for (TYPE *to((TYPE*)(res._c)+Parent::SIZE); to>(TYPE*)(Parent::_c); --to) |
|
|
@ -428,6 +477,54 @@ template<typename TYPE> class Matrix<TYPE, 0, 0>: public MatrixBase<TYPE> { |
|
|
|
return res; |
|
|
|
return res; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Matrix& inv() { |
|
|
|
|
|
|
|
/// calculate using gauss-jordan algorithmus
|
|
|
|
|
|
|
|
/// @see http://www.mathebibel.de/inverse-matrix-berechnen-nach-gauss-jordan
|
|
|
|
|
|
|
|
Matrix o(*this); // left side
|
|
|
|
|
|
|
|
*this = i(); // right side
|
|
|
|
|
|
|
|
/// 1. lower left part
|
|
|
|
|
|
|
|
for (size_t column(0); column<this->COLUMNS; ++column) { |
|
|
|
|
|
|
|
if (column<this->ROWS) { |
|
|
|
|
|
|
|
/// 2. normalize pivot to one
|
|
|
|
|
|
|
|
TYPE pivot(o(column, column)); |
|
|
|
|
|
|
|
if (pivot!=1) { |
|
|
|
|
|
|
|
o(column, column) = 1; |
|
|
|
|
|
|
|
for (size_t pos(column+1); pos<this->COLUMNS; ++pos) |
|
|
|
|
|
|
|
o(column, pos)/=pivot; |
|
|
|
|
|
|
|
for (size_t pos(0); pos<this->COLUMNS; ++pos) |
|
|
|
|
|
|
|
this->at(column, pos)/=pivot; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/// 3. nullify lower triangle
|
|
|
|
|
|
|
|
for (size_t row(column+1); row<this->ROWS; ++row) { |
|
|
|
|
|
|
|
TYPE pivot(o(row, column)); |
|
|
|
|
|
|
|
if (pivot!=0) { |
|
|
|
|
|
|
|
o(row, column) = 0; |
|
|
|
|
|
|
|
for (size_t pos(column+1); pos<this->COLUMNS; ++pos) |
|
|
|
|
|
|
|
o(row, pos) -= pivot*o(column, pos); |
|
|
|
|
|
|
|
for (size_t pos(0); pos<this->COLUMNS; ++pos) |
|
|
|
|
|
|
|
this->at(row, pos) -= pivot*this->at(column, pos); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/// 4. nullify the upper triangle
|
|
|
|
|
|
|
|
const size_t LASTCOL(this->COLUMNS-1); |
|
|
|
|
|
|
|
const size_t LASTROW(this->ROWS-1); |
|
|
|
|
|
|
|
for (size_t column(1); column<this->COLUMNS; ++column) { |
|
|
|
|
|
|
|
for (size_t row(0); row<column && row<LASTROW; ++row) { |
|
|
|
|
|
|
|
TYPE pivot(o(row, column)); |
|
|
|
|
|
|
|
if (pivot!=0) { |
|
|
|
|
|
|
|
o(row, column) = 0; |
|
|
|
|
|
|
|
for (size_t pos(column+1); pos<this->COLUMNS; ++pos) |
|
|
|
|
|
|
|
o(row, pos) -= pivot*o(column, pos); |
|
|
|
|
|
|
|
for (size_t pos(0); pos<this->COLUMNS; ++pos) |
|
|
|
|
|
|
|
this->at(row, pos) -= pivot*this->at(column, pos); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return *this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
///}
|
|
|
|
///}
|
|
|
|
|
|
|
|
|
|
|
|
//................................................................methods
|
|
|
|
//................................................................methods
|
|
|
@ -494,4 +591,3 @@ template<typename TYPE, size_t ROWS, size_t COLUMNS> |
|
|
|
s<<'\n'; |
|
|
|
s<<'\n'; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|