/* * Tiny Vector Matrix Library * Dense Vector Matrix Libary of Tiny size using Expression Templates * * Copyright (C) 2001 - 2003 Olaf Petzold * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: VectorFunctions.h,v 1.17 2005/03/25 07:11:29 opetzold Exp $ */ #ifndef TVMET_XPR_VECTOR_FUNCTIONS_H #define TVMET_XPR_VECTOR_FUNCTIONS_H namespace tvmet { /* forwards */ template class Vector; /********************************************************* * PART I: DECLARATION *********************************************************/ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Vector arithmetic functions add, sub, mul and div *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * function(XprVector, XprVector) */ #define TVMET_DECLARE_MACRO(NAME) \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME, \ XprVector, \ XprVector \ >, \ Sz \ > \ NAME (const XprVector& lhs, \ const XprVector& rhs) TVMET_CXX_ALWAYS_INLINE; TVMET_DECLARE_MACRO(add) // per se element wise TVMET_DECLARE_MACRO(sub) // per se element wise TVMET_DECLARE_MACRO(mul) // per se element wise namespace element_wise { TVMET_DECLARE_MACRO(div) // not defined for vectors } #undef TVMET_DECLARE_MACRO /* * function(XprVector, POD) * function(POD, XprVector) * Note: - operations +,-,*,/ are per se element wise */ #define TVMET_DECLARE_MACRO(NAME, POD) \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME< typename E::value_type, POD >, \ XprVector, \ XprLiteral< POD > \ >, \ Sz \ > \ NAME (const XprVector& lhs, \ POD rhs) TVMET_CXX_ALWAYS_INLINE; \ \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME< POD, typename E::value_type>, \ XprLiteral< POD >, \ XprVector \ >, \ Sz \ > \ NAME (POD lhs, \ const XprVector& rhs) TVMET_CXX_ALWAYS_INLINE; TVMET_DECLARE_MACRO(add, int) TVMET_DECLARE_MACRO(sub, int) TVMET_DECLARE_MACRO(mul, int) TVMET_DECLARE_MACRO(div, int) #if defined(TVMET_HAVE_LONG_LONG) TVMET_DECLARE_MACRO(add, long long int) TVMET_DECLARE_MACRO(sub, long long int) TVMET_DECLARE_MACRO(mul, long long int) TVMET_DECLARE_MACRO(div, long long int) #endif TVMET_DECLARE_MACRO(add, float) TVMET_DECLARE_MACRO(sub, float) TVMET_DECLARE_MACRO(mul, float) TVMET_DECLARE_MACRO(div, float) TVMET_DECLARE_MACRO(add, double) TVMET_DECLARE_MACRO(sub, double) TVMET_DECLARE_MACRO(mul, double) TVMET_DECLARE_MACRO(div, double) #if defined(TVMET_HAVE_LONG_DOUBLE) TVMET_DECLARE_MACRO(add, long double) TVMET_DECLARE_MACRO(sub, long double) TVMET_DECLARE_MACRO(mul, long double) TVMET_DECLARE_MACRO(div, long double) #endif #undef TVMET_DECLARE_MACRO #if defined(TVMET_HAVE_COMPLEX) /* * function(XprMatrix, complex) * function(complex, XprMatrix) * Note: - operations +,-,*,/ are per se element wise * \todo type promotion */ #define TVMET_DECLARE_MACRO(NAME) \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME< typename E::value_type, std::complex >, \ XprVector, \ XprLiteral< std::complex > \ >, \ Sz \ > \ NAME (const XprVector& lhs, \ const std::complex& rhs) TVMET_CXX_ALWAYS_INLINE; \ \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME< std::complex, typename E::value_type>, \ XprLiteral< std::complex >, \ XprVector \ >, \ Sz \ > \ NAME (const std::complex& lhs, \ const XprVector& rhs) TVMET_CXX_ALWAYS_INLINE; TVMET_DECLARE_MACRO(add) TVMET_DECLARE_MACRO(sub) TVMET_DECLARE_MACRO(mul) TVMET_DECLARE_MACRO(div) #undef TVMET_DECLARE_MACRO #endif // defined(TVMET_HAVE_COMPLEX) /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * vector specific functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ template typename NumericTraits::sum_type sum(const XprVector& v) TVMET_CXX_ALWAYS_INLINE; template typename NumericTraits::sum_type product(const XprVector& v) TVMET_CXX_ALWAYS_INLINE; template typename PromoteTraits< typename E1::value_type, typename E2::value_type >::value_type dot(const XprVector& lhs, const XprVector& rhs) TVMET_CXX_ALWAYS_INLINE; template typename PromoteTraits::value_type dot(const Vector& lhs, const XprVector& rhs) TVMET_CXX_ALWAYS_INLINE; template typename PromoteTraits::value_type dot(const XprVector& lhs, const Vector& rhs) TVMET_CXX_ALWAYS_INLINE; template Vector< typename PromoteTraits< typename E1::value_type, typename E2::value_type >::value_type, 3 > cross(const XprVector& lhs, const XprVector& rhs) TVMET_CXX_ALWAYS_INLINE; template Vector< typename PromoteTraits::value_type, 3> cross(const Vector& lhs, const XprVector& rhs) TVMET_CXX_ALWAYS_INLINE; template Vector< typename PromoteTraits::value_type, 3> cross(const XprVector& lhs, const Vector& rhs) TVMET_CXX_ALWAYS_INLINE; template typename NumericTraits::sum_type norm1(const XprVector& v) TVMET_CXX_ALWAYS_INLINE; template typename NumericTraits::sum_type norm2(const XprVector& v) TVMET_CXX_ALWAYS_INLINE; template XprVector< XprBinOp< Fcnl_div, XprVector, XprLiteral >, Sz > normalize(const XprVector& v) TVMET_CXX_ALWAYS_INLINE; /********************************************************* * PART II: IMPLEMENTATION *********************************************************/ /* * function(XprVector, XprVector) */ #define TVMET_IMPLEMENT_MACRO(NAME) \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME, \ XprVector, \ XprVector \ >, \ Sz \ > \ NAME (const XprVector& lhs, const XprVector& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME, \ XprVector, \ XprVector \ > expr_type; \ return XprVector(expr_type(lhs, rhs)); \ } TVMET_IMPLEMENT_MACRO(add) // per se element wise TVMET_IMPLEMENT_MACRO(sub) // per se element wise TVMET_IMPLEMENT_MACRO(mul) // per se element wise namespace element_wise { TVMET_IMPLEMENT_MACRO(div) // not defined for vectors } #undef TVMET_IMPLEMENT_MACRO /* * function(XprVector, POD) * function(POD, XprVector) * Note: - operations +,-,*,/ are per se element wise */ #define TVMET_IMPLEMENT_MACRO(NAME, POD) \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME< typename E::value_type, POD >, \ XprVector, \ XprLiteral< POD > \ >, \ Sz \ > \ NAME (const XprVector& lhs, POD rhs) { \ typedef XprBinOp< \ Fcnl_##NAME< typename E::value_type, POD >, \ XprVector, \ XprLiteral< POD > \ > expr_type; \ return XprVector( \ expr_type(lhs, XprLiteral< POD >(rhs))); \ } \ \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME< POD, typename E::value_type>, \ XprLiteral< POD >, \ XprVector \ >, \ Sz \ > \ NAME (POD lhs, const XprVector& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME< POD, typename E::value_type>, \ XprLiteral< POD >, \ XprVector \ > expr_type; \ return XprVector( \ expr_type(XprLiteral< POD >(lhs), rhs)); \ } TVMET_IMPLEMENT_MACRO(add, int) TVMET_IMPLEMENT_MACRO(sub, int) TVMET_IMPLEMENT_MACRO(mul, int) TVMET_IMPLEMENT_MACRO(div, int) #if defined(TVMET_HAVE_LONG_LONG) TVMET_IMPLEMENT_MACRO(add, long long int) TVMET_IMPLEMENT_MACRO(sub, long long int) TVMET_IMPLEMENT_MACRO(mul, long long int) TVMET_IMPLEMENT_MACRO(div, long long int) #endif TVMET_IMPLEMENT_MACRO(add, float) TVMET_IMPLEMENT_MACRO(sub, float) TVMET_IMPLEMENT_MACRO(mul, float) TVMET_IMPLEMENT_MACRO(div, float) TVMET_IMPLEMENT_MACRO(add, double) TVMET_IMPLEMENT_MACRO(sub, double) TVMET_IMPLEMENT_MACRO(mul, double) TVMET_IMPLEMENT_MACRO(div, double) #if defined(TVMET_HAVE_LONG_DOUBLE) TVMET_IMPLEMENT_MACRO(add, long double) TVMET_IMPLEMENT_MACRO(sub, long double) TVMET_IMPLEMENT_MACRO(mul, long double) TVMET_IMPLEMENT_MACRO(div, long double) #endif #undef TVMET_IMPLEMENT_MACRO #if defined(TVMET_HAVE_COMPLEX) /* * function(XprMatrix, complex) * function(complex, XprMatrix) * Note: - operations +,-,*,/ are per se element wise * \todo type promotion */ #define TVMET_IMPLEMENT_MACRO(NAME) \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME< typename E::value_type, std::complex >, \ XprVector, \ XprLiteral< std::complex > \ >, \ Sz \ > \ NAME (const XprVector& lhs, const std::complex& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME< typename E::value_type, std::complex >, \ XprVector, \ XprLiteral< std::complex > \ > expr_type; \ return XprVector( \ expr_type(lhs, XprLiteral< std::complex >(rhs))); \ } \ \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME< std::complex, typename E::value_type>, \ XprLiteral< std::complex >, \ XprVector \ >, \ Sz \ > \ NAME (const std::complex& lhs, const XprVector& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME< std::complex, typename E::value_type>, \ XprLiteral< std::complex >, \ XprVector \ > expr_type; \ return XprVector( \ expr_type(XprLiteral< std::complex >(lhs), rhs)); \ } TVMET_IMPLEMENT_MACRO(add) TVMET_IMPLEMENT_MACRO(sub) TVMET_IMPLEMENT_MACRO(mul) TVMET_IMPLEMENT_MACRO(div) #undef TVMET_IMPLEMENT_MACRO #endif // defined(TVMET_HAVE_COMPLEX) /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * vector specific functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /** * \fn sum(const XprVector& v) * \brief Compute the sum of the vector expression. * \ingroup _unary_function * * Simply compute the sum of the given vector as: * \f[ * \sum_{i = 0}^{Sz-1} v[i] * \f] */ template inline typename NumericTraits::sum_type sum(const XprVector& v) { return meta::Vector::sum(v); } /** * \fn product(const XprVector& v) * \brief Compute the product of the vector elements. * \ingroup _unary_function * * Simply computer the product of the given vector expression as: * \f[ * \prod_{i = 0}^{Sz - 1} v[i] * \f] */ template inline typename NumericTraits::sum_type product(const XprVector& v) { return meta::Vector::product(v); } /** * \fn dot(const XprVector& lhs, const XprVector& rhs) * \brief Compute the dot/inner product * \ingroup _binary_function * * Compute the dot product as: * \f[ * \sum_{i = 0}^{Sz - 1} ( lhs[i] * rhs[i] ) * \f] * where lhs is a column vector and rhs is a row vector, both vectors * have the same dimension. */ template inline typename PromoteTraits< typename E1::value_type, typename E2::value_type >::value_type dot(const XprVector& lhs, const XprVector& rhs) { return meta::Vector::dot(lhs, rhs); } /** * \fn dot(const Vector& lhs, const XprVector& rhs) * \brief Compute the dot/inner product * \ingroup _binary_function * * Compute the dot product as: * \f[ * \sum_{i = 0}^{Sz - 1} ( lhs[i] * rhs[i] ) * \f] * where lhs is a column vector and rhs is a row vector, both vectors * have the same dimension. */ template inline typename PromoteTraits::value_type dot(const Vector& lhs, const XprVector& rhs) { return meta::Vector::dot(lhs, rhs); } /** * \fn dot(const XprVector& lhs, const Vector& rhs) * \brief Compute the dot/inner product * \ingroup _binary_function * * Compute the dot product as: * \f[ * \sum_{i = 0}^{Sz - 1} ( lhs[i] * rhs[i] ) * \f] * where lhs is a column vector and rhs is a row vector, both vectors * have the same dimension. */ template inline typename PromoteTraits::value_type dot(const XprVector& lhs, const Vector& rhs) { return meta::Vector::dot(lhs, rhs); } /** * \fn cross(const XprVector& lhs, const XprVector& rhs) * \brief Compute the cross/outer product * \ingroup _binary_function * \note working only for vectors of size = 3 * \todo Implement vector outer product as ET and MT, returning a XprVector */ template inline Vector< typename PromoteTraits< typename E1::value_type, typename E2::value_type >::value_type, 3 > cross(const XprVector& lhs, const XprVector& rhs) { typedef typename PromoteTraits< typename E1::value_type, typename E2::value_type >::value_type value_type; return Vector(lhs(1)*rhs(2) - rhs(1)*lhs(2), rhs(0)*lhs(2) - lhs(0)*rhs(2), lhs(0)*rhs(1) - rhs(0)*lhs(1)); } /** * \fn cross(const XprVector& lhs, const Vector& rhs) * \brief Compute the cross/outer product * \ingroup _binary_function * \note working only for vectors of size = 3 * \todo Implement vector outer product as ET and MT, returning a XprVector */ template inline Vector< typename PromoteTraits::value_type, 3> cross(const XprVector& lhs, const Vector& rhs) { typedef typename PromoteTraits< typename E::value_type, T>::value_type value_type; return Vector(lhs(1)*rhs(2) - rhs(1)*lhs(2), rhs(0)*lhs(2) - lhs(0)*rhs(2), lhs(0)*rhs(1) - rhs(0)*lhs(1)); } /** * \fn cross(const Vector& lhs, const XprVector& rhs) * \brief Compute the cross/outer product * \ingroup _binary_function * \note working only for vectors of size = 3 * \todo Implement vector outer product as ET and MT, returning a XprVector */ template inline Vector< typename PromoteTraits::value_type, 3> cross(const Vector& lhs, const XprVector& rhs) { typedef typename PromoteTraits< typename E2::value_type, T1>::value_type value_type; return Vector(lhs(1)*rhs(2) - rhs(1)*lhs(2), rhs(0)*lhs(2) - lhs(0)*rhs(2), lhs(0)*rhs(1) - rhs(0)*lhs(1)); } /** * \fn norm1(const XprVector& v) * \brief The \f$l_1\f$ norm of a vector expression. * \ingroup _unary_function * The norm of any vector is just the square root of the dot product of * a vector with itself, or * * \f[ * |Vector v| = |v| = \sum_{i=0}^{Sz-1}\,|v[i]| * \f] */ template inline typename NumericTraits::sum_type norm1(const XprVector& v) { return sum(abs(v)); } /** * \fn norm2(const XprVector& v) * \brief The euklidian norm (or \f$l_2\f$ norm) of a vector expression. * \ingroup _unary_function * The norm of any vector is just the square root of the dot product of * a vector with itself, or * * \f[ * |Vector v| = |v| = \sqrt{ \sum_{i=0}^{Sz-1}\,v[i]^2 } * \f] * * \note The internal cast for Vector avoids warnings on sqrt. */ template inline typename NumericTraits::sum_type norm2(const XprVector& v) { typedef typename E::value_type value_type; return static_cast( std::sqrt(static_cast(dot(v, v))) ); } /** * \fn normalize(const XprVector& v) * \brief Normalize the given vector expression. * \ingroup _unary_function * \sa norm2 * * using the equation: * \f[ * \frac{Vector v}{\sqrt{ \sum_{i=0}^{Sz-1}\,v[i]^2 }} * \f] */ template inline XprVector< XprBinOp< Fcnl_div, XprVector, XprLiteral >, Sz > normalize(const XprVector& v) { typedef typename E::value_type value_type; typedef XprBinOp< Fcnl_div, XprVector, XprLiteral > expr_type; return XprVector( expr_type(v, XprLiteral< value_type >(norm2(v)))); } } // namespace tvmet #endif // TVMET_XPR_VECTOR_FUNCTIONS_H // Local Variables: // mode:C++ // End: