fmatvec  2.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  typedef AT value_type;
66 // /*! \brief Standard constructor
67 // *
68 // * The standard constructor.
69 // * \param m The number of rows
70 // * \param n The number of columns
71 // * */
72 // Matrix(int m, int n) {}
73 
82  AT& operator()(int i, int j) {
83  FMATVEC_ASSERT(i>=0, AT);
84  FMATVEC_ASSERT(j>=0, AT);
85  FMATVEC_ASSERT(i<rows(), AT);
86  FMATVEC_ASSERT(j<cols(), AT);
87 
88  return e(i,j);
89  }
90 
95  const AT& operator()(int i, int j) const {
96  FMATVEC_ASSERT(i>=0, AT);
97  FMATVEC_ASSERT(j>=0, AT);
98  FMATVEC_ASSERT(i<rows(), AT);
99  FMATVEC_ASSERT(j<cols(), AT);
100 
101  return e(i,j);
102  }
103 
104  AT& e(int i, int j);
105 
106  const AT& e(int i, int j) const;
107 
112  int rows() const;
113 
118  int cols() const;
119 
124  operator std::vector<std::vector<AT>>() const;
125  };
126 
127  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) {
128  for(int i=0; i<rows(); i++)
129  for(int j=0; j<cols(); j++)
130  e(i,j) = A.e(i,j);
131  return *this;
132  }
133 
140  template <class Type, class Row, class Col, class AT> void dump(const char* str, const Matrix<Type,Row,Col,AT> &A) {
141  std::ofstream os(str);
142  for (int i=0; i < A.rows(); ++i) {
143  for (int j=0; j < A.cols(); ++j)
144  os << std::setw(14) << A.e(i,j);
145 
146  if (i != A.rows() - 1)
147  os << std::endl;
148  }
149  os.close();
150  }
151 
152  template <class Type, class Row, class Col, class AT>
153  Matrix<Type,Row,Col,AT>::operator std::vector<std::vector<AT>>() const {
154  std::vector<std::vector<AT>> ret(rows());
155  for(int r=0; r<rows(); r++) {
156  ret[r].resize(cols());
157  for(int c=0; c<cols(); c++)
158  ret[r][c]=e(r,c);
159  }
160  return ret;
161  }
162 
163  template <class Row, class AT> class SquareMatrix {
164  };
165 
166  template <class Row, class AT> class Vector {
167  };
168 
169  template <class Col, class AT> class RowVector {
170  };
171 }
172 
173 #endif
174 
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
void dump(const char *str, const Matrix< Type, Row, Col, AT > &A)
Matrix dump.
Definition: matrix.h:140
Definition: fmatvec.h:44
int cols() const
Number of columns.
const AT & operator()(int i, int j) const
Element operator.
Definition: matrix.h:95
Namespace fmatvec.
Definition: _memory.cc:28
int rows() const
Number of rows.