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: */
74 typename
cml::remove_const
<Element
>::type
,
75 typename
array_type::transposed_type::generator_type
,
79 /* To simplify the matrix row and column operators: */
82 typename
array_type::row_array_type::generator_type
87 typename
array_type::col_array_type::generator_type
93 /** Set this matrix to zero. */
95 typedef cml::et::OpAssign
<Element
,Element
> OpT
;
96 cml::et::UnrollAssignment
<OpT
>(*this,Element(0));
100 /** Set this matrix to the identity.
102 * This only makes sense for a square matrix, but no error will be
103 * signaled if the matrix is not square.
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);
114 /** Set this matrix to its transpose.
116 * This only makes sense for a square matrix, but no error will be
117 * signaled if the matrix is not square.
119 matrix_type
& transpose() {
120 /* transpose() returns a temporary: */
121 *this = cml::transpose(*this);
125 /** Set this matrix to its inverse.
127 * This only makes sense for a square matrix, but no error will be
128 * signaled if the matrix is not square.
130 matrix_type
& inverse() {
131 /* inverse() returns a temporary: */
132 *this = cml::inverse(*this);
136 /* NOTE: minimize() and maximize() no longer supported (Jesse) */
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
));
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
));
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
);
174 /** Default constructor.
176 * @throws same as the ArrayType constructor.
185 /** Return the matrix size as a pair. */
186 matrix_size
size() const {
187 return matrix_size(this->rows(),this->cols());
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());
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());
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
];
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
];
213 /* Define common class operators: */
219 CML_MAT_COPY_FROM_FIXED_ARRAY(
220 array_type::array_rows
, array_type::array_cols
)
222 CML_MAT_COPY_FROM_MATTYPE
223 CML_MAT_COPY_FROM_MAT
224 CML_MAT_COPY_FROM_MATXPR
230 CML_MAT_ASSIGN_FROM_MATTYPE
232 CML_MAT_ASSIGN_FROM_MAT(=, et::OpAssign
)
233 CML_MAT_ASSIGN_FROM_MAT(+=, et::OpAddAssign
)
234 CML_MAT_ASSIGN_FROM_MAT(-=, et::OpSubAssign
)
236 CML_MAT_ASSIGN_FROM_MATXPR(=, et::OpAssign
)
237 CML_MAT_ASSIGN_FROM_MATXPR(+=, et::OpAddAssign
)
238 CML_MAT_ASSIGN_FROM_MATXPR(-=, et::OpSubAssign
)
240 CML_MAT_ASSIGN_FROM_SCALAR(*=, et::OpMulAssign
)
241 CML_MAT_ASSIGN_FROM_SCALAR(/=, et::OpDivAssign
)
243 CML_ACCUMULATED_MATRIX_MULT(const matrix_type
&)
245 template<typename E
, class AT
, typename BO
, typename L
>
246 CML_ACCUMULATED_MATRIX_MULT(const TEMPLATED_MATRIX_MACRO
&)
249 CML_ACCUMULATED_MATRIX_MULT(MATXPR_ARG_TYPE
)
254 value_type
basis_element(size_t i
, size_t j
, row_basis
) const {
258 value_type
basis_element(size_t i
, size_t j
, col_basis
) const {
262 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
, row_basis
) {
266 void set_basis_element(size_t i
, size_t j
, ELEMENT_ARG_TYPE s
, col_basis
) {
273 /* Braces should only be used for testing: */
274 #if defined(CML_ENABLE_MATRIX_BRACES)
275 CML_MATRIX_BRACE_OPERATORS
283 // -------------------------------------------------------------------------