matrix.h
00001 /* Copyright (C) 2003-2005  Martin Förg
00002 
00003  * This library is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU Lesser General Public
00005  * License as published by the Free Software Foundation; either
00006  * version 2.1 of the License, or (at your option) any later version.
00007  * 
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Lesser General Public License for more details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public
00014  * License along with this library; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00016  *
00017  * Contact:
00018  *   martin.o.foerg@googlemail.com
00019  *
00020  */
00021 
00022 #ifndef matrix_h
00023 #define matrix_h
00024 
00025 #include <cassert>
00026 #include <fstream>
00027 #include <sstream>
00028 #include <iomanip>
00029 #include <vector>
00030 #include "range.h"
00031 
00036 namespace fmatvec {
00037 
00038   class Noinit { };
00039   class Init { };
00040   class Eye { };
00041 
00042   static Noinit NONINIT = Noinit();
00043   static Init INIT = Init();
00044   static Eye EYE = Eye();
00045 
00048   //enum Initialization{INIT,NONINIT,EYE};
00049 
00056   template <class Type, class Row, class Col, class AT> class Matrix {
00057 
00058     protected:
00059 
00061 
00062       //AT* ele;
00063 
00064       void deepCopy(const Matrix<Type,Row,Col,AT> &A);
00065 
00067 
00068     public:
00069 //      /*! \brief Standard constructor
00070 //       * 
00071 //       * The standard constructor.
00072 //       * \param m The number of rows
00073 //       * \param n The number of columns
00074 //       * */
00075 //      Matrix(int m, int n) {};
00076 
00085       AT& operator()(int i, int j) {
00086 #ifndef FMATVEC_NO_BOUNDS_CHECK
00087         assert(i>=0);
00088         assert(j>=0);
00089         assert(i<rows());
00090         assert(j<cols());
00091 #endif
00092 
00093         return e(i,j);
00094       }
00095 
00100       const AT& operator()(int i, int j) const {
00101 #ifndef FMATVEC_NO_BOUNDS_CHECK
00102         assert(i>=0);
00103         assert(j>=0);
00104         assert(i<rows());
00105         assert(j<cols());
00106 #endif
00107 
00108         return e(i,j);
00109       }
00110 
00111       AT& e(int i, int j);
00112 
00113       const AT& e(int i, int j) const;
00114 
00119       int rows() const;
00120 
00125       int cols() const;
00126 
00131       operator std::vector<std::vector<AT> >();
00132   };
00133 
00134 
00135 
00136   template <class Type, class Row, class Col, class AT> void Matrix<Type,Row,Col,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) { 
00137     for(int i=0; i<rows(); i++) 
00138       for(int j=0; j<cols(); j++) 
00139         e(i,j) = A.e(i,j);
00140   }
00141 
00149   template <class Type, class Row, class Col, class AT> std::ostream& operator<<(std::ostream &os, const Matrix<Type,Row,Col,AT> &A) {
00150     os << A.rows() << " x " << A.cols() << std::endl;
00151     os << "[ ";
00152     for (int i=0; i < A.rows(); ++i) {
00153       for (int j=0; j < A.cols(); ++j) 
00154         os << std::setw(14) << A.e(i,j);
00155 
00156       if (i != A.rows() - 1)
00157         os << std::endl  << "  ";
00158     }
00159     os << " ]";
00160     return os;
00161   }
00162 
00170   template <class Type, class Row, class Col, class AT> std::istream& operator>>(std::istream &is, Matrix<Type,Row,Col,AT> &A) {
00171     int m, n;
00172     char c;
00173     is >> m >> c >> n >> c;
00174     Matrix<General,Var,Var,AT> B(m,n,NONINIT);
00175     for (int i=0; i < B.rows(); ++i) 
00176       for (int j=0; j < B.cols(); ++j) 
00177         is >> B.e(i,j);
00178     is >> c;
00179     A = B;
00180     return is;
00181   }
00182 
00189   template <class Type, class Row, class Col, class AT> void dump(const char* str, const Matrix<Type,Row,Col,AT> &A) {
00190     std::ofstream os(str);
00191     for (int i=0; i < A.rows(); ++i) {
00192       for (int j=0; j < A.cols(); ++j) 
00193         os << std::setw(14) << A.e(i,j);
00194 
00195       if (i != A.rows() - 1)
00196         os << std::endl;
00197     }
00198     os.close();
00199   }
00200 
00201   template <class Type, class Row, class Col, class AT>
00202     Matrix<Type,Row,Col,AT>::operator std::vector<std::vector<AT> >() {
00203       std::vector<std::vector<AT> > ret(rows());
00204       for(int r=0; r<rows(); r++) {
00205         ret[r].resize(cols());
00206         for(int c=0; c<cols(); c++)
00207           ret[r][c]=e(r,c);
00208       }
00209       return ret;
00210     }
00211 
00212   template <class Row, class AT> class SquareMatrix {
00213   };
00214 
00215   template <class Row, class AT> class Vector {
00216   };
00217 
00218   template <class Col, class AT> class RowVector {
00219   };
00220 }
00221 
00222 #endif
00223