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 fixed_matrix_h
14 #define fixed_matrix_h
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>
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
>
31 /* Shorthand for the generator: */
32 typedef fixed
<Rows
,Cols
> generator_type
;
34 /* Shorthand for the array type: */
35 typedef fixed_2D
<Element
,Rows
,Cols
,Layout
> 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 typedef matrix_type
& expr_reference
;
52 typedef const matrix_type
& expr_const_reference
;
54 /* For matching by basis: */
55 typedef BasisOrient basis_orient
;
57 /* For matching by memory layout: */
58 typedef typename
array_type::layout layout
;
60 /* For matching by storage type if necessary: */
61 typedef typename
array_type::memory_tag memory_tag
;
63 /* For matching by size type if necessary: */
64 typedef typename
array_type::size_tag size_tag
;
66 /* For matching by result type: */
67 typedef cml::et::matrix_result_tag result_tag
;
69 /* For matching by assignability: */
70 typedef cml::et::assignable_tag assignable_tag
;
72 /* To simplify the matrix transpose operator: */
75 typename
array_type::transposed_type::generator_type
,
80 /* To simplify the matrix row and column operators: */
83 typename
array_type::row_array_type::generator_type
88 typename
array_type::col_array_type::generator_type
94 /** Set this matrix to zero. */
96 typedef cml::et::OpAssign
<Element
,Element
> OpT
;
97 cml::et::UnrollAssignment
<OpT
>(*this,Element(0));
101 /** Set this matrix to the identity.
103 * This only makes sense for a square matrix, but no error will be
104 * signaled if the matrix is not square.
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);
115 /** Set this matrix to its transpose.
117 * This only makes sense for a square matrix, but no error will be
118 * signaled if the matrix is not square.
120 matrix_type
& transpose() {
121 /* transpose() returns a temporary: */
122 *this = cml::transpose(*this);
126 /** Set this matrix to its inverse.
128 * This only makes sense for a square matrix, but no error will be
129 * signaled if the matrix is not square.
131 matrix_type
& inverse() {
132 /* inverse() returns a temporary: */
133 *this = cml::inverse(*this);
137 /* NOTE: minimize() and maximize() no longer supported (Jesse) */
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
));
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
));
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
);
175 /** Default constructor.
177 * @throws same as the ArrayType constructor.
186 /** Return the matrix size as a pair. */
187 matrix_size
size() const {
188 return matrix_size(this->rows(),this->cols());
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());
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());
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
];
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
];
214 /* Define common class operators: */
220 CML_MAT_COPY_FROM_FIXED_ARRAY(
221 array_type::array_rows
, array_type::array_cols
)
223 CML_MAT_COPY_FROM_MATTYPE
224 CML_MAT_COPY_FROM_MAT
225 CML_MAT_COPY_FROM_MATXPR
231 CML_MAT_ASSIGN_FROM_MATTYPE
233 CML_MAT_ASSIGN_FROM_MAT(=, et::OpAssign
)
234 CML_MAT_ASSIGN_FROM_MAT(+=, et::OpAddAssign
)
235 CML_MAT_ASSIGN_FROM_MAT(-=, et::OpSubAssign
)
237 CML_MAT_ASSIGN_FROM_MATXPR(=, et::OpAssign
)
238 CML_MAT_ASSIGN_FROM_MATXPR(+=, et::OpAddAssign
)
239 CML_MAT_ASSIGN_FROM_MATXPR(-=, et::OpSubAssign
)
241 CML_MAT_ASSIGN_FROM_SCALAR(*=, et::OpMulAssign
)
242 CML_MAT_ASSIGN_FROM_SCALAR(/=, et::OpDivAssign
)
244 CML_ACCUMULATED_MATRIX_MULT(const matrix_type
&)
246 template<typename E
, class AT
, typename BO
, typename L
>
247 CML_ACCUMULATED_MATRIX_MULT(const TEMPLATED_MATRIX_MACRO
&)
250 CML_ACCUMULATED_MATRIX_MULT(MATXPR_ARG_TYPE
)
255 value_type
basis_element(size_t i
, size_t j
, row_basis
) const {
259 value_type
basis_element(size_t i
, size_t j
, col_basis
) const {
263 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
, row_basis
) {
267 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
, col_basis
) {
274 /* Braces should only be used for testing: */
275 #if defined(CML_ENABLE_MATRIX_BRACES)
276 CML_MATRIX_BRACE_OPERATORS
284 // -------------------------------------------------------------------------