fmatvec  0.0.0
Fast Matrix/Vector Library
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 "types.h"
35 
40 namespace fmatvec {
41 
44  //enum Initialization{INIT,NONINIT,EYE};
45 
52  template <class Type, class Row, class Col, class AT> class Matrix {
53 
54  protected:
55 
57 
58  //AT* ele;
59 
60  inline Matrix<Type,Row,Col,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
61 
63 
64  public:
65  static constexpr bool isVector {false};
66  typedef AT value_type;
67  typedef Type shape_type;
68 // /*! \brief Standard constructor
69 // *
70 // * The standard constructor.
71 // * \param m The number of rows
72 // * \param n The number of columns
73 // * */
74 // Matrix(int m, int n) {}
75 
84  AT& operator()(int i, int j) {
85  FMATVEC_ASSERT(i>=0, AT);
86  FMATVEC_ASSERT(j>=0, AT);
87  FMATVEC_ASSERT(i<rows(), AT);
88  FMATVEC_ASSERT(j<cols(), AT);
89 
90  return e(i,j);
91  }
92 
97  const AT& operator()(int i, int j) const {
98  FMATVEC_ASSERT(i>=0, AT);
99  FMATVEC_ASSERT(j>=0, AT);
100  FMATVEC_ASSERT(i<rows(), AT);
101  FMATVEC_ASSERT(j<cols(), AT);
102 
103  return e(i,j);
104  }
105 
106  AT& e(int i, int j);
107 
108  const AT& e(int i, int j) const;
109 
114  int rows() const;
115 
120  int cols() const;
121 
126  operator std::vector<std::vector<AT>>() const;
127  };
128 
129  template <class Type, class Row, class Col, class AT> inline Matrix<Type,Row,Col,AT>& Matrix<Type,Row,Col,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
130  for(int i=0; i<rows(); i++)
131  for(int j=0; j<cols(); j++)
132  e(i,j) = A.e(i,j);
133  return *this;
134  }
135 
142  template <class Type, class Row, class Col, class AT> void dump(const char* str, const Matrix<Type,Row,Col,AT> &A) {
143  std::ofstream os(str);
144  for (int i=0; i < A.rows(); ++i) {
145  for (int j=0; j < A.cols(); ++j)
146  os << std::setw(14) << A.e(i,j);
147 
148  if (i != A.rows() - 1)
149  os << std::endl;
150  }
151  os.close();
152  }
153 
154  template <class Type, class Row, class Col, class AT>
155  Matrix<Type,Row,Col,AT>::operator std::vector<std::vector<AT>>() const {
156  std::vector<std::vector<AT>> ret(rows());
157  for(int r=0; r<rows(); r++) {
158  ret[r].resize(cols());
159  for(int c=0; c<cols(); c++)
160  ret[r][c]=e(r,c);
161  }
162  return ret;
163  }
164 
165  template <class Row, class AT> class SquareMatrix {
166  };
167 
168  template <class Row, class AT> class Vector {
169  };
170 
171  template <class Col, class AT> class RowVector {
172  };
173 }
174 
175 #endif
176 
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
void dump(const char *str, const Matrix< Type, Row, Col, AT > &A)
Matrix dump.
Definition: matrix.h:142
Definition: fmatvec.h:44
int cols() const
Number of columns.
const AT & operator()(int i, int j) const
Element operator.
Definition: matrix.h:97
Namespace fmatvec.
Definition: _memory.cc:28
int rows() const
Number of rows.