fmatvec  0.0.0
Fast Matrix/Vector Library
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 
29 namespace 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  typedef AT value_type;
65  typedef Diagonal shape_type;
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 
239  AT* operator()() {
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  const 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 
289  explicit Matrix<Diagonal,Ref,Ref,AT>(const std::vector<std::vector<AT>> &m);
290 
295  explicit operator std::vector<std::vector<AT>>() const;
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>::Matrix(const std::vector<std::vector<AT>> &m) : Matrix<Diagonal,Ref,Ref,AT>(static_cast<int>(m.size())) {
315  for(size_t r=0; r<m.size(); ++r) {
316  if(n!=static_cast<int>(m[r].size()))
317  throw std::runtime_error("The rows of the input have different lenght.");
318  for(size_t c=0; c<m[r].size(); ++c) {
319  if(r==c)
320  e(r)=m[r][r];
321  if(r!=c && (fabs(m[r][c])>1e-13 || fabs(m[r][c])>1e-13))
322  throw std::runtime_error("The input is not diagonal.");
323  }
324  }
325  }
326 
327 
328  template <class AT>
330  for(int i=0; i<rows(); i++)
331  e(i) = val;
332  return *this;
333  }
334 
335  template <class AT>
337  for(int i=0; i<n; i++)
338  e(i) = A.e(i);
339  return *this;
340  }
341 
342  template <class AT>
343  Matrix<Diagonal,Ref,Ref,AT>::operator std::vector<std::vector<AT>>() const {
344  std::vector<std::vector<AT>> ret(rows());
345  for(int r=0; r<rows(); r++) {
346  ret[r].resize(cols());
347  for(int c=0; c<cols(); c++)
348  ret[r][c]=e(r,c);
349  }
350  return ret;
351  }
352 
353 }
354 
355 #endif
AT & operator()(int i)
Element operator.
Definition: diagonal_matrix.h:205
Matrix(const Matrix< Diagonal, Ref, Ref, AT > &A)
Copy Constructor.
Definition: diagonal_matrix.h:88
Matrix< Diagonal, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: diagonal_matrix.h:135
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: fmatvec.h:41
Matrix()
Standard constructor.
Definition: diagonal_matrix.h:71
AT * operator()()
Pointer operator.
Definition: diagonal_matrix.h:239
Definition: types.h:85
const AT & operator()(int i, int j) const
Element operator.
Definition: diagonal_matrix.h:175
int cols() const
Number of columns.
Definition: diagonal_matrix.h:253
const AT * operator()() const
Pointer operator.
Definition: diagonal_matrix.h:230
int size() const
Number of rows and columns.
Definition: diagonal_matrix.h:259
Definition: types.h:94
Matrix< Diagonal, Ref, Ref, AT > & operator=(const Matrix< Diagonal, Ref, Ref, AT > &A)
Assignment operator.
Definition: diagonal_matrix.h:123
Matrix< Diagonal, Ref, Ref, AT > & operator&=(Matrix< Diagonal, Ref, Ref, AT > &A)
Reference operator.
Definition: diagonal_matrix.h:147
int cols() const
Number of columns.
const AT & operator()(int i) const
Element operator.
Definition: diagonal_matrix.h:189
Namespace fmatvec.
Definition: _memory.cc:28
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: diagonal_matrix.h:268
Shape class for diagonal matrices.
Definition: types.h:164
Matrix< Diagonal, Ref, Ref, AT > & init(const AT &val=AT())
Initialization.
Definition: diagonal_matrix.h:329
Definition: types.h:84
int rows() const
Number of rows.
Definition: diagonal_matrix.h:247
int rows() const
Number of rows.
This is a matrix class for diagonal matrices.
Definition: diagonal_matrix.h:39