fmatvec 0.0.0

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
40 using Matrix<General,Var,Var,AT>::M;
41
42 public:
43 static constexpr bool isVector {false};
44
45 using value_type = AT;
46 using shape_type = Square;
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) noexcept : Matrix<General,Var,Var,AT>(std::move(src)) {}
63 SquareMatrix<Var,AT>& operator=(SquareMatrix<Var,AT> &&src) noexcept {
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
78 SquareMatrix(const SquareMatrix<Var,AT> &A) : Matrix<General,Var,Var,AT>(A) {
79 }
80
85 template<class Row>
86 SquareMatrix(const SquareMatrix<Row,AT> &A) : Matrix<General,Var,Var,AT>(A) {
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
129 SquareMatrix<Var,AT>& operator=(const SquareMatrix<Var,AT> &A) {
130 Matrix<General,Var,Var,AT>::operator=(A);
131 return *this;
132 }
133
140 template <class Type, class Row, class Col> SquareMatrix<Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A) {
141 Matrix<General,Var,Var,AT>::operator=(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);
153 Matrix<General,Var,Var,AT>::operator<<=(A);
154 return *this;
155 }
156 // move
157 SquareMatrix<Var,AT>& operator<<=(SquareMatrix<Var,AT> &&src) {
158 FMATVEC_ASSERT(src.rows() == src.cols(), AT);
159 Matrix<General,Var,Var,AT>::operator<<=(std::move(src));
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
185 explicit SquareMatrix(const std::vector<std::vector<AT>> &m);
186
187 inline const SquareMatrix<Var,AT> T() const;
188 };
189
190 template <class AT>
191 inline const SquareMatrix<Var,AT> SquareMatrix<Var,AT>::T() const {
192 SquareMatrix<Var,AT> A(size(),NONINIT);
193 for(int i=0; i<M; i++)
194 for(int j=0; j<M; j++)
195 A.e(i,j) = e(j,i);
196 return A;
197 }
198
199 template <class AT>
200 inline SquareMatrix<Var,AT>::operator std::vector<std::vector<AT>>() const {
201 std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
202 for(int r=0; r<size(); r++) {
203 for(int c=0; c<size(); c++)
204 ret[r][c]= e(r,c);
205 }
206 return ret;
207 }
208
209 template <class AT>
210 inline SquareMatrix<Var,AT>::SquareMatrix(const std::vector<std::vector<AT>> &m) : Matrix<General,Var,Var,AT>(static_cast<int>(m.size()),static_cast<int>(m[0].size()),NONINIT) {
211 for(int r=0; r<rows(); r++) {
212 if(static_cast<int>(m[r].size())!=cols())
213 throw std::runtime_error("The rows of the input have different length.");
214 for(int c=0; c<cols(); c++)
215 e(r,c)=m[r][c];
216 }
217 }
218
219 }
220
221 #endif
fmatvec::Matrix
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
fmatvec::SquareMatrix< Var, AT >::SquareMatrix
SquareMatrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_square_matrix.h:94
fmatvec::Var
Definition: types.h:101
fmatvec::Square
Dummy shape class for square matrices.
Definition: types.h:169
fmatvec::SquareMatrix< Var, AT >::SquareMatrix
SquareMatrix()
Standard constructor.
Definition: var_square_matrix.h:52
fmatvec::SquareMatrix< Var, AT >::operator=
SquareMatrix< Var, AT > & operator=(const SquareMatrix< Var, AT > &A)
Assignment operator.
Definition: var_square_matrix.h:129
fmatvec::SquareMatrix< Var, AT >::SquareMatrix
SquareMatrix(const SquareMatrix< Var, AT > &A)
Copy Constructor.
Definition: var_square_matrix.h:78
fmatvec::SquareMatrix< Var, AT >::operator=
SquareMatrix< Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_square_matrix.h:140
fmatvec::SquareMatrix
Definition: fmatvec.h:45
fmatvec::SquareMatrix< Var, AT >::SquareMatrix
SquareMatrix(const SquareMatrix< Row, AT > &A)
Copy Constructor.
Definition: var_square_matrix.h:86
fmatvec::SquareMatrix< Var, AT >
This is a matrix class of general quadratic matrices.
Definition: var_square_matrix.h:38
fmatvec::Matrix::cols
int cols() const
Number of columns.
fmatvec::SquareMatrix< Var, AT >::size
int size() const
Size.
Definition: var_square_matrix.h:167
fmatvec
Namespace fmatvec.
Definition: _memory.cc:28
fmatvec::Noinit
Definition: types.h:88
fmatvec::General
Shape class for general matrices.
Definition: types.h:112
fmatvec::Matrix::rows
int rows() const
Number of rows.
fmatvec::SquareMatrix< Var, AT >::resize
void resize(int m, int n)
Definition: var_square_matrix.h:117