fmatvec 0.0.0

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
52 inline Matrix<GeneralBand,Ref,Ref,AT>& copy(const Matrix<GeneralBand,Ref,Ref,AT> &A);
53
55
56 public:
57 static constexpr bool isVector {false};
58
59 using value_type = AT;
60 using shape_type = GeneralBand;
61
66 explicit Matrix() : memory(), ele(0) {
67 }
68
69 // template<class Ini=All<AT>>
70 // 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_) {
71 // init(ini);
72 // }
73 // template<class Ini=All<AT>>
74 // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_*(n_+n_+1)), ele((AT*)memory.get()), n(n_), kl(n_), ku(n_) {
75 // init(ini);
76 // }
77
78 explicit Matrix(int n_, int kl_, int ku_, Noinit) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { }
79 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); }
80 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); }
81 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_) { }
82 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); }
83 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); }
84
90 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) {
91 copy(A);
92 }
93
99 template<class Type, class Row, class Col>
100 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) {
101 FMATVEC_ASSERT(A.rows() == A.cols(), AT);
102 copy(A);
103 }
104
114 explicit Matrix(int n_, int kl_, int ku_, AT* ele_) : memory(), ele(ele_), n(n_), kl(kl_), ku(ku_) {
115 }
116
119 ~Matrix() = default;
120
121 Matrix<GeneralBand,Ref,Ref,AT>& resize(int n_, int kl_, int ku_, Noinit) {
122 n = n_; kl = kl_; ku = ku_;
123 memory.resize(n*(kl+ku+1));
124 ele = (AT*)memory.get();
125 return *this;
126 }
127
128 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); }
129
130 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); }
131
138 inline Matrix<GeneralBand,Ref,Ref,AT>& operator=(const Matrix<GeneralBand,Ref,Ref,AT> &A) {
139 FMATVEC_ASSERT(n == A.n, AT);
140 return copy(A);
141 }
142
149 template<class Type, class Row, class Col>
150 inline Matrix<GeneralBand,Ref,Ref,AT>& operator=(const Matrix<Type,Row,Col,AT> &A) {
151 FMATVEC_ASSERT(n == A.rows(), AT);
152 FMATVEC_ASSERT(n == A.cols(), AT);
153 return copy(A);
154 }
155
162 inline Matrix<GeneralBand,Ref,Ref,AT>& operator&=(Matrix<GeneralBand,Ref,Ref,AT> &A) {
163 n = A.n;
164 memory = A.memory;
165 ele = A.ele;
166 return *this;
167 }
168
175 template<class Type, class Row, class Col>
176 inline Matrix<GeneralBand,Ref,Ref,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
177 if(n!=A.n) resize(A.rows(),NONINIT);
178 return copy(A);
179 }
180
190 const AT& operator()(int i, int j) const {
191 FMATVEC_ASSERT(i>=0, AT);
192 FMATVEC_ASSERT(j>=0, AT);
193 FMATVEC_ASSERT(i<n, AT);
194 FMATVEC_ASSERT(j<n, AT);
195 // FMATVEC_ASSERT(i-j<=kl, AT);
196 // FMATVEC_ASSERT(i-j>=-ku, AT);
197 static AT zero=0;
198
199 return ((i-j>kl) || (i-j<-ku)) ? zero : ele[ku+i+j*(kl+ku)];
200 }
201
207 AT* operator()() {return ele;}
208
213 const AT* operator()() const {return ele;}
214
219 int size() const {return n;}
220
225 int rows() const {return n;}
226
231 int cols() const {return n;}
232
240 const CBLAS_ORDER blasOrder() const {
241 return CblasColMajor;
242 }
243
244 // /*! \brief Diagonal operator
245 // *
246 // * See operator()(int)
247 // * */
248 // inline const Vector<Ref,AT> operator()(int i) const;
249 //
250 // /*! \brief Diagonal operator
251 // *
252 // * If i<0, the i-th subdiagonal is returned. Otherwise the i-th
253 // * superdiagonal is returned.
254 // * \param i The i-th super- and subdiagonal,
255 // * respectively.
256 // * */
257 // inline Vector<Ref,AT> operator()(int i);
258
266 inline Matrix<GeneralBand,Ref,Ref,AT>& init(const AT &val=AT());
267 inline Matrix<GeneralBand,Ref,Ref,AT>& init(Init, const AT &a=AT()) { return init(a); }
268 inline Matrix<GeneralBand,Ref,Ref,AT>& init(Eye, const AT &val=1);
269 inline Matrix<GeneralBand,Ref,Ref,AT>& init(Noinit, const AT &a=AT()) { return *this; }
270
275 explicit inline operator std::vector<std::vector<AT>>() const;
276 };
277
278 template <class AT>
279 inline Matrix<GeneralBand,Ref,Ref,AT>& Matrix<GeneralBand,Ref,Ref,AT>::init(const AT& val) {
280 for(int i=0; i<kl+ku+1; i++)
281 for(int j=0; j<n; j++)
282 ele[i+j*(kl+ku+1)] = val;
283 return *this;
284 }
285
286 template <class AT>
287 inline Matrix<GeneralBand,Ref,Ref,AT>& Matrix<GeneralBand,Ref,Ref,AT>::init(Eye, const AT &val) {
288 for(int i=0; i<n; i++)
289 ele[i] = val;
290 return *this;
291 }
292
293 // template <class AT>
294 // inline Vector<Ref,AT> Matrix<GeneralBand,Ref,Ref,AT>::operator()(int i) {
295 // FMATVEC_ASSERT(i<=ku, AT);
296 // FMATVEC_ASSERT(i>=-kl, AT);
297 //
298 // //return Vector<Ref,AT>(n-i,ku+kl+1,memory,ele[ku-i + (i>0?i:0)*(kl+ku+1)]);
299 // return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
300 // //(ku-i,i>0?i:0));
301 // }
302 //
303 // template <class AT>
304 // inline const Vector<Ref,AT> Matrix<GeneralBand,Ref,Ref,AT>::operator()(int i) const {
305 // FMATVEC_ASSERT(i<=ku, AT);
306 // FMATVEC_ASSERT(i>=-kl, AT);
307 //
308 // return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
309 // //(ku-i,i>0?i:0));
310 // }
311
312 template <class AT>
313 inline Matrix<GeneralBand,Ref,Ref,AT>::operator std::vector<std::vector<AT>>() const {
314 std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
315 for(int r=0; r<rows(); r++) {
316 for(int c=0; c<cols(); c++)
317 ret[r][c]=operator()(r,c);
318 }
319 return ret;
320 }
321
323
324 template <class AT>
325 inline Matrix<GeneralBand,Ref,Ref,AT>& Matrix<GeneralBand,Ref,Ref,AT>::copy(const Matrix<GeneralBand,Ref,Ref,AT> &A) {
326 for(int i=0; i<kl+ku+1; i++)
327 for(int j=0; j<n; j++)
328 ele[i+j*(kl+ku+1)] = A.ele[i+j*(kl+ku+1)];
329 return *this;
330 }
331
333
334 }
335
336 #endif
fmatvec::Matrix
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
fmatvec::Init
Definition: types.h:89
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::Matrix
Matrix(const Matrix< GeneralBand, Ref, Ref, AT > &A)
Copy Constructor.
Definition: band_matrix.h:90
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >
This is a matrix class for general band matrices.
Definition: band_matrix.h:40
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::blasOrder
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: band_matrix.h:240
fmatvec::GeneralBand
Shape class for general band matrices.
Definition: types.h:120
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::operator()
const AT * operator()() const
Pointer operator.
Definition: band_matrix.h:213
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::Matrix
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: band_matrix.h:100
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::size
int size() const
Size.
Definition: band_matrix.h:219
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::operator=
Matrix< GeneralBand, Ref, Ref, AT > & operator=(const Matrix< GeneralBand, Ref, Ref, AT > &A)
Assignment operator.
Definition: band_matrix.h:138
fmatvec::Ref
Definition: types.h:98
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::rows
int rows() const
Number of rows.
Definition: band_matrix.h:225
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::operator()
AT * operator()()
Pointer operator.
Definition: band_matrix.h:207
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::Matrix
Matrix(int n_, int kl_, int ku_, AT *ele_)
Regular Constructor.
Definition: band_matrix.h:114
fmatvec::Matrix::cols
int cols() const
Number of columns.
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::operator=
Matrix< GeneralBand, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: band_matrix.h:150
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::Matrix
Matrix()
Standard constructor.
Definition: band_matrix.h:66
fmatvec
Namespace fmatvec.
Definition: _memory.cc:28
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::operator&=
Matrix< GeneralBand, Ref, Ref, AT > & operator&=(Matrix< GeneralBand, Ref, Ref, AT > &A)
Reference operator.
Definition: band_matrix.h:162
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::cols
int cols() const
Number of columns.
Definition: band_matrix.h:231
fmatvec::Noinit
Definition: types.h:88
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::operator()
const AT & operator()(int i, int j) const
Element operator.
Definition: band_matrix.h:190
fmatvec::Matrix< GeneralBand, Ref, Ref, AT >::init
Matrix< GeneralBand, Ref, Ref, AT > & init(const AT &val=AT())
Diagonal operator.
Definition: band_matrix.h:279
fmatvec::Matrix::rows
int rows() const
Number of rows.
fmatvec::Eye
Definition: types.h:90