fmatvec 0.0.0

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 using value_type = AT;
67 using shape_type = 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(),std::vector<AT>(cols()));
157 for(int r=0; r<rows(); r++) {
158 for(int c=0; c<cols(); c++)
159 ret[r][c]=e(r,c);
160 }
161 return ret;
162 }
163
164 template <class Row, class AT> class SquareMatrix {
165 };
166
167 template <class Row, class AT> class Vector {
168 };
169
170 template <class Col, class AT> class RowVector {
171 };
172 }
173
174 #endif
175
fmatvec::Matrix
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
fmatvec::Matrix::operator()
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
fmatvec::dump
void dump(const char *str, const Matrix< Type, Row, Col, AT > &A)
Matrix dump.
Definition: matrix.h:142
fmatvec::SquareMatrix
Definition: fmatvec.h:45
fmatvec::Matrix::cols
int cols() const
Number of columns.
fmatvec::Matrix::operator()
const AT & operator()(int i, int j) const
Element operator.
Definition: matrix.h:97
fmatvec
Namespace fmatvec.
Definition: _memory.cc:28
fmatvec::Matrix::rows
int rows() const
Number of rows.