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, AT> {
40 
41  protected:
42 
44 
45  Memory<AT> memory;
46  AT *ele;
47  int n;
48 
49  void deepCopy(const Matrix<Diagonal, AT> &x);
50 
51  Matrix(int n_, Memory<AT> memory_, const AT* ele_) : memory(memory_), ele((AT*)ele_), n(n_) {
52  }
53 
54  const AT* elePtr(int i) const {
55  return ele+i;
56  }
57 
58  AT* elePtr(int i) {
59  return ele+i;
60  }
61 
63 
64  public:
65 
70  Matrix() : memory(), ele(0), n(0) {
71  }
72 
81  Matrix(int n_) : memory(n_), ele((AT*)memory.get()), n(n_) {
82 
83 #ifndef FMATVEC_NO_INITIALIZATION
84  init(0);
85 #endif
86  }
87 
98  n = n_;
99 
100  memory.resize(n);
101  ele = (AT*)memory.get();
102 
103 #ifndef FMATVEC_NO_INITIALIZATION
104  init(0);
105 #endif
106  return *this;
107  }
108 
120  Matrix<Diagonal, AT>& resize(int n_, Initialization ini, const AT &a=0) {
121  n = n_;
122 
123  memory.resize(n);
124  ele = (AT*)memory.get();
125 
126  if(ini == INIT)
127  init(a);
128  else if(ini == EYE )
129  init(1);
130 
131  return *this;
132  }
133 
144  Matrix(int n_, Initialization ini, const AT& a=0) : memory(n_), ele((AT*)memory.get()), n(n_) {
145 
146  if(ini == INIT) {
147  AT *el=ele;
148  for(int i=0; i<n; i++)
149  *el++=a;
150  } else if(ini == EYE ) {
151  AT *el=ele;
152  for(int i=0; i<n; i++)
153  *el++= 1;
154  }
155  }
156 
164  Matrix(const Matrix<Diagonal, AT> &A) : memory(A.memory), ele(A.ele) ,n(A.n) {
165  }
166 
170  }
171 
178  Matrix<Diagonal, AT>& operator<<(const Matrix<Diagonal, AT> &A);
179 
187 
197 #ifndef FMATVEC_NO_DEEP_ASSIGNMENT
198  return operator<<(A);
199 #else
200  return operator>>(A);
201 #endif
202  }
203 
215  const AT& operator()(int i, int j) const {
216 
217  static AT zero=0;
218 
219 #ifndef FMATVEC_NO_BOUNDS_CHECK
220  assert(i>=0);
221  assert(j>=0);
222  assert(i<n);
223  assert(j<n);
224 #endif
225 
226  return i==j ? ele[i] : zero;
227  };
228 
233  const AT& operator()(int i) const {
234 
235 #ifndef FMATVEC_NO_BOUNDS_CHECK
236  assert(i>=0);
237  assert(i<n);
238 #endif
239 
240  return ele[i];
241  };
242 
253  AT& operator()(int i) {
254 
255 #ifndef FMATVEC_NO_BOUNDS_CHECK
256  assert(i>=0);
257  assert(i<n);
258 #endif
259 
260  return ele[i];
261  };
262 
267  const AT* operator()() const {
268  return ele;
269  };
270 
276  AT* operator()() {
277  return ele;
278  };
279 
284  int rows() const {return n;};
285 
290  int cols() const {return n;};
291 
296  int size() const {return n;};
297 
305  const CBLAS_ORDER blasOrder() const {
306  return CblasColMajor;
307  };
308 
314  Matrix<Diagonal, AT> copy() const;
315 
323  Matrix<Diagonal, AT>& init(const AT &a);
324 
329  operator std::vector<std::vector<AT> >();
330  };
331 
332  template <class AT>
334  if(n==0) {
335  n=A.n;
336  } else
337  assert(n == A.n);
338 
339  memory = A.memory;
340  ele = A.ele;
341 
342  return *this;
343  }
344 
345  template <class AT>
347 
348  if(A.size() == 0)
349  return *this;
350 
351  if(n==0) {
352  n = A.n;
353  memory.resize(n);
354  ele = (AT*)memory.get();
355  } else
356  assert(n == A.n);
357 
358  deepCopy(A);
359 
360  return *this;
361  }
362 
363  template <class AT>
365 
366  for(int i=0; i<rows(); i++)
367  operator()(i) = val;
368 
369  return *this;
370  }
371 
372 
373  template <class AT>
375 
377 
378  A.deepCopy(*this);
379 
380  return A;
381  }
382 
383  template <class AT>
385  for(int i=0; i<n; i++)
386  operator()(i) = A(i);
387  }
388 
389  template <> void Matrix<Diagonal, double>::deepCopy(const Matrix<Diagonal, double> &A);
390 
391  template <class AT>
392  Matrix<Diagonal, AT>::operator std::vector<std::vector<AT> >() {
393  std::vector<std::vector<AT> > ret(rows());
394  for(int r=0; r<rows(); r++) {
395  ret[r].resize(cols());
396  for(int c=0; c<cols(); c++)
397  ret[r][c]=operator()(r,c);
398  }
399  return ret;
400  }
401 
402 }
403 
404 #endif