Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Eurobot_2012_Secondary by
VectorFunctions.h
00001 /* 00002 * Tiny Vector Matrix Library 00003 * Dense Vector Matrix Libary of Tiny size using Expression Templates 00004 * 00005 * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net> 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 * 00021 * $Id: VectorFunctions.h,v 1.21 2007-06-23 15:59:00 opetzold Exp $ 00022 */ 00023 00024 #ifndef TVMET_XPR_VECTOR_FUNCTIONS_H 00025 #define TVMET_XPR_VECTOR_FUNCTIONS_H 00026 00027 namespace tvmet { 00028 00029 00030 /* forwards */ 00031 template<class T, std::size_t Sz> class Vector; 00032 00033 00034 /********************************************************* 00035 * PART I: DECLARATION 00036 *********************************************************/ 00037 00038 00039 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00040 * Vector arithmetic functions add, sub, mul and div 00041 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 00042 00043 00044 /* 00045 * function(XprVector<E1, Sz>, XprVector<E2, Sz>) 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) // per se element wise 00061 TVMET_DECLARE_MACRO(sub) // per se element wise 00062 TVMET_DECLARE_MACRO(mul) // per se element wise 00063 namespace element_wise { 00064 TVMET_DECLARE_MACRO(div) // not defined for vectors 00065 } 00066 00067 #undef TVMET_DECLARE_MACRO 00068 00069 00070 /* 00071 * function(XprVector<E, Sz>, POD) 00072 * function(POD, XprVector<E, Sz>) 00073 * Note: - operations +,-,*,/ are per se element wise 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 * function(XprMatrix<E, Rows, Cols>, complex<T>) 00135 * function(complex<T>, XprMatrix<E, Rows, Cols>) 00136 * Note: - operations +,-,*,/ are per se element wise 00137 * \todo type promotion 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 * vector specific functions 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 * PART II: IMPLEMENTATION 00260 *********************************************************/ 00261 00262 00263 /* 00264 * function(XprVector<E1, Sz>, XprVector<E2, Sz>) 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) // per se element wise 00287 TVMET_IMPLEMENT_MACRO(sub) // per se element wise 00288 TVMET_IMPLEMENT_MACRO(mul) // per se element wise 00289 namespace element_wise { 00290 TVMET_IMPLEMENT_MACRO(div) // not defined for vectors 00291 } 00292 00293 #undef TVMET_IMPLEMENT_MACRO 00294 00295 00296 /* 00297 * function(XprVector<E, Sz>, POD) 00298 * function(POD, XprVector<E, Sz>) 00299 * Note: - operations +,-,*,/ are per se element wise 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 * function(XprMatrix<E, Rows, Cols>, complex<T>) 00377 * function(complex<T>, XprMatrix<E, Rows, Cols>) 00378 * Note: - operations +,-,*,/ are per se element wise 00379 * \todo type promotion 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 * vector specific functions 00434 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 00435 00436 00437 /** 00438 * \fn sum(const XprVector<E, Sz>& v) 00439 * \brief Compute the sum of the vector expression. 00440 * \ingroup _unary_function 00441 * 00442 * Simply compute the sum of the given vector as: 00443 * \f[ 00444 * \sum_{i = 0}^{Sz-1} v[i] 00445 * \f] 00446 */ 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 00455 /** 00456 * \fn product(const XprVector<E, Sz>& v) 00457 * \brief Compute the product of the vector elements. 00458 * \ingroup _unary_function 00459 * 00460 * Simply computer the product of the given vector expression as: 00461 * \f[ 00462 * \prod_{i = 0}^{Sz - 1} v[i] 00463 * \f] 00464 */ 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 00473 /** 00474 * \fn dot(const XprVector<E1, Sz>& lhs, const XprVector<E2, Sz>& rhs) 00475 * \brief Compute the dot/inner product 00476 * \ingroup _binary_function 00477 * 00478 * Compute the dot product as: 00479 * \f[ 00480 * \sum_{i = 0}^{Sz - 1} ( lhs[i] * rhs[i] ) 00481 * \f] 00482 * where lhs is a column vector and rhs is a row vector, both vectors 00483 * have the same dimension. 00484 */ 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 00496 /** 00497 * \fn dot(const Vector<T, Sz>& lhs, const XprVector<E, Sz>& rhs) 00498 * \brief Compute the dot/inner product 00499 * \ingroup _binary_function 00500 * 00501 * Compute the dot product as: 00502 * \f[ 00503 * \sum_{i = 0}^{Sz - 1} ( lhs[i] * rhs[i] ) 00504 * \f] 00505 * where lhs is a column vector and rhs is a row vector, both vectors 00506 * have the same dimension. 00507 */ 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 00516 /** 00517 * \fn dot(const XprVector<E, Sz>& lhs, const Vector<T, Sz>& rhs) 00518 * \brief Compute the dot/inner product 00519 * \ingroup _binary_function 00520 * 00521 * Compute the dot product as: 00522 * \f[ 00523 * \sum_{i = 0}^{Sz - 1} ( lhs[i] * rhs[i] ) 00524 * \f] 00525 * where lhs is a column vector and rhs is a row vector, both vectors 00526 * have the same dimension. 00527 */ 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 00536 /** 00537 * \fn cross(const XprVector<E1, 3>& lhs, const XprVector<E2, 3>& rhs) 00538 * \brief Compute the cross/outer product 00539 * \ingroup _binary_function 00540 * \note working only for vectors of size = 3 00541 * \todo Implement vector outer product as ET and MT, returning a XprVector 00542 */ 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 00563 /** 00564 * \fn cross(const XprVector<E, 3>& lhs, const Vector<T, 3>& rhs) 00565 * \brief Compute the cross/outer product 00566 * \ingroup _binary_function 00567 * \note working only for vectors of size = 3 00568 * \todo Implement vector outer product as ET and MT, returning a XprVector 00569 */ 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 00583 /** 00584 * \fn cross(const Vector<T, 3>& lhs, const XprVector<E, 3>& rhs) 00585 * \brief Compute the cross/outer product 00586 * \ingroup _binary_function 00587 * \note working only for vectors of size = 3 00588 * \todo Implement vector outer product as ET and MT, returning a XprVector 00589 */ 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 00603 /** 00604 * \fn norm1(const XprVector<E, Sz>& v) 00605 * \brief The \f$l_1\f$ norm of a vector expression. 00606 * \ingroup _unary_function 00607 * The norm of any vector is just the square root of the dot product of 00608 * a vector with itself, or 00609 * 00610 * \f[ 00611 * |Vector<T, Sz> v| = |v| = \sum_{i=0}^{Sz-1}\,|v[i]| 00612 * \f] 00613 */ 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 00622 /** 00623 * \fn norm2(const XprVector<E, Sz>& v) 00624 * \brief The euklidian norm (or \f$l_2\f$ norm) of a vector expression. 00625 * \ingroup _unary_function 00626 * The norm of any vector is just the square root of the dot product of 00627 * a vector with itself, or 00628 * 00629 * \f[ 00630 * |Vector<T, Sz> v| = |v| = \sqrt{ \sum_{i=0}^{Sz-1}\,v[i]^2 } 00631 * \f] 00632 * 00633 * \note The internal cast for Vector<int> avoids warnings on sqrt. 00634 */ 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 00644 /** 00645 * \fn normalize(const XprVector<E, Sz>& v) 00646 * \brief Normalize the given vector expression. 00647 * \ingroup _unary_function 00648 * \sa norm2 00649 * 00650 * using the equation: 00651 * \f[ 00652 * \frac{Vector<T, Sz> v}{\sqrt{ \sum_{i=0}^{Sz-1}\,v[i]^2 }} 00653 * \f] 00654 */ 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 } // namespace tvmet 00678 00679 #endif // TVMET_XPR_VECTOR_FUNCTIONS_H 00680 00681 // Local Variables: 00682 // mode:C++ 00683 // tab-width:8 00684 // End:
Generated on Tue Jul 12 2022 21:02:14 by
1.7.2
