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: VectorEval.h,v 1.18 2007-06-23 15:58:58 opetzold Exp $
22 */
23
24 #ifndef TVMET_VECTOR_EVAL_H
25 #define TVMET_VECTOR_EVAL_H
26
27 namespace tvmet {
28
29
30 /********************************************************************
31 * functions all_elements/any_elements
32 ********************************************************************/
33
34
35 /**
36 * \fn bool all_elements(const XprVector<E, Sz>& e)
37 * \brief check on statements for all elements
38 * \ingroup _unary_function
39 * This is for use with boolean operators like
40 * \par Example:
41 * \code
42 * all_elements(vector > 0) {
43 * // true branch
44 * } else {
45 * // false branch
46 * }
47 * \endcode
48 * \sa \ref compare
49 */
50 template<class E, std::size_t Sz>
51 inline
all_elements(const XprVector<E,Sz> & e)52 bool all_elements(const XprVector<E, Sz>& e) {
53 return meta::Vector<Sz>::all_elements(e);
54 }
55
56
57 /**
58 * \fn bool any_elements(const XprVector<E, Sz>& e)
59 * \brief check on statements for any elements
60 * \ingroup _unary_function
61 * This is for use with boolean operators like
62 * \par Example:
63 * \code
64 * any_elements(vector > 0) {
65 * // true branch
66 * } else {
67 * // false branch
68 * }
69 * \endcode
70 * \sa \ref compare
71 */
72 template<class E, std::size_t Sz>
73 inline
any_elements(const XprVector<E,Sz> & e)74 bool any_elements(const XprVector<E, Sz>& e) {
75 return meta::Vector<Sz>::any_elements(e);
76 }
77
78
79 /*
80 * trinary evaluation functions with vectors and xpr of
81 * XprVector<E1, Sz> ? Vector<T2, Sz> : Vector<T3, Sz>
82 * XprVector<E1, Sz> ? Vector<T2, Sz> : XprVector<E3, Sz>
83 * XprVector<E1, Sz> ? XprVector<E2, Sz> : Vector<T3, Sz>
84 * XprVector<E1, Sz> ? XprVector<E2, Sz> : XprVector<E3, Sz>
85 */
86
87 /**
88 * eval(const XprVector<E1, Sz>& e1, const Vector<T2, Sz>& v2, const Vector<T3, Sz>& v3)
89 * \brief Evals the vector expressions.
90 * \ingroup _trinary_function
91 * This eval is for the a?b:c syntax, since it's not allowed to overload
92 * these operators.
93 */
94 template<class E1, class T2, class T3, std::size_t Sz>
95 inline
96 XprVector<
97 XprEval<
98 XprVector<E1, Sz>,
99 VectorConstReference<T2, Sz>,
100 VectorConstReference<T3, Sz>
101 >,
102 Sz
103 >
eval(const XprVector<E1,Sz> & e1,const Vector<T2,Sz> & v2,const Vector<T3,Sz> & v3)104 eval(const XprVector<E1, Sz>& e1, const Vector<T2, Sz>& v2, const Vector<T3, Sz>& v3) {
105 typedef XprEval<
106 XprVector<E1, Sz>,
107 VectorConstReference<T2, Sz>,
108 VectorConstReference<T3, Sz>
109 > expr_type;
110 return XprVector<expr_type, Sz>(
111 expr_type(e1, v2.const_ref(), v3.const_ref()));
112 }
113
114
115 /**
116 * eval(const XprVector<E1, Sz>& e1, const Vector<T2, Sz>& v2, const XprVector<E3, Sz>& e3)
117 * \brief Evals the vector expressions.
118 * \ingroup _trinary_function
119 * This eval is for the a?b:c syntax, since it's not allowed to overload
120 * these operators.
121 */
122 template<class E1, class T2, class E3, std::size_t Sz>
123 inline
124 XprVector<
125 XprEval<
126 XprVector<E1, Sz>,
127 VectorConstReference<T2, Sz>,
128 XprVector<E3, Sz>
129 >,
130 Sz
131 >
eval(const XprVector<E1,Sz> & e1,const Vector<T2,Sz> & v2,const XprVector<E3,Sz> & e3)132 eval(const XprVector<E1, Sz>& e1, const Vector<T2, Sz>& v2, const XprVector<E3, Sz>& e3) {
133 typedef XprEval<
134 XprVector<E1, Sz>,
135 VectorConstReference<T2, Sz>,
136 XprVector<E3, Sz>
137 > expr_type;
138 return XprVector<expr_type, Sz>(
139 expr_type(e1, v2.const_ref(), e3));
140 }
141
142
143 /**
144 * eval(const XprVector<E1, Sz>& e1, const XprVector<E2, Sz>& e2, const Vector<T3, Sz>& v3)
145 * \brief Evals the vector 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 Sz>
151 inline
152 XprVector<
153 XprEval<
154 XprVector<E1, Sz>,
155 XprVector<E2, Sz>,
156 VectorConstReference<T3, Sz>
157 >,
158 Sz
159 >
eval(const XprVector<E1,Sz> & e1,const XprVector<E2,Sz> & e2,const Vector<T3,Sz> & v3)160 eval(const XprVector<E1, Sz>& e1, const XprVector<E2, Sz>& e2, const Vector<T3, Sz>& v3) {
161 typedef XprEval<
162 XprVector<E1, Sz>,
163 XprVector<E2, Sz>,
164 VectorConstReference<T3, Sz>
165 > expr_type;
166 return XprVector<expr_type, Sz>(
167 expr_type(e1, e2, v3.const_ref()));
168 }
169
170
171 /**
172 * eval(const XprVector<E1, Sz>& e1, const XprVector<E2, Sz>& e2, const XprVector<E3, Sz>& e3)
173 * \brief Evals the vector expressions.
174 * \ingroup _trinary_function
175 * This eval is for the a?b:c syntax, since it's not allowed to overload
176 * these operators.
177 */
178 template<class E1, class E2, class E3, std::size_t Sz>
179 inline
180 XprVector<
181 XprEval<
182 XprVector<E1, Sz>,
183 XprVector<E2, Sz>,
184 XprVector<E3, Sz>
185 >,
186 Sz
187 >
eval(const XprVector<E1,Sz> & e1,const XprVector<E2,Sz> & e2,const XprVector<E3,Sz> & e3)188 eval(const XprVector<E1, Sz>& e1, const XprVector<E2, Sz>& e2, const XprVector<E3, Sz>& e3) {
189 typedef XprEval<
190 XprVector<E1, Sz>,
191 XprVector<E2, Sz>,
192 XprVector<E3, Sz>
193 > expr_type;
194 return XprVector<expr_type, Sz>(expr_type(e1, e2, e3));
195 }
196
197
198 /*
199 * trinary evaluation functions with vectors, xpr of and POD
200 *
201 * XprVector<E, Sz> ? POD1 : POD2
202 * XprVector<E1, Sz> ? POD : XprVector<E3, Sz>
203 * XprVector<E1, Sz> ? XprVector<E2, Sz> : POD
204 */
205 #define TVMET_IMPLEMENT_MACRO(POD) \
206 template<class E, std::size_t Sz> \
207 inline \
208 XprVector< \
209 XprEval< \
210 XprVector<E, Sz>, \
211 XprLiteral< POD >, \
212 XprLiteral< POD > \
213 >, \
214 Sz \
215 > \
216 eval(const XprVector<E, Sz>& e, POD x2, POD x3) { \
217 typedef XprEval< \
218 XprVector<E, Sz>, \
219 XprLiteral< POD >, \
220 XprLiteral< POD > \
221 > expr_type; \
222 return XprVector<expr_type, Sz>( \
223 expr_type(e, XprLiteral< POD >(x2), XprLiteral< POD >(x3))); \
224 } \
225 \
226 template<class E1, class E3, std::size_t Sz> \
227 inline \
228 XprVector< \
229 XprEval< \
230 XprVector<E1, Sz>, \
231 XprLiteral< POD >, \
232 XprVector<E3, Sz> \
233 >, \
234 Sz \
235 > \
236 eval(const XprVector<E1, Sz>& e1, POD x2, const XprVector<E3, Sz>& e3) { \
237 typedef XprEval< \
238 XprVector<E1, Sz>, \
239 XprLiteral< POD >, \
240 XprVector<E3, Sz> \
241 > expr_type; \
242 return XprVector<expr_type, Sz>( \
243 expr_type(e1, XprLiteral< POD >(x2), e3)); \
244 } \
245 \
246 template<class E1, class E2, std::size_t Sz> \
247 inline \
248 XprVector< \
249 XprEval< \
250 XprVector<E1, Sz>, \
251 XprVector<E2, Sz>, \
252 XprLiteral< POD > \
253 >, \
254 Sz \
255 > \
256 eval(const XprVector<E1, Sz>& e1, const XprVector<E2, Sz>& e2, POD x3) { \
257 typedef XprEval< \
258 XprVector<E1, Sz>, \
259 XprVector<E2, Sz>, \
260 XprLiteral< POD > \
261 > expr_type; \
262 return XprVector<expr_type, Sz>( \
263 expr_type(e1, e2, XprLiteral< POD >(x3))); \
264 }
265
266 TVMET_IMPLEMENT_MACRO(int)
267
268 #if defined(TVMET_HAVE_LONG_LONG)
TVMET_IMPLEMENT_MACRO(long long int)269 TVMET_IMPLEMENT_MACRO(long long int)
270 #endif // defined(TVMET_HAVE_LONG_LONG)
271
272 TVMET_IMPLEMENT_MACRO(float)
273 TVMET_IMPLEMENT_MACRO(double)
274
275 #if defined(TVMET_HAVE_LONG_DOUBLE)
276 TVMET_IMPLEMENT_MACRO(long double)
277 #endif // defined(TVMET_HAVE_LONG_DOUBLE)
278
279 #undef TVMET_IMPLEMENT_MACRO
280
281
282 /*
283 * trinary evaluation functions with vectors, xpr of and complex<> types
284 *
285 * XprVector<E, Sz> e, std::complex<T> z2, std::complex<T> z3
286 * XprVector<E1, Sz> e1, std::complex<T> z2, XprVector<E3, Sz> e3
287 * XprVector<E1, Sz> e1, XprVector<E2, Sz> e2, std::complex<T> z3
288 */
289 #if defined(TVMET_HAVE_COMPLEX)
290
291
292 /**
293 * eval(const XprVector<E, Sz>& e, std::complex<T> z2, std::complex<T> z3)
294 * \brief Evals the vector expressions.
295 * \ingroup _trinary_function
296 * This eval is for the a?b:c syntax, since it's not allowed to overload
297 * these operators.
298 */
299 template<class E, std::size_t Sz, class T>
300 inline
301 XprVector<
302 XprEval<
303 XprVector<E, Sz>,
304 XprLiteral< std::complex<T> >,
305 XprLiteral< std::complex<T> >
306 >,
307 Sz
308 >
309 eval(const XprVector<E, Sz>& e, std::complex<T> z2, std::complex<T> z3) {
310 typedef XprEval<
311 XprVector<E, Sz>,
312 XprLiteral< std::complex<T> >,
313 XprLiteral< std::complex<T> >
314 > expr_type;
315 return XprVector<expr_type, Sz>(
316 expr_type(e, XprLiteral< std::complex<T> >(z2), XprLiteral< std::complex<T> >(z3)));
317 }
318
319 /**
320 * eval(const XprVector<E1, Sz>& e1, std::complex<T> z2, const XprVector<E3, Sz>& e3)
321 * \brief Evals the vector expressions.
322 * \ingroup _trinary_function
323 * This eval is for the a?b:c syntax, since it's not allowed to overload
324 * these operators.
325 */
326 template<class E1, class E3, std::size_t Sz, class T>
327 inline
328 XprVector<
329 XprEval<
330 XprVector<E1, Sz>,
331 XprLiteral< std::complex<T> >,
332 XprVector<E3, Sz>
333 >,
334 Sz
335 >
eval(const XprVector<E1,Sz> & e1,std::complex<T> z2,const XprVector<E3,Sz> & e3)336 eval(const XprVector<E1, Sz>& e1, std::complex<T> z2, const XprVector<E3, Sz>& e3) {
337 typedef XprEval<
338 XprVector<E1, Sz>,
339 XprLiteral< std::complex<T> >,
340 XprVector<E3, Sz>
341 > expr_type;
342 return XprVector<expr_type, Sz>(
343 expr_type(e1, XprLiteral< std::complex<T> >(z2), e3));
344 }
345
346 /**
347 * eval(const XprVector<E1, Sz>& e1, const XprVector<E2, Sz>& e2, std::complex<T> z3)
348 * \brief Evals the vector expressions.
349 * \ingroup _trinary_function
350 * This eval is for the a?b:c syntax, since it's not allowed to overload
351 * these operators.
352 */
353 template<class E1, class E2, std::size_t Sz, class T>
354 inline
355 XprVector<
356 XprEval<
357 XprVector<E1, Sz>,
358 XprVector<E2, Sz>,
359 XprLiteral< std::complex<T> >
360 >,
361 Sz
362 >
eval(const XprVector<E1,Sz> & e1,const XprVector<E2,Sz> & e2,std::complex<T> z3)363 eval(const XprVector<E1, Sz>& e1, const XprVector<E2, Sz>& e2, std::complex<T> z3) {
364 typedef XprEval<
365 XprVector<E1, Sz>,
366 XprVector<E2, Sz>,
367 XprLiteral< std::complex<T> >
368 > expr_type;
369 return XprVector<expr_type, Sz>(
370 expr_type(e1, e2, XprLiteral< std::complex<T> >(z3)));
371 }
372 #endif // defined(TVMET_HAVE_COMPLEX)
373
374
375 } // namespace tvmet
376
377 #endif // TVMET_VECTOR_EVAL_H
378
379 // Local Variables:
380 // mode:C++
381 // tab-width:8
382 // End:
383