#ifndef __MATH_MATRIX__ #define __MATH_MATRIX__ #include #include #include #include namespace rpl { template struct math_matrix : base_matrix { typedef base_matrix base_type; math_matrix() : base_type() {} math_matrix(size_t m, size_t n) : base_type(m,n) {} math_matrix(const math_matrix & a) : base_type(a) {} math_matrix(const base_matrix & a) : base_type(a) {} template math_matrix(const matrix_expression &ae) : base_type(ae) {} math_matrix & operator= (const math_matrix& a) { if (this != &a) // Beware of self-assignment base_type::operator=(a); return *this;} ~math_matrix() {} size_t get_no_of_rows() const { return this->size1(); } size_t get_no_of_columns() const { return this->size2(); } void set_no_of_rows(size_t i) { base_type::resize(i,this->size2()); } void set_no_of_columns(size_t j) { base_type::resize(this->size1(), j); } math_vector get_row_vector(size_t i) { return row(*this,i); } math_vector get_column_vector(size_t i) { return column(*this,i); } void swap_rows (size_t i, size_t j) { row(*this,i).swap(row(*this,j)); } void swap_columns (size_t i, size_t j) { column(*this,i).swap(column(*this,j)); } const T member(size_t i, size_t j) const { return base_type::operator()(i,j) ;} void sto(size_t i, size_t j, const T & x) { base_type::operator()(i,j) = x ;} static base_type identity(size_t m) { identity_matrix tmp(m); return tmp; } static base_type zero(size_t m, size_t n) { zero_matrix tmp(m,n); return tmp; } void diag(const T & a, const T & b); void negate() { *this = - (*this); }; T trace(); }; template void math_matrix::diag(const T & a, const T & b) { for(size_t i = 0; i < this->size1(); ++i) { for (size_t j = 0; j < this->size2() ; ++j ) this->operator()(i,j) = b; this->operator()(i,i) = a; } } template inline void multiply(math_vector & v, const math_matrix & a, const math_vector & x) { v = prod(a, x); } template inline void multiply(math_matrix & v, const math_matrix & a, const math_matrix & x) { v = prod(a, x); } template inline void multiply(math_matrix & v, const math_matrix & a, const T & x) { v = a * x; } template inline void add(math_matrix & a, const math_matrix & b, const math_matrix & c) { a = b + c; } template inline T math_matrix::trace() { assert( this->size1() == this->size2()); size_t n = this->size1(); matrix_vector_slice > diag (*this, slice (0, 1, n), slice (0, 0, n)); return std::accumulate(diag.begin(), diag.end(), T(0), std::plus()); } }; // end namespace #endif