]> Dogcows Code - chaz/yoink/blob - src/Moof/cml/matrix/fixed.h
extreme refactoring
[chaz/yoink] / src / Moof / cml / matrix / fixed.h
1 /* -*- C++ -*- ------------------------------------------------------------
2
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
4
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
7
8 *-----------------------------------------------------------------------*/
9 /** @file
10 * @brief
11 */
12
13 #ifndef fixed_matrix_h
14 #define fixed_matrix_h
15
16 #include <cml/core/fixed_2D.h>
17 #include <cml/matrix/matrix_expr.h>
18 #include <cml/matrix/class_ops.h>
19 #include <cml/matrix/matrix_unroller.h>
20
21 namespace cml {
22
23 /** Fixed-size, fixed-memory matrix. */
24 template<typename Element, int Rows, int Cols,
25 typename BasisOrient, typename Layout>
26 class matrix<Element,fixed<Rows,Cols>,BasisOrient,Layout>
27 : public fixed_2D<Element,Rows,Cols,Layout>
28 {
29 public:
30
31 /* Shorthand for the generator: */
32 typedef fixed<Rows,Cols> generator_type;
33
34 /* Shorthand for the array type: */
35 typedef fixed_2D<Element,Rows,Cols,Layout> array_type;
36
37 /* Shorthand for the type of this matrix: */
38 typedef matrix<Element,generator_type,BasisOrient,Layout> matrix_type;
39
40 /* For integration into the expression template code: */
41 typedef matrix_type expr_type;
42
43 /* For integration into the expression template code: */
44 typedef matrix_type temporary_type;
45
46 /* Standard: */
47 typedef typename array_type::value_type value_type;
48 typedef typename array_type::reference reference;
49 typedef typename array_type::const_reference const_reference;
50
51 typedef matrix_type& expr_reference;
52 typedef const matrix_type& expr_const_reference;
53
54 /* For matching by basis: */
55 typedef BasisOrient basis_orient;
56
57 /* For matching by memory layout: */
58 typedef typename array_type::layout layout;
59
60 /* For matching by storage type if necessary: */
61 typedef typename array_type::memory_tag memory_tag;
62
63 /* For matching by size type if necessary: */
64 typedef typename array_type::size_tag size_tag;
65
66 /* For matching by result type: */
67 typedef cml::et::matrix_result_tag result_tag;
68
69 /* For matching by assignability: */
70 typedef cml::et::assignable_tag assignable_tag;
71
72 /* To simplify the matrix transpose operator: */
73 typedef matrix<
74 Element,
75 typename array_type::transposed_type::generator_type,
76 BasisOrient,
77 Layout
78 > transposed_type;
79
80 /* To simplify the matrix row and column operators: */
81 typedef vector<
82 Element,
83 typename array_type::row_array_type::generator_type
84 > row_vector_type;
85
86 typedef vector<
87 Element,
88 typename array_type::col_array_type::generator_type
89 > col_vector_type;
90
91
92 public:
93
94 /** Set this matrix to zero. */
95 matrix_type& zero() {
96 typedef cml::et::OpAssign<Element,Element> OpT;
97 cml::et::UnrollAssignment<OpT>(*this,Element(0));
98 return *this;
99 }
100
101 /** Set this matrix to the identity.
102 *
103 * This only makes sense for a square matrix, but no error will be
104 * signaled if the matrix is not square.
105 */
106 matrix_type& identity() {
107 for(size_t i = 0; i < this->rows(); ++ i) {
108 for(size_t j = 0; j < this->cols(); ++ j) {
109 (*this)(i,j) = value_type((i == j)?1:0);
110 }
111 }
112 return *this;
113 }
114
115 /** Set this matrix to its transpose.
116 *
117 * This only makes sense for a square matrix, but no error will be
118 * signaled if the matrix is not square.
119 */
120 matrix_type& transpose() {
121 /* transpose() returns a temporary: */
122 *this = cml::transpose(*this);
123 return *this;
124 }
125
126 /** Set this matrix to its inverse.
127 *
128 * This only makes sense for a square matrix, but no error will be
129 * signaled if the matrix is not square.
130 */
131 matrix_type& inverse() {
132 /* inverse() returns a temporary: */
133 *this = cml::inverse(*this);
134 return *this;
135 }
136
137 /* NOTE: minimize() and maximize() no longer supported (Jesse) */
138
139 #if 0
140 /** Pairwise minimum of this matrix with another. */
141 template<typename E, class AT, typename L>
142 void minimize(const matrix<E,AT,basis_orient,L>& v) {
143 /* XXX This should probably use ScalarPromote: */
144 for (size_t i = 0; i < this->rows(); ++i) {
145 for (size_t j = 0; j < this->cols(); ++j) {
146 (*this)(i,j) = std::min((*this)(i,j),v(i,j));
147 }
148 }
149 }
150
151 /** Pairwise maximum of this matrix with another. */
152 template<typename E, class AT, typename L>
153 void maximize(const matrix<E,AT,basis_orient,L>& v) {
154 /* XXX This should probably use ScalarPromote: */
155 for (size_t i = 0; i < this->rows(); ++i) {
156 for (size_t j = 0; j < this->cols(); ++j) {
157 (*this)(i,j) = std::max((*this)(i,j),v(i,j));
158 }
159 }
160 }
161 #endif
162
163 /* Set each element to a random number in the range [min,max] */
164 void random(ELEMENT_ARG_TYPE min, ELEMENT_ARG_TYPE max) {
165 for(size_t i = 0; i < this->rows(); ++i) {
166 for(size_t j = 0; j < this->cols(); ++j) {
167 (*this)(i,j) = cml::random_real(min,max);
168 }
169 }
170 }
171
172
173 public:
174
175 /** Default constructor.
176 *
177 * @throws same as the ArrayType constructor.
178 * @sa cml::fixed
179 * @sa cml::dynamic
180 */
181 matrix() {}
182
183
184 public:
185
186 /** Return the matrix size as a pair. */
187 matrix_size size() const {
188 return matrix_size(this->rows(),this->cols());
189 }
190
191 /** Return element j of basis vector i. */
192 value_type basis_element(size_t i, size_t j) const {
193 return basis_element(i,j,basis_orient());
194 }
195
196 /** Set the given basis element. */
197 void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s) {
198 set_basis_element(i,j,s,basis_orient());
199 }
200
201
202 public:
203
204 /* Define common class operators: */
205
206 CML_CONSTRUCT_MAT_22
207 CML_CONSTRUCT_MAT_33
208 CML_CONSTRUCT_MAT_44
209
210 CML_MAT_COPY_FROM_FIXED_ARRAY(
211 array_type::array_rows, array_type::array_cols)
212
213 CML_MAT_COPY_FROM_MATTYPE
214 CML_MAT_COPY_FROM_MAT
215 CML_MAT_COPY_FROM_MATXPR
216
217 CML_ASSIGN_MAT_22
218 CML_ASSIGN_MAT_33
219 CML_ASSIGN_MAT_44
220
221 CML_MAT_ASSIGN_FROM_MATTYPE
222
223 CML_MAT_ASSIGN_FROM_MAT(=, et::OpAssign)
224 CML_MAT_ASSIGN_FROM_MAT(+=, et::OpAddAssign)
225 CML_MAT_ASSIGN_FROM_MAT(-=, et::OpSubAssign)
226
227 CML_MAT_ASSIGN_FROM_MATXPR(=, et::OpAssign)
228 CML_MAT_ASSIGN_FROM_MATXPR(+=, et::OpAddAssign)
229 CML_MAT_ASSIGN_FROM_MATXPR(-=, et::OpSubAssign)
230
231 CML_MAT_ASSIGN_FROM_SCALAR(*=, et::OpMulAssign)
232 CML_MAT_ASSIGN_FROM_SCALAR(/=, et::OpDivAssign)
233
234 CML_ACCUMULATED_MATRIX_MULT(const matrix_type&)
235
236 template<typename E, class AT, typename BO, typename L>
237 CML_ACCUMULATED_MATRIX_MULT(const TEMPLATED_MATRIX_MACRO&)
238
239 template<class XprT>
240 CML_ACCUMULATED_MATRIX_MULT(MATXPR_ARG_TYPE)
241
242
243 protected:
244
245 value_type basis_element(size_t i, size_t j, row_basis) const {
246 return (*this)(i,j);
247 }
248
249 value_type basis_element(size_t i, size_t j, col_basis) const {
250 return (*this)(j,i);
251 }
252
253 void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, row_basis) {
254 (*this)(i,j) = s;
255 }
256
257 void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, col_basis) {
258 (*this)(j,i) = s;
259 }
260
261
262 public:
263
264 /* Braces should only be used for testing: */
265 #if defined(CML_ENABLE_MATRIX_BRACES)
266 CML_MATRIX_BRACE_OPERATORS
267 #endif
268 };
269
270 } // namespace cml
271
272 #endif
273
274 // -------------------------------------------------------------------------
275 // vim:ft=cpp
This page took 0.03948 seconds and 4 git commands to generate.