|
|
@ -52,13 +52,13 @@ namespace math { |
|
|
|
|
|
|
|
|
|
|
|
template<typename TYPE, typename ARRAY=TYPE*> class MatrixBase { |
|
|
|
template<typename TYPE, typename ARRAY=TYPE*> class MatrixBase { |
|
|
|
|
|
|
|
|
|
|
|
//..............................................................constants
|
|
|
|
//..............................................................variables
|
|
|
|
public: |
|
|
|
protected: |
|
|
|
|
|
|
|
|
|
|
|
const size_t ROWS; |
|
|
|
size_t ROWS; |
|
|
|
const size_t COLUMNS; |
|
|
|
size_t COLUMNS; |
|
|
|
const size_t SIZE; |
|
|
|
size_t SIZE; |
|
|
|
const size_t MEM_SIZE; |
|
|
|
size_t MEM_SIZE; |
|
|
|
|
|
|
|
|
|
|
|
//...............................................................typedefs
|
|
|
|
//...............................................................typedefs
|
|
|
|
public: |
|
|
|
public: |
|
|
@ -157,6 +157,14 @@ template<typename TYPE, typename ARRAY=TYPE*> class MatrixBase { |
|
|
|
return ConstRowVector(*this, (TYPE*)_c+row*COLUMNS); |
|
|
|
return ConstRowVector(*this, (TYPE*)_c+row*COLUMNS); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t rows() const { |
|
|
|
|
|
|
|
return ROWS; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t columns() const { |
|
|
|
|
|
|
|
return COLUMNS; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
///}
|
|
|
|
///}
|
|
|
|
|
|
|
|
|
|
|
|
/// @name operators
|
|
|
|
/// @name operators
|
|
|
@ -511,6 +519,19 @@ template<typename TYPE> class Matrix<TYPE, 0, 0>: public MatrixBase<TYPE> { |
|
|
|
///@name special operations
|
|
|
|
///@name special operations
|
|
|
|
///{
|
|
|
|
///{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Matrix& resize(size_t rows, size_t columns) { |
|
|
|
|
|
|
|
if (rows!=this->ROWS||columns!=this->COLUMNS) { |
|
|
|
|
|
|
|
delete Parent::_c; |
|
|
|
|
|
|
|
Parent::_c = new TYPE[rows*columns]; |
|
|
|
|
|
|
|
this->ROWS = rows; |
|
|
|
|
|
|
|
this->COLUMNS = columns; |
|
|
|
|
|
|
|
this->SIZE = rows*columns; |
|
|
|
|
|
|
|
this->MEM_SIZE = sizeof(TYPE)*rows*columns; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
memset(Parent::_c, 0, Parent::MEM_SIZE); |
|
|
|
|
|
|
|
return *this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Matrix& apply(std::function<void(TYPE&)> fn) { |
|
|
|
Matrix& apply(std::function<void(TYPE&)> fn) { |
|
|
|
Parent::apply(fn); |
|
|
|
Parent::apply(fn); |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
@ -661,9 +682,9 @@ template<typename TYPE, size_t ROWS, size_t COLUMNS> |
|
|
|
|
|
|
|
|
|
|
|
template<typename TYPE, size_t ROWS, size_t COLUMNS> |
|
|
|
template<typename TYPE, size_t ROWS, size_t COLUMNS> |
|
|
|
std::ostream& operator<<(std::ostream& s, const Matrix<TYPE, ROWS, COLUMNS>& m) { |
|
|
|
std::ostream& operator<<(std::ostream& s, const Matrix<TYPE, ROWS, COLUMNS>& m) { |
|
|
|
s<<'['<<ROWS<<'x'<<COLUMNS<<"]{"; |
|
|
|
s<<'['<<m.rows()<<'x'<<m.columns()<<"]{"; |
|
|
|
for (size_t row = 0; row < m.ROWS; ++row) { |
|
|
|
for (size_t row = 0; row < m.rows(); ++row) { |
|
|
|
for (size_t column = 0; column < m.COLUMNS; ++column) { |
|
|
|
for (size_t column = 0; column < m.columns(); ++column) { |
|
|
|
if (row!=0||column!=0) s<<','; |
|
|
|
if (row!=0||column!=0) s<<','; |
|
|
|
s<<m(row, column); |
|
|
|
s<<m(row, column); |
|
|
|
} |
|
|
|
} |
|
|
@ -679,12 +700,37 @@ template<typename TYPE, size_t ROWS, size_t COLUMNS> |
|
|
|
TYPE val(0); |
|
|
|
TYPE val(0); |
|
|
|
std::string s; |
|
|
|
std::string s; |
|
|
|
if (!in.get(c) || c!='[') throw err; |
|
|
|
if (!in.get(c) || c!='[') throw err; |
|
|
|
if (!std::getline(in, s, 'x') || !(std::stringstream(s)>>sz) || sz!=m.ROWS) throw err; |
|
|
|
if (!std::getline(in, s, 'x') || !(std::stringstream(s)>>sz) || sz!=m.rows()) throw err; |
|
|
|
if (!std::getline(in, s, ']') || !(std::stringstream(s)>>sz) || sz!=m.COLUMNS) throw err; |
|
|
|
if (!std::getline(in, s, ']') || !(std::stringstream(s)>>sz) || sz!=m.columns()) throw err; |
|
|
|
|
|
|
|
if (!in.get(c) || c!='{') throw err; |
|
|
|
|
|
|
|
for (size_t row = 0; row < m.rows(); ++row) { |
|
|
|
|
|
|
|
for (size_t column = 0; column < m.columns(); ++column) { |
|
|
|
|
|
|
|
if (row==m.rows()-1&&column==m.columns()-1) { |
|
|
|
|
|
|
|
if (!std::getline(in, s, '}') || !(std::stringstream(s)>>val)) throw err; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (!std::getline(in, s, ',') || !(std::stringstream(s)>>val)) throw err; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
m(row, column) = val; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return in; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename TYPE> |
|
|
|
|
|
|
|
std::istream& operator>>(std::istream& in, Matrix<TYPE, 0, 0>& m) { |
|
|
|
|
|
|
|
std::ios_base::failure err("illegal matrix format"); |
|
|
|
|
|
|
|
char c(0); |
|
|
|
|
|
|
|
size_t rows(0), columns(0); |
|
|
|
|
|
|
|
TYPE val(0); |
|
|
|
|
|
|
|
std::string s; |
|
|
|
|
|
|
|
if (!in.get(c) || c!='[') throw err; |
|
|
|
|
|
|
|
if (!std::getline(in, s, 'x') || !(std::stringstream(s)>>rows) || rows<=0) throw err; |
|
|
|
|
|
|
|
if (!std::getline(in, s, ']') || !(std::stringstream(s)>>columns) || columns<=0) throw err; |
|
|
|
|
|
|
|
m.resize(rows, columns); |
|
|
|
if (!in.get(c) || c!='{') throw err; |
|
|
|
if (!in.get(c) || c!='{') throw err; |
|
|
|
for (size_t row = 0; row < m.ROWS; ++row) { |
|
|
|
for (size_t row = 0; row < m.rows(); ++row) { |
|
|
|
for (size_t column = 0; column < m.COLUMNS; ++column) { |
|
|
|
for (size_t column = 0; column < m.columns(); ++column) { |
|
|
|
if (row==m.ROWS-1&&column==m.COLUMNS) { |
|
|
|
if (row==m.rows()-1&&column==m.columns()-1) { |
|
|
|
if (!std::getline(in, s, '}') || !(std::stringstream(s)>>val)) throw err; |
|
|
|
if (!std::getline(in, s, '}') || !(std::stringstream(s)>>val)) throw err; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (!std::getline(in, s, ',') || !(std::stringstream(s)>>val)) throw err; |
|
|
|
if (!std::getline(in, s, ',') || !(std::stringstream(s)>>val)) throw err; |
|
|
|