]> Dogcows Code - chaz/yoink/blob - src/moof/cml/matrix/fixed.h
bugfix: win32 packaging script temp directories
[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 typename cml::remove_const<Element>::type,
75 typename array_type::transposed_type::generator_type,
76 BasisOrient, Layout
77 > transposed_type;
78
79 /* To simplify the matrix row and column operators: */
80 typedef vector<
81 Element,
82 typename array_type::row_array_type::generator_type
83 > row_vector_type;
84
85 typedef vector<
86 Element,
87 typename array_type::col_array_type::generator_type
88 > col_vector_type;
89
90
91 public:
92
93 /** Set this matrix to zero. */
94 matrix_type& zero() {
95 typedef cml::et::OpAssign<Element,Element> OpT;
96 cml::et::UnrollAssignment<OpT>(*this,Element(0));
97 return *this;
98 }
99
100 /** Set this matrix to the identity.
101 *
102 * This only makes sense for a square matrix, but no error will be
103 * signaled if the matrix is not square.
104 */
105 matrix_type& identity() {
106 for(size_t i = 0; i < this->rows(); ++ i) {
107 for(size_t j = 0; j < this->cols(); ++ j) {
108 (*this)(i,j) = value_type((i == j)?1:0);
109 }
110 }
111 return *this;
112 }
113
114 /** Set this matrix to its transpose.
115 *
116 * This only makes sense for a square matrix, but no error will be
117 * signaled if the matrix is not square.
118 */
119 matrix_type& transpose() {
120 /* transpose() returns a temporary: */
121 *this = cml::transpose(*this);
122 return *this;
123 }
124
125 /** Set this matrix to its inverse.
126 *
127 * This only makes sense for a square matrix, but no error will be
128 * signaled if the matrix is not square.
129 */
130 matrix_type& inverse() {
131 /* inverse() returns a temporary: */
132 *this = cml::inverse(*this);
133 return *this;
134 }
135
136 /* NOTE: minimize() and maximize() no longer supported (Jesse) */
137
138 #if 0
139 /** Pairwise minimum of this matrix with another. */
140 template<typename E, class AT, typename L>
141 void minimize(const matrix<E,AT,basis_orient,L>& v) {
142 /* XXX This should probably use ScalarPromote: */
143 for (size_t i = 0; i < this->rows(); ++i) {
144 for (size_t j = 0; j < this->cols(); ++j) {
145 (*this)(i,j) = std::min((*this)(i,j),v(i,j));
146 }
147 }
148 }
149
150 /** Pairwise maximum of this matrix with another. */
151 template<typename E, class AT, typename L>
152 void maximize(const matrix<E,AT,basis_orient,L>& v) {
153 /* XXX This should probably use ScalarPromote: */
154 for (size_t i = 0; i < this->rows(); ++i) {
155 for (size_t j = 0; j < this->cols(); ++j) {
156 (*this)(i,j) = std::max((*this)(i,j),v(i,j));
157 }
158 }
159 }
160 #endif
161
162 /* Set each element to a random number in the range [min,max] */
163 void random(ELEMENT_ARG_TYPE min, ELEMENT_ARG_TYPE max) {
164 for(size_t i = 0; i < this->rows(); ++i) {
165 for(size_t j = 0; j < this->cols(); ++j) {
166 (*this)(i,j) = cml::random_real(min,max);
167 }
168 }
169 }
170
171
172 public:
173
174 /** Default constructor.
175 *
176 * @throws same as the ArrayType constructor.
177 * @sa cml::fixed
178 * @sa cml::dynamic
179 */
180 matrix() {}
181
182
183 public:
184
185 /** Return the matrix size as a pair. */
186 matrix_size size() const {
187 return matrix_size(this->rows(),this->cols());
188 }
189
190 /** Return element j of basis vector i. */
191 value_type basis_element(size_t i, size_t j) const {
192 return basis_element(i,j,basis_orient());
193 }
194
195 /** Set the given basis element. */
196 void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s) {
197 set_basis_element(i,j,s,basis_orient());
198 }
199
200 /** Set the matrix row from the given vector. */
201 void set_row(size_t i, const row_vector_type& row) {
202 for(size_t j = 0; j < this->cols(); ++ j) (*this)(i,j) = row[j];
203 }
204
205 /** Set the matrix column from the given vector. */
206 void set_col(size_t j, const col_vector_type& col) {
207 for(size_t i = 0; i < this->rows(); ++ i) (*this)(i,j) = col[i];
208 }
209
210
211 public:
212
213 /* Define common class operators: */
214
215 CML_CONSTRUCT_MAT_22
216 CML_CONSTRUCT_MAT_33
217 CML_CONSTRUCT_MAT_44
218
219 CML_MAT_COPY_FROM_FIXED_ARRAY(
220 array_type::array_rows, array_type::array_cols)
221
222 CML_MAT_COPY_FROM_MATTYPE
223 CML_MAT_COPY_FROM_MAT
224 CML_MAT_COPY_FROM_MATXPR
225
226 CML_ASSIGN_MAT_22
227 CML_ASSIGN_MAT_33
228 CML_ASSIGN_MAT_44
229
230 CML_MAT_ASSIGN_FROM_MATTYPE
231
232 CML_MAT_ASSIGN_FROM_MAT(=, et::OpAssign)
233 CML_MAT_ASSIGN_FROM_MAT(+=, et::OpAddAssign)
234 CML_MAT_ASSIGN_FROM_MAT(-=, et::OpSubAssign)
235
236 CML_MAT_ASSIGN_FROM_MATXPR(=, et::OpAssign)
237 CML_MAT_ASSIGN_FROM_MATXPR(+=, et::OpAddAssign)
238 CML_MAT_ASSIGN_FROM_MATXPR(-=, et::OpSubAssign)
239
240 CML_MAT_ASSIGN_FROM_SCALAR(*=, et::OpMulAssign)
241 CML_MAT_ASSIGN_FROM_SCALAR(/=, et::OpDivAssign)
242
243 CML_ACCUMULATED_MATRIX_MULT(const matrix_type&)
244
245 template<typename E, class AT, typename BO, typename L>
246 CML_ACCUMULATED_MATRIX_MULT(const TEMPLATED_MATRIX_MACRO&)
247
248 template<class XprT>
249 CML_ACCUMULATED_MATRIX_MULT(MATXPR_ARG_TYPE)
250
251
252 protected:
253
254 value_type basis_element(size_t i, size_t j, row_basis) const {
255 return (*this)(i,j);
256 }
257
258 value_type basis_element(size_t i, size_t j, col_basis) const {
259 return (*this)(j,i);
260 }
261
262 void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, row_basis) {
263 (*this)(i,j) = s;
264 }
265
266 void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, col_basis) {
267 (*this)(j,i) = s;
268 }
269
270
271 public:
272
273 /* Braces should only be used for testing: */
274 #if defined(CML_ENABLE_MATRIX_BRACES)
275 CML_MATRIX_BRACE_OPERATORS
276 #endif
277 };
278
279 } // namespace cml
280
281 #endif
282
283 // -------------------------------------------------------------------------
284 // vim:ft=cpp
This page took 0.04495 seconds and 4 git commands to generate.