00001
00002
00003
00004 template <class TMatrix2D>
00005 Matrix3D< TMatrix2D>::Matrix3D(unsigned int rows, unsigned int cols, unsigned int depth,
00006 const typename TMatrix2D::RingType* ring, string name) : rows_m(rows),
00007 cols_m(cols) ,
00008 ring_m(ring),
00009 name_m (name)
00010 {
00011 assert(rows>=0 && cols>=0 && depth>=0);
00012
00013 matrix2DList_m.resize(depth);
00014
00015 for (unsigned int i =0; i < depth; i++)
00016 {
00017 matrix2DList_m[i] = new TMatrix2D(rows_m, cols_m, ring_m);
00018 }
00019 }
00020
00021
00022
00023 template <class TMatrix2D>
00024 Matrix3D< TMatrix2D>::Matrix3D( unsigned int rows, unsigned int cols,
00025 const vector< TMatrix2D *> & matlist,
00026 const typename TMatrix2D::RingType* ring, string name ): rows_m(rows),
00027 cols_m(cols) ,
00028 ring_m(ring),
00029 name_m (name)
00030 {
00031 matrix2DList_m.resize( matlist.size() );
00032 for (unsigned int k =0; k < matlist.size(); k++)
00033 {
00034 assert( matlist[k]!=NULL);
00035 assert( *(matlist[k])->getColNum == cols_m ) ;
00036 assert( *(matlist[k])->getRowNum == rows_m ) ;
00037 assert( *(matlist[k])->getRing == ring_m ) ;
00038 matrix2DList_m[k] = matlist[k];
00039 }
00040
00041 }
00042 template <class TMatrix2D>
00043 void Matrix3D< TMatrix2D>::checkDimensions(unsigned int row, unsigned int col, unsigned int depth) const
00044 {
00045 assert(depth<matrix2DList_m.size() );
00046 assert(row<rows_m );
00047 assert(col<cols_m );
00048 }
00049
00050
00051 template <class TMatrix2D>
00052 const typename TMatrix2D::RingType* Matrix3D< TMatrix2D>::getRing() const
00053 {
00054 return ring_m;
00055 }
00056
00057
00058 template <class TMatrix2D>
00059 void Matrix3D< TMatrix2D>::setName(string name )
00060 {
00061 name_m = name; return;
00062 };
00063
00064 template <class TMatrix2D>
00065 std::string Matrix3D< TMatrix2D>::getName(string name ) const
00066 {
00067 return name_m;
00068 };
00069
00070
00071 template <class TMatrix2D>
00072 unsigned int Matrix3D< TMatrix2D>::getRowNum() const
00073 {
00074 return rows_m;
00075 };
00076
00077
00078 template <class TMatrix2D>
00079 unsigned int Matrix3D< TMatrix2D>::getColNum() const
00080 {
00081 return cols_m;
00082 };
00083
00084
00085 template <class TMatrix2D>
00086 unsigned int Matrix3D< TMatrix2D>::getZNum() const
00087 {
00088 return matrix2DList_m.size();
00089 };
00090
00091
00092 template <class TMatrix2D>
00093 void Matrix3D< TMatrix2D>::setFrontalMatrix( unsigned int index, TMatrix2D * matrix)
00094 {
00095 assert( index< matrix2DList_m.size() );
00096 assert( matrix != NULL );
00097 assert( ring_m == matrix->getRing() );
00098
00099
00100 assert( matrix->getColNum()==cols_m );
00101 assert( matrix->getRowNum()==rows_m );
00102
00103 delete matrix2DList_m[index];
00104
00105 matrix2DList_m[index]= matrix;
00106
00107 }
00108
00109 template <class TMatrix2D>
00110 void Matrix3D< TMatrix2D>::appendFrontalMatrix( TMatrix2D * matrix)
00111 {
00112 assert( matrix != NULL );
00113 assert( ring_m==matrix->getRing() );
00114
00115 assert( matrix->getColNum()==cols_m );
00116 assert( matrix->getRowNum()==rows_m );
00117
00118 matrix2DList_m.push_back( matrix);
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 template <class TMatrix2D>
00132 const TMatrix2D & Matrix3D< TMatrix2D>::getFrontalMatrixConstRef( unsigned int index) const
00133 {
00134 assert(index<matrix2DList_m.size() );
00135
00136 return *(matrix2DList_m[index]);
00137 }
00138
00139
00140
00141 template <class TMatrix2D>
00142 TMatrix2D & Matrix3D< TMatrix2D>::getFrontalMatrixRef( unsigned int index)
00143 {
00144 assert(index<matrix2DList_m.size() );
00145
00146 return *(matrix2DList_m[index]);
00147 }
00148
00149
00150 template <class TMatrix2D >
00151 Matrix3D< TMatrix2D >* Matrix3D< TMatrix2D >::leftMultiply(
00152 const TMatrix2D * mat) const
00153 {
00154 assert( mat!=0 );
00155 assert( ring_m==mat->getRing() );
00156
00157 assert( mat->getColNum()==rows_m );
00158
00159 Matrix3D* res = new Matrix3D(mat->getRowNum(),cols_m, matrix2DList_m.size(), ring_m);
00160
00161 for (unsigned int k =0; k < matrix2DList_m.size(); k++)
00162 {
00163 res->setFrontalMatrix(k, (*mat)*(*matrix2DList_m[k]) );
00164 }
00165 return res;
00166 }
00167
00168
00169 template <class TMatrix2D>
00170 Matrix3D< TMatrix2D >* Matrix3D< TMatrix2D>::rightMultiply(const TMatrix2D * mat) const
00171 {
00172 assert( mat!=0 );
00173 assert( ring_m==mat->getRing() );
00174
00175 assert( mat->getRowNum()==cols_m );
00176
00177 Matrix3D* res = new Matrix3D( rows_m,mat->getColNum(), matrix2DList_m.size(), ring_m);
00178
00179 for (unsigned int k =0; k < matrix2DList_m.size(); k++)
00180 {
00181 res->setFrontalMatrix(k, (*matrix2DList_m[k])*(*mat) );
00182 }
00183 return res;
00184 }
00185
00186
00187 template <class TMatrix2D>
00188 void Matrix3D< TMatrix2D>::setVal(unsigned int row, unsigned int col, unsigned int z, typename TMatrix2D::ElementType val)
00189 {
00190 checkDimensions(row,col,z);
00191
00192 matrix2DList_m[z]->setVal(row,col,val);
00193 }
00194
00195
00196 template <class TMatrix2D>
00197 typename TMatrix2D::ElementType Matrix3D< TMatrix2D>::getVal(unsigned int row, unsigned int col, unsigned int z) const
00198 {
00199 checkDimensions(row,col,z);
00200 return matrix2DList_m[z]->getVal(row,col);
00201 }
00202
00203
00204 template <class TMatrix2D>
00205 Matrix3D< TMatrix2D>* Matrix3D< TMatrix2D>::getTransversalForm() const
00206 {
00207 Matrix3D * transversal3DMat = new Matrix3D(matrix2DList_m.size(), cols_m, rows_m, ring_m );
00208
00209 for (unsigned int i =0; i < rows_m; i++)
00210 {
00211 for (unsigned int j =0; j < cols_m; j++)
00212 {
00213 for (unsigned int k=0 ;k < matrix2DList_m.size(); k++)
00214 {
00215 transversal3DMat->setVal(k , j, i , getVal(i , j , k) );
00216 }
00217 }
00218 }
00219 return transversal3DMat;
00220 }
00221
00222 template <class TMatrix2D>
00223 void Matrix3D< TMatrix2D>::printTransversalView() const
00224 {
00225
00226 Matrix3D * transversal3DMat = getTransversalForm();
00227
00228 transversal3DMat->print3DMatrix();
00229 delete transversal3DMat;
00230 }
00231
00232
00233 template <class TMatrix2D>
00234 void Matrix3D< TMatrix2D>::print3DMatrix(ostream & os) const
00235 {
00236 os << " --3DMatrix " << name_m << std::endl;
00237 os << "{" << std::endl;
00238 for (unsigned int i=0; i<matrix2DList_m.size(); i++)
00239 {
00240 if (i>0)
00241 os << ",";
00242 os << "--k = " << i << std::endl;
00243 os << *(matrix2DList_m[i]) << std::endl;
00244
00245 }
00246 os << "}" << std::endl;
00247 }
00248
00249
00255 template <class TMatrix2D>
00256 inline Matrix3D<TMatrix2D>* Matrix3D< TMatrix2D>::computeFrontalMatrixBasis( ) const
00257 {
00258
00259 Matrix3D< TMatrix2D >* frontalMatrixBasis = new Matrix3D< TMatrix2D >(rows_m, cols_m, 0, ring_m, "FrontalMatrixBasis" ) ;
00260
00261
00262 unsigned int matrixSize = rows_m * cols_m;
00263
00264 unsigned int matrixCount = getZNum();
00265
00266 TMatrix2D matricesAsRows(matrixCount, matrixSize, ring_m ,"matricesAsRows" );
00267
00268 for (unsigned int z=0; z < matrixCount ; z++)
00269 {
00270 TVector<typename TMatrix2D::RingType> * matrix_i_AsVector = getFrontalMatrixConstRef(z).convertToVector();
00271
00272 for (unsigned int col=0; col < matrixSize ; col++)
00273 {
00274 matricesAsRows.setVal(z,col, matrix_i_AsVector->getVal(col) );
00275 }
00276 delete matrix_i_AsVector;
00277 }
00278
00279
00280 TMatrix2D * independentMatricesAsRows = matricesAsRows.getRowEchelonForm();
00281
00282 independentMatricesAsRows->setName("independentMatricesAsRows");
00283 #ifdef DEBUG
00284
00285
00286 #endif
00287 unsigned int independentMatricesCount = independentMatricesAsRows->getRank();
00288
00289
00290 for (unsigned int z=0; z < independentMatricesAsRows->getRowNum() ; z++)
00291 {
00292 TVector<typename TMatrix2D::RingType> * matrixAsVector = independentMatricesAsRows->getRowAsVector(z);
00293 if ( matrixAsVector->isNotZero() )
00294 {
00295
00296 TMatrix2D * fm = new TMatrix2D(rows_m, cols_m, ring_m, *(matrixAsVector) );
00297
00298
00299 frontalMatrixBasis->appendFrontalMatrix( fm ) ;
00300 }
00301 delete matrixAsVector;
00302 }
00303 assert(independentMatricesCount == frontalMatrixBasis->getZNum() );
00304
00305 delete independentMatricesAsRows;
00306
00307 return frontalMatrixBasis;
00308 }
00309
00310
00311
00312
00313 template <class TMatrix2D>
00314 void Matrix3D< TMatrix2D>::print3DMatrix() const
00315 {
00316 print3DMatrix(std::cerr);
00317 }
00318
00319
00320 template <class TMatrix2D>
00321 void Matrix3D< TMatrix2D>::clear()
00322 {
00323 for (unsigned int i=0; i<matrix2DList_m.size(); i++)
00324 {
00325 delete matrix2DList_m[i];
00326 }
00327 matrix2DList_m.resize(0);
00328
00329 }
00330
00331
00332 template <class TMatrix2D>
00333 Matrix3D< TMatrix2D>::~Matrix3D()
00334 {
00335 for (unsigned int i=0; i<matrix2DList_m.size(); i++)
00336 {
00337 delete matrix2DList_m[i];
00338 }
00339 }
00340
00341
00342 template <class TMatrix2D>
00343 std::ostream & operator<<(std::ostream & out, const Matrix3D< TMatrix2D> & z)
00344 {
00345 z.print3DMatrix(out);
00346 return out;
00347 }
00348