1 /*
2  * Tiny Vector Matrix Library
3  * Dense Vector Matrix Libary of Tiny size using Expression Templates
4  *
5  * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * $Id: MatrixEval.h,v 1.18 2007-06-23 15:58:58 opetzold Exp $
22  */
23 
24 #ifndef TVMET_MATRIX_EVAL_H
25 #define TVMET_MATRIX_EVAL_H
26 
27 namespace tvmet {
28 
29 
30 /**
31  * \fn bool all_elements(const XprMatrix<E, Rows, Cols>& e)
32  * \brief check on statements for all elements
33  * \ingroup _unary_function
34  * This is for use with boolean operators like
35  * \par Example:
36  * \code
37  * all_elements(matrix > 0) {
38  *     // true branch
39  * } else {
40  *     // false branch
41  * }
42  * \endcode
43  * \sa \ref compare
44  */
45 template<class E, std::size_t Rows, std::size_t Cols>
46 inline
all_elements(const XprMatrix<E,Rows,Cols> & e)47 bool all_elements(const XprMatrix<E, Rows, Cols>& e) {
48   return meta::Matrix<Rows, Cols, 0, 0>::all_elements(e);
49 }
50 
51 
52 /**
53  * \fn bool any_elements(const XprMatrix<E, Rows, Cols>& e)
54  * \brief check on statements for any elements
55  * \ingroup _unary_function
56  * This is for use with boolean operators like
57  * \par Example:
58  * \code
59  * any_elements(matrix > 0) {
60  *     // true branch
61  * } else {
62  *     // false branch
63  * }
64  * \endcode
65  * \sa \ref compare
66  */
67 template<class E, std::size_t Rows, std::size_t Cols>
68 inline
any_elements(const XprMatrix<E,Rows,Cols> & e)69 bool any_elements(const XprMatrix<E, Rows, Cols>& e) {
70   return meta::Matrix<Rows, Cols, 0, 0>::any_elements(e);
71 }
72 
73 
74 /*
75  * trinary evaluation functions with matrizes and xpr of
76  *
77  * XprMatrix<E1, Rows, Cols> ? Matrix<T2, Rows, Cols> : Matrix<T3, Rows, Cols>
78  * XprMatrix<E1, Rows, Cols> ? Matrix<T2, Rows, Cols> : XprMatrix<E3, Rows, Cols>
79  * XprMatrix<E1, Rows, Cols> ? XprMatrix<E2, Rows, Cols> : Matrix<T3, Rows, Cols>
80  * XprMatrix<E1, Rows, Cols> ? XprMatrix<E2, Rows, Cols> : XprMatrix<E3, Rows, Cols>
81  */
82 
83 /**
84  * \fn eval(const XprMatrix<E1, Rows, Cols>& e1, const Matrix<T2, Rows, Cols>& m2, const Matrix<T3, Rows, Cols>& m3)
85  * \brief Evals the matrix expressions.
86  * \ingroup _trinary_function
87  * This eval is for the a?b:c syntax, since it's not allowed to overload
88  * these operators.
89  */
90 template<class E1, class T2, class T3, std::size_t Rows, std::size_t Cols>
91 inline
92 XprMatrix<
93   XprEval<
94     XprMatrix<E1, Rows, Cols>,
95     MatrixConstReference<T2, Rows, Cols>,
96     MatrixConstReference<T3, Rows, Cols>
97   >,
98   Rows, Cols
99 >
eval(const XprMatrix<E1,Rows,Cols> & e1,const Matrix<T2,Rows,Cols> & m2,const Matrix<T3,Rows,Cols> & m3)100 eval(const XprMatrix<E1, Rows, Cols>& e1,
101      const Matrix<T2, Rows, Cols>& m2,
102      const Matrix<T3, Rows, Cols>& m3) {
103   typedef XprEval<
104     XprMatrix<E1, Rows, Cols>,
105     MatrixConstReference<T2, Rows, Cols>,
106     MatrixConstReference<T3, Rows, Cols>
107   > 							expr_type;
108   return XprMatrix<expr_type, Rows, Cols>(
109     expr_type(e1, m2.const_ref(), m3.const_ref()));
110 }
111 
112 
113 /**
114  * \fn eval(const XprMatrix<E1, Rows, Cols>& e1, const Matrix<T2, Rows, Cols>& m2, const XprMatrix<E3, Rows, Cols>& e3)
115  * \brief Evals the matrix expressions.
116  * \ingroup _trinary_function
117  * This eval is for the a?b:c syntax, since it's not allowed to overload
118  * these operators.
119  */
120 template<class E1, class T2, class E3, std::size_t Rows, std::size_t Cols>
121 inline
122 XprMatrix<
123   XprEval<
124     XprMatrix<E1, Rows, Cols>,
125     MatrixConstReference<T2, Rows, Cols>,
126     XprMatrix<E3, Rows, Cols>
127   >,
128   Rows, Cols
129 >
eval(const XprMatrix<E1,Rows,Cols> & e1,const Matrix<T2,Rows,Cols> & m2,const XprMatrix<E3,Rows,Cols> & e3)130 eval(const XprMatrix<E1, Rows, Cols>& e1,
131      const Matrix<T2, Rows, Cols>& m2,
132      const XprMatrix<E3, Rows, Cols>& e3) {
133   typedef XprEval<
134     XprMatrix<E1, Rows, Cols>,
135     MatrixConstReference<T2, Rows, Cols>,
136     XprMatrix<E3, Rows, Cols>
137   > 							expr_type;
138   return XprMatrix<expr_type, Rows, Cols>(
139     expr_type(e1, m2.const_ref(), e3));
140 }
141 
142 
143 /**
144  * \fn eval(const XprMatrix<E1, Rows, Cols>& e1, const XprMatrix<E2, Rows, Cols>& e2, const Matrix<T3, Rows, Cols>& m3)
145  * \brief Evals the matrix expressions.
146  * \ingroup _trinary_function
147  * This eval is for the a?b:c syntax, since it's not allowed to overload
148  * these operators.
149  */
150 template<class E1, class E2, class T3, std::size_t Rows, std::size_t Cols>
151 inline
152 XprMatrix<
153   XprEval<
154     XprMatrix<E1, Rows, Cols>,
155     XprMatrix<E2, Rows, Cols>,
156     MatrixConstReference<T3, Rows, Cols>
157   >,
158   Rows, Cols
159 >
eval(const XprMatrix<E1,Rows,Cols> & e1,const XprMatrix<E2,Rows,Cols> & e2,const Matrix<T3,Rows,Cols> & m3)160 eval(const XprMatrix<E1, Rows, Cols>& e1,
161     const  XprMatrix<E2, Rows, Cols>& e2,
162      const Matrix<T3, Rows, Cols>& m3) {
163   typedef XprEval<
164     XprMatrix<E1, Rows, Cols>,
165     XprMatrix<E2, Rows, Cols>,
166     MatrixConstReference<T3, Rows, Cols>
167   > 							expr_type;
168   return XprMatrix<expr_type, Rows, Cols>(
169     expr_type(e1, e2, m3.const_ref()));
170 }
171 
172 
173 /**
174  * \fn eval(const XprMatrix<E1, Rows, Cols>& e1, const XprMatrix<E2, Rows, Cols>& e2, const XprMatrix<E3, Rows, Cols>& e3)
175  * \brief Evals the matrix expressions.
176  * \ingroup _trinary_function
177  * This eval is for the a?b:c syntax, since it's not allowed to overload
178  * these operators.
179  */
180 template<class E1, class E2, class E3, std::size_t Rows, std::size_t Cols>
181 inline
182 XprMatrix<
183   XprEval<
184     XprMatrix<E1, Rows, Cols>,
185     XprMatrix<E2, Rows, Cols>,
186     XprMatrix<E3, Rows, Cols>
187   >,
188   Rows, Cols
189 >
eval(const XprMatrix<E1,Rows,Cols> & e1,const XprMatrix<E2,Rows,Cols> & e2,const XprMatrix<E3,Rows,Cols> & e3)190 eval(const XprMatrix<E1, Rows, Cols>& e1,
191      const XprMatrix<E2, Rows, Cols>& e2,
192      const XprMatrix<E3, Rows, Cols>& e3) {
193   typedef XprEval<
194     XprMatrix<E1, Rows, Cols>,
195     XprMatrix<E2, Rows, Cols>,
196     XprMatrix<E3, Rows, Cols>
197   > 							expr_type;
198   return XprMatrix<expr_type, Rows, Cols>(expr_type(e1, e2, e3));
199 }
200 
201 
202 /*
203  * trinary evaluation functions with matrizes, xpr of and POD
204  *
205  * XprMatrix<E, Rows, Cols> ? POD1 : POD2
206  * XprMatrix<E1, Rows, Cols> ? POD : XprMatrix<E3, Rows, Cols>
207  * XprMatrix<E1, Rows, Cols> ? XprMatrix<E2, Rows, Cols> : POD
208  */
209 #define TVMET_IMPLEMENT_MACRO(POD)               			\
210 template<class E, std::size_t Rows, std::size_t Cols>			\
211 inline               							\
212 XprMatrix<               						\
213   XprEval<               						\
214     XprMatrix<E, Rows, Cols>,               			      	\
215     XprLiteral< POD >,               					\
216     XprLiteral< POD >               					\
217   >,                							\
218   Rows, Cols								\
219 >               							\
220 eval(const XprMatrix<E, Rows, Cols>& e, POD x2, POD x3) {      		\
221   typedef XprEval<               					\
222     XprMatrix<E, Rows, Cols>,               				\
223     XprLiteral< POD >,                					\
224     XprLiteral< POD >                					\
225   > 							expr_type; 	\
226   return XprMatrix<expr_type, Rows, Cols>(				\
227     expr_type(e, XprLiteral< POD >(x2), XprLiteral< POD >(x3))); 	\
228 }               							\
229                								\
230 template<class E1, class E3, std::size_t Rows, std::size_t Cols> 	\
231 inline               							\
232 XprMatrix<               						\
233   XprEval<               						\
234     XprMatrix<E1, Rows, Cols>,               				\
235     XprLiteral< POD >,               					\
236     XprMatrix<E3, Rows, Cols>               				\
237   >,                							\
238   Rows, Cols								\
239 >               							\
240 eval(const XprMatrix<E1, Rows, Cols>& e1, POD x2, const XprMatrix<E3, Rows, Cols>& e3) { \
241   typedef XprEval<               					\
242     XprMatrix<E1, Rows, Cols>,               				\
243     XprLiteral< POD >,                					\
244     XprMatrix<E3, Rows, Cols>               				\
245   > 							expr_type; 	\
246   return XprMatrix<expr_type, Rows, Cols>(				\
247     expr_type(e1, XprLiteral< POD >(x2), e3)); 				\
248 }               							\
249                								\
250 template<class E1, class E2, std::size_t Rows, std::size_t Cols>	\
251 inline               							\
252 XprMatrix<               						\
253   XprEval<               						\
254     XprMatrix<E1, Rows, Cols>,               				\
255     XprMatrix<E2, Rows, Cols>,               				\
256     XprLiteral< POD >               					\
257   >,                							\
258   Rows, Cols								\
259 >               							\
260 eval(const XprMatrix<E1, Rows, Cols>& e1, const XprMatrix<E2, Rows, Cols>& e2, POD x3) { \
261   typedef XprEval<               					\
262     XprMatrix<E1, Rows, Cols>,               				\
263     XprMatrix<E2, Rows, Cols>,               				\
264     XprLiteral< POD >                					\
265   > 							expr_type; 	\
266   return XprMatrix<expr_type, Rows, Cols>(				\
267     expr_type(e1, e2, XprLiteral< POD >(x3))); 				\
268 }
269 
270 TVMET_IMPLEMENT_MACRO(int)
271 
272 #if defined(TVMET_HAVE_LONG_LONG)
TVMET_IMPLEMENT_MACRO(long long int)273 TVMET_IMPLEMENT_MACRO(long long int)
274 #endif
275 
276 TVMET_IMPLEMENT_MACRO(float)
277 TVMET_IMPLEMENT_MACRO(double)
278 
279 #if defined(TVMET_HAVE_LONG_DOUBLE)
280 TVMET_IMPLEMENT_MACRO(long double)
281 #endif
282 
283 #undef TVMET_IMPLEMENT_MACRO
284 
285 
286 /*
287  * trinary evaluation functions with matrizes, xpr of and complex<> types
288  *
289  * XprMatrix<E, Rows, Cols> e, std::complex<T> z2, std::complex<T> z3
290  * XprMatrix<E1, Rows, Cols> e1, std::complex<T> z2, XprMatrix<E3, Rows, Cols> e3
291  * XprMatrix<E1, Rows, Cols> e1, XprMatrix<E2, Rows, Cols> e2, std::complex<T> z3
292  */
293 #if defined(TVMET_HAVE_COMPLEX)
294 
295 /**
296  * \fn eval(const XprMatrix<E, Rows, Cols>& e, const std::complex<T>& x2, const std::complex<T>& x3)
297  * \brief Evals the matrix expressions.
298  * \ingroup _trinary_function
299  * This eval is for the a?b:c syntax, since it's not allowed to overload
300  * these operators.
301  */
302 template<class E, std::size_t Rows, std::size_t Cols, class T>
303 inline
304 XprMatrix<
305   XprEval<
306     XprMatrix<E, Rows, Cols>,
307     XprLiteral< std::complex<T> >,
308     XprLiteral< std::complex<T> >
309   >,
310   Rows, Cols
311 >
312 eval(const XprMatrix<E, Rows, Cols>& e, const std::complex<T>& x2, const std::complex<T>& x3) {
313   typedef XprEval<
314     XprMatrix<E, Rows, Cols>,
315     XprLiteral< std::complex<T> >,
316     XprLiteral< std::complex<T> >
317   > 							expr_type;
318   return XprMatrix<expr_type, Rows, Cols>(
319     expr_type(e, XprLiteral< std::complex<T> >(x2), XprLiteral< std::complex<T> >(x3)));
320 }
321 
322 
323 /**
324  * \fn eval(const XprMatrix<E1, Rows, Cols>& e1, const std::complex<T>& x2, const XprMatrix<E3, Rows, Cols>& e3)
325  * \brief Evals the matrix expressions.
326  * \ingroup _trinary_function
327  * This eval is for the a?b:c syntax, since it's not allowed to overload
328  * these operators.
329  */
330 template<class E1, class E3, std::size_t Rows, std::size_t Cols, class T>
331 inline
332 XprMatrix<
333   XprEval<
334     XprMatrix<E1, Rows, Cols>,
335     XprLiteral< std::complex<T> >,
336     XprMatrix<E3, Rows, Cols>
337   >,
338   Rows, Cols
339 >
eval(const XprMatrix<E1,Rows,Cols> & e1,const std::complex<T> & x2,const XprMatrix<E3,Rows,Cols> & e3)340 eval(const XprMatrix<E1, Rows, Cols>& e1, const std::complex<T>& x2, const XprMatrix<E3, Rows, Cols>& e3) {
341   typedef XprEval<
342     XprMatrix<E1, Rows, Cols>,
343     XprLiteral< std::complex<T> >,
344     XprMatrix<E3, Rows, Cols>
345   > 							expr_type;
346   return XprMatrix<expr_type, Rows, Cols>(
347     expr_type(e1, XprLiteral< std::complex<T> >(x2), e3));
348 }
349 
350 
351 /**
352  * \fn eval(const XprMatrix<E1, Rows, Cols>& e1, const XprMatrix<E2, Rows, Cols>& e2, const std::complex<T>& x3)
353  * \brief Evals the matrix expressions.
354  * \ingroup _trinary_function
355  * This eval is for the a?b:c syntax, since it's not allowed to overload
356  * these operators.
357  */
358 template<class E1, class E2, std::size_t Rows, std::size_t Cols, class T>
359 inline
360 XprMatrix<
361   XprEval<
362     XprMatrix<E1, Rows, Cols>,
363     XprMatrix<E2, Rows, Cols>,
364     XprLiteral< std::complex<T> >
365   >,
366   Rows, Cols
367 >
eval(const XprMatrix<E1,Rows,Cols> & e1,const XprMatrix<E2,Rows,Cols> & e2,const std::complex<T> & x3)368 eval(const XprMatrix<E1, Rows, Cols>& e1, const XprMatrix<E2, Rows, Cols>& e2, const std::complex<T>& x3) {
369   typedef XprEval<
370     XprMatrix<E1, Rows, Cols>,
371     XprMatrix<E2, Rows, Cols>,
372     XprLiteral< std::complex<T> >
373   > 							expr_type;
374   return XprMatrix<expr_type, Rows, Cols>(
375     expr_type(e1, e2, XprLiteral< std::complex<T> >(x3)));
376 }
377 #endif // defined(TVMET_HAVE_COMPLEX)
378 
379 
380 } // namespace tvmet
381 
382 #endif // TVMET_MATRIX_EVAL_H
383 
384 // Local Variables:
385 // mode:C++
386 // tab-width:8
387 // End:
388