]> Dogcows Code - chaz/yoink/blob - src/Moof/cml/matrix/fixed.h
f35bf1c64a4f5081ec74e76849cd63aa7cd468b3
[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 /** Set the matrix row from the given vector. */
202 void set_row(size_t i, const row_vector_type& row) {
203 for(size_t j = 0; j < this->cols(); ++ j) (*this)(i,j) = row[j];
204 }
205
206 /** Set the matrix column from the given vector. */
207 void set_col(size_t j, const col_vector_type& col) {
208 for(size_t i = 0; i < this->rows(); ++ i) (*this)(i,j) = col[i];
209 }
210
211
212 public:
213
214 /* Define common class operators: */
215
216 CML_CONSTRUCT_MAT_22
217 CML_CONSTRUCT_MAT_33
218 CML_CONSTRUCT_MAT_44
219
220 CML_MAT_COPY_FROM_FIXED_ARRAY(
221 array_type::array_rows, array_type::array_cols)
222
223 CML_MAT_COPY_FROM_MATTYPE
224 CML_MAT_COPY_FROM_MAT
225 CML_MAT_COPY_FROM_MATXPR
226
227 CML_ASSIGN_MAT_22
228 CML_ASSIGN_MAT_33
229 CML_ASSIGN_MAT_44
230
231 CML_MAT_ASSIGN_FROM_MATTYPE
232
233 CML_MAT_ASSIGN_FROM_MAT(=, et::OpAssign)
234 CML_MAT_ASSIGN_FROM_MAT(+=, et::OpAddAssign)
235 CML_MAT_ASSIGN_FROM_MAT(-=, et::OpSubAssign)
236
237 CML_MAT_ASSIGN_FROM_MATXPR(=, et::OpAssign)
238 CML_MAT_ASSIGN_FROM_MATXPR(+=, et::OpAddAssign)
239 CML_MAT_ASSIGN_FROM_MATXPR(-=, et::OpSubAssign)
240
241 CML_MAT_ASSIGN_FROM_SCALAR(*=, et::OpMulAssign)
242 CML_MAT_ASSIGN_FROM_SCALAR(/=, et::OpDivAssign)
243
244 CML_ACCUMULATED_MATRIX_MULT(const matrix_type&)
245
246 template<typename E, class AT, typename BO, typename L>
247 CML_ACCUMULATED_MATRIX_MULT(const TEMPLATED_MATRIX_MACRO&)
248
249 template<class XprT>
250 CML_ACCUMULATED_MATRIX_MULT(MATXPR_ARG_TYPE)
251
252
253 protected:
254
255 value_type basis_element(size_t i, size_t j, row_basis) const {
256 return (*this)(i,j);
257 }
258
259 value_type basis_element(size_t i, size_t j, col_basis) const {
260 return (*this)(j,i);
261 }
262
263 void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, row_basis) {
264 (*this)(i,j) = s;
265 }
266
267 void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, col_basis) {
268 (*this)(j,i) = s;
269 }
270
271
272 public:
273
274 /* Braces should only be used for testing: */
275 #if defined(CML_ENABLE_MATRIX_BRACES)
276 CML_MATRIX_BRACE_OPERATORS
277 #endif
278 };
279
280 } // namespace cml
281
282 #endif
283
284 // -------------------------------------------------------------------------
285 // vim:ft=cpp
This page took 0.047677 seconds and 4 git commands to generate.