]>
Dogcows Code - chaz/yoink/blob - cml/matrix/class_ops.h
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 *-----------------------------------------------------------------------*/
12 * @note GCC4 requires a copy constructor to elide---it won't elide a
13 * compiler-generated copy constructor!
16 #ifndef matrix_class_ops_h
17 #define matrix_class_ops_h
19 #if defined(_MSC_VER) && _MSC_VER < 1400
20 #pragma warning(disable:4003)
21 // XXX Horrible hack to turn off warnings about "not enough actual params"
22 // for the macros below.
25 /* This is to circumvent the problem of commas in a macro argument. It's
26 * used to instantiate CML_ACCUMULATED_MATRIX_MULT:
28 #define TEMPLATED_MATRIX_MACRO matrix<E,AT,BO,L>
30 /* XXX HACK!!! This is a hack to resize in the assign() functions only when
31 * auto resizing is turned off.
33 #if !defined(CML_MATRIX_RESIZE_ON_ASSIGNMENT)
34 #define _DO_MATRIX_SET_RESIZE(_R_,_C_) cml::et::detail::Resize(*this,_R_,_C_)
36 #define _DO_MATRIX_SET_RESIZE(_R_,_C_)
39 /** Set a matrix from 2x2 values.
41 * The layout assumed for the values is that of the matrix being assigned.
43 #define CML_ASSIGN_MAT_22 \
46 ELEMENT_ARG_TYPE e00, ELEMENT_ARG_TYPE e01, \
47 ELEMENT_ARG_TYPE e10, ELEMENT_ARG_TYPE e11 \
50 _DO_MATRIX_SET_RESIZE(2,2); \
51 /* This is overkill, but simplifies size checking: */ \
52 value_type v[2][2] = {{e00,e01},{e10,e11}}; \
53 typedef et::OpAssign<Element,Element> OpT; \
54 typedef const value_type element; \
55 cml::matrix<element, external<2,2>, basis_orient, row_major> \
57 et::UnrollAssignment<OpT>(*this,src); \
61 /** Create a matrix from 3x3 values.
63 * The layout assumed for the values is that of the matrix being assigned.
65 #define CML_ASSIGN_MAT_33 \
68 ELEMENT_ARG_TYPE e00, ELEMENT_ARG_TYPE e01, ELEMENT_ARG_TYPE e02, \
69 ELEMENT_ARG_TYPE e10, ELEMENT_ARG_TYPE e11, ELEMENT_ARG_TYPE e12, \
70 ELEMENT_ARG_TYPE e20, ELEMENT_ARG_TYPE e21, ELEMENT_ARG_TYPE e22 \
73 _DO_MATRIX_SET_RESIZE(3,3); \
74 /* This is overkill, but simplifies size checking: */ \
75 value_type v[3][3] = { \
80 typedef et::OpAssign<Element,Element> OpT; \
81 typedef const value_type element; \
82 cml::matrix<element, external<3,3>, basis_orient, row_major> \
84 et::UnrollAssignment<OpT>(*this,src); \
88 /** Create a matrix from 4x4 values.
90 * The layout assumed for the values is that of the matrix being assigned.
92 #define CML_ASSIGN_MAT_44 \
95 ELEMENT_ARG_TYPE e00, ELEMENT_ARG_TYPE e01, \
96 ELEMENT_ARG_TYPE e02, ELEMENT_ARG_TYPE e03, \
97 ELEMENT_ARG_TYPE e10, ELEMENT_ARG_TYPE e11, \
98 ELEMENT_ARG_TYPE e12, ELEMENT_ARG_TYPE e13, \
99 ELEMENT_ARG_TYPE e20, ELEMENT_ARG_TYPE e21, \
100 ELEMENT_ARG_TYPE e22, ELEMENT_ARG_TYPE e23, \
101 ELEMENT_ARG_TYPE e30, ELEMENT_ARG_TYPE e31, \
102 ELEMENT_ARG_TYPE e32, ELEMENT_ARG_TYPE e33 \
105 _DO_MATRIX_SET_RESIZE(4,4); \
106 /* This is overkill, but simplifies size checking: */ \
107 value_type v[4][4] = { \
113 typedef et::OpAssign<Element,Element> OpT; \
114 typedef const value_type element; \
115 cml::matrix<element, external<4,4>, basis_orient, row_major> \
117 et::UnrollAssignment<OpT>(*this,src); \
121 /** Create a matrix from 2x2 values.
123 * The layout assumed for the values is that of the matrix being assigned.
125 #define CML_CONSTRUCT_MAT_22 \
127 ELEMENT_ARG_TYPE e00, ELEMENT_ARG_TYPE e01, \
128 ELEMENT_ARG_TYPE e10, ELEMENT_ARG_TYPE e11 \
137 /** Create a matrix from 3x3 values.
139 * The layout assumed for the values is that of the matrix being assigned.
141 #define CML_CONSTRUCT_MAT_33 \
143 ELEMENT_ARG_TYPE e00, ELEMENT_ARG_TYPE e01, ELEMENT_ARG_TYPE e02, \
144 ELEMENT_ARG_TYPE e10, ELEMENT_ARG_TYPE e11, ELEMENT_ARG_TYPE e12, \
145 ELEMENT_ARG_TYPE e20, ELEMENT_ARG_TYPE e21, ELEMENT_ARG_TYPE e22 \
155 /** Create a matrix from 4x4 values.
157 * The layout assumed for the values is that of the matrix being assigned.
159 #define CML_CONSTRUCT_MAT_44 \
161 ELEMENT_ARG_TYPE e00, ELEMENT_ARG_TYPE e01, \
162 ELEMENT_ARG_TYPE e02, ELEMENT_ARG_TYPE e03, \
163 ELEMENT_ARG_TYPE e10, ELEMENT_ARG_TYPE e11, \
164 ELEMENT_ARG_TYPE e12, ELEMENT_ARG_TYPE e13, \
165 ELEMENT_ARG_TYPE e20, ELEMENT_ARG_TYPE e21, \
166 ELEMENT_ARG_TYPE e22, ELEMENT_ARG_TYPE e23, \
167 ELEMENT_ARG_TYPE e30, ELEMENT_ARG_TYPE e31, \
168 ELEMENT_ARG_TYPE e32, ELEMENT_ARG_TYPE e33 \
179 /** Copy-construct a matrix from a fixed-size array of values. */
180 #define CML_MAT_COPY_FROM_FIXED_ARRAY(_R_,_C_) \
181 matrix(const value_type m[_R_][_C_]) { \
182 typedef et::OpAssign<Element,Element> OpT; \
183 cml::matrix<const value_type, external<_R_,_C_>, \
184 basis_orient, row_major> src(&m[0][0]); \
185 et::UnrollAssignment<OpT>(*this,src); \
188 /** Copy-construct a matrix from a runtime-sized array of values. */
189 #define CML_MAT_COPY_FROM_ARRAY(_add_) \
190 matrix(const value_type* const v, size_t R, size_t C) _add_ { \
191 typedef et::OpAssign<Element,Element> OpT; \
192 cml::matrix<value_type, external<>, basis_orient, \
193 row_major > src(const_cast<value_type*>(v),R,C); \
194 et::UnrollAssignment<OpT>(*this,src); \
197 /** Copy this matrix from another using the given elementwise op.
199 * @internal This is required for GCC4, since it won't elide the default
202 #define CML_MAT_COPY_FROM_MATTYPE \
203 matrix(const matrix_type& m) : array_type() { \
204 typedef et::OpAssign <Element,Element> OpT; \
205 et::UnrollAssignment<OpT>(*this,m); \
208 /** Copy this matrix from another using the given elementwise op.
210 * This allows copies from arbitrary matrix types.
212 #define CML_MAT_COPY_FROM_MAT \
213 template<typename E, class AT, typename BO, typename L> \
214 matrix(const TEMPLATED_MATRIX_MACRO& m) { \
215 typedef et::OpAssign <Element,E> OpT; \
216 et::UnrollAssignment<OpT>(*this,m); \
219 /** Declare a function to copy this matrix from a matrix expression. */
220 #define CML_MAT_COPY_FROM_MATXPR \
221 template<class XprT> \
222 matrix(MATXPR_ARG_TYPE e) { \
223 /* Verify that a promotion exists at compile time: */ \
224 typedef typename et::MatrixPromote< \
225 matrix_type, typename XprT::result_type>::type result_type; \
226 typedef typename XprT::value_type src_value_type; \
227 typedef et::OpAssign <Element,src_value_type> OpT; \
228 et::UnrollAssignment<OpT>(*this,e); \
231 #if defined(CML_USE_GENERATED_MATRIX_ASSIGN_OP)
232 #define CML_MAT_ASSIGN_FROM_MATTYPE
234 /** Assign from the same matrix type.
236 * @param m the matrix to copy from.
238 * @note This is required for GCC4, otherwise it generates a slow
239 * assignment operator using memcpy.
241 * @note ICC9/Linux-x86 seems to prefer its own assignment operator (need
242 * to figure out why).
244 #define CML_MAT_ASSIGN_FROM_MATTYPE \
245 matrix_type& operator=(const matrix_type& m) { \
246 typedef et::OpAssign<Element,Element> OpT; \
247 et::UnrollAssignment<OpT>(*this,m); \
253 /** Assign this matrix from another using the given elementwise op.
255 * This allows assignment from arbitrary matrix types.
257 * @param _op_ the operator (e.g. +=)
258 * @param _op_name_ the op functor (e.g. et::OpAssign)
260 #define CML_MAT_ASSIGN_FROM_MAT(_op_, _op_name_) \
261 template<typename E, class AT, typename BO, typename L> matrix_type& \
262 operator _op_ (const TEMPLATED_MATRIX_MACRO& m) { \
263 typedef _op_name_ <Element,E> OpT; \
264 et::UnrollAssignment<OpT>(*this,m); \
268 /** Declare a function to assign this matrix from a matrix expression.
270 * @param _op_ the operator (e.g. +=)
271 * @param _op_name_ the op functor (e.g. et::OpAssign)
273 #define CML_MAT_ASSIGN_FROM_MATXPR(_op_, _op_name_) \
274 template<class XprT> matrix_type& \
275 operator _op_ (MATXPR_ARG_TYPE e) { \
276 /* Verify that a promotion exists at compile time: */ \
277 typedef typename et::MatrixPromote< \
278 matrix_type, typename XprT::result_type>::type result_type; \
279 typedef typename XprT::value_type src_value_type; \
280 typedef _op_name_ <Element,src_value_type> OpT; \
281 et::UnrollAssignment<OpT>(*this,e); \
285 /** Declare a function to assign this matrix from a scalar.
287 * @param _op_ the operator (e.g. +=)
288 * @param _op_name_ the op functor (e.g. et::OpAssign)
290 * @internal This shouldn't be used for ops, like +=, which aren't
291 * defined in vector algebra.
293 #define CML_MAT_ASSIGN_FROM_SCALAR(_op_, _op_name_) \
294 matrix_type& operator _op_ (ELEMENT_ARG_TYPE s) { \
295 typedef _op_name_ <Element,value_type> OpT; \
296 et::UnrollAssignment<OpT>(*this,s); \
301 /** Accumulated matrix multiplication.
303 * @throws std::invalid_argument if the matrices are not square.
305 #define CML_ACCUMULATED_MATRIX_MULT(_arg_type_) \
306 matrix_type& operator*=(_arg_type_ m) { \
307 typedef typename et::MatrixPromote< \
308 matrix_type, _arg_type_>::type result_type; \
309 cml::et::CheckedSquare(*this, typename result_type::size_tag()); \
310 return (*this = (*this)*m); \
314 /* These should only be used for testing: */
315 #define CML_MATRIX_BRACE_OPERATORS \
316 template<class Matrix> struct row_ref { \
317 typedef typename Matrix::reference reference; \
318 reference operator[](size_t col) { return m(row,col); } \
323 template<class Matrix> struct const_row_ref { \
324 typedef typename Matrix::const_reference const_reference; \
325 const_reference operator[](size_t col) const { return m(row,col); } \
330 row_ref<matrix_type> operator[](size_t row) { \
331 row_ref<matrix_type> ref = { *this, row }; return ref; \
334 const_row_ref<matrix_type> operator[](size_t row) const { \
335 const_row_ref<matrix_type> ref = { *this, row }; return ref; \
340 // -------------------------------------------------------------------------
This page took 0.049634 seconds and 4 git commands to generate.