// Copyright 2008 Nanorex, Inc. See LICENSE file for details. #ifndef NX_MATRIX_H #define NX_MATRIX_H #include namespace Nanorex { /* CLASS: NXMatrix */ /** * Column-major matrix of dimensions M x N * */ template class NXMatrix { public: NXMatrix() throw() {} ~NXMatrix() throw() {} T const *const data(void) const throw() { return elems; } T *const data(void) throw() { return elems; } /// Access value at m-th row, n-th column T const& operator () (int const& m, int const& n) const throw(); /// Access value at m-th row, n-th column T& operator () (int const& m, int const& n) throw(); /// Access n-th column NXVectorRef col(int const& n) throw(); template NXMatrix& operator += (NXMatrix const& A) throw(); template NXMatrix& operator -= (NXMatrix const& A) throw(); template NXMatrix& operator *= (T1 const& c) throw(); template NXMatrix& operator /= (T1 const& c) throw(); private: T elems[M*N]; }; typedef NXMatrix NXMatrix33d; typedef NXMatrix NXMatrix33f; typedef NXMatrix NXMatrix44d; typedef NXMatrix NXMatrix44f; // Access value at m-th row, n-th column template inline T const& NXMatrix::operator () (int const& m, int const& n) const throw() { return elems[n*M + m]; } // Access value at m-th row, n-th column template inline T& NXMatrix::operator () (int const& m, int const& n) throw() { return elems[n*M + m]; } // Access n-th column template inline NXVectorRef NXMatrix::col(int const& n) throw() { return NXVectorRef(elems + n*M); } template template inline NXMatrix& NXMatrix::operator += (NXMatrix const& A) throw() { // Exploit isomorphism with M*N-dimensional vectors NXVectorRef selfRef(elems); NXVectorRef ARef(A.data()); selfRef += ARef; return *this; } template template inline NXMatrix& NXMatrix::operator -= (NXMatrix const& A) throw() { // Exploit isomorphism with M*N-dimensional vectors NXVectorRef selfRef(elems); NXVectorRef ARef(A.data()); selfRef -= ARef; return *this; } template template inline NXMatrix& NXMatrix::operator *= (T1 const& c) throw() { // Exploit isomorphism with M*N-dimensional vectors NXVectorRef selfRef(elems); selfRef *= c; return *this; } template template inline NXMatrix& NXMatrix::operator /= (T1 const& c) throw() { // Exploit isomorphism with M*N-dimensional vectors NXVectorRef selfRef(elems); T const one_by_c = T(1) / c; selfRef *= one_by_c; return *this; } /// Matrix-matrix multiplication (inefficient) template NXMatrix operator * (NXMatrix const& A, NXMatrix const& B) { NXMatrix C; // result for(int m=0; m NXVector operator * (NXMatrix const& A, NXVector const& x) { NXVector y; // result y.zero(); for(int n=0; n