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 <stdexcept>
30 #include <limits>
31 #include <vector>
32 #include <boost/scope_exit.hpp>
33 #include "range.h"
34 #include "toString.h"
35 
40 namespace fmatvec {
41 
42  class Noinit { };
43  class Init { };
44  class Eye { };
45 
46  static Noinit NONINIT = Noinit();
47  static Init INIT = Init();
48  static Eye EYE = Eye();
49 
52  //enum Initialization{INIT,NONINIT,EYE};
53 
60  template <class Type, class Row, class Col, class AT> class Matrix {
61 
62  protected:
63 
65 
66  //AT* ele;
67 
68  void deepCopy(const Matrix<Type,Row,Col,AT> &A);
69 
71 
72  public:
73  typedef AT value_type;
74 // /*! \brief Standard constructor
75 // *
76 // * The standard constructor.
77 // * \param m The number of rows
78 // * \param n The number of columns
79 // * */
80 // Matrix(int m, int n) {};
81 
90  AT& operator()(int i, int j) {
91 #ifndef FMATVEC_NO_BOUNDS_CHECK
92  assert(i>=0);
93  assert(j>=0);
94  assert(i<rows());
95  assert(j<cols());
96 #endif
97 
98  return e(i,j);
99  }
100 
105  const AT& operator()(int i, int j) const {
106 #ifndef FMATVEC_NO_BOUNDS_CHECK
107  assert(i>=0);
108  assert(j>=0);
109  assert(i<rows());
110  assert(j<cols());
111 #endif
112 
113  return e(i,j);
114  }
115 
116  AT& e(int i, int j);
117 
118  const AT& e(int i, int j) const;
119 
124  int rows() const;
125 
130  int cols() const;
131 
136  operator std::vector<std::vector<AT> >() const;
137  };
138 
139 
140 
141  template <class Type, class Row, class Col, class AT> void Matrix<Type,Row,Col,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
142  for(int i=0; i<rows(); i++)
143  for(int j=0; j<cols(); j++)
144  e(i,j) = A.e(i,j);
145  }
146 
154  template <class Type, class Row, class Col, class AT> std::ostream& operator<<(std::ostream &os, const Matrix<Type,Row,Col,AT> &A) {
155  os << toString(static_cast<std::vector<std::vector<AT>>>(A));
156  return os;
157  }
158 
166  template <class Type, class Row, class Col, class AT> std::istream& operator>>(std::istream &is, Matrix<Type,Row,Col,AT> &A) {
167  std::ios_base::iostate oldEx=is.exceptions();
168  std::ios_base::fmtflags oldFlags=is.flags();
169  BOOST_SCOPE_EXIT_TPL(&is, oldEx, oldFlags) {
170  is.exceptions(oldEx);
171  is.flags(oldFlags);
172  } BOOST_SCOPE_EXIT_END
173  is.exceptions(std::ios::failbit | std::ios::badbit);
174  is.flags(std::ios_base::skipws);
175  char c;
176  AT e;
177  while((c=is.peek())==' ' || c=='\n') is.get();
178  if(c!='[') {
179  // its a scalar (written without the [ ] )
180  is>>e;
181  A = Matrix<Type,Row,Col,AT>(std::vector<std::vector<AT>>(1, std::vector<AT>(1, e)));
182  return is;
183  }
184  // its vector or matrix
185  is.get();
186  std::vector<std::vector<AT>> m(1);
187  int r=0;
188  while(true) {
189  is>>e;
190  m[r].push_back(e);
191  while((c=is.peek())==' ') is.get();
192  if (c==',' ) { is.get(); }
193  else if(c==';' || c=='\n') { is.get(); m.resize(++r + 1); }
194  else if(c==']' ) { is.get(); break; }
195  }
196  A = m;
197  return is;
198  }
199 
206  template <class Type, class Row, class Col, class AT> void dump(const char* str, const Matrix<Type,Row,Col,AT> &A) {
207  std::ofstream os(str);
208  for (int i=0; i < A.rows(); ++i) {
209  for (int j=0; j < A.cols(); ++j)
210  os << std::setw(14) << A.e(i,j);
211 
212  if (i != A.rows() - 1)
213  os << std::endl;
214  }
215  os.close();
216  }
217 
218  template <class Type, class Row, class Col, class AT>
219  Matrix<Type,Row,Col,AT>::operator std::vector<std::vector<AT> >() const {
220  std::vector<std::vector<AT> > ret(rows());
221  for(int r=0; r<rows(); r++) {
222  ret[r].resize(cols());
223  for(int c=0; c<cols(); c++)
224  ret[r][c]=e(r,c);
225  }
226  return ret;
227  }
228 
229  template <class Row, class AT> class SquareMatrix {
230  };
231 
232  template <class Row, class AT> class Vector {
233  };
234 
235  template <class Col, class AT> class RowVector {
236  };
237 }
238 
239 #endif
240 
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
Definition: matrix.h:43
void dump(const char *str, const Matrix< Type, Row, Col, AT > &A)
Matrix dump.
Definition: matrix.h:206
int rows() const
Number of rows.
int cols() const
Number of columns.
Definition: fmatvec.h:44
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:166
const AT & operator()(int i, int j) const
Element operator.
Definition: matrix.h:105
Definition: matrix.h:42
Definition: matrix.h:44

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home