matrix.h
1 /* Copyright (C) 2003-2005 Martin Förg
2 
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact:
18  * martin.o.foerg@googlemail.com
19  *
20  */
21 
22 #ifndef matrix_h
23 #define matrix_h
24 
25 #define FMATVEC_SIZE_CHECK
26 #define FMATVEC_VOID_CHECK
27 
28 #include <cassert>
29 #include <fstream>
30 #include <sstream>
31 #include <iomanip>
32 #include <vector>
33 
38 namespace fmatvec {
39 
42  enum Initialization{INIT,NONINIT,EYE};
43 
50  template <class ST, class AT> class Matrix {
51 
52  protected:
53 
55 
56  AT* ele;
57  int m, n;
58 
59  void deepCopy(const Matrix<ST, AT> &A);
60 
62 
63  public:
70  Matrix(int m, int n) {};
71 
80  AT& operator()(int i, int j);
81 
86  const AT& operator()(int i, int j) const;
87 
92  int rows() const {return m;};
93 
98  int cols() const {return n;};
99 
104  operator std::vector<std::vector<AT> >();
105  };
106 
107 
108 
109  template <class ST, class AT> void Matrix<ST, AT>::deepCopy(const Matrix<ST, AT> &A) {
110  for(int i=0; i<m; i++)
111  for(int j=0; j<n; j++)
112  operator()(i,j) = A(i,j);
113  }
114 
122  template <class ST, class AT> std::ostream& operator<<(std::ostream &os, const Matrix<ST, AT> &A) {
123  os << A.rows() << " x " << A.cols() << std::endl;
124  os << "[ ";
125  for (int i=0; i < A.rows(); ++i) {
126  for (int j=0; j < A.cols(); ++j)
127  os << std::setw(14) << A(i,j);
128 
129  if (i != A.rows() - 1)
130  os << std::endl << " ";
131  }
132  os << " ]";
133  return os;
134  }
135 
143  template <class ST, class AT> std::istream& operator>>(std::istream &is, Matrix<ST, AT> &A) {
144  int m, n;
145  char c;
146  is >> m >> c >> n >> c;
147  A.resize(m,n,NONINIT);
148  for (int i=0; i < A.rows(); ++i)
149  for (int j=0; j < A.cols(); ++j)
150  is >> A(i,j);
151  is >> c;
152  return is;
153  }
154 
161  template <class ST, class AT> void dump(const char* str, const Matrix<ST, AT> &A) {
162  std::ofstream os(str);
163  for (int i=0; i < A.rows(); ++i) {
164  for (int j=0; j < A.cols(); ++j)
165  os << std::setw(14) << A(i,j);
166 
167  if (i != A.rows() - 1)
168  os << std::endl;
169  }
170  os.close();
171  }
172 
173  template <class ST, class AT>
174  Matrix<ST, AT>::operator std::vector<std::vector<AT> >() {
175  std::vector<std::vector<AT> > ret(rows());
176  for(int r=0; r<rows(); r++) {
177  ret[r].resize(cols());
178  for(int c=0; c<cols(); c++)
179  ret[r][c]=operator()(r,c);
180  }
181  return ret;
182  }
183 
184 }
185 
186 #endif
187