fmatvec  0.0.0
Fast Matrix/Vector Library
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 
47 
48  public:
49 
50  static constexpr bool isVector {false};
51  typedef AT value_type;
52  typedef Square shape_type;
53 
54  public:
55 
60  explicit SquareMatrix() : Matrix<General,Ref,Ref,AT>() { }
61 
62 // template<class Ini=All<AT>>
63 // SquareMatrix(int m, Ini ini=All<AT>()) : Matrix<General,Ref,Ref,AT>(m,m,ini) { }
64 
65  explicit SquareMatrix(int m, Noinit ini) : Matrix<General,Ref,Ref,AT>(m,m,ini) { }
66  explicit SquareMatrix(int m, Init ini=INIT, const AT &a=AT()) : Matrix<General,Ref,Ref,AT>(m,m,ini,a) { }
67  explicit SquareMatrix(int m, Eye ini, const AT &a=1) : Matrix<General,Ref,Ref,AT>(m,m,ini,a) { }
68 
75 
81  template<class Row>
83 
89  template<class Type, class Row, class Col>
90  explicit SquareMatrix(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Ref,Ref,AT>(A) {
91  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
92  }
93 
101  explicit SquareMatrix(int m, AT* ele) : Matrix<General,Ref,Ref,AT>(m,m,ele) { }
102 
103  SquareMatrix(const char *str) : Matrix<General,Ref,Ref,AT>(str) { }
104 
105  SquareMatrix<Ref,AT>& resize(int m, Noinit) {
106  Matrix<General,Ref,Ref,AT>::resize(m,m,Noinit());
107  return *this;
108  }
109 
110  SquareMatrix<Ref,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) {
111  Matrix<General,Ref,Ref,AT>::resize(m,m,ini,a);
112  return *this;
113  }
114 
115  SquareMatrix<Ref,AT>& resize(int m, Eye ini, const AT &a=1) {
116  Matrix<General,Ref,Ref,AT>::resize(m,m,ini,a);
117  return *this;
118  }
119 
122  void resize(int m, int n) {
123  if(n!=m)
124  throw std::runtime_error("Cannot resize a square matrix with different dimensions for rows and columns.");
125  resize(m);
126  }
127 
136  return *this;
137  }
138 
145  template<class Type, class Row, class Col> inline SquareMatrix<Ref,AT>& operator=(const Matrix<Type,Row,Col,AT> &A) {
146  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
148  return *this;
149  }
150 
159  return *this;
160  }
161 
163  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
165  return *this;
166  }
167 
174  template<class Type, class Row, class Col> SquareMatrix<Ref,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
175  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
177  return *this;
178  }
179 
184  int size() const {return m;}
185  int rows() const {return m;}
186  int cols() const {return m;}
187 
188  using Matrix<General,Ref,Ref,AT>::operator();
189  using Matrix<General,Ref,Ref,AT>::e;
190 
195  explicit inline operator std::vector<std::vector<AT>>() const;
196 
197  const SquareMatrix<Ref,AT> T() const;
198  };
199 
200  template <class AT>
201  inline SquareMatrix<Ref,AT>::operator std::vector<std::vector<AT>>() const {
202  std::vector<std::vector<AT>> ret(size());
203  for(int r=0; r<size(); r++) {
204  ret[r].resize(size());
205  for(int c=0; c<size(); c++)
206  ret[r][c]= e(r,c);
207  }
208  return ret;
209  }
210 
211  template <class AT>
212  inline const SquareMatrix<Ref,AT> SquareMatrix<Ref,AT>::T() const {
213  SquareMatrix<Ref,AT> A(size(),NONINIT);
214  for(int i=0; i<m; i++)
215  for(int j=0; j<m; j++)
216  A.e(i,j) = e(j,i);
217  return A;
218  }
219 
220 }
221 
222 #endif
SquareMatrix< Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: square_matrix.h:145
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
int rows() const
Number of rows.
Definition: general_matrix.h:304
SquareMatrix(int m, AT *ele)
Regular Constructor.
Definition: square_matrix.h:101
Dummy shape class for square matrices.
Definition: types.h:185
SquareMatrix(const SquareMatrix< Row, AT > &A)
Copy Constructor.
Definition: square_matrix.h:82
SquareMatrix< Ref, AT > & operator=(const SquareMatrix< Ref, AT > &A)
Assignment operator.
Definition: square_matrix.h:134
int size() const
Size.
Definition: square_matrix.h:184
void resize(int m, int n)
Definition: square_matrix.h:122
Definition: types.h:98
Definition: fmatvec.h:44
int cols() const
Number of columns.
This is a matrix class for general matrices.
Definition: general_matrix.h:41
SquareMatrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: square_matrix.h:90
Namespace fmatvec.
Definition: _memory.cc:28
int cols() const
Number of columns.
Definition: general_matrix.h:310
SquareMatrix()
Standard constructor.
Definition: square_matrix.h:60
Definition: types.h:88
Basic shape class for matrices.
Definition: types.h:136
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:74
SquareMatrix< Ref, AT > & operator&=(SquareMatrix< Ref, AT > &A)
Reference operator.
Definition: square_matrix.h:157
int rows() const
Number of rows.