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 
66  }
67 
72  template<class Row>
74  }
75 
80  template<class Type, class Row, class Col>
81  explicit SquareMatrix(const Matrix<Type,Row,Col,AT>& A) : Matrix<General,Var,Var,AT>(A) {
82  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
83  }
84 
85  SquareMatrix(const char *str) : Matrix<General,Var,Var,AT>(str) { }
86 
87  SquareMatrix<Var,AT>& resize(int m, Noinit) {
88  Matrix<General,Var,Var,AT>::resize(m,m,Noinit());
89  return *this;
90  }
91 
92  SquareMatrix<Var,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) {
93  Matrix<General,Var,Var,AT>::resize(m,m,ini,a);
94  return *this;
95  }
96 
97  SquareMatrix<Var,AT>& resize(int m, Eye ini, const AT &a=1) {
98  Matrix<General,Var,Var,AT>::resize(m,m,ini,a);
99  return *this;
100  }
101 
104  void resize(int m, int n) {
105  if(n!=m)
106  throw std::runtime_error("Cannot resize a square matrix with different dimensions for rows and columns.");
107  resize(m);
108  }
109 
118  return *this;
119  }
120 
127  template <class Type, class Row, class Col> SquareMatrix<Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A) {
129  return *this;
130  }
131 
138  template<class Type, class Row, class Col> SquareMatrix<Var,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
139  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
141  return *this;
142  }
143 
148  int size() const {return M;}
149  int rows() const {return M;}
150  int cols() const {return M;}
151 
152  using Matrix<General,Var,Var,AT>::operator();
153  using Matrix<General,Var,Var,AT>::e;
154 
159  explicit inline operator std::vector<std::vector<AT>>() const;
160 
161  inline const SquareMatrix<Var,AT> T() const;
162  };
163 
164  template <class AT>
165  inline const SquareMatrix<Var,AT> SquareMatrix<Var,AT>::T() const {
166  SquareMatrix<Var,AT> A(size(),NONINIT);
167  for(int i=0; i<M; i++)
168  for(int j=0; j<M; j++)
169  A.e(i,j) = e(j,i);
170  return A;
171  }
172 
173  template <class AT>
174  inline SquareMatrix<Var,AT>::operator std::vector<std::vector<AT>>() const {
175  std::vector<std::vector<AT>> ret(size());
176  for(int r=0; r<size(); r++) {
177  ret[r].resize(size());
178  for(int c=0; c<size(); c++)
179  ret[r][c]= e(r,c);
180  }
181  return ret;
182  }
183 
184 }
185 
186 #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:81
Definition: types.h:97
Dummy shape class for square matrices.
Definition: types.h:181
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:116
SquareMatrix(const SquareMatrix< Var, AT > &A)
Copy Constructor.
Definition: var_square_matrix.h:65
SquareMatrix< Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_square_matrix.h:127
Definition: fmatvec.h:44
SquareMatrix(const SquareMatrix< Row, AT > &A)
Copy Constructor.
Definition: var_square_matrix.h:73
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:148
Namespace fmatvec.
Definition: _memory.cc:28
Definition: types.h:84
Basic shape class for matrices.
Definition: types.h:132
int rows() const
Number of rows.
void resize(int m, int n)
Definition: var_square_matrix.h:104