00001
00014 #ifndef _MATRIX_H
00015 #define _MATRIX_H
00016
00017 #include <Vector.H>
00018
00019
00020 #define MxN_VecOfVec Vector<Vector<T, M>, N>
00021 #define NxM_VecOfVec Vector<Vector<T, N>, M>
00022 #define NxL_VecOfVec Vector<Vector<T, N>, L>
00023 #define MxL_VecOfVec Vector<Vector<T, M>, L>
00024
00025
00026 namespace xchen
00027 {
00029 template <class T, int M, int N>
00030 class Matrix
00031 {
00032 public:
00033 Matrix( ) { }
00034 Matrix(const MxN_VecOfVec& m) : vOfv(m) { }
00035
00036 MxN_VecOfVec& operator()() { return vOfv; }
00037 const MxN_VecOfVec& operator()() const { return vOfv; }
00038
00039 T& operator() (int r, int c) { return vOfv[c][r]; }
00040 const T& operator() (int r, int c) const { return vOfv[c][r]; }
00041 void SetIdentity();
00042 void SetFromColumnMajorData(T* data);
00043 void GetColumnMajorData(T* data) const;
00044
00045
00046 Vector<T, M> operator()(const Vector<T, N>& x) const { return (*this) * x; }
00047 Vector<T, M> operator*(const Vector<T, N>& x) const;
00048
00049 template <int L>
00050 Matrix<T,M,L> operator*(const Matrix<T,N,L>& B);
00051
00052 void GetTranspose(Matrix<T,N,M>* t) const;
00053 void GetInverse(Matrix<T,M,M>* i) const;
00054 Matrix<T,N,M>* GetTranspose() const { Matrix<T,N,M>* t = new Matrix<T,N,M>; GetTranspose(t); return t; }
00055 Matrix<T,M,M>* GetInverse() const { Matrix<T,N,M>* i = new Matrix<T,N,M>; GetInverse(i); return t; }
00056
00057 friend ostream &operator<<<>(ostream &os,const Matrix &m);
00058
00059 private:
00060 MxN_VecOfVec vOfv;
00061 };
00062
00063
00064
00065
00066
00067 template <class T, int M, int N>
00068 inline void Matrix<T, M, N> :: SetFromColumnMajorData(T* data)
00069 {
00070 typename MxN_VecOfVec::iterator iter = vOfv.begin();
00071 for(; iter != vOfv.end(); iter++)
00072 {
00073 copy(data, data+M, iter->begin());
00074 data += M;
00075 }
00076 }
00077
00078 template <class T, int M, int N>
00079 inline void Matrix<T, M, N> :: GetColumnMajorData(T* data) const
00080 {
00081 int i = 0;
00082 for(typename MxN_VecOfVec::const_iterator iter1 = vOfv.begin(); iter1 != vOfv.end(); iter1++)
00083 for(typename Vector<T,M>::const_iterator iter2 = iter1->begin(); iter2 != iter1->end(); iter2++)
00084 {
00085 data[i++] = *iter2;
00086 }
00087 }
00088
00089 template <class T, int M, int N>
00090 inline void Matrix<T, M, N> :: SetIdentity()
00091 {
00092 fill(vOfv.begin(), vOfv.end(), Vector<T,M>(0.0));
00093 assert( M==N );
00094 for(int i=0; i<M; i++)
00095 vOfv[i][i] = 1.0;
00096 }
00097
00098 template <class T, int M, int N>
00099 inline Vector<T, M> Matrix<T, M, N> :: operator*(const Vector<T, N>& x) const
00100 {
00101 typename MxN_VecOfVec::const_iterator itr1 = vOfv.begin();
00102 typename Vector<T, N>::const_iterator itr2 = x.begin();
00103 Vector<T, M> ret(0.0);
00104 for(; itr1 != vOfv.end(); itr1++, itr2++)
00105 {
00106 ret += (*itr1) * (*itr2);
00107 }
00108 return ret;
00109 }
00110
00111 template <class T, int M, int N>
00112 template <int L>
00113 inline Matrix<T,M,L> Matrix<T, M, N> :: operator*(const Matrix<T,N,L>& B)
00114 {
00115 Vector<T,M> col_vecs[L];
00116 typename NxL_VecOfVec::const_iterator itr = B().begin();
00117
00118 for(int i=0; itr != B().end(); itr++, i++)
00119 {
00120 col_vecs[i] = (*this) * (*itr);
00121 }
00122 return Matrix<T,M,L>(col_vecs);
00123 }
00124
00125 template <class T, int M, int N>
00126 inline void Matrix<T, M, N> :: GetTranspose(Matrix<T,N,M>* t) const
00127 {
00128 typename MxN_VecOfVec::const_iterator iter1 = vOfv.begin();
00129 for(int col=0; iter1 != vOfv.end(); iter1++, col++)
00130 {
00131 typename Vector<T,M>::const_iterator iter2 = iter1->begin();
00132 for(int row=0; iter2 != iter1->end(); iter2++, row++)
00133 {
00134 (*t)(col, row) = *iter2;
00135 }
00136 }
00137 }
00138
00139 template <class T, int M, int N>
00140 inline void Matrix<T,M,N> :: GetInverse(Matrix<T,M,M>* i) const
00141 {
00142 assert( M==N );
00143 cerr << "Matrix::GetInverse() not implemented yet.\n";
00144 }
00145
00146
00147
00148 template <class T, int M, int N>
00149 inline ostream &operator<< (ostream &os,const Matrix<T,M,N >&m)
00150 {
00151 Matrix<T,N,M>* t = m.GetTranspose();
00152 copy((*t)().begin(), (*t)().end(), ostream_iterator< Vector<T,N> >(cout, "\n"));
00153 delete t;
00154 return os;
00155 }
00156
00157
00158
00159 typedef Vector<Vector<double, 2>, 2> dVecOfVec22;
00160 typedef Vector<Vector<double, 2>, 3> dVecOfVec23;
00161 typedef Vector<Vector<double, 2>, 4> dVecOfVec24;
00162 typedef Vector<Vector<double, 2>, 5> dVecOfVec25;
00163
00164 typedef Vector<Vector<double, 3>, 2> dVecOfVec32;
00165 typedef Vector<Vector<double, 3>, 3> dVecOfVec33;
00166 typedef Vector<Vector<double, 3>, 4> dVecOfVec34;
00167 typedef Vector<Vector<double, 3>, 5> dVecOfVec35;
00168
00169 typedef Vector<Vector<double, 4>, 2> dVecOfVec42;
00170 typedef Vector<Vector<double, 4>, 3> dVecOfVec43;
00171 typedef Vector<Vector<double, 4>, 4> dVecOfVec44;
00172 typedef Vector<Vector<double, 4>, 5> dVecOfVec45;
00173
00174 typedef Matrix<double, 2, 2> dMatrix22;
00175 typedef Matrix<double, 2, 2> dMatrix23;
00176 typedef Matrix<double, 3, 3> dMatrix24;
00177 typedef Matrix<double, 3, 3> dMatrix25;
00178
00179 typedef Matrix<double, 3, 3> dMatrix32;
00180 typedef Matrix<double, 3, 3> dMatrix33;
00181 typedef Matrix<double, 3, 4> dMatrix34;
00182 typedef Matrix<double, 3, 5> dMatrix35;
00183
00184 typedef Matrix<double, 4, 2> dMatrix42;
00185 typedef Matrix<double, 4, 3> dMatrix43;
00186 typedef Matrix<double, 4, 4> dMatrix44;
00187 typedef Matrix<double, 4, 5> dMatrix45;
00188
00189 typedef dMatrix44 HMatrix;
00190
00191 }
00192
00193
00194 #endif