fmatvec  0.0.0
diagonal_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 diagonal_matrix_h
23#define diagonal_matrix_h
24
25#include "matrix.h"
26#include "types.h"
27#include "_memory.h"
28
29namespace fmatvec {
30
39 template <class AT> class Matrix<Diagonal,Ref,Ref,AT> {
40
41 protected:
42
44
45 Memory<AT> memory;
46 AT *ele;
47 int n{0};
48
50
51 const AT* elePtr(int i) const {
52 return ele+i;
53 }
54
55 AT* elePtr(int i) {
56 return ele+i;
57 }
58
60
61 public:
62 static constexpr bool isVector {false};
63
64 using value_type = AT;
65 using shape_type = Diagonal;
66
71 explicit Matrix() : memory(), ele(0) { }
72
73 // template<class Ini=All<AT>>
74 // Matrix(int n_, Ini ini=All<AT>()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini); }
75 // template<class Ini=All<AT>>
76 // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini); }
77
78 explicit Matrix(int n_, Noinit) : memory(n_), ele((AT*)memory.get()), n(n_) { }
79 explicit Matrix(int n_, Init ini=INIT, const AT &a=AT()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(a); }
80 explicit Matrix(int n_, Eye ini, const AT &a=1) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini,a); }
81 explicit Matrix(int m_, int n_, Noinit) : memory(n_), ele((AT*)memory.get()), n(n_) { }
82
88 Matrix(const Matrix<Diagonal,Ref,Ref,AT> &A) : memory(A.n), ele((AT*)memory.get()), n(A.n) {
89 copy(A);
90 }
91
97 template<class Type, class Row, class Col>
98 Matrix(const Matrix<Type,Row,Col,AT> &A) : memory(A.rows()), ele((AT*)memory.get()), n(A.rows()) {
99 FMATVEC_ASSERT(A.rows() == A.cols(), AT);
100 copy(A);
101 }
102
105 ~Matrix() = default;
106
107 Matrix<Diagonal,Ref,Ref,AT>& resize(int n_, Noinit) {
108 n = n_;
109 memory.resize(n);
110 ele = (AT*)memory.get();
111 return *this;
112 }
113
114 Matrix<Diagonal,Ref,Ref,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) { resize(n,Noinit()).init(a); }
115 Matrix<Diagonal,Ref,Ref,AT>& resize(int n, Eye ini, const AT &a=1) { resize(n,Noinit()).init(ini,a); }
116
124 FMATVEC_ASSERT(n == A.n, AT);
125 return copy(A);
126 }
127
134 template<class Type, class Row, class Col>
136 FMATVEC_ASSERT(n == A.rows(), AT);
137 FMATVEC_ASSERT(n == A.cols(), AT);
138 return copy(A);
139 }
140
148 n=A.n;
149 memory = A.memory;
150 ele = A.ele;
151 return *this;
152 }
153
160 template<class Type, class Row, class Col>
161 inline Matrix<Diagonal,Ref,Ref,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
162 if(n!=A.n) resize(A.rows(),NONINIT);
163 return copy(A);
164 }
165
175 const AT& operator()(int i, int j) const {
176
177 FMATVEC_ASSERT(i>=0, AT);
178 FMATVEC_ASSERT(j>=0, AT);
179 FMATVEC_ASSERT(i<n, AT);
180 FMATVEC_ASSERT(j<n, AT);
181
182 return e(i,j);
183 }
184
189 const AT& operator()(int i) const {
190
191 FMATVEC_ASSERT(i>=0, AT);
192 FMATVEC_ASSERT(i<n, AT);
193
194 return e(i);
195 }
196
205 AT& operator()(int i) {
206
207 FMATVEC_ASSERT(i>=0, AT);
208 FMATVEC_ASSERT(i<n, AT);
209
210 return e(i);
211 }
212
213 const AT& e(int i, int j) const {
214 static AT zero=0;
215 return i==j ? ele[i] : zero;
216 }
217
218 const AT& e(int i) const {
219 return ele[i];
220 }
221
222 AT& e(int i) {
223 return ele[i];
224 }
225
230 const AT* operator()() const {
231 return ele;
232 }
233
240 return ele;
241 }
242
247 int rows() const {return n;}
248
253 int cols() const {return n;}
254
259 int size() const {return n;}
260
268 CBLAS_ORDER blasOrder() const {
269 return CblasColMajor;
270 }
271
279 Matrix<Diagonal,Ref,Ref,AT>& init(const AT &val=AT());
280 inline Matrix<Diagonal,Ref,Ref,AT>& init(Init, const AT &a=AT()) { return init(a); }
281 inline Matrix<Diagonal,Ref,Ref,AT>& init(Eye, const AT &a=1) { return init(a); }
282 inline Matrix<Diagonal,Ref,Ref,AT>& init(Noinit, const AT &a=AT()) { return *this; }
283
288 explicit operator std::vector<std::vector<AT>>() const;
289
295 explicit Matrix(const std::vector<std::vector<AT>> &m);
296
297// /*! \brief Cast to AT.
298// *
299// * \return The AT representation of the matrix
300// * */
301// explicit operator AT() const {
302// FMATVEC_ASSERT(n==1, AT);
303// return ele[0];
304// }
305//
306// /*! \brief AT Constructor.
307// * Constructs and initializes a matrix with a AT object.
308// * \param x The AT the matrix will be initialized with.
309// * */
310// explicit Matrix(const AT &x) : memory(1), ele((AT*)memory.get()), n(1) { ele[0] = x; }
311 };
312
313 template <class AT>
314 Matrix<Diagonal,Ref,Ref,AT>::operator std::vector<std::vector<AT>>() const {
315 std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols(),AT()));
316 for(int r=0; r<rows(); r++) {
317 ret[r][r]=e(r);
318 }
319 return ret;
320 }
321
322 template <class AT>
323 Matrix<Diagonal,Ref,Ref,AT>::Matrix(const std::vector<std::vector<AT>> &m) : memory(m.size()), ele((AT*)memory.get()), n(static_cast<int>(m.size())) {
324 for(size_t r=0; r<m.size(); ++r) {
325 if(n!=static_cast<int>(m[r].size()))
326 throw std::runtime_error("The rows of the input have different lenght.");
327 for(size_t c=0; c<m[r].size(); ++c) {
328 if(r==c)
329 e(r)=m[r][r];
330 if(r!=c && (fabs(m[r][c])>1e-13 || fabs(m[r][c])>1e-13))
331 throw std::runtime_error("The input is not diagonal.");
332 }
333 }
334 }
335
336 template <class AT>
338 for(int i=0; i<rows(); i++)
339 e(i) = val;
340 return *this;
341 }
342
343 template <class AT>
345 for(int i=0; i<n; i++)
346 e(i) = A.e(i);
347 return *this;
348 }
349
350}
351
352#endif
Shape class for diagonal matrices.
Definition: types.h:148
Definition: types.h:93
This is a matrix class for diagonal matrices.
Definition: diagonal_matrix.h:39
int rows() const
Number of rows.
Definition: diagonal_matrix.h:247
AT & operator()(int i)
Element operator.
Definition: diagonal_matrix.h:205
const AT * operator()() const
Pointer operator.
Definition: diagonal_matrix.h:230
Matrix< Diagonal, Ref, Ref, AT > & operator&=(Matrix< Diagonal, Ref, Ref, AT > &A)
Reference operator.
Definition: diagonal_matrix.h:147
AT * operator()()
Pointer operator.
Definition: diagonal_matrix.h:239
int cols() const
Number of columns.
Definition: diagonal_matrix.h:253
int size() const
Number of rows and columns.
Definition: diagonal_matrix.h:259
Matrix(const Matrix< Diagonal, Ref, Ref, AT > &A)
Copy Constructor.
Definition: diagonal_matrix.h:88
CBLAS_ORDER blasOrder() const
Storage convention.
Definition: diagonal_matrix.h:268
Matrix< Diagonal, Ref, Ref, AT > & operator=(const Matrix< Diagonal, Ref, Ref, AT > &A)
Assignment operator.
Definition: diagonal_matrix.h:123
const AT & operator()(int i) const
Element operator.
Definition: diagonal_matrix.h:189
Matrix< Diagonal, Ref, Ref, AT > & init(const AT &val=AT())
Initialization.
Definition: diagonal_matrix.h:337
Matrix< Diagonal, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: diagonal_matrix.h:135
Matrix()
Standard constructor.
Definition: diagonal_matrix.h:71
const AT & operator()(int i, int j) const
Element operator.
Definition: diagonal_matrix.h:175
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: diagonal_matrix.h:98
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:52
int rows() const
Number of rows.
int cols() const
Number of columns.
Definition: types.h:92
Definition: types.h:102
Namespace fmatvec.
Definition: _memory.cc:28