/* * 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: VectorOperators.h,v 1.14 2004/06/10 16:36:55 opetzold Exp $ */ #ifndef TVMET_VECTOR_OPERATORS_H #define TVMET_VECTOR_OPERATORS_H namespace tvmet { /********************************************************* * PART I: DECLARATION *********************************************************/ template inline std::ostream& operator<<(std::ostream& os, const Vector& rhs) TVMET_CXX_ALWAYS_INLINE; /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Member operators (arithmetic and bit ops) *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * update_operator(Vector, Vector) * update_operator(Vector, XprVector) * Note: per se element wise */ #define TVMET_DECLARE_MACRO(NAME, OP) \ template \ Vector& \ operator OP (Vector& lhs, \ const Vector& rhs) TVMET_CXX_ALWAYS_INLINE; \ \ template \ Vector& \ operator OP (Vector& lhs, \ const XprVector& rhs) TVMET_CXX_ALWAYS_INLINE; TVMET_DECLARE_MACRO(add_eq, +=) // per se element wise TVMET_DECLARE_MACRO(sub_eq, -=) // per se element wise TVMET_DECLARE_MACRO(mul_eq, *=) // per se element wise namespace element_wise { TVMET_DECLARE_MACRO(div_eq, /=) // not defined for vectors } #undef TVMET_DECLARE_MACRO /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Vector arithmetic operators implemented by functions * add, sub, mul and div *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * operator(Vector, Vector) * operator(Vector, XprVector) * operator(XprVector, Vector) */ #define TVMET_DECLARE_MACRO(NAME, OP) \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME, \ VectorConstReference, \ VectorConstReference \ >, \ Sz \ > \ operator OP (const Vector& lhs, \ const Vector& rhs) TVMET_CXX_ALWAYS_INLINE; \ \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME, \ XprVector, \ VectorConstReference \ >, \ Sz \ > \ operator OP (const XprVector& lhs, \ const Vector& rhs) TVMET_CXX_ALWAYS_INLINE; \ \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME, \ VectorConstReference, \ XprVector \ >, \ Sz \ > \ operator OP (const Vector& 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 /* * operator(Vector, POD) * operator(POD, Vector) * Note: operations +,-,*,/ are per se element wise */ #define TVMET_DECLARE_MACRO(NAME, OP, POD) \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME< T, POD >, \ VectorConstReference, \ XprLiteral< POD > \ >, \ Sz \ > \ operator OP (const Vector& lhs, \ POD rhs) TVMET_CXX_ALWAYS_INLINE; \ \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME< POD, T>, \ XprLiteral< POD >, \ VectorConstReference \ >, \ Sz \ > \ operator OP (POD lhs, \ const Vector& 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) /* * operator(Vector, Sz>, std::complex) * operator(std::complex, Vector, Sz>) * Note: operations +,-,*,/ are per se element wise * \todo type promotion */ #define TVMET_DECLARE_MACRO(NAME, OP) \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME< std::complex, std::complex >, \ VectorConstReference< std::complex, Sz>, \ XprLiteral< std::complex > \ >, \ Sz \ > \ operator OP (const Vector, Sz>& lhs, \ const std::complex& rhs) TVMET_CXX_ALWAYS_INLINE; \ \ template \ XprVector< \ XprBinOp< \ Fcnl_##NAME< std::complex, std::complex >, \ XprLiteral< std::complex >, \ VectorConstReference< std::complex, Sz> \ >, \ Sz \ > \ operator OP (const std::complex& lhs, \ const Vector< std::complex, Sz>& 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 TVMET_DECLARE_MACRO(div, /) // per se element wise #undef TVMET_DECLARE_MACRO #endif // defined(TVMET_HAVE_COMPLEX) /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * global unary operators *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * unary_operator(Vector) * Note: per se element wise */ #define TVMET_DECLARE_MACRO(NAME, OP) \ template \ XprVector< \ XprUnOp< \ Fcnl_##NAME, \ VectorConstReference \ >, \ Sz \ > \ operator OP (const Vector& rhs) TVMET_CXX_ALWAYS_INLINE; TVMET_DECLARE_MACRO(neg, -) #undef TVMET_DECLARE_MACRO /********************************************************* * PART II: IMPLEMENTATION *********************************************************/ /** * \fn operator<<(std::ostream& os, const Vector& rhs) * \brief Overload operator for i/o * \ingroup _binary_operator */ template inline std::ostream& operator<<(std::ostream& os, const Vector& rhs) { return rhs.print_on(os); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Member operators (arithmetic and bit ops) *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * update_operator(Vector, Vector) * update_operator(Vector, XprVector) * Note: per se element wise */ #define TVMET_IMPLEMENT_MACRO(NAME, OP) \ template \ inline Vector& \ operator OP (Vector& lhs, const Vector& rhs) { \ return lhs.M_##NAME(rhs); \ } \ \ template \ inline Vector& \ operator OP (Vector& lhs, const XprVector& rhs) { \ return lhs.M_##NAME(rhs); \ } TVMET_IMPLEMENT_MACRO(add_eq, +=) // per se element wise TVMET_IMPLEMENT_MACRO(sub_eq, -=) // per se element wise TVMET_IMPLEMENT_MACRO(mul_eq, *=) // per se element wise namespace element_wise { TVMET_IMPLEMENT_MACRO(div_eq, /=) // not defined for vectors } #undef TVMET_IMPLEMENT_MACRO /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Vector arithmetic operators implemented by functions * add, sub, mul and div *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * operator(Vector, Vector) * operator(Vector, XprVector) * operator(XprVector, Vector) */ #define TVMET_IMPLEMENT_MACRO(NAME, OP) \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME, \ VectorConstReference, \ VectorConstReference \ >, \ Sz \ > \ operator OP (const Vector& lhs, const Vector& rhs) { \ return NAME (lhs, rhs); \ } \ \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME, \ XprVector, \ VectorConstReference \ >, \ Sz \ > \ operator OP (const XprVector& lhs, const Vector& rhs) { \ return NAME (lhs, rhs); \ } \ \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME, \ VectorConstReference, \ XprVector \ >, \ Sz \ > \ operator OP (const Vector& lhs, const XprVector& rhs) { \ return NAME (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 /* * operator(Vector, POD) * operator(POD, Vector) * Note: operations +,-,*,/ are per se element wise */ #define TVMET_IMPLEMENT_MACRO(NAME, OP, POD) \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME< T, POD >, \ VectorConstReference, \ XprLiteral< POD > \ >, \ Sz \ > \ operator OP (const Vector& lhs, POD rhs) { \ return NAME (lhs, rhs); \ } \ \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME< POD, T>, \ XprLiteral< POD >, \ VectorConstReference \ >, \ Sz \ > \ operator OP (POD lhs, const Vector& rhs) { \ return NAME (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) /* * operator(Vector, Sz>, std::complex) * operator(std::complex, Vector, Sz>) * Note: operations +,-,*,/ are per se element wise * \todo type promotion */ #define TVMET_IMPLEMENT_MACRO(NAME, OP) \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME< std::complex, std::complex >, \ VectorConstReference< std::complex, Sz>, \ XprLiteral< std::complex > \ >, \ Sz \ > \ operator OP (const Vector, Sz>& lhs, \ const std::complex& rhs) { \ return NAME (lhs, rhs); \ } \ \ template \ inline \ XprVector< \ XprBinOp< \ Fcnl_##NAME< std::complex, std::complex >, \ XprLiteral< std::complex >, \ VectorConstReference< std::complex, Sz> \ >, \ Sz \ > \ operator OP (const std::complex& lhs, \ const Vector< std::complex, Sz>& rhs) { \ return NAME (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 TVMET_IMPLEMENT_MACRO(div, /) // per se element wise #undef TVMET_IMPLEMENT_MACRO #endif // defined(TVMET_HAVE_COMPLEX) /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * global unary operators *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * unary_operator(Vector) * Note: per se element wise */ #define TVMET_IMPLEMENT_MACRO(NAME, OP) \ template \ inline \ XprVector< \ XprUnOp< \ Fcnl_##NAME, \ VectorConstReference \ >, \ Sz \ > \ operator OP (const Vector& rhs) { \ typedef XprUnOp< \ Fcnl_##NAME, \ VectorConstReference \ > expr_type; \ return XprVector(expr_type(rhs.const_ref())); \ } TVMET_IMPLEMENT_MACRO(neg, -) #undef TVMET_IMPLEMENT_MACRO } // namespace tvmet #endif // TVMET_VECTOR_OPERATORS_H // Local Variables: // mode:C++ // End: