00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef TVMET_XPR_VECTOR_FUNCTIONS_H
00025 #define TVMET_XPR_VECTOR_FUNCTIONS_H
00026
00027 namespace tvmet {
00028
00029
00030
00031 template<class T, std::size_t Sz> class Vector;
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #define TVMET_DECLARE_MACRO(NAME) \
00048 template<class E1, class E2, std::size_t Sz> \
00049 XprVector< \
00050 XprBinOp< \
00051 Fcnl_##NAME<typename E1::value_type, typename E2::value_type>, \
00052 XprVector<E1, Sz>, \
00053 XprVector<E2, Sz> \
00054 >, \
00055 Sz \
00056 > \
00057 NAME (const XprVector<E1, Sz>& lhs, \
00058 const XprVector<E2, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE;
00059
00060 TVMET_DECLARE_MACRO(add)
00061 TVMET_DECLARE_MACRO(sub)
00062 TVMET_DECLARE_MACRO(mul)
00063 namespace element_wise {
00064 TVMET_DECLARE_MACRO(div)
00065 }
00066
00067 #undef TVMET_DECLARE_MACRO
00068
00069
00070
00071
00072
00073
00074
00075 #define TVMET_DECLARE_MACRO(NAME, POD) \
00076 template<class E, std::size_t Sz> \
00077 XprVector< \
00078 XprBinOp< \
00079 Fcnl_##NAME< typename E::value_type, POD >, \
00080 XprVector<E, Sz>, \
00081 XprLiteral< POD > \
00082 >, \
00083 Sz \
00084 > \
00085 NAME (const XprVector<E, Sz>& lhs, \
00086 POD rhs) TVMET_CXX_ALWAYS_INLINE; \
00087 \
00088 template<class E, std::size_t Sz> \
00089 XprVector< \
00090 XprBinOp< \
00091 Fcnl_##NAME< POD, typename E::value_type>, \
00092 XprLiteral< POD >, \
00093 XprVector<E, Sz> \
00094 >, \
00095 Sz \
00096 > \
00097 NAME (POD lhs, \
00098 const XprVector<E, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE;
00099
00100 TVMET_DECLARE_MACRO(add, int)
00101 TVMET_DECLARE_MACRO(sub, int)
00102 TVMET_DECLARE_MACRO(mul, int)
00103 TVMET_DECLARE_MACRO(div, int)
00104
00105 #if defined(TVMET_HAVE_LONG_LONG)
00106 TVMET_DECLARE_MACRO(add, long long int)
00107 TVMET_DECLARE_MACRO(sub, long long int)
00108 TVMET_DECLARE_MACRO(mul, long long int)
00109 TVMET_DECLARE_MACRO(div, long long int)
00110 #endif
00111
00112 TVMET_DECLARE_MACRO(add, float)
00113 TVMET_DECLARE_MACRO(sub, float)
00114 TVMET_DECLARE_MACRO(mul, float)
00115 TVMET_DECLARE_MACRO(div, float)
00116
00117 TVMET_DECLARE_MACRO(add, double)
00118 TVMET_DECLARE_MACRO(sub, double)
00119 TVMET_DECLARE_MACRO(mul, double)
00120 TVMET_DECLARE_MACRO(div, double)
00121
00122 #if defined(TVMET_HAVE_LONG_DOUBLE)
00123 TVMET_DECLARE_MACRO(add, long double)
00124 TVMET_DECLARE_MACRO(sub, long double)
00125 TVMET_DECLARE_MACRO(mul, long double)
00126 TVMET_DECLARE_MACRO(div, long double)
00127 #endif
00128
00129 #undef TVMET_DECLARE_MACRO
00130
00131
00132 #if defined(TVMET_HAVE_COMPLEX)
00133
00134
00135
00136
00137
00138
00139 #define TVMET_DECLARE_MACRO(NAME) \
00140 template<class E, std::size_t Sz, class T> \
00141 XprVector< \
00142 XprBinOp< \
00143 Fcnl_##NAME< typename E::value_type, std::complex<T> >, \
00144 XprVector<E, Sz>, \
00145 XprLiteral< std::complex<T> > \
00146 >, \
00147 Sz \
00148 > \
00149 NAME (const XprVector<E, Sz>& lhs, \
00150 const std::complex<T>& rhs) TVMET_CXX_ALWAYS_INLINE; \
00151 \
00152 template<class E, std::size_t Sz, class T> \
00153 XprVector< \
00154 XprBinOp< \
00155 Fcnl_##NAME< std::complex<T>, typename E::value_type>, \
00156 XprLiteral< std::complex<T> >, \
00157 XprVector<E, Sz> \
00158 >, \
00159 Sz \
00160 > \
00161 NAME (const std::complex<T>& lhs, \
00162 const XprVector<E, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE;
00163
00164 TVMET_DECLARE_MACRO(add)
00165 TVMET_DECLARE_MACRO(sub)
00166 TVMET_DECLARE_MACRO(mul)
00167 TVMET_DECLARE_MACRO(div)
00168
00169 #undef TVMET_DECLARE_MACRO
00170
00171 #endif // defined(TVMET_HAVE_COMPLEX)
00172
00173
00174
00175
00176
00177
00178
00179 template<class E, std::size_t Sz>
00180 typename NumericTraits<typename E::value_type>::sum_type
00181 sum(const XprVector<E, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
00182
00183
00184 template<class E, std::size_t Sz>
00185 typename NumericTraits<typename E::value_type>::sum_type
00186 product(const XprVector<E, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
00187
00188
00189 template<class E1, class E2, std::size_t Sz>
00190 typename PromoteTraits<
00191 typename E1::value_type,
00192 typename E2::value_type
00193 >::value_type
00194 dot(const XprVector<E1, Sz>& lhs,
00195 const XprVector<E2, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE;
00196
00197
00198 template<class T, class E, std::size_t Sz>
00199 typename PromoteTraits<T, typename E::value_type>::value_type
00200 dot(const Vector<T, Sz>& lhs,
00201 const XprVector<E, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE;
00202
00203
00204 template<class E, class T, std::size_t Sz>
00205 typename PromoteTraits<T, typename E::value_type>::value_type
00206 dot(const XprVector<E, Sz>& lhs,
00207 const Vector<T, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE;
00208
00209
00210 template<class E1, class E2>
00211 Vector<
00212 typename PromoteTraits<
00213 typename E1::value_type,
00214 typename E2::value_type
00215 >::value_type,
00216 3
00217 >
00218 cross(const XprVector<E1, 3>& lhs,
00219 const XprVector<E2, 3>& rhs) TVMET_CXX_ALWAYS_INLINE;
00220
00221
00222 template<class T, class E>
00223 Vector<
00224 typename PromoteTraits<T, typename E::value_type>::value_type, 3>
00225 cross(const Vector<T, 3>& lhs,
00226 const XprVector<E, 3>& rhs) TVMET_CXX_ALWAYS_INLINE;
00227
00228
00229 template<class E, class T>
00230 Vector<
00231 typename PromoteTraits<T, typename E::value_type>::value_type, 3>
00232 cross(const XprVector<E, 3>& lhs,
00233 const Vector<T, 3>& rhs) TVMET_CXX_ALWAYS_INLINE;
00234
00235
00236 template<class E, std::size_t Sz>
00237 typename NumericTraits<typename E::value_type>::sum_type
00238 norm1(const XprVector<E, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
00239
00240
00241 template<class E, std::size_t Sz>
00242 typename NumericTraits<typename E::value_type>::sum_type
00243 norm2(const XprVector<E, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
00244
00245
00246 template<class E, std::size_t Sz>
00247 XprVector<
00248 XprBinOp<
00249 Fcnl_div<typename E::value_type, typename E::value_type>,
00250 XprVector<E, Sz>,
00251 XprLiteral<typename E::value_type>
00252 >,
00253 Sz
00254 >
00255 normalize(const XprVector<E, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 #define TVMET_IMPLEMENT_MACRO(NAME) \
00267 template<class E1, class E2, std::size_t Sz> \
00268 inline \
00269 XprVector< \
00270 XprBinOp< \
00271 Fcnl_##NAME<typename E1::value_type, typename E2::value_type>, \
00272 XprVector<E1, Sz>, \
00273 XprVector<E2, Sz> \
00274 >, \
00275 Sz \
00276 > \
00277 NAME (const XprVector<E1, Sz>& lhs, const XprVector<E2, Sz>& rhs) { \
00278 typedef XprBinOp< \
00279 Fcnl_##NAME<typename E1::value_type, typename E2::value_type>, \
00280 XprVector<E1, Sz>, \
00281 XprVector<E2, Sz> \
00282 > expr_type; \
00283 return XprVector<expr_type, Sz>(expr_type(lhs, rhs)); \
00284 }
00285
00286 TVMET_IMPLEMENT_MACRO(add)
00287 TVMET_IMPLEMENT_MACRO(sub)
00288 TVMET_IMPLEMENT_MACRO(mul)
00289 namespace element_wise {
00290 TVMET_IMPLEMENT_MACRO(div)
00291 }
00292
00293 #undef TVMET_IMPLEMENT_MACRO
00294
00295
00296
00297
00298
00299
00300
00301 #define TVMET_IMPLEMENT_MACRO(NAME, POD) \
00302 template<class E, std::size_t Sz> \
00303 inline \
00304 XprVector< \
00305 XprBinOp< \
00306 Fcnl_##NAME< typename E::value_type, POD >, \
00307 XprVector<E, Sz>, \
00308 XprLiteral< POD > \
00309 >, \
00310 Sz \
00311 > \
00312 NAME (const XprVector<E, Sz>& lhs, POD rhs) { \
00313 typedef XprBinOp< \
00314 Fcnl_##NAME< typename E::value_type, POD >, \
00315 XprVector<E, Sz>, \
00316 XprLiteral< POD > \
00317 > expr_type; \
00318 return XprVector<expr_type, Sz>( \
00319 expr_type(lhs, XprLiteral< POD >(rhs))); \
00320 } \
00321 \
00322 template<class E, std::size_t Sz> \
00323 inline \
00324 XprVector< \
00325 XprBinOp< \
00326 Fcnl_##NAME< POD, typename E::value_type>, \
00327 XprLiteral< POD >, \
00328 XprVector<E, Sz> \
00329 >, \
00330 Sz \
00331 > \
00332 NAME (POD lhs, const XprVector<E, Sz>& rhs) { \
00333 typedef XprBinOp< \
00334 Fcnl_##NAME< POD, typename E::value_type>, \
00335 XprLiteral< POD >, \
00336 XprVector<E, Sz> \
00337 > expr_type; \
00338 return XprVector<expr_type, Sz>( \
00339 expr_type(XprLiteral< POD >(lhs), rhs)); \
00340 }
00341
00342 TVMET_IMPLEMENT_MACRO(add, int)
00343 TVMET_IMPLEMENT_MACRO(sub, int)
00344 TVMET_IMPLEMENT_MACRO(mul, int)
00345 TVMET_IMPLEMENT_MACRO(div, int)
00346
00347 #if defined(TVMET_HAVE_LONG_LONG)
00348 TVMET_IMPLEMENT_MACRO(add, long long int)
00349 TVMET_IMPLEMENT_MACRO(sub, long long int)
00350 TVMET_IMPLEMENT_MACRO(mul, long long int)
00351 TVMET_IMPLEMENT_MACRO(div, long long int)
00352 #endif
00353
00354 TVMET_IMPLEMENT_MACRO(add, float)
00355 TVMET_IMPLEMENT_MACRO(sub, float)
00356 TVMET_IMPLEMENT_MACRO(mul, float)
00357 TVMET_IMPLEMENT_MACRO(div, float)
00358
00359 TVMET_IMPLEMENT_MACRO(add, double)
00360 TVMET_IMPLEMENT_MACRO(sub, double)
00361 TVMET_IMPLEMENT_MACRO(mul, double)
00362 TVMET_IMPLEMENT_MACRO(div, double)
00363
00364 #if defined(TVMET_HAVE_LONG_DOUBLE)
00365 TVMET_IMPLEMENT_MACRO(add, long double)
00366 TVMET_IMPLEMENT_MACRO(sub, long double)
00367 TVMET_IMPLEMENT_MACRO(mul, long double)
00368 TVMET_IMPLEMENT_MACRO(div, long double)
00369 #endif
00370
00371 #undef TVMET_IMPLEMENT_MACRO
00372
00373
00374 #if defined(TVMET_HAVE_COMPLEX)
00375
00376
00377
00378
00379
00380
00381 #define TVMET_IMPLEMENT_MACRO(NAME) \
00382 template<class E, std::size_t Sz, class T> \
00383 inline \
00384 XprVector< \
00385 XprBinOp< \
00386 Fcnl_##NAME< typename E::value_type, std::complex<T> >, \
00387 XprVector<E, Sz>, \
00388 XprLiteral< std::complex<T> > \
00389 >, \
00390 Sz \
00391 > \
00392 NAME (const XprVector<E, Sz>& lhs, const std::complex<T>& rhs) { \
00393 typedef XprBinOp< \
00394 Fcnl_##NAME< typename E::value_type, std::complex<T> >, \
00395 XprVector<E, Sz>, \
00396 XprLiteral< std::complex<T> > \
00397 > expr_type; \
00398 return XprVector<expr_type, Sz>( \
00399 expr_type(lhs, XprLiteral< std::complex<T> >(rhs))); \
00400 } \
00401 \
00402 template<class E, std::size_t Sz, class T> \
00403 inline \
00404 XprVector< \
00405 XprBinOp< \
00406 Fcnl_##NAME< std::complex<T>, typename E::value_type>, \
00407 XprLiteral< std::complex<T> >, \
00408 XprVector<E, Sz> \
00409 >, \
00410 Sz \
00411 > \
00412 NAME (const std::complex<T>& lhs, const XprVector<E, Sz>& rhs) { \
00413 typedef XprBinOp< \
00414 Fcnl_##NAME< std::complex<T>, typename E::value_type>, \
00415 XprLiteral< std::complex<T> >, \
00416 XprVector<E, Sz> \
00417 > expr_type; \
00418 return XprVector<expr_type, Sz>( \
00419 expr_type(XprLiteral< std::complex<T> >(lhs), rhs)); \
00420 }
00421
00422 TVMET_IMPLEMENT_MACRO(add)
00423 TVMET_IMPLEMENT_MACRO(sub)
00424 TVMET_IMPLEMENT_MACRO(mul)
00425 TVMET_IMPLEMENT_MACRO(div)
00426
00427 #undef TVMET_IMPLEMENT_MACRO
00428
00429 #endif // defined(TVMET_HAVE_COMPLEX)
00430
00431
00432
00433
00434
00435
00436
00447 template<class E, std::size_t Sz>
00448 inline
00449 typename NumericTraits<typename E::value_type>::sum_type
00450 sum(const XprVector<E, Sz>& v) {
00451 return meta::Vector<Sz>::sum(v);
00452 }
00453
00454
00465 template<class E, std::size_t Sz>
00466 inline
00467 typename NumericTraits<typename E::value_type>::sum_type
00468 product(const XprVector<E, Sz>& v) {
00469 return meta::Vector<Sz>::product(v);
00470 }
00471
00472
00485 template<class E1, class E2, std::size_t Sz>
00486 inline
00487 typename PromoteTraits<
00488 typename E1::value_type,
00489 typename E2::value_type
00490 >::value_type
00491 dot(const XprVector<E1, Sz>& lhs, const XprVector<E2, Sz>& rhs) {
00492 return meta::Vector<Sz>::dot(lhs, rhs);
00493 }
00494
00495
00508 template<class T, class E, std::size_t Sz>
00509 inline
00510 typename PromoteTraits<T, typename E::value_type>::value_type
00511 dot(const Vector<T, Sz>& lhs, const XprVector<E, Sz>& rhs) {
00512 return meta::Vector<Sz>::dot(lhs, rhs);
00513 }
00514
00515
00528 template<class E, class T, std::size_t Sz>
00529 inline
00530 typename PromoteTraits<T, typename E::value_type>::value_type
00531 dot(const XprVector<E, Sz>& lhs, const Vector<T, Sz>& rhs) {
00532 return meta::Vector<Sz>::dot(lhs, rhs);
00533 }
00534
00535
00543 template<class E1, class E2>
00544 inline
00545 Vector<
00546 typename PromoteTraits<
00547 typename E1::value_type,
00548 typename E2::value_type
00549 >::value_type,
00550 3
00551 >
00552 cross(const XprVector<E1, 3>& lhs, const XprVector<E2, 3>& rhs) {
00553 typedef typename PromoteTraits<
00554 typename E1::value_type,
00555 typename E2::value_type
00556 >::value_type value_type;
00557 return Vector<value_type, 3>(lhs(1)*rhs(2) - rhs(1)*lhs(2),
00558 rhs(0)*lhs(2) - lhs(0)*rhs(2),
00559 lhs(0)*rhs(1) - rhs(0)*lhs(1));
00560 }
00561
00562
00570 template<class E, class T>
00571 inline
00572 Vector<
00573 typename PromoteTraits<T, typename E::value_type>::value_type, 3>
00574 cross(const XprVector<E, 3>& lhs, const Vector<T, 3>& rhs) {
00575 typedef typename PromoteTraits<
00576 typename E::value_type, T>::value_type value_type;
00577 return Vector<value_type, 3>(lhs(1)*rhs(2) - rhs(1)*lhs(2),
00578 rhs(0)*lhs(2) - lhs(0)*rhs(2),
00579 lhs(0)*rhs(1) - rhs(0)*lhs(1));
00580 }
00581
00582
00590 template<class T1, class E2>
00591 inline
00592 Vector<
00593 typename PromoteTraits<T1, typename E2::value_type>::value_type, 3>
00594 cross(const Vector<T1, 3>& lhs, const XprVector<E2, 3>& rhs) {
00595 typedef typename PromoteTraits<
00596 typename E2::value_type, T1>::value_type value_type;
00597 return Vector<value_type, 3>(lhs(1)*rhs(2) - rhs(1)*lhs(2),
00598 rhs(0)*lhs(2) - lhs(0)*rhs(2),
00599 lhs(0)*rhs(1) - rhs(0)*lhs(1));
00600 }
00601
00602
00614 template<class E, std::size_t Sz>
00615 inline
00616 typename NumericTraits<typename E::value_type>::sum_type
00617 norm1(const XprVector<E, Sz>& v) {
00618 return sum(abs(v));
00619 }
00620
00621
00635 template<class E, std::size_t Sz>
00636 inline
00637 typename NumericTraits<typename E::value_type>::sum_type
00638 norm2(const XprVector<E, Sz>& v) {
00639 typedef typename E::value_type value_type;
00640 return static_cast<value_type>( std::sqrt(static_cast<value_type>(dot(v, v))) );
00641 }
00642
00643
00655 template<class E, std::size_t Sz>
00656 inline
00657 XprVector<
00658 XprBinOp<
00659 Fcnl_div<typename E::value_type, typename E::value_type>,
00660 XprVector<E, Sz>,
00661 XprLiteral<typename E::value_type>
00662 >,
00663 Sz
00664 >
00665 normalize(const XprVector<E, Sz>& v) {
00666 typedef typename E::value_type value_type;
00667 typedef XprBinOp<
00668 Fcnl_div<value_type, value_type>,
00669 XprVector<E, Sz>,
00670 XprLiteral<value_type>
00671 > expr_type;
00672 return XprVector<expr_type, Sz>(
00673 expr_type(v, XprLiteral< value_type >(norm2(v))));
00674 }
00675
00676
00677 }
00678
00679 #endif // TVMET_XPR_VECTOR_FUNCTIONS_H
00680
00681
00682
00683
00684