All Classes Namespaces Functions Variables 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  typedef AT AtomicType;
70 // /*! \brief Standard constructor
71 // *
72 // * The standard constructor.
73 // * \param m The number of rows
74 // * \param n The number of columns
75 // * */
76 // Matrix(int m, int n) {};
77 
86  AT& operator()(int i, int j) {
87 #ifndef FMATVEC_NO_BOUNDS_CHECK
88  assert(i>=0);
89  assert(j>=0);
90  assert(i<rows());
91  assert(j<cols());
92 #endif
93 
94  return e(i,j);
95  }
96 
101  const AT& operator()(int i, int j) const {
102 #ifndef FMATVEC_NO_BOUNDS_CHECK
103  assert(i>=0);
104  assert(j>=0);
105  assert(i<rows());
106  assert(j<cols());
107 #endif
108 
109  return e(i,j);
110  }
111 
112  AT& e(int i, int j);
113 
114  const AT& e(int i, int j) const;
115 
120  int rows() const;
121 
126  int cols() const;
127 
132  operator std::vector<std::vector<AT> >();
133  };
134 
135 
136 
137  template <class Type, class Row, class Col, class AT> void Matrix<Type,Row,Col,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
138  for(int i=0; i<rows(); i++)
139  for(int j=0; j<cols(); j++)
140  e(i,j) = A.e(i,j);
141  }
142 
150  template <class Type, class Row, class Col, class AT> std::ostream& operator<<(std::ostream &os, const Matrix<Type,Row,Col,AT> &A) {
151  os << A.rows() << " x " << A.cols() << std::endl;
152  os << "[ ";
153  for (int i=0; i < A.rows(); ++i) {
154  for (int j=0; j < A.cols(); ++j)
155  os << std::setw(14) << A.e(i,j);
156 
157  if (i != A.rows() - 1)
158  os << std::endl << " ";
159  }
160  os << " ]";
161  return os;
162  }
163 
171  template <class Type, class Row, class Col, class AT> std::istream& operator>>(std::istream &is, Matrix<Type,Row,Col,AT> &A) {
172  int m, n;
173  char c;
174  is >> m >> c >> n >> c;
175  Matrix<General,Var,Var,AT> B(m,n,NONINIT);
176  for (int i=0; i < B.rows(); ++i)
177  for (int j=0; j < B.cols(); ++j)
178  is >> B.e(i,j);
179  is >> c;
180  A = B;
181  return is;
182  }
183 
190  template <class Type, class Row, class Col, class AT> void dump(const char* str, const Matrix<Type,Row,Col,AT> &A) {
191  std::ofstream os(str);
192  for (int i=0; i < A.rows(); ++i) {
193  for (int j=0; j < A.cols(); ++j)
194  os << std::setw(14) << A.e(i,j);
195 
196  if (i != A.rows() - 1)
197  os << std::endl;
198  }
199  os.close();
200  }
201 
202  template <class Type, class Row, class Col, class AT>
203  Matrix<Type,Row,Col,AT>::operator std::vector<std::vector<AT> >() {
204  std::vector<std::vector<AT> > ret(rows());
205  for(int r=0; r<rows(); r++) {
206  ret[r].resize(cols());
207  for(int c=0; c<cols(); c++)
208  ret[r][c]=e(r,c);
209  }
210  return ret;
211  }
212 
213  template <class Row, class AT> class SquareMatrix {
214  };
215 
216  template <class Row, class AT> class Vector {
217  };
218 
219  template <class Col, class AT> class RowVector {
220  };
221 }
222 
223 #endif
224 
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
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:86
Definition: matrix.h:39
int rows() const
Number of rows.
Definition: var_general_matrix.h:233
void dump(const char *str, const Matrix< Type, Row, Col, AT > &A)
Matrix dump.
Definition: matrix.h:190
int rows() const
Number of rows.
int cols() const
Number of columns.
Definition: fmatvec.h:44
int cols() const
Number of columns.
Definition: var_general_matrix.h:239
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:171
const AT & operator()(int i, int j) const
Element operator.
Definition: matrix.h:101
Definition: matrix.h:38
Definition: matrix.h:40

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML