fmatvec 0.0.0

fixed_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 fixed_square_matrix_h
23 #define fixed_square_matrix_h
24
25 #include "fixed_general_matrix.h"
26
27 namespace fmatvec {
28
38 template <int M, class AT> class SquareMatrix<Fixed<M>,AT> : public Matrix<General,Fixed<M>,Fixed<M>,AT> {
39
40 public:
41 static constexpr bool isVector {false};
42 using value_type = AT;
43 using shape_type = Square;
44
45 // template<class Ini=All<AT>>
46 // SquareMatrix(Ini ini=All<AT>()) : Matrix<General,Fixed<M>,Fixed<M>,AT>(ini) { }
47 // template<class Ini=All<AT>>
48 // SquareMatrix(int m, Ini ini=All<AT>()) : Matrix<General,Fixed<M>,Fixed<M>,AT>(ini) { }
49
50 explicit SquareMatrix(Noinit ini) : Matrix<General,Fixed<M>,Fixed<M>,AT>(ini) { }
51 explicit SquareMatrix(Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<M>,Fixed<M>,AT>(ini,a) { }
52 explicit SquareMatrix(Eye ini, const AT &a=1) : Matrix<General,Fixed<M>,Fixed<M>,AT>(ini,a) { }
53 explicit SquareMatrix(int m, Noinit ini) : Matrix<General,Fixed<M>,Fixed<M>,AT>(ini) { FMATVEC_ASSERT(m==M, AT); }
54 explicit SquareMatrix(int m, Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<M>,Fixed<M>,AT>(ini,a) { FMATVEC_ASSERT(m==M, AT); }
55 explicit SquareMatrix(int m, Eye ini, const AT &a=1) : Matrix<General,Fixed<M>,Fixed<M>,AT>(ini,a) { FMATVEC_ASSERT(m==M, AT); }
56
61 SquareMatrix(const SquareMatrix<Fixed<M>,AT> &A) = default;
62
67 template<class Row>
68 SquareMatrix(const SquareMatrix<Row,AT> &A) : Matrix<General,Fixed<M>,Fixed<M>,AT>(A) { }
69
74 template<class Type, class Row, class Col>
75 explicit SquareMatrix(const Matrix<Type,Row,Col,AT>& A) : Matrix<General,Fixed<M>,Fixed<M>,AT>(A) { }
76
77 SquareMatrix(const char *str) : Matrix<General,Fixed<M>,Fixed<M>,AT>(str) { }
78
85 inline SquareMatrix<Fixed<M>,AT>& operator=(const SquareMatrix<Fixed<M>,AT> &A) = default;
86
93 template <class Type, class Row, class Col>
94 inline SquareMatrix<Fixed<M>,AT>& operator=(const Matrix<Type,Row,Col,AT> &A) {
95 Matrix<General,Fixed<M>,Fixed<M>,AT>::operator=(A);
96 return *this;
97 }
98
105 template<class Type, class Row, class Col>
106 inline SquareMatrix<Fixed<M>,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) { return operator=(A); }
107
112 constexpr int size() const {return M;}
113 constexpr int rows() const {return M;}
114 constexpr int cols() const {return M;}
115
116 using Matrix<General,Fixed<M>,Fixed<M>,AT>::operator();
117 using Matrix<General,Fixed<M>,Fixed<M>,AT>::e;
118
123 explicit inline operator std::vector<std::vector<AT>>() const;
124
130 explicit SquareMatrix(const std::vector<std::vector<AT>> &m);
131
132 inline const SquareMatrix<Fixed<M>,AT> T() const;
133 };
134
135 template <int M, class AT>
136 inline const SquareMatrix<Fixed<M>,AT> SquareMatrix<Fixed<M>,AT>::T() const {
137 SquareMatrix<Fixed<M>,AT> A(NONINIT);
138 for(int i=0; i<M; i++)
139 for(int j=0; j<M; j++)
140 A.e(i,j) = e(j,i);
141 return A;
142 }
143
144 template <int M, class AT>
145 inline SquareMatrix<Fixed<M>,AT>::operator std::vector<std::vector<AT>>() const {
146 std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
147 for(int r=0; r<size(); r++) {
148 for(int c=0; c<size(); c++)
149 ret[r][c]= e(r,c);
150 }
151 return ret;
152 }
153
154 template <int M, class AT>
155 inline SquareMatrix<Fixed<M>,AT>::SquareMatrix(const std::vector<std::vector<AT>> &m) : Matrix<General,Fixed<M>,Fixed<M>,AT>(NONINIT) {
156 for(int r=0; r<rows(); r++) {
157 if(static_cast<int>(m[r].size())!=cols())
158 throw std::runtime_error("The rows of the input have different length.");
159 for(int c=0; c<cols(); c++)
160 e(r,c)=m[r][c];
161 }
162 }
163
164 }
165
166 #endif
fmatvec::Fixed
Definition: fmatvec.h:39
fmatvec::SquareMatrix< Fixed< M >, AT >::SquareMatrix
SquareMatrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_square_matrix.h:75
fmatvec::Matrix
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
fmatvec::SquareMatrix< Fixed< M >, AT >::size
constexpr int size() const
Size.
Definition: fixed_square_matrix.h:112
fmatvec::Init
Definition: types.h:89
fmatvec::SquareMatrix< Fixed< M >, AT >::operator=
SquareMatrix< Fixed< M >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_square_matrix.h:94
fmatvec::Square
Dummy shape class for square matrices.
Definition: types.h:169
fmatvec::SquareMatrix< Fixed< M >, AT >::SquareMatrix
SquareMatrix(const SquareMatrix< Row, AT > &A)
Copy Constructor.
Definition: fixed_square_matrix.h:68
fmatvec::SquareMatrix
Definition: fmatvec.h:45
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::Eye
Definition: types.h:90