fmatvec  0.0.0
Fast Matrix/Vector Library
var_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 var_square_matrix_h
23 #define var_square_matrix_h
24 
25 #include "fixed_general_matrix.h"
26 
27 namespace fmatvec {
28 
38  template <class AT> class SquareMatrix<Var,AT> : public Matrix<General,Var,Var,AT> {
39 
41 
42  public:
43  static constexpr bool isVector {false};
44 
45  typedef AT value_type;
46  typedef Square shape_type;
47 
52  explicit SquareMatrix() : Matrix<General,Var,Var,AT>() { }
53 
54 // template<class Ini=All<AT>>
55 // SquareMatrix(int m, Ini ini=All<AT>()) : Matrix<General,Var,Var,AT>(m,m,ini) { }
56 
57  explicit SquareMatrix(int m, Noinit ini) : Matrix<General,Var,Var,AT>(m,m,ini) { }
58  explicit SquareMatrix(int m, Init ini=INIT, const AT &a=AT()) : Matrix<General,Var,Var,AT>(m,m,ini,a) { }
59  explicit SquareMatrix(int m, Eye ini, const AT &a=1) : Matrix<General,Var,Var,AT>(m,m,ini,a) { }
60 
61  // move
62  SquareMatrix(SquareMatrix<Var,AT> &&src) : Matrix<General,Var,Var,AT>(std::move(src)) {}
63  SquareMatrix<Var,AT>& operator=(SquareMatrix<Var,AT> &&src) {
64  M=src.M;
65  src.M=0;
66  this->N=src.N;
67  src.N=0;
68  delete[]this->ele;
69  this->ele=src.ele;
70  src.ele=nullptr;
71  return *this;
72  }
73 
79  }
80 
85  template<class Row>
87  }
88 
93  template<class Type, class Row, class Col>
94  explicit SquareMatrix(const Matrix<Type,Row,Col,AT>& A) : Matrix<General,Var,Var,AT>(A) {
95  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
96  }
97 
98  SquareMatrix(const char *str) : Matrix<General,Var,Var,AT>(str) { }
99 
100  SquareMatrix<Var,AT>& resize(int m, Noinit) {
101  Matrix<General,Var,Var,AT>::resize(m,m,Noinit());
102  return *this;
103  }
104 
105  SquareMatrix<Var,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) {
106  Matrix<General,Var,Var,AT>::resize(m,m,ini,a);
107  return *this;
108  }
109 
110  SquareMatrix<Var,AT>& resize(int m, Eye ini, const AT &a=1) {
111  Matrix<General,Var,Var,AT>::resize(m,m,ini,a);
112  return *this;
113  }
114 
117  void resize(int m, int n) {
118  if(n!=m)
119  throw std::runtime_error("Cannot resize a square matrix with different dimensions for rows and columns.");
120  resize(m);
121  }
122 
131  return *this;
132  }
133 
140  template <class Type, class Row, class Col> SquareMatrix<Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A) {
142  return *this;
143  }
144 
151  template<class Type, class Row, class Col> SquareMatrix<Var,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
152  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
154  return *this;
155  }
156  // move
157  SquareMatrix<Var,AT>& operator<<=(SquareMatrix<Var,AT> &&src) {
158  FMATVEC_ASSERT(src.rows() == src.cols(), AT);
160  return *this;
161  }
162 
167  int size() const {return M;}
168  int rows() const {return M;}
169  int cols() const {return M;}
170 
171  using Matrix<General,Var,Var,AT>::operator();
172  using Matrix<General,Var,Var,AT>::e;
173 
178  explicit inline operator std::vector<std::vector<AT>>() const;
179 
180  inline const SquareMatrix<Var,AT> T() const;
181  };
182 
183  template <class AT>
184  inline const SquareMatrix<Var,AT> SquareMatrix<Var,AT>::T() const {
185  SquareMatrix<Var,AT> A(size(),NONINIT);
186  for(int i=0; i<M; i++)
187  for(int j=0; j<M; j++)
188  A.e(i,j) = e(j,i);
189  return A;
190  }
191 
192  template <class AT>
193  inline SquareMatrix<Var,AT>::operator std::vector<std::vector<AT>>() const {
194  std::vector<std::vector<AT>> ret(size());
195  for(int r=0; r<size(); r++) {
196  ret[r].resize(size());
197  for(int c=0; c<size(); c++)
198  ret[r][c]= e(r,c);
199  }
200  return ret;
201  }
202 
203 }
204 
205 #endif
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
SquareMatrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_square_matrix.h:94
Definition: types.h:101
Dummy shape class for square matrices.
Definition: types.h:185
SquareMatrix()
Standard constructor.
Definition: var_square_matrix.h:52
SquareMatrix< Var, AT > & operator=(const SquareMatrix< Var, AT > &A)
Assignment operator.
Definition: var_square_matrix.h:129
SquareMatrix(const SquareMatrix< Var, AT > &A)
Copy Constructor.
Definition: var_square_matrix.h:78
SquareMatrix< Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_square_matrix.h:140
Definition: fmatvec.h:44
SquareMatrix(const SquareMatrix< Row, AT > &A)
Copy Constructor.
Definition: var_square_matrix.h:86
This is a matrix class of general quadratic matrices.
Definition: var_square_matrix.h:38
int cols() const
Number of columns.
int size() const
Size.
Definition: var_square_matrix.h:167
Namespace fmatvec.
Definition: _memory.cc:28
Definition: types.h:88
Basic shape class for matrices.
Definition: types.h:136
int rows() const
Number of rows.
void resize(int m, int n)
Definition: var_square_matrix.h:117