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 dynamic_matrix_h
14 #define dynamic_matrix_h
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>
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
>
31 /* Shorthand for the generator: */
32 typedef dynamic
<Alloc
> generator_type
;
34 /* Shorthand for the array type: */
35 typedef dynamic_2D
<Element
,Layout
,Alloc
> array_type
;
37 /* Shorthand for the type of this matrix: */
38 typedef matrix
<Element
,generator_type
,BasisOrient
,Layout
> matrix_type
;
40 /* For integration into the expression template code: */
41 typedef matrix_type expr_type
;
43 /* For integration into the expression template code: */
44 typedef matrix_type temporary_type
;
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
;
51 /* For integration into the expression templates code: */
52 typedef matrix_type
& expr_reference
;
53 typedef const matrix_type
& expr_const_reference
;
55 /* For matching by basis: */
56 typedef BasisOrient basis_orient
;
58 /* For matching by memory layout: */
59 typedef typename
array_type::layout layout
;
61 /* For matching by storage type: */
62 typedef typename
array_type::memory_tag memory_tag
;
64 /* For matching by size type if necessary: */
65 typedef typename
array_type::size_tag size_tag
;
67 /* For matching by resizability: */
68 typedef typename
array_type::resizing_tag resizing_tag
;
70 /* For matching by result type: */
71 typedef cml::et::matrix_result_tag result_tag
;
73 /* For matching by assignability: */
74 typedef cml::et::assignable_tag assignable_tag
;
76 /* To simplify the matrix transpose operator: */
79 typename
array_type::transposed_type::generator_type
,
84 /* To simplify the matrix row and column operators: */
87 typename
array_type::row_array_type::generator_type
92 typename
array_type::col_array_type::generator_type
98 /** Set this matrix to zero. */
100 typedef cml::et::OpAssign
<Element
,Element
> OpT
;
101 cml::et::UnrollAssignment
<OpT
>(*this,Element(0));
105 /** Set this matrix to the identity.
107 * This only makes sense for a square matrix, but no error will be
108 * signaled if the matrix is not square.
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);
119 /** Set this matrix to its transpose.
121 * This only makes sense for a square matrix, but no error will be
122 * signaled if the matrix is not square.
124 matrix_type
& transpose() {
125 /* transpose() returns a temporary: */
126 *this = cml::transpose(*this);
130 /** Set this matrix to its inverse.
132 * This only makes sense for a square matrix, but no error will be
133 * signaled if the matrix is not square.
135 matrix_type
& inverse() {
136 /* inverse() returns a temporary: */
137 *this = cml::inverse(*this);
141 /* NOTE: minimize() and maximize() no longer supported (Jesse) */
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
));
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
));
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
);
179 /** Default constructor. */
182 /** Constructor for dynamically-sized arrays.
184 * @param rows specify the number of rows.
185 * @param cols specify the number of cols.
187 explicit matrix(size_t rows
, size_t cols
)
188 : array_type(rows
,cols
) {}
193 /** Return the matrix size as a pair. */
194 matrix_size
size() const {
195 return matrix_size(this->rows(),this->cols());
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());
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());
208 /** Set the matrix row from the given vector. */
209 void set_row(size_t i
, const row_vector_type
& row
) {
210 for(size_t j
= 0; j
< this->cols(); ++ j
) (*this)(i
,j
) = row
[j
];
213 /** Set the matrix column from the given vector. */
214 void set_col(size_t j
, const col_vector_type
& col
) {
215 for(size_t i
= 0; i
< this->rows(); ++ i
) (*this)(i
,j
) = col
[i
];
221 /* Define common class operators: */
227 CML_MAT_COPY_FROM_ARRAY(: array_type())
228 CML_MAT_COPY_FROM_MATTYPE
229 CML_MAT_COPY_FROM_MAT
230 CML_MAT_COPY_FROM_MATXPR
236 CML_MAT_ASSIGN_FROM_MATTYPE
238 CML_MAT_ASSIGN_FROM_MAT(=, et::OpAssign
)
239 CML_MAT_ASSIGN_FROM_MAT(+=, et::OpAddAssign
)
240 CML_MAT_ASSIGN_FROM_MAT(-=, et::OpSubAssign
)
242 CML_MAT_ASSIGN_FROM_MATXPR(=, et::OpAssign
)
243 CML_MAT_ASSIGN_FROM_MATXPR(+=, et::OpAddAssign
)
244 CML_MAT_ASSIGN_FROM_MATXPR(-=, et::OpSubAssign
)
246 CML_MAT_ASSIGN_FROM_SCALAR(*=, et::OpMulAssign
)
247 CML_MAT_ASSIGN_FROM_SCALAR(/=, et::OpDivAssign
)
249 /** Accumulated matrix multiplication.
251 * This only makes sense for a square matrix, but no error will be
252 * signaled if the matrix is not square.
254 matrix_type
& operator*=(const matrix_type
& m
) {
255 /* Matrix multiplication returns a temporary: */
260 /** Accumulated matrix multiplication.
262 * This only makes sense for a square matrix, but no error will be
263 * signaled if the matrix is not square.
265 template<typename E
, class AT
, typename BO
, typename L
> matrix_type
&
266 operator*=(const matrix
<E
,AT
,BO
,L
>& m
) {
267 /* Matrix multiplication returns a temporary: */
272 /** Accumulated matrix multiplication.
274 * This only makes sense for a square matrix, but no error will be
275 * signaled if the matrix is not square.
277 template<class XprT
> matrix_type
&
278 operator*=(MATXPR_ARG_TYPE e
) {
279 /* Verify that a promotion exists at compile time: */
280 typedef typename
et::MatrixPromote
<
281 matrix_type
, typename
XprT::result_type
>::type result_type
;
282 /* Matrix multiplication returns a temporary: */
290 value_type
basis_element(size_t i
, size_t j
, row_basis
) const {
294 value_type
basis_element(size_t i
, size_t j
, col_basis
) const {
298 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
, row_basis
) {
302 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
, col_basis
) {
309 /* Braces should only be used for testing: */
310 #if defined(CML_ENABLE_MATRIX_BRACES)
311 CML_MATRIX_BRACE_OPERATORS
319 // -------------------------------------------------------------------------