All Classes Namespaces Functions Typedefs Enumerations Pages
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 #include <cassert>
26 #include <fstream>
27 #include <sstream>
28 #include <iomanip>
29 #include <vector>
30 #include "range.h"
31 
36 namespace fmatvec {
37 
38  class Noinit { };
39  class Init { };
40  class Eye { };
41 
42  static Noinit NONINIT = Noinit();
43  static Init INIT = Init();
44  static Eye EYE = Eye();
45 
48  //enum Initialization{INIT,NONINIT,EYE};
49 
56  template <class Type, class Row, class Col, class AT> class Matrix {
57 
58  protected:
59 
61 
62  //AT* ele;
63 
64  void deepCopy(const Matrix<Type,Row,Col,AT> &A);
65 
67 
68  public:
69 // /*! \brief Standard constructor
70 // *
71 // * The standard constructor.
72 // * \param m The number of rows
73 // * \param n The number of columns
74 // * */
75 // Matrix(int m, int n) {};
76 
85  AT& operator()(int i, int j) {
86 #ifndef FMATVEC_NO_BOUNDS_CHECK
87  assert(i>=0);
88  assert(j>=0);
89  assert(i<rows());
90  assert(j<cols());
91 #endif
92 
93  return e(i,j);
94  }
95 
100  const AT& operator()(int i, int j) const {
101 #ifndef FMATVEC_NO_BOUNDS_CHECK
102  assert(i>=0);
103  assert(j>=0);
104  assert(i<rows());
105  assert(j<cols());
106 #endif
107 
108  return e(i,j);
109  }
110 
111  AT& e(int i, int j);
112 
113  const AT& e(int i, int j) const;
114 
119  int rows() const;
120 
125  int cols() const;
126 
131  operator std::vector<std::vector<AT> >();
132  };
133 
134 
135 
136  template <class Type, class Row, class Col, class AT> void Matrix<Type,Row,Col,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
137  for(int i=0; i<rows(); i++)
138  for(int j=0; j<cols(); j++)
139  e(i,j) = A.e(i,j);
140  }
141 
149  template <class Type, class Row, class Col, class AT> std::ostream& operator<<(std::ostream &os, const Matrix<Type,Row,Col,AT> &A) {
150  os << A.rows() << " x " << A.cols() << std::endl;
151  os << "[ ";
152  for (int i=0; i < A.rows(); ++i) {
153  for (int j=0; j < A.cols(); ++j)
154  os << std::setw(14) << A.e(i,j);
155 
156  if (i != A.rows() - 1)
157  os << std::endl << " ";
158  }
159  os << " ]";
160  return os;
161  }
162 
170  template <class Type, class Row, class Col, class AT> std::istream& operator>>(std::istream &is, Matrix<Type,Row,Col,AT> &A) {
171  int m, n;
172  char c;
173  is >> m >> c >> n >> c;
174  Matrix<General,Var,Var,AT> B(m,n,NONINIT);
175  for (int i=0; i < B.rows(); ++i)
176  for (int j=0; j < B.cols(); ++j)
177  is >> B.e(i,j);
178  is >> c;
179  A = B;
180  return is;
181  }
182 
189  template <class Type, class Row, class Col, class AT> void dump(const char* str, const Matrix<Type,Row,Col,AT> &A) {
190  std::ofstream os(str);
191  for (int i=0; i < A.rows(); ++i) {
192  for (int j=0; j < A.cols(); ++j)
193  os << std::setw(14) << A.e(i,j);
194 
195  if (i != A.rows() - 1)
196  os << std::endl;
197  }
198  os.close();
199  }
200 
201  template <class Type, class Row, class Col, class AT>
202  Matrix<Type,Row,Col,AT>::operator std::vector<std::vector<AT> >() {
203  std::vector<std::vector<AT> > ret(rows());
204  for(int r=0; r<rows(); r++) {
205  ret[r].resize(cols());
206  for(int c=0; c<cols(); c++)
207  ret[r][c]=e(r,c);
208  }
209  return ret;
210  }
211 
212  template <class Row, class AT> class SquareMatrix {
213  };
214 
215  template <class Row, class AT> class Vector {
216  };
217 
218  template <class Col, class AT> class RowVector {
219  };
220 }
221 
222 #endif
223 
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:56
This is a matrix class for general matrices.
Definition: var_general_matrix.h:38
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:85
Definition: matrix.h:39
int rows() const
Number of rows.
Definition: var_general_matrix.h:232
Definition: matrix.h:218
void dump(const char *str, const Matrix< Type, Row, Col, AT > &A)
Matrix dump.
Definition: matrix.h:189
int rows() const
Number of rows.
int cols() const
Number of columns.
Definition: matrix.h:212
Definition: matrix.h:215
int cols() const
Number of columns.
Definition: var_general_matrix.h:238
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:170
const AT & operator()(int i, int j) const
Element operator.
Definition: matrix.h:100
Definition: matrix.h:38
Definition: matrix.h:40

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML