fmatvec  0.0.0
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  using value_type = AT;
52  using shape_type = Square;
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 
202  explicit SquareMatrix(const std::vector<std::vector<AT>> &m);
203 
204  const SquareMatrix<Ref,AT> T() const;
205  };
206 
207  template <class AT>
208  inline SquareMatrix<Ref,AT>::operator std::vector<std::vector<AT>>() const {
209  std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
210  for(int r=0; r<size(); r++) {
211  for(int c=0; c<size(); c++)
212  ret[r][c]= e(r,c);
213  }
214  return ret;
215  }
216 
217  template <class AT>
218  inline SquareMatrix<Ref,AT>::SquareMatrix(const std::vector<std::vector<AT>> &m) : Matrix<General,Ref,Ref,AT>(static_cast<int>(m.size()),static_cast<int>(m[0].size()),NONINIT) {
219  for(int r=0; r<rows(); r++) {
220  if(static_cast<int>(m[r].size())!=cols())
221  throw std::runtime_error("The rows of the input have different length.");
222  for(int c=0; c<cols(); c++)
223  e(r,c)=m[r][c];
224  }
225  }
226 
227  template <class AT>
228  inline const SquareMatrix<Ref,AT> SquareMatrix<Ref,AT>::T() const {
229  SquareMatrix<Ref,AT> A(size(),NONINIT);
230  for(int i=0; i<m; i++)
231  for(int j=0; j<m; j++)
232  A.e(i,j) = e(j,i);
233  return A;
234  }
235 
236 }
237 
238 #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:42
int rows() const
Number of rows.
Definition: general_matrix.h:307
SquareMatrix(int m, AT *ele)
Regular Constructor.
Definition: square_matrix.h:101
Dummy shape class for square matrices.
Definition: types.h:169
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:45
int cols() const
Number of columns.
This is a matrix class for general matrices.
Definition: general_matrix.h:42
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:313
SquareMatrix()
Standard constructor.
Definition: square_matrix.h:60
Definition: types.h:88
Shape class for general matrices.
Definition: types.h:112
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.