Matrix Library
1、簡介
一個矩陣計算的庫,有了它,你可以很簡單地進行矩陣的計算,目前還有一些不完善的地方,還在繼續開發。
采用范型設計,可以對多種數據類型支持
對常用相關運算符進行重載
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);t
emplate <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);
//streamtemplate <class T> istream& operator>> (istream& is,
Mat<T>& m);template <class T> ostream& operator<< (ostream &os,
const Mat<T>& m);//epsilontemplate <class T> T epsilon (const T& v);