1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
13 #ifndef external_matrix_h
14 #define external_matrix_h
16 #include <cml/core/external_2D.h>
17 #include <cml/matrix/matrix_expr.h>
18 #include <cml/matrix/class_ops.h>
19 #include <cml/matrix/matrix_unroller.h>
20 #include <cml/matrix/dynamic.h>
24 /** Fixed-size, external-memory matrix. */
25 template<typename Element
, int Rows
, int Cols
,
26 typename BasisOrient
, typename Layout
>
27 class matrix
<Element
,external
<Rows
,Cols
>,BasisOrient
,Layout
>
28 : public external_2D
<Element
,Rows
,Cols
,Layout
>
32 /* Shorthand for the generator: */
33 typedef external
<Rows
,Cols
> generator_type
;
35 /* Shorthand for the array type: */
36 typedef external_2D
<Element
,Rows
,Cols
,Layout
> array_type
;
38 /* Shorthand for the type of this matrix: */
39 typedef matrix
<Element
,generator_type
,BasisOrient
,Layout
> matrix_type
;
41 /* For integration into the expression template code: */
42 typedef matrix_type expr_type
;
44 /* For integration into the expression template code: */
45 typedef matrix
<Element
,fixed
<Rows
,Cols
>,BasisOrient
,Layout
> temporary_type
;
46 /* Note: this ensures that an external matrix is copied into the proper
47 * temporary; external<> temporaries are not allowed.
51 typedef typename
array_type::value_type value_type
;
52 typedef typename
array_type::reference reference
;
53 typedef typename
array_type::const_reference const_reference
;
55 typedef matrix_type
& expr_reference
;
56 typedef const matrix_type
& expr_const_reference
;
58 /* For matching by basis: */
59 typedef BasisOrient basis_orient
;
61 /* For matching by memory layout: */
62 typedef typename
array_type::layout layout
;
64 /* For matching by storage type if necessary: */
65 typedef typename
array_type::memory_tag memory_tag
;
67 /* For matching by size type if necessary: */
68 typedef typename
array_type::size_tag size_tag
;
70 /* For matching by resizability: */
71 typedef typename
array_type::resizing_tag resizing_tag
;
73 /* For matching by result-type: */
74 typedef cml::et::matrix_result_tag result_tag
;
76 /* For matching by assignability: */
77 typedef cml::et::assignable_tag assignable_tag
;
79 /* To simplify the matrix transpose operator: */
82 typename
array_type::transposed_type::generator_type
,
87 /* To simplify the matrix row and column operators: */
90 typename
array_type::row_array_type::generator_type
95 typename
array_type::col_array_type::generator_type
101 /** Set this matrix to zero. */
102 matrix_type
& zero() {
103 typedef cml::et::OpAssign
<Element
,Element
> OpT
;
104 cml::et::UnrollAssignment
<OpT
>(*this,Element(0));
108 /** Set this matrix to the identity.
110 * This only makes sense for a square matrix, but no error will be
111 * signaled if the matrix is not square.
113 matrix_type
& identity() {
114 for(size_t i
= 0; i
< this->rows(); ++ i
) {
115 for(size_t j
= 0; j
< this->cols(); ++ j
) {
116 (*this)(i
,j
) = value_type((i
== j
)?1:0);
122 /** Set this matrix to its transpose.
124 * This only makes sense for a square matrix, but no error will be
125 * signaled if the matrix is not square.
127 matrix_type
& transpose() {
128 /* transpose() returns a temporary: */
129 *this = transpose(*this);
133 /** Set this matrix to its inverse.
135 * This only makes sense for a square matrix, but no error will be
136 * signaled if the matrix is not square.
138 matrix_type
& inverse() {
139 /* inverse() returns a temporary: */
140 *this = cml::inverse(*this);
144 /* NOTE: minimize() and maximize() no longer supported (Jesse) */
147 /** Pairwise minimum of this matrix with another. */
148 template<typename E
, class AT
, typename L
>
149 void minimize(const matrix
<E
,AT
,basis_orient
,L
>& v
) {
150 /* XXX This should probably use ScalarPromote: */
151 for (size_t i
= 0; i
< this->rows(); ++i
) {
152 for (size_t j
= 0; j
< this->cols(); ++j
) {
153 (*this)(i
,j
) = std::min((*this)(i
,j
),v(i
,j
));
158 /** Pairwise maximum of this matrix with another. */
159 template<typename E
, class AT
, typename L
>
160 void maximize(const matrix
<E
,AT
,basis_orient
,L
>& v
) {
161 /* XXX This should probably use ScalarPromote: */
162 for (size_t i
= 0; i
< this->rows(); ++i
) {
163 for (size_t j
= 0; j
< this->cols(); ++j
) {
164 (*this)(i
,j
) = std::max((*this)(i
,j
),v(i
,j
));
170 /* Set each element to a random number in the range [min,max] */
171 void random(ELEMENT_ARG_TYPE min
, ELEMENT_ARG_TYPE max
) {
172 for(size_t i
= 0; i
< this->rows(); ++i
) {
173 for(size_t j
= 0; j
< this->cols(); ++j
) {
174 (*this)(i
,j
) = random_real(min
,max
);
182 /** Constructor for fixed-size external matrices.
184 * The array must be given as a pointer to Element*, not a
185 * multi-dimensional array. The caller owns the pointer, and is
186 * responsible for doing any necessary memory management.
188 * @param ptr specify the external pointer.
190 * @throws same as the ArrayType constructor.
192 explicit matrix(value_type ptr
[Rows
][Cols
]) : array_type(ptr
) {}
194 /** Constructor for fixed-size external matrices.
196 * The array must be given as a pointer to Element*, not a
197 * multi-dimensional array. The caller owns the pointer, and is
198 * responsible for doing any necessary memory management.
200 * @param ptr specify the external pointer.
202 * @throws same as the ArrayType constructor.
204 explicit matrix(value_type
* ptr
) : array_type(ptr
) {}
209 /** Return the matrix size as a pair. */
210 matrix_size
size() const {
211 return matrix_size(this->rows(),this->cols());
214 /** Return element j of basis vector i. */
215 value_type
basis_element(size_t i
, size_t j
) const {
216 return basis_element(i
,j
,basis_orient());
219 /** Set the given basis element. */
220 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
) {
221 set_basis_element(i
,j
,s
,basis_orient());
231 /* Define class operators for external matrices. Note: external matrices
232 * cannot be copy-constructed, but they can be assigned to:
234 CML_MAT_ASSIGN_FROM_MATTYPE
236 CML_MAT_ASSIGN_FROM_MAT(=, et::OpAssign
)
237 CML_MAT_ASSIGN_FROM_MAT(+=, et::OpAddAssign
)
238 CML_MAT_ASSIGN_FROM_MAT(-=, et::OpSubAssign
)
240 CML_MAT_ASSIGN_FROM_MATXPR(=, et::OpAssign
)
241 CML_MAT_ASSIGN_FROM_MATXPR(+=, et::OpAddAssign
)
242 CML_MAT_ASSIGN_FROM_MATXPR(-=, et::OpSubAssign
)
244 CML_MAT_ASSIGN_FROM_SCALAR(*=, et::OpMulAssign
)
245 CML_MAT_ASSIGN_FROM_SCALAR(/=, et::OpDivAssign
)
247 CML_ACCUMULATED_MATRIX_MULT(const matrix_type
&)
249 template<typename E
, class AT
, typename BO
, typename L
>
250 CML_ACCUMULATED_MATRIX_MULT(const TEMPLATED_MATRIX_MACRO
&)
253 CML_ACCUMULATED_MATRIX_MULT(MATXPR_ARG_TYPE
)
258 value_type
basis_element(size_t i
, size_t j
, row_basis
) const {
262 value_type
basis_element(size_t i
, size_t j
, col_basis
) const {
266 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
, row_basis
) {
270 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
, col_basis
) {
277 /* Braces should only be used for testing: */
278 #if defined(CML_ENABLE_MATRIX_BRACES)
279 CML_MATRIX_BRACE_OPERATORS
283 /** Dynamic-size, external-memory matrix. */
284 template<typename Element
, typename BasisOrient
, typename Layout
>
285 class matrix
<Element
,external
<-1,-1>,BasisOrient
,Layout
>
286 : public external_2D
<Element
,-1,-1,Layout
>
290 /* Shorthand for the generator: */
291 typedef external
<> generator_type
;
293 /* Shorthand for the array type: */
294 typedef external_2D
<Element
,-1,-1,Layout
> array_type
;
296 /* Shorthand for the type of this matrix: */
297 typedef matrix
<Element
,generator_type
,BasisOrient
,Layout
> matrix_type
;
299 /* For integration into the expression template code: */
300 typedef matrix_type expr_type
;
302 /* For integration into the expression template code: */
303 typedef matrix
<Element
,dynamic
<>,BasisOrient
,Layout
> temporary_type
;
304 /* Note: this ensures that an external matrix is copied into the proper
305 * temporary; external<> temporaries are not allowed.
309 typedef typename
array_type::value_type value_type
;
310 typedef typename
array_type::reference reference
;
311 typedef typename
array_type::const_reference const_reference
;
313 typedef matrix_type
& expr_reference
;
314 typedef const matrix_type
& expr_const_reference
;
316 /* For matching by basis: */
317 typedef BasisOrient basis_orient
;
319 /* For matching by memory layout: */
320 typedef typename
array_type::layout layout
;
322 /* For matching by storage type if necessary: */
323 typedef typename
array_type::memory_tag memory_tag
;
325 /* For matching by size type if necessary: */
326 typedef typename
array_type::size_tag size_tag
;
328 /* For matching by resizability: */
329 typedef typename
array_type::resizing_tag resizing_tag
;
331 /* For matching by result-type: */
332 typedef cml::et::matrix_result_tag result_tag
;
334 /* For matching by assignability: */
335 typedef cml::et::assignable_tag assignable_tag
;
337 /* To simplify the matrix transpose operator: */
340 typename
array_type::transposed_type::generator_type
,
345 /* To simplify the matrix row and column operators: */
348 typename
array_type::row_array_type::generator_type
353 typename
array_type::col_array_type::generator_type
359 /** Set this matrix to zero. */
360 matrix_type
& zero() {
361 typedef cml::et::OpAssign
<Element
,Element
> OpT
;
362 cml::et::UnrollAssignment
<OpT
>(*this,Element(0));
366 /** Set this matrix to the identity.
368 * This only makes sense for a square matrix, but no error will be
369 * signaled if the matrix is not square.
371 matrix_type
& identity() {
372 for(size_t i
= 0; i
< this->rows(); ++ i
) {
373 for(size_t j
= 0; j
< this->cols(); ++ j
) {
374 (*this)(i
,j
) = value_type((i
== j
)?1:0);
380 /** Set this matrix to its transpose.
382 * This only makes sense for a square matrix, but no error will be
383 * signaled if the matrix is not square.
385 matrix_type
& transpose() {
386 /* transpose() returns a temporary: */
387 *this = cml::transpose(*this);
391 /** Set this matrix to its inverse.
393 * This only makes sense for a square matrix, but no error will be
394 * signaled if the matrix is not square.
396 matrix_type
& inverse() {
397 /* inverse() returns a temporary: */
398 *this = inverse(*this);
402 /** Pairwise minimum of this matrix with another. */
403 template<typename E
, class AT
, typename L
>
404 void minimize(const matrix
<E
,AT
,basis_orient
,L
>& v
) {
405 /* XXX This should probably use ScalarPromote: */
406 for (size_t i
= 0; i
< this->rows(); ++i
) {
407 for (size_t j
= 0; j
< this->cols(); ++j
) {
408 (*this)[i
] = std::min((*this)(i
,j
),v(i
,j
));
413 /** Pairwise maximum of this matrix with another. */
414 template<typename E
, class AT
, class BO
, typename L
>
415 void maximize(const matrix
<E
,AT
,basis_orient
,L
>& v
) {
416 /* XXX This should probably use ScalarPromote: */
417 for (size_t i
= 0; i
< this->rows(); ++i
) {
418 for (size_t j
= 0; j
< this->cols(); ++j
) {
419 (*this)[i
] = std::max((*this)(i
,j
),v(i
,j
));
424 /* Set each element to a random number in the range [min,max] */
425 void random(ELEMENT_ARG_TYPE min
, ELEMENT_ARG_TYPE max
) {
426 for(size_t i
= 0; i
< this->rows(); ++i
) {
427 for(size_t j
= 0; j
< this->cols(); ++j
) {
428 (*this)(i
,j
) = cml::random_real(min
,max
);
436 /** Constructor for fixed-size external matrices.
438 * The array must be given as a pointer to Element*, not a
439 * multi-dimensional array. The caller owns the pointer, and is
440 * responsible for doing any necessary memory management.
442 * @param ptr specify the external pointer.
444 * @throws same as the ArrayType constructor.
446 explicit matrix(value_type
* const ptr
, size_t rows
, size_t cols
)
447 : array_type(ptr
,rows
,cols
) {}
452 /** Return the matrix size as a pair. */
453 matrix_size
size() const {
454 return matrix_size(this->rows(),this->cols());
457 /** Return element j of basis vector i. */
458 value_type
basis_element(size_t i
, size_t j
) const {
459 return basis_element(i
,j
,basis_orient());
462 /** Set the given basis element. */
463 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
) {
464 set_basis_element(i
,j
,s
,basis_orient());
474 /* Define class operators for external matrices. Note: external matrices
475 * cannot be copy-constructed, but they can be assigned to:
477 CML_MAT_ASSIGN_FROM_MATTYPE
479 CML_MAT_ASSIGN_FROM_MAT(=, et::OpAssign
)
480 CML_MAT_ASSIGN_FROM_MAT(+=, et::OpAddAssign
)
481 CML_MAT_ASSIGN_FROM_MAT(-=, et::OpSubAssign
)
483 CML_MAT_ASSIGN_FROM_MATXPR(=, et::OpAssign
)
484 CML_MAT_ASSIGN_FROM_MATXPR(+=, et::OpAddAssign
)
485 CML_MAT_ASSIGN_FROM_MATXPR(-=, et::OpSubAssign
)
487 CML_MAT_ASSIGN_FROM_SCALAR(*=, et::OpMulAssign
)
488 CML_MAT_ASSIGN_FROM_SCALAR(/=, et::OpDivAssign
)
490 CML_ACCUMULATED_MATRIX_MULT(const matrix_type
&)
492 template<typename E
, class AT
, typename BO
, typename L
>
493 CML_ACCUMULATED_MATRIX_MULT(const TEMPLATED_MATRIX_MACRO
&)
496 CML_ACCUMULATED_MATRIX_MULT(MATXPR_ARG_TYPE
)
501 value_type
basis_element(size_t i
, size_t j
, row_basis
) const {
505 value_type
basis_element(size_t i
, size_t j
, col_basis
) const {
509 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
, row_basis
) {
513 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
, col_basis
) {
520 /* Braces should only be used for testing: */
521 #if defined(CML_ENABLE_MATRIX_BRACES)
522 CML_MATRIX_BRACE_OPERATORS
530 // -------------------------------------------------------------------------