All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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 #include <stdexcept>
27 
28 namespace fmatvec {
29 
39  template <class AT> class SquareMatrix<Ref,AT> : public Matrix<General,Ref,Ref,AT> {
40 
48 
49  public:
50 
51  typedef AT AtomicType;
52 
54 
55  template <class T> friend SquareMatrix<Ref,T> trans(const SquareMatrix<Ref,T> &A);
56 
59 
60  protected:
61 
62  SquareMatrix(int n, int lda, int tp, Memory<AT> memory, const AT* ele) : Matrix<General,Ref,Ref,AT>(n, n, lda, tp, memory, ele) {
63  }
64 
66 
67  public:
68 
74 
75 // template<class Ini=All<AT> >
76 // SquareMatrix(int m, Ini ini=All<AT>()) : Matrix<General,Ref,Ref,AT>(m,m,ini) { }
77 
78  SquareMatrix(int m, Noinit ini) : Matrix<General,Ref,Ref,AT>(m,m,ini) { }
79  SquareMatrix(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Ref,Ref,AT>(m,m,ini,a) { }
80  SquareMatrix(int m, Eye ini, const AT &a=1) : Matrix<General,Ref,Ref,AT>(m,m,ini,a) { }
81 
89  SquareMatrix(int m, AT* ele) : Matrix<General,Ref,Ref,AT>(m,m,ele) { }
90 
91  SquareMatrix(const char *str) : Matrix<General,Ref,Ref,AT>(str) { }
92 
101 
107 #ifndef FMATVEC_NO_SIZE_CHECK
108  assert(A.rows() == A.cols());
109 #endif
110  }
111 
112  template<class Type, class Row, class Col>
113  explicit SquareMatrix(const Matrix<Type,Row,Col,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
114  }
115 
116  SquareMatrix<Ref,AT>& resize() {
117  Matrix<General,Ref,Ref,AT>::resize();
118  return *this;
119  }
120 
121  SquareMatrix<Ref,AT>& resize(int m, Noinit) {
122  Matrix<General,Ref,Ref,AT>::resize(m,m,Noinit());
123  return *this;
124  }
125 
126  SquareMatrix<Ref,AT>& resize(int m, Init ini=INIT, const AT &a=0) {
127  Matrix<General,Ref,Ref,AT>::resize(m,m,ini,a);
128  return *this;
129  }
130 
131  SquareMatrix<Ref,AT>& resize(int m, Eye ini, const AT &a=1) {
132  Matrix<General,Ref,Ref,AT>::resize(m,m,ini,a);
133  return *this;
134  }
135 
138  void resize(int m, int n) {
139  if(n!=m)
140  throw std::runtime_error("Cannot resize a square matrix with different dimensions for rows and columns.");
141  resize(m);
142  }
143 
154  return *this;
155  }
156 
163  template<class Type, class Row, class Col> SquareMatrix<Ref,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A) {
164 #ifndef FMATVEC_NO_SIZE_CHECK
165  assert(A.rows() == A.cols());
166 #endif
168  return *this;
169  }
170 
179  return *this;
180  }
181 
183 #ifndef FMATVEC_NO_SIZE_CHECK
184  assert(A.rows() == A.cols());
185 #endif
187  return *this;
188  }
189 
194  int size() const {return m;};
195 
201  inline SquareMatrix<Ref,AT> copy() const;
202 
204 
209  inline operator std::vector<std::vector<AT> >();
210 
212  return SquareMatrix<Ref,AT>(n, lda, tp?false:true, memory, ele);
213  }
214 
215  const SquareMatrix<Ref,AT> T() const {
216  return SquareMatrix<Ref,AT>(n, lda, tp?false:true, memory, ele);
217  }
218  };
219 
220  template <class AT>
222 
223  SquareMatrix<Ref,AT> A(m,NONINIT);
224  A.deepCopy(*this);
225 
226  return A;
227  }
228 
229  template <class AT>
230  inline SquareMatrix<Ref,AT>::operator std::vector<std::vector<AT> >() {
231  std::vector<std::vector<AT> > ret(size());
232  if(tp) {
233  for(int r=0; r<size(); r++) {
234  ret[r].resize(size());
235  for(int c=0; c<size(); c++)
236  ret[r][c]= this->et(r,c);
237  }
238  }
239  else {
240  for(int r=0; r<size(); r++) {
241  ret[r].resize(size());
242  for(int c=0; c<size(); c++)
243  ret[r][c]= this->er(r,c);
244  }
245  }
246  return ret;
247  }
248 
249 }
250 
251 #endif
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
SquareMatrix(int m, AT *ele)
Regular Constructor.
Definition: square_matrix.h:89
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:86
int rows() const
Number of rows.
Definition: general_matrix.h:272
SquareMatrix< Ref, AT > & operator=(const SquareMatrix< Ref, AT > &A)
Assignment operator.
Definition: square_matrix.h:152
void resize(int m, int n)
Definition: square_matrix.h:138
Definition: types.h:62
int size() const
Size.
Definition: square_matrix.h:194
Definition: fmatvec.h:44
This is a matrix class for general matrices.
Definition: general_matrix.h:40
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:171
SquareMatrix()
Standard constructor.
Definition: square_matrix.h:73
SquareMatrix(const Matrix< General, Ref, Ref, AT > &A)
Copy Constructor.
Definition: square_matrix.h:106
Definition: matrix.h:38
RowVector< Ref, AT > trans(const Vector< Ref, AT > &x)
Transpose of a vector.
Definition: linear_algebra.h:1470
Basic shape class for matrices.
Definition: types.h:100
This is a matrix class of general quadratic matrices.
Definition: square_matrix.h:39
SquareMatrix(const SquareMatrix< Ref, AT > &A)
Copy Constructor.
Definition: square_matrix.h:100
SquareMatrix< Ref, AT > & operator>>(const SquareMatrix< Ref, AT > &A)
Reference operator.
Definition: square_matrix.h:177
This is an index class for creating submatrices.
Definition: range.h:44
int cols() const
Number of columns.
Definition: general_matrix.h:278

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML