square_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 square_matrix_h
23 #define square_matrix_h
24 
25 #include "general_matrix.h"
26 
27 namespace fmatvec {
28 
38  template <class AT> class SquareMatrix : public Matrix<General, AT> {
39 
47 
48  public:
49 
51 
52  template <class T> friend SquareMatrix<T> trans(const SquareMatrix<T> &A);
53 
54  friend const SquareMatrix<AT> Matrix<General, AT>::operator()(const Index &I) const;
56 
57  protected:
58 
59  SquareMatrix(int n, int lda, int tp, Memory<AT> memory, const AT* ele) : Matrix<General, AT>(n, n, lda, tp, memory, ele) {
60  }
61 
63 
64  public:
65 
71  }
72 
81  SquareMatrix(int m) : Matrix<General, AT>(m,m) {
82  }
83 
93  SquareMatrix(int m, int n) : Matrix<General, AT>(m,m) {
94  assert(m == n);
95  }
96 
104  SquareMatrix(int m, AT* ele) : Matrix<General, AT>(m,m,ele) {
105  }
106 
117  SquareMatrix(int m, Initialization ini, const AT &a=0) : Matrix<General, AT>(m,m,ini,a) {
118  }
119 
131  SquareMatrix(int m, int n, Initialization ini, const AT &a=0) : Matrix<General, AT>(m,m,ini,a) {
132  assert(m == n);
133  }
134 
146  return *this;
147  }
148 
160  SquareMatrix<AT>& resize(int n,Initialization ini, const AT &a=0) {
161  Matrix<General, AT>::resize(n,n,ini,a);
162  return *this;
163  }
164 
173  }
174 
179  explicit SquareMatrix(const Matrix<General, AT>& A) : Matrix<General, AT>(A) {
180 #ifdef FMATVEC_SIZE_CHECK
181  assert(A.rows() == A.cols());
182 #endif
183  }
184 
194 #ifndef FMATVEC_NO_DEEP_ASSIGNMENT
195  return operator<<(A);
196 #else
197  return operator>>(A);
198 #endif
199  }
200 
207  SquareMatrix<AT>& operator<<(const SquareMatrix<AT>& A) {
209  return *this;
210  }
211 
218  template<class T> SquareMatrix<AT>& operator<<(const Matrix<T, AT> &A) {
220  return *this;
221  }
222 
231  return *this;
232  }
233 
238  int size() const {return m;};
239 
245  SquareMatrix<AT> copy() const;
246 
249 
254  operator std::vector<std::vector<AT> >();
255 
256  SquareMatrix<AT> T() {
257  return SquareMatrix<AT>(n, lda, tp?false:true, memory, ele);
258  }
259 
260  const SquareMatrix<AT> T() const {
261  return SquareMatrix<AT>(n, lda, tp?false:true, memory, ele);
262  }
263  };
264 
265  template <class AT>
267 
268  SquareMatrix<AT> A(m,NONINIT);
269  A.deepCopy(*this);
270 
271  return A;
272  }
273 
274  template <class AT>
275  SquareMatrix<AT>::operator std::vector<std::vector<AT> >() {
276  std::vector<std::vector<AT> > ret(size());
277  if(tp) {
278  for(int r=0; r<size(); r++) {
279  ret[r].resize(size());
280  for(int c=0; c<size(); c++)
281  ret[r][c]= ele[r*lda+c]; // ret[r][c]=operator()(r,c);
282  }
283  }
284  else {
285  for(int r=0; r<size(); r++) {
286  ret[r].resize(size());
287  for(int c=0; c<size(); c++)
288  ret[r][c]= ele[r+c*lda]; // ret[r][c]=operator()(r,c);
289  }
290  }
291  return ret;
292  }
293 
294 }
295 
296 #endif