]> Dogcows Code - chaz/yoink/blob - src/Moof/cml/matrix/dynamic.h
beginnings of scene rendering
[chaz/yoink] / src / Moof / cml / matrix / dynamic.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 dynamic_matrix_h
14 #define dynamic_matrix_h
15
16 #include <cml/core/dynamic_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 /** Resizeable, dynamic-memory matrix. */
24 template<typename Element, typename Alloc,
25 typename BasisOrient, typename Layout>
26 class matrix<Element,dynamic<Alloc>,BasisOrient,Layout>
27 : public dynamic_2D<Element,Layout,Alloc>
28 {
29 public:
30
31 /* Shorthand for the generator: */
32 typedef dynamic<Alloc> generator_type;
33
34 /* Shorthand for the array type: */
35 typedef dynamic_2D<Element,Layout,Alloc> 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 /* For integration into the expression templates code: */
52 typedef matrix_type& expr_reference;
53 typedef const matrix_type& expr_const_reference;
54
55 /* For matching by basis: */
56 typedef BasisOrient basis_orient;
57
58 /* For matching by memory layout: */
59 typedef typename array_type::layout layout;
60
61 /* For matching by storage type: */
62 typedef typename array_type::memory_tag memory_tag;
63
64 /* For matching by size type if necessary: */
65 typedef typename array_type::size_tag size_tag;
66
67 /* For matching by resizability: */
68 typedef typename array_type::resizing_tag resizing_tag;
69
70 /* For matching by result type: */
71 typedef cml::et::matrix_result_tag result_tag;
72
73 /* For matching by assignability: */
74 typedef cml::et::assignable_tag assignable_tag;
75
76 /* To simplify the matrix transpose operator: */
77 typedef matrix<
78 Element,
79 typename array_type::transposed_type::generator_type,
80 BasisOrient,
81 Layout
82 > transposed_type;
83
84 /* To simplify the matrix row and column operators: */
85 typedef vector<
86 Element,
87 typename array_type::row_array_type::generator_type
88 > row_vector_type;
89
90 typedef vector<
91 Element,
92 typename array_type::col_array_type::generator_type
93 > col_vector_type;
94
95
96 public:
97
98 /** Set this matrix to zero. */
99 matrix_type& zero() {
100 typedef cml::et::OpAssign<Element,Element> OpT;
101 cml::et::UnrollAssignment<OpT>(*this,Element(0));
102 return *this;
103 }
104
105 /** Set this matrix to the identity.
106 *
107 * This only makes sense for a square matrix, but no error will be
108 * signaled if the matrix is not square.
109 */
110 matrix_type& identity() {
111 for(size_t i = 0; i < this->rows(); ++ i) {
112 for(size_t j = 0; j < this->cols(); ++ j) {
113 (*this)(i,j) = value_type((i == j)?1:0);
114 }
115 }
116 return *this;
117 }
118
119 /** Set this matrix to its transpose.
120 *
121 * This only makes sense for a square matrix, but no error will be
122 * signaled if the matrix is not square.
123 */
124 matrix_type& transpose() {
125 /* transpose() returns a temporary: */
126 *this = cml::transpose(*this);
127 return *this;
128 }
129
130 /** Set this matrix to its inverse.
131 *
132 * This only makes sense for a square matrix, but no error will be
133 * signaled if the matrix is not square.
134 */
135 matrix_type& inverse() {
136 /* inverse() returns a temporary: */
137 *this = cml::inverse(*this);
138 return *this;
139 }
140
141 /* NOTE: minimize() and maximize() no longer supported (Jesse) */
142
143 #if 0
144 /** Pairwise minimum of this matrix with another. */
145 template<typename E, class AT, typename L>
146 void minimize(const matrix<E,AT,basis_orient,L>& v) {
147 /* XXX This should probably use ScalarPromote: */
148 for (size_t i = 0; i < this->rows(); ++i) {
149 for (size_t j = 0; j < this->cols(); ++j) {
150 (*this)(i,j) = std::min((*this)(i,j),v(i,j));
151 }
152 }
153 }
154
155 /** Pairwise maximum of this matrix with another. */
156 template<typename E, class AT, typename L>
157 void maximize(const matrix<E,AT,basis_orient,L>& v) {
158 /* XXX This should probably use ScalarPromote: */
159 for (size_t i = 0; i < this->rows(); ++i) {
160 for (size_t j = 0; j < this->cols(); ++j) {
161 (*this)(i,j) = std::max((*this)(i,j),v(i,j));
162 }
163 }
164 }
165 #endif
166
167 /* Set each element to a random number in the range [min,max] */
168 void random(ELEMENT_ARG_TYPE min, ELEMENT_ARG_TYPE max) {
169 for(size_t i = 0; i < this->rows(); ++i) {
170 for(size_t j = 0; j < this->cols(); ++j) {
171 (*this)(i,j) = cml::random_real(min,max);
172 }
173 }
174 }
175
176
177 public:
178
179 /** Default constructor. */
180 matrix() {}
181
182 /** Constructor for dynamically-sized arrays.
183 *
184 * @param rows specify the number of rows.
185 * @param cols specify the number of cols.
186 */
187 explicit matrix(size_t rows, size_t cols)
188 : array_type(rows,cols) {}
189
190
191 public:
192
193 /** Return the matrix size as a pair. */
194 matrix_size size() const {
195 return matrix_size(this->rows(),this->cols());
196 }
197
198 /** Return element j of basis vector i. */
199 value_type basis_element(size_t i, size_t j) const {
200 return basis_element(i,j,basis_orient());
201 }
202
203 /** Set the given basis element. */
204 void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s) {
205 set_basis_element(i,j,s,basis_orient());
206 }
207
208
209 public:
210
211 /* Define common class operators: */
212
213 CML_CONSTRUCT_MAT_22
214 CML_CONSTRUCT_MAT_33
215 CML_CONSTRUCT_MAT_44
216
217 CML_MAT_COPY_FROM_ARRAY(: array_type())
218 CML_MAT_COPY_FROM_MATTYPE
219 CML_MAT_COPY_FROM_MAT
220 CML_MAT_COPY_FROM_MATXPR
221
222 CML_ASSIGN_MAT_22
223 CML_ASSIGN_MAT_33
224 CML_ASSIGN_MAT_44
225
226 CML_MAT_ASSIGN_FROM_MATTYPE
227
228 CML_MAT_ASSIGN_FROM_MAT(=, et::OpAssign)
229 CML_MAT_ASSIGN_FROM_MAT(+=, et::OpAddAssign)
230 CML_MAT_ASSIGN_FROM_MAT(-=, et::OpSubAssign)
231
232 CML_MAT_ASSIGN_FROM_MATXPR(=, et::OpAssign)
233 CML_MAT_ASSIGN_FROM_MATXPR(+=, et::OpAddAssign)
234 CML_MAT_ASSIGN_FROM_MATXPR(-=, et::OpSubAssign)
235
236 CML_MAT_ASSIGN_FROM_SCALAR(*=, et::OpMulAssign)
237 CML_MAT_ASSIGN_FROM_SCALAR(/=, et::OpDivAssign)
238
239 /** Accumulated matrix multiplication.
240 *
241 * This only makes sense for a square matrix, but no error will be
242 * signaled if the matrix is not square.
243 */
244 matrix_type& operator*=(const matrix_type& m) {
245 /* Matrix multiplication returns a temporary: */
246 *this = (*this)*m;
247 return *this;
248 }
249
250 /** Accumulated matrix multiplication.
251 *
252 * This only makes sense for a square matrix, but no error will be
253 * signaled if the matrix is not square.
254 */
255 template<typename E, class AT, typename BO, typename L> matrix_type&
256 operator*=(const matrix<E,AT,BO,L>& m) {
257 /* Matrix multiplication returns a temporary: */
258 *this = (*this)*m;
259 return *this;
260 }
261
262 /** Accumulated matrix multiplication.
263 *
264 * This only makes sense for a square matrix, but no error will be
265 * signaled if the matrix is not square.
266 */
267 template<class XprT> matrix_type&
268 operator*=(MATXPR_ARG_TYPE e) {
269 /* Verify that a promotion exists at compile time: */
270 typedef typename et::MatrixPromote<
271 matrix_type, typename XprT::result_type>::type result_type;
272 /* Matrix multiplication returns a temporary: */
273 *this = (*this)*e;
274 return *this;
275 }
276
277
278 protected:
279
280 value_type basis_element(size_t i, size_t j, row_basis) const {
281 return (*this)(i,j);
282 }
283
284 value_type basis_element(size_t i, size_t j, col_basis) const {
285 return (*this)(j,i);
286 }
287
288 void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, row_basis) {
289 (*this)(i,j) = s;
290 }
291
292 void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, col_basis) {
293 (*this)(j,i) = s;
294 }
295
296
297 public:
298
299 /* Braces should only be used for testing: */
300 #if defined(CML_ENABLE_MATRIX_BRACES)
301 CML_MATRIX_BRACE_OPERATORS
302 #endif
303 };
304
305 } // namespace cml
306
307 #endif
308
309 // -------------------------------------------------------------------------
310 // vim:ft=cpp
This page took 0.046211 seconds and 4 git commands to generate.