00001 /* 00002 File: SparseMat.h 00003 00004 Function: Defines a sparse matrix. 00005 00006 Author(s): Andrew Willmott 00007 00008 Copyright: (c) 1995-2000, Andrew Willmott 00009 */ 00010 00011 #ifndef __SparseMat__ 00012 #define __SparseMat__ 00013 00014 #include "vl/VL.h" 00015 #include "vl/Mat.h" 00016 #include "vl/SparseVec.h" 00017 #include "vl/SubSMat.h" 00018 #include <iostream.h> 00019 00020 00021 // --- SparseMat Class -------------------------------------------------------- 00022 00023 00024 class TSparseMat 00025 { 00026 public: 00027 00028 // Constructors 00029 00030 TSparseMat(); // Null matrix: no space allocated 00031 TSparseMat(Int rows, Int cols); // Ordinary uninitialised matrix 00032 TSparseMat(Int rows, Int cols, ZeroOrOne k); // I * k 00033 TSparseMat(Int rows, Int cols, Block k);// block matrix (m[i][j] = k) 00034 TSparseMat(const TSparseMat &m); // Copy constructor 00035 TSparseMat(const TSubSMat &m); // Conversion constructors... 00036 TSparseMat(const TMat &m); 00037 00038 ~TSparseMat(); 00039 00040 // Accessor functions 00041 00042 inline Int Rows() const { return rows; }; 00043 inline Int Cols() const { return cols; }; 00044 00045 inline TMSparseVec &operator [] (Int i); // Indexing by row 00046 inline const TMSparseVec 00047 &operator [] (Int i) const; // no checking 00048 00049 inline TMSparseVec *Ref() const; // Return pointer to data 00050 00051 // Assignment operators 00052 00053 TSparseMat &operator = (const TSparseMat &m); 00054 TSparseMat &operator = (const TSubSMat &m); 00055 TSparseMat &operator = (const TMat &m); 00056 inline TSparseMat &operator = (ZeroOrOne k); 00057 inline TSparseMat &operator = (Block k); 00058 00059 Void SetSize(Int m, Int n); 00060 Bool IsSquare() const { return(rows == cols); }; 00061 00062 // Matrix initialisers 00063 00064 Void MakeZero(); 00065 Void MakeDiag(TMReal k = vl_one); 00066 Void MakeBlock(TMReal k = vl_one); 00067 00068 protected: 00069 // Private ... 00070 TMSparseVec *row; 00071 Int rows; 00072 Int cols; 00073 }; 00074 00075 00076 // --- SparseMat In-Place Operators ------------------------------------------- 00077 00078 TSparseMat &operator += (TSparseMat &m, const TSparseMat &n); 00079 TSparseMat &operator -= (TSparseMat &m, const TSparseMat &n); 00080 TSparseMat &operator *= (TSparseMat &m, const TSparseMat &n); 00081 TSparseMat &operator *= (TSparseMat &m, TMReal s); 00082 TSparseMat &operator /= (TSparseMat &m, TMReal s); 00083 00084 // --- SparseMat Comparison Operators ----------------------------------------- 00085 00086 Bool operator == (const TSparseMat &m, const TSparseMat &n); 00087 Bool operator != (const TSparseMat &m, const TSparseMat &n); 00088 00089 // --- SparseMat Arithmetic Operators ----------------------------------------- 00090 00091 TSparseMat operator + (const TSparseMat &m, const TSparseMat &n); 00092 TSparseMat operator - (const TSparseMat &m, const TSparseMat &n); 00093 TSparseMat operator - (const TSparseMat &m); 00094 TSparseMat operator * (const TSparseMat &m, const TSparseMat &n); 00095 TSparseMat operator * (const TSparseMat &m, TMReal s); 00096 TSparseMat operator / (const TSparseMat &m, TMReal s); 00097 00098 TSparseVec &operator *= (TSparseVec &v, const TSparseMat &m); 00099 TMSparseVec operator * (const TSparseVec &v, const TSparseMat &m); 00100 TSparseVec operator * (const TSparseMat &m, const TSparseVec &v); 00101 TMVec &operator *= (TMVec &v, const TSparseMat &m); 00102 TMVec operator * (const TMVec &v, const TSparseMat &m); 00103 TMVec operator * (const TSparseMat &m, const TMVec &v); 00104 00105 TSparseMat trans(const TSparseMat &m); 00106 TMReal trace(const TSparseMat &m); 00107 TSparseMat oprod(const TSparseVec &a, const TSparseVec &b); 00108 TSparseMat oprods(const TVec &a, const TVec &b); 00109 TSparseMat inv(const TSparseMat &m, TMReal *determinant = 0, TMReal pEps = 1e-20); 00110 00111 // --- Mat Input & Output ----------------------------------------------------- 00112 00113 ostream &operator << (ostream &s, const TSparseMat &m); 00114 istream &operator >> (istream &s, TSparseMat &m); 00115 00116 // --- SparseMat Inlines ------------------------------------------------------ 00117 00118 00119 inline TMSparseVec &TSparseMat::operator [] (Int i) 00120 { 00121 CheckRange(i, 0, Rows(), "(SparseMat::[i]) i index out of range"); 00122 00123 return(row[i]); 00124 } 00125 00126 inline const TMSparseVec &TSparseMat::operator [] (Int i) const 00127 { 00128 CheckRange(i, 0, Rows(), "(SparseMat::[i]) i index out of range"); 00129 00130 return(row[i]); 00131 } 00132 00133 inline TMSparseVec *TSparseMat::Ref() const 00134 { 00135 return(row); 00136 } 00137 00138 inline TSparseMat &TSparseMat::operator = (ZeroOrOne k) 00139 { 00140 MakeDiag(k); 00141 return(SELF); 00142 }; 00143 00144 inline TSparseMat &TSparseMat::operator = (Block k) 00145 { 00146 MakeBlock((ZeroOrOne) k); 00147 return(SELF); 00148 }; 00149 00150 #endif 00151