fmatvec  2.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  typedef AT value_type;
51 
52  public:
53 
58  explicit SquareMatrix() : Matrix<General,Ref,Ref,AT>() { }
59 
60 // template<class Ini=All<AT>>
61 // SquareMatrix(int m, Ini ini=All<AT>()) : Matrix<General,Ref,Ref,AT>(m,m,ini) { }
62 
63  explicit SquareMatrix(int m, Noinit ini) : Matrix<General,Ref,Ref,AT>(m,m,ini) { }
64  explicit SquareMatrix(int m, Init ini=INIT, const AT &a=AT()) : Matrix<General,Ref,Ref,AT>(m,m,ini,a) { }
65  explicit SquareMatrix(int m, Eye ini, const AT &a=1) : Matrix<General,Ref,Ref,AT>(m,m,ini,a) { }
66 
73 
79  template<class Row>
81 
87  template<class Type, class Row, class Col>
88  explicit SquareMatrix(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Ref,Ref,AT>(A) {
89  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
90  }
91 
99  explicit SquareMatrix(int m, AT* ele) : Matrix<General,Ref,Ref,AT>(m,m,ele) { }
100 
101  SquareMatrix(const char *str) : Matrix<General,Ref,Ref,AT>(str) { }
102 
103  SquareMatrix<Ref,AT>& resize(int m, Noinit) {
104  Matrix<General,Ref,Ref,AT>::resize(m,m,Noinit());
105  return *this;
106  }
107 
108  SquareMatrix<Ref,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) {
109  Matrix<General,Ref,Ref,AT>::resize(m,m,ini,a);
110  return *this;
111  }
112 
113  SquareMatrix<Ref,AT>& resize(int m, Eye ini, const AT &a=1) {
114  Matrix<General,Ref,Ref,AT>::resize(m,m,ini,a);
115  return *this;
116  }
117 
120  void resize(int m, int n) {
121  if(n!=m)
122  throw std::runtime_error("Cannot resize a square matrix with different dimensions for rows and columns.");
123  resize(m);
124  }
125 
134  return *this;
135  }
136 
143  template<class Type, class Row, class Col> inline SquareMatrix<Ref,AT>& operator=(const Matrix<Type,Row,Col,AT> &A) {
144  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
146  return *this;
147  }
148 
157  return *this;
158  }
159 
161  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
163  return *this;
164  }
165 
172  template<class Type, class Row, class Col> SquareMatrix<Ref,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
173  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
175  return *this;
176  }
177 
182  int size() const {return m;}
183 
186 
191  explicit inline operator std::vector<std::vector<AT>>() const;
192 
193  const SquareMatrix<Ref,AT> T() const;
194  };
195 
196  template <class AT>
197  inline SquareMatrix<Ref,AT>::operator std::vector<std::vector<AT>>() const {
198  std::vector<std::vector<AT>> ret(size());
199  for(int r=0; r<size(); r++) {
200  ret[r].resize(size());
201  for(int c=0; c<size(); c++)
202  ret[r][c]= this->er(r,c);
203  }
204  return ret;
205  }
206 
207  template <class AT>
208  inline const SquareMatrix<Ref,AT> SquareMatrix<Ref,AT>::T() const {
209  SquareMatrix<Ref,AT> A(size(),NONINIT);
210  for(int i=0; i<m; i++)
211  for(int j=0; j<m; j++)
212  A.e(i,j) = e(j,i);
213  return A;
214  }
215 
216 }
217 
218 #endif
SquareMatrix< Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: square_matrix.h:143
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
int rows() const
Number of rows.
Definition: general_matrix.h:278
SquareMatrix(int m, AT *ele)
Regular Constructor.
Definition: square_matrix.h:99
SquareMatrix(const SquareMatrix< Row, AT > &A)
Copy Constructor.
Definition: square_matrix.h:80
SquareMatrix< Ref, AT > & operator=(const SquareMatrix< Ref, AT > &A)
Assignment operator.
Definition: square_matrix.h:132
int size() const
Size.
Definition: square_matrix.h:182
void resize(int m, int n)
Definition: square_matrix.h:120
Definition: types.h:94
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:88
Namespace fmatvec.
Definition: _memory.cc:28
int cols() const
Number of columns.
Definition: general_matrix.h:284
SquareMatrix()
Standard constructor.
Definition: square_matrix.h:58
Definition: types.h:84
Basic shape class for matrices.
Definition: types.h:132
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:72
SquareMatrix< Ref, AT > & operator&=(SquareMatrix< Ref, AT > &A)
Reference operator.
Definition: square_matrix.h:155
int rows() const
Number of rows.