fmatvec  2.0.0
Fast Matrix/Vector Library
band_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 general_band_matrix_h
23 #define general_band_matrix_h
24 
25 #include "vector.h"
26 #include "types.h"
27 #include "_memory.h"
28 #include <cstdlib>
29 
30 namespace fmatvec {
31 
40  template <class AT> class Matrix<GeneralBand,Ref,Ref,AT> {
41 
43 
44  protected:
45 
46  Memory<AT> memory;
47  AT *ele;
48  int n{0};
49  int kl{0};
50  int ku{0};
51 
53 
55 
56  public:
57 
58  typedef AT value_type;
59 
64  explicit Matrix() : memory(), ele(0) {
65  }
66 
67 // template<class Ini=All<AT>>
68 // Matrix(int n_, int kl_, int ku_, Ini ini=All<AT>()) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) {
69 // init(ini);
70 // }
71 // template<class Ini=All<AT>>
72 // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_*(n_+n_+1)), ele((AT*)memory.get()), n(n_), kl(n_), ku(n_) {
73 // init(ini);
74 // }
75 
76  explicit Matrix(int n_, int kl_, int ku_, Noinit) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { }
77  explicit Matrix(int n_, int kl_, int ku_, Init ini=INIT, const AT &a=AT()) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(a); }
78  explicit Matrix(int n_, int kl_, int ku_, Eye ini, const AT &a=1) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(ini,a); }
79  explicit Matrix(int m_, int n_, int kl_, int ku_, Noinit) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { }
80  explicit Matrix(int m_, int n_, int kl_, int ku_, Init ini=INIT, const AT &a=AT()) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(a); }
81  explicit Matrix(int m_, int n_, int kl_, int ku_, Eye ini, const AT &a=1) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(ini,a); }
82 
88  Matrix(const Matrix<GeneralBand,Ref,Ref,AT> &A) : memory(A.n*(A.kl+A.ku+1)), ele((AT*)memory.get()), n(A.n), kl(A.kl), ku(A.ku) {
89  copy(A);
90  }
91 
97  template<class Type, class Row, class Col>
98  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : memory(A.rows()*(2*A.rows()-1)), ele((AT*)memory.get()), n(A.rows()), kl(n-1), ku(n-1) {
99  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
100  copy(A);
101  }
102 
112  explicit Matrix(int n_, int kl_, int ku_, AT* ele_) : memory(), ele(ele_), n(n_), kl(kl_), ku(ku_) {
113  }
114 
117  ~Matrix() = default;
118 
119  Matrix<GeneralBand,Ref,Ref,AT>& resize(int n_, int kl_, int ku_, Noinit) {
120  n = n_; kl = kl_; ku = ku_;
121  memory.resize(n*(kl+ku+1));
122  ele = (AT*)memory.get();
123  return *this;
124  }
125 
126  Matrix<GeneralBand,Ref,Ref,AT>& resize(int n, int kl, int ku=0, Init ini=INIT, const AT &a=AT()) { return resize(n,kl,ku,Noinit()).init(a); }
127 
128  Matrix<GeneralBand,Ref,Ref,AT>& resize(int n, int kl, int ku, Eye ini, const AT &a=1) { return resize(n,kl,ku,Noinit()).init(a); }
129 
137  FMATVEC_ASSERT(n == A.n, AT);
138  return copy(A);
139  }
140 
147  template<class Type, class Row, class Col>
149  FMATVEC_ASSERT(n == A.rows(), AT);
150  FMATVEC_ASSERT(n == A.cols(), AT);
151  return copy(A);
152  }
153 
161  n = A.n;
162  memory = A.memory;
163  ele = A.ele;
164  return *this;
165  }
166 
173  template<class Type, class Row, class Col>
175  if(n!=A.n) resize(A.rows(),NONINIT);
176  return copy(A);
177  }
178 
188  const AT& operator()(int i, int j) const {
189  FMATVEC_ASSERT(i>=0, AT);
190  FMATVEC_ASSERT(j>=0, AT);
191  FMATVEC_ASSERT(i<n, AT);
192  FMATVEC_ASSERT(j<n, AT);
193  // FMATVEC_ASSERT(i-j<=kl, AT);
194  // FMATVEC_ASSERT(i-j>=-ku, AT);
195  static AT zero=0;
196 
197  return ((i-j>kl) || (i-j<-ku)) ? zero : ele[ku+i+j*(kl+ku)];
198  }
199 
205  AT* operator()() {return ele;}
206 
211  const AT* operator()() const {return ele;}
212 
217  int size() const {return n;}
218 
223  int rows() const {return n;}
224 
229  int cols() const {return n;}
230 
238  const CBLAS_ORDER blasOrder() const {
239  return CblasColMajor;
240  }
241 
242 // /*! \brief Diagonal operator
243 // *
244 // * See operator()(int)
245 // * */
246 // inline const Vector<Ref,AT> operator()(int i) const;
247 //
248 // /*! \brief Diagonal operator
249 // *
250 // * If i<0, the i-th subdiagonal is returned. Otherwise the i-th
251 // * superdiagonal is returned.
252 // * \param i The i-th super- and subdiagonal,
253 // * respectively.
254 // * */
255 // inline Vector<Ref,AT> operator()(int i);
256 
264  inline Matrix<GeneralBand,Ref,Ref,AT>& init(const AT &val=AT());
265  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Init, const AT &a=AT()) { return init(a); }
266  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Eye, const AT &val=1);
267  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Noinit, const AT &a=AT()) { return *this; }
268 
273  explicit inline operator std::vector<std::vector<AT>>() const;
274  };
275 
276  template <class AT>
278  for(int i=0; i<kl+ku+1; i++)
279  for(int j=0; j<n; j++)
280  ele[i+j*(kl+ku+1)] = val;
281  return *this;
282  }
283 
284  template <class AT>
286  for(int i=0; i<n; i++)
287  ele[i] = val;
288  return *this;
289  }
290 
291 // template <class AT>
292 // inline Vector<Ref,AT> Matrix<GeneralBand,Ref,Ref,AT>::operator()(int i) {
293 // FMATVEC_ASSERT(i<=ku, AT);
294 // FMATVEC_ASSERT(i>=-kl, AT);
295 //
296 // //return Vector<Ref,AT>(n-i,ku+kl+1,memory,ele[ku-i + (i>0?i:0)*(kl+ku+1)]);
297 // return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
298 // //(ku-i,i>0?i:0));
299 // }
300 //
301 // template <class AT>
302 // inline const Vector<Ref,AT> Matrix<GeneralBand,Ref,Ref,AT>::operator()(int i) const {
303 // FMATVEC_ASSERT(i<=ku, AT);
304 // FMATVEC_ASSERT(i>=-kl, AT);
305 //
306 // return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
307 // //(ku-i,i>0?i:0));
308 // }
309 
310  template <class AT>
311  inline Matrix<GeneralBand,Ref,Ref,AT>::operator std::vector<std::vector<AT>>() const {
312  std::vector<std::vector<AT>> ret(rows());
313  for(int r=0; r<rows(); r++) {
314  ret[r].resize(cols());
315  for(int c=0; c<cols(); c++)
316  ret[r][c]=operator()(r,c);
317  }
318  return ret;
319  }
320 
322 
323  template <class AT>
325  for(int i=0; i<kl+ku+1; i++)
326  for(int j=0; j<n; j++)
327  ele[i+j*(kl+ku+1)] = A.ele[i+j*(kl+ku+1)];
328  return *this;
329  }
330 
332 
333 }
334 
335 #endif
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Definition: types.h:85
Matrix(const Matrix< GeneralBand, Ref, Ref, AT > &A)
Copy Constructor.
Definition: band_matrix.h:88
This is a matrix class for general band matrices.
Definition: band_matrix.h:40
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: band_matrix.h:238
Shape class for general band matrices.
Definition: types.h:140
const AT * operator()() const
Pointer operator.
Definition: band_matrix.h:211
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: band_matrix.h:98
int size() const
Size.
Definition: band_matrix.h:217
Matrix< GeneralBand, Ref, Ref, AT > & operator=(const Matrix< GeneralBand, Ref, Ref, AT > &A)
Assignment operator.
Definition: band_matrix.h:136
Definition: types.h:94
int rows() const
Number of rows.
Definition: band_matrix.h:223
AT * operator()()
Pointer operator.
Definition: band_matrix.h:205
Matrix(int n_, int kl_, int ku_, AT *ele_)
Regular Constructor.
Definition: band_matrix.h:112
int cols() const
Number of columns.
Matrix< GeneralBand, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: band_matrix.h:148
Matrix()
Standard constructor.
Definition: band_matrix.h:64
Namespace fmatvec.
Definition: _memory.cc:28
Matrix< GeneralBand, Ref, Ref, AT > & operator&=(Matrix< GeneralBand, Ref, Ref, AT > &A)
Reference operator.
Definition: band_matrix.h:160
int cols() const
Number of columns.
Definition: band_matrix.h:229
Definition: types.h:84
const AT & operator()(int i, int j) const
Element operator.
Definition: band_matrix.h:188
Matrix< GeneralBand, Ref, Ref, AT > & init(const AT &val=AT())
Diagonal operator.
Definition: band_matrix.h:277
int rows() const
Number of rows.
Definition: types.h:86