Matrix Library
1、简介
一个矩阵计算的库,有了它,你可以很简单地进行矩阵的计算,目前还有一些不完善的地方,还在继续开发。
- 采用范型设计,可以对多种数据类型支持
- 对常用相关运算符进行重载
项目主页git@github.com:libing64/Matrix.git
2、类声明文件
template <class T>
class Mat
{
public:
// Constructors 构造函数
Mat (size_t nRow, size_t nCol);
Mat (size_t nRow, size_t nCol, const T& t );
Mat (size_t nRow, size_t nCol, const T* Array);
~Mat ();
// Copy constructors 复制函数
Mat (const Mat<T>& m);
void clone ();
// Assignment operators
Mat<T>& operator= (const Mat<T>& m);
//size 大小
size_t size () { return this->nRow * this->nCol; }
size_t typesize () { return sizeof(T); }
size_t colsize () { return this->nRow; } // No. of elements in a column, i.e., it's row number
size_t rowsize () { return this->nCol; } // No. of elements in a row, i.e., it's column number
size_t rowno () { return this->nRow; } // No. of rows
size_t colno () { return this->nCol; } // No. of columns
void resize (size_t nRow, size_t nCol, const T& dval = T(0));
void free ();
const T& operator() (size_t i, size_t j) const;
// Unary operators 单目运算符
Mat<T> operator+ () const { return *this; }
Mat<T> operator- () const;
Mat<T> operator~ () const;
Mat<T> operator! () const;
// Computed assignment赋值计算
Mat<T>& operator+= (const Mat<T>& m);
Mat<T>& operator-= (const Mat<T>& m);
Mat<T>& operator*= (const Mat<T>& m);
Mat<T>& operator/= (const Mat<T>& m);
Mat<T>& operator*= (const T& x);
Mat<T>& operator/= (const T& x);
// Miscellaneous methods
void null ();
void unit ();
void rand (int rmin=-1, int rmax=1, int rseed=0);
T sum ();
T min ();
T max ();
bool lud (valarray<size_t>& ri, T* pDet = NULL);
void lubksb (const valarray<size_t>& ri, const valarray<T>& v, valarray<T>& s) const;
void lumpove (const Mat<T>& ludm, const valarray<size_t>& ri, const valarray<T>& v, valarray<T>& s) const;
bool solve_lu (const valarray<T>& v, valarray<T>& s) const { return solve( v, s); }
bool svd (Mat<T>& vc, valarray<T>& w);
void svbksb (const Mat<T>& vc, const valarray<T>& w, const valarray<T>& b, valarray<T>& x) const;
bool solve_sv (const valarray<T>& v, valarray<T>& s) const;
void qrd (Mat<T>& r);
void qrbksb (const Mat<T>& r, const valarray<T>& v, valarray<T>& s) const;
bool solve_qr (const valarray<T>& v, valarray<T>& s) const;
bool chold ();
void cholbksb (const valarray<T>& v, valarray<T>& s) const;
bool solve_chol (const valarray<T>& v, valarray<T>& s) const;
bool eigen (valarray<T>& eival) const;
bool eigen (valarray<T>& eival, Mat<T>& eivec) const;
bool eigen (valarray<T>& rev, valarray<T>& iev) const;
bool eigen (valarray<T>& rev, valarray<T>& iev, Mat<T>& eivec) const;
// Inversion TODO
bool inv ();
bool inv_lu ();
bool inv_sv ();
bool inv_qr ();
// Utility methods
bool solve (const valarray<T>& v, valarray<T>& s) const;
bool solve (const Mat<T>& v, Mat<T>& s) const;
Mat<T> adj () const;
T cofact (size_t row, size_t col) const;
T det () const;
T cond () const;
size_t rank () const;
T norm1 () const;
T norm2 () const;
T normI () const;
T normF () const;
//display
void show();
int nRow;
int nCol;
T* data;
private:
};
3、重载运算符
/
// Non-member binary operators
//
template <class T> Mat<T> operator+ (const Mat<T>& m1, const Mat<T>& m2);
template <class T> Mat<T> operator- (const Mat<T>& m1, const Mat<T>& m2);
template <class T> Mat<T> operator* (const Mat<T>& m1, const Mat<T>& m2);
template <class T> Mat<T> operator* (const Mat<T>& m, const T& el);
template <class T> Mat<T> operator* (const T& el, const Mat<T>& m);
template <class T> Mat<T> operator/ (const Mat<T>& m1, const Mat<T>& m2);
template <class T> Mat<T> operator/ (const Mat<T>& m, const T& el);
template <class T> Mat<T> operator/ (const T& el, const Mat<T>& m);
template <class T> bool operator== (const Mat<T>& m1, const Mat<T>& m2);
template <class T> bool operator!= (const Mat<T>& m1, const Mat<T>& m2);
/
// Non-member functions
//
template <class T> Mat<T> mpow (const Mat<T>& m, size_t n);
template <class T> Mat<T> mabs (const Mat<T>& m);
template <class T> Mat<T> mfloor (const Mat<T>& m);
template <class T> Mat<T> mceil (const Mat<T>& m);
template <class T> void mswap (Mat<T>& x, Mat<T>& y);
//stream
template <class T> istream& operator>> (istream& is, Mat<T>& m);
template <class T> ostream& operator<< (ostream &os, const Mat<T>& m);
//epsilon
template <class T> T epsilon (const T& v);