Shuto Naruse
/
Eurobot2012_Secondary
Eurobot2012_Secondary
Fork of Eurobot_2012_Secondary by
tvmet/xpr/VectorFunctions.h@1:cc2a9eb0bd55, 2012-10-17 (annotated)
- Committer:
- narshu
- Date:
- Wed Oct 17 22:25:31 2012 +0000
- Revision:
- 1:cc2a9eb0bd55
Commit before publishing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
narshu | 1:cc2a9eb0bd55 | 1 | /* |
narshu | 1:cc2a9eb0bd55 | 2 | * Tiny Vector Matrix Library |
narshu | 1:cc2a9eb0bd55 | 3 | * Dense Vector Matrix Libary of Tiny size using Expression Templates |
narshu | 1:cc2a9eb0bd55 | 4 | * |
narshu | 1:cc2a9eb0bd55 | 5 | * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net> |
narshu | 1:cc2a9eb0bd55 | 6 | * |
narshu | 1:cc2a9eb0bd55 | 7 | * This library is free software; you can redistribute it and/or |
narshu | 1:cc2a9eb0bd55 | 8 | * modify it under the terms of the GNU Lesser General Public |
narshu | 1:cc2a9eb0bd55 | 9 | * License as published by the Free Software Foundation; either |
narshu | 1:cc2a9eb0bd55 | 10 | * version 2.1 of the License, or (at your option) any later version. |
narshu | 1:cc2a9eb0bd55 | 11 | * |
narshu | 1:cc2a9eb0bd55 | 12 | * This library is distributed in the hope that it will be useful, |
narshu | 1:cc2a9eb0bd55 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
narshu | 1:cc2a9eb0bd55 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
narshu | 1:cc2a9eb0bd55 | 15 | * Lesser General Public License for more details. |
narshu | 1:cc2a9eb0bd55 | 16 | * |
narshu | 1:cc2a9eb0bd55 | 17 | * You should have received a copy of the GNU Lesser General Public |
narshu | 1:cc2a9eb0bd55 | 18 | * License along with this library; if not, write to the Free Software |
narshu | 1:cc2a9eb0bd55 | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
narshu | 1:cc2a9eb0bd55 | 20 | * |
narshu | 1:cc2a9eb0bd55 | 21 | * $Id: VectorFunctions.h,v 1.21 2007-06-23 15:59:00 opetzold Exp $ |
narshu | 1:cc2a9eb0bd55 | 22 | */ |
narshu | 1:cc2a9eb0bd55 | 23 | |
narshu | 1:cc2a9eb0bd55 | 24 | #ifndef TVMET_XPR_VECTOR_FUNCTIONS_H |
narshu | 1:cc2a9eb0bd55 | 25 | #define TVMET_XPR_VECTOR_FUNCTIONS_H |
narshu | 1:cc2a9eb0bd55 | 26 | |
narshu | 1:cc2a9eb0bd55 | 27 | namespace tvmet { |
narshu | 1:cc2a9eb0bd55 | 28 | |
narshu | 1:cc2a9eb0bd55 | 29 | |
narshu | 1:cc2a9eb0bd55 | 30 | /* forwards */ |
narshu | 1:cc2a9eb0bd55 | 31 | template<class T, std::size_t Sz> class Vector; |
narshu | 1:cc2a9eb0bd55 | 32 | |
narshu | 1:cc2a9eb0bd55 | 33 | |
narshu | 1:cc2a9eb0bd55 | 34 | /********************************************************* |
narshu | 1:cc2a9eb0bd55 | 35 | * PART I: DECLARATION |
narshu | 1:cc2a9eb0bd55 | 36 | *********************************************************/ |
narshu | 1:cc2a9eb0bd55 | 37 | |
narshu | 1:cc2a9eb0bd55 | 38 | |
narshu | 1:cc2a9eb0bd55 | 39 | /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
narshu | 1:cc2a9eb0bd55 | 40 | * Vector arithmetic functions add, sub, mul and div |
narshu | 1:cc2a9eb0bd55 | 41 | *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
narshu | 1:cc2a9eb0bd55 | 42 | |
narshu | 1:cc2a9eb0bd55 | 43 | |
narshu | 1:cc2a9eb0bd55 | 44 | /* |
narshu | 1:cc2a9eb0bd55 | 45 | * function(XprVector<E1, Sz>, XprVector<E2, Sz>) |
narshu | 1:cc2a9eb0bd55 | 46 | */ |
narshu | 1:cc2a9eb0bd55 | 47 | #define TVMET_DECLARE_MACRO(NAME) \ |
narshu | 1:cc2a9eb0bd55 | 48 | template<class E1, class E2, std::size_t Sz> \ |
narshu | 1:cc2a9eb0bd55 | 49 | XprVector< \ |
narshu | 1:cc2a9eb0bd55 | 50 | XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 51 | Fcnl_##NAME<typename E1::value_type, typename E2::value_type>, \ |
narshu | 1:cc2a9eb0bd55 | 52 | XprVector<E1, Sz>, \ |
narshu | 1:cc2a9eb0bd55 | 53 | XprVector<E2, Sz> \ |
narshu | 1:cc2a9eb0bd55 | 54 | >, \ |
narshu | 1:cc2a9eb0bd55 | 55 | Sz \ |
narshu | 1:cc2a9eb0bd55 | 56 | > \ |
narshu | 1:cc2a9eb0bd55 | 57 | NAME (const XprVector<E1, Sz>& lhs, \ |
narshu | 1:cc2a9eb0bd55 | 58 | const XprVector<E2, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 59 | |
narshu | 1:cc2a9eb0bd55 | 60 | TVMET_DECLARE_MACRO(add) // per se element wise |
narshu | 1:cc2a9eb0bd55 | 61 | TVMET_DECLARE_MACRO(sub) // per se element wise |
narshu | 1:cc2a9eb0bd55 | 62 | TVMET_DECLARE_MACRO(mul) // per se element wise |
narshu | 1:cc2a9eb0bd55 | 63 | namespace element_wise { |
narshu | 1:cc2a9eb0bd55 | 64 | TVMET_DECLARE_MACRO(div) // not defined for vectors |
narshu | 1:cc2a9eb0bd55 | 65 | } |
narshu | 1:cc2a9eb0bd55 | 66 | |
narshu | 1:cc2a9eb0bd55 | 67 | #undef TVMET_DECLARE_MACRO |
narshu | 1:cc2a9eb0bd55 | 68 | |
narshu | 1:cc2a9eb0bd55 | 69 | |
narshu | 1:cc2a9eb0bd55 | 70 | /* |
narshu | 1:cc2a9eb0bd55 | 71 | * function(XprVector<E, Sz>, POD) |
narshu | 1:cc2a9eb0bd55 | 72 | * function(POD, XprVector<E, Sz>) |
narshu | 1:cc2a9eb0bd55 | 73 | * Note: - operations +,-,*,/ are per se element wise |
narshu | 1:cc2a9eb0bd55 | 74 | */ |
narshu | 1:cc2a9eb0bd55 | 75 | #define TVMET_DECLARE_MACRO(NAME, POD) \ |
narshu | 1:cc2a9eb0bd55 | 76 | template<class E, std::size_t Sz> \ |
narshu | 1:cc2a9eb0bd55 | 77 | XprVector< \ |
narshu | 1:cc2a9eb0bd55 | 78 | XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 79 | Fcnl_##NAME< typename E::value_type, POD >, \ |
narshu | 1:cc2a9eb0bd55 | 80 | XprVector<E, Sz>, \ |
narshu | 1:cc2a9eb0bd55 | 81 | XprLiteral< POD > \ |
narshu | 1:cc2a9eb0bd55 | 82 | >, \ |
narshu | 1:cc2a9eb0bd55 | 83 | Sz \ |
narshu | 1:cc2a9eb0bd55 | 84 | > \ |
narshu | 1:cc2a9eb0bd55 | 85 | NAME (const XprVector<E, Sz>& lhs, \ |
narshu | 1:cc2a9eb0bd55 | 86 | POD rhs) TVMET_CXX_ALWAYS_INLINE; \ |
narshu | 1:cc2a9eb0bd55 | 87 | \ |
narshu | 1:cc2a9eb0bd55 | 88 | template<class E, std::size_t Sz> \ |
narshu | 1:cc2a9eb0bd55 | 89 | XprVector< \ |
narshu | 1:cc2a9eb0bd55 | 90 | XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 91 | Fcnl_##NAME< POD, typename E::value_type>, \ |
narshu | 1:cc2a9eb0bd55 | 92 | XprLiteral< POD >, \ |
narshu | 1:cc2a9eb0bd55 | 93 | XprVector<E, Sz> \ |
narshu | 1:cc2a9eb0bd55 | 94 | >, \ |
narshu | 1:cc2a9eb0bd55 | 95 | Sz \ |
narshu | 1:cc2a9eb0bd55 | 96 | > \ |
narshu | 1:cc2a9eb0bd55 | 97 | NAME (POD lhs, \ |
narshu | 1:cc2a9eb0bd55 | 98 | const XprVector<E, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 99 | |
narshu | 1:cc2a9eb0bd55 | 100 | TVMET_DECLARE_MACRO(add, int) |
narshu | 1:cc2a9eb0bd55 | 101 | TVMET_DECLARE_MACRO(sub, int) |
narshu | 1:cc2a9eb0bd55 | 102 | TVMET_DECLARE_MACRO(mul, int) |
narshu | 1:cc2a9eb0bd55 | 103 | TVMET_DECLARE_MACRO(div, int) |
narshu | 1:cc2a9eb0bd55 | 104 | |
narshu | 1:cc2a9eb0bd55 | 105 | #if defined(TVMET_HAVE_LONG_LONG) |
narshu | 1:cc2a9eb0bd55 | 106 | TVMET_DECLARE_MACRO(add, long long int) |
narshu | 1:cc2a9eb0bd55 | 107 | TVMET_DECLARE_MACRO(sub, long long int) |
narshu | 1:cc2a9eb0bd55 | 108 | TVMET_DECLARE_MACRO(mul, long long int) |
narshu | 1:cc2a9eb0bd55 | 109 | TVMET_DECLARE_MACRO(div, long long int) |
narshu | 1:cc2a9eb0bd55 | 110 | #endif |
narshu | 1:cc2a9eb0bd55 | 111 | |
narshu | 1:cc2a9eb0bd55 | 112 | TVMET_DECLARE_MACRO(add, float) |
narshu | 1:cc2a9eb0bd55 | 113 | TVMET_DECLARE_MACRO(sub, float) |
narshu | 1:cc2a9eb0bd55 | 114 | TVMET_DECLARE_MACRO(mul, float) |
narshu | 1:cc2a9eb0bd55 | 115 | TVMET_DECLARE_MACRO(div, float) |
narshu | 1:cc2a9eb0bd55 | 116 | |
narshu | 1:cc2a9eb0bd55 | 117 | TVMET_DECLARE_MACRO(add, double) |
narshu | 1:cc2a9eb0bd55 | 118 | TVMET_DECLARE_MACRO(sub, double) |
narshu | 1:cc2a9eb0bd55 | 119 | TVMET_DECLARE_MACRO(mul, double) |
narshu | 1:cc2a9eb0bd55 | 120 | TVMET_DECLARE_MACRO(div, double) |
narshu | 1:cc2a9eb0bd55 | 121 | |
narshu | 1:cc2a9eb0bd55 | 122 | #if defined(TVMET_HAVE_LONG_DOUBLE) |
narshu | 1:cc2a9eb0bd55 | 123 | TVMET_DECLARE_MACRO(add, long double) |
narshu | 1:cc2a9eb0bd55 | 124 | TVMET_DECLARE_MACRO(sub, long double) |
narshu | 1:cc2a9eb0bd55 | 125 | TVMET_DECLARE_MACRO(mul, long double) |
narshu | 1:cc2a9eb0bd55 | 126 | TVMET_DECLARE_MACRO(div, long double) |
narshu | 1:cc2a9eb0bd55 | 127 | #endif |
narshu | 1:cc2a9eb0bd55 | 128 | |
narshu | 1:cc2a9eb0bd55 | 129 | #undef TVMET_DECLARE_MACRO |
narshu | 1:cc2a9eb0bd55 | 130 | |
narshu | 1:cc2a9eb0bd55 | 131 | |
narshu | 1:cc2a9eb0bd55 | 132 | #if defined(TVMET_HAVE_COMPLEX) |
narshu | 1:cc2a9eb0bd55 | 133 | /* |
narshu | 1:cc2a9eb0bd55 | 134 | * function(XprMatrix<E, Rows, Cols>, complex<T>) |
narshu | 1:cc2a9eb0bd55 | 135 | * function(complex<T>, XprMatrix<E, Rows, Cols>) |
narshu | 1:cc2a9eb0bd55 | 136 | * Note: - operations +,-,*,/ are per se element wise |
narshu | 1:cc2a9eb0bd55 | 137 | * \todo type promotion |
narshu | 1:cc2a9eb0bd55 | 138 | */ |
narshu | 1:cc2a9eb0bd55 | 139 | #define TVMET_DECLARE_MACRO(NAME) \ |
narshu | 1:cc2a9eb0bd55 | 140 | template<class E, std::size_t Sz, class T> \ |
narshu | 1:cc2a9eb0bd55 | 141 | XprVector< \ |
narshu | 1:cc2a9eb0bd55 | 142 | XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 143 | Fcnl_##NAME< typename E::value_type, std::complex<T> >, \ |
narshu | 1:cc2a9eb0bd55 | 144 | XprVector<E, Sz>, \ |
narshu | 1:cc2a9eb0bd55 | 145 | XprLiteral< std::complex<T> > \ |
narshu | 1:cc2a9eb0bd55 | 146 | >, \ |
narshu | 1:cc2a9eb0bd55 | 147 | Sz \ |
narshu | 1:cc2a9eb0bd55 | 148 | > \ |
narshu | 1:cc2a9eb0bd55 | 149 | NAME (const XprVector<E, Sz>& lhs, \ |
narshu | 1:cc2a9eb0bd55 | 150 | const std::complex<T>& rhs) TVMET_CXX_ALWAYS_INLINE; \ |
narshu | 1:cc2a9eb0bd55 | 151 | \ |
narshu | 1:cc2a9eb0bd55 | 152 | template<class E, std::size_t Sz, class T> \ |
narshu | 1:cc2a9eb0bd55 | 153 | XprVector< \ |
narshu | 1:cc2a9eb0bd55 | 154 | XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 155 | Fcnl_##NAME< std::complex<T>, typename E::value_type>, \ |
narshu | 1:cc2a9eb0bd55 | 156 | XprLiteral< std::complex<T> >, \ |
narshu | 1:cc2a9eb0bd55 | 157 | XprVector<E, Sz> \ |
narshu | 1:cc2a9eb0bd55 | 158 | >, \ |
narshu | 1:cc2a9eb0bd55 | 159 | Sz \ |
narshu | 1:cc2a9eb0bd55 | 160 | > \ |
narshu | 1:cc2a9eb0bd55 | 161 | NAME (const std::complex<T>& lhs, \ |
narshu | 1:cc2a9eb0bd55 | 162 | const XprVector<E, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 163 | |
narshu | 1:cc2a9eb0bd55 | 164 | TVMET_DECLARE_MACRO(add) |
narshu | 1:cc2a9eb0bd55 | 165 | TVMET_DECLARE_MACRO(sub) |
narshu | 1:cc2a9eb0bd55 | 166 | TVMET_DECLARE_MACRO(mul) |
narshu | 1:cc2a9eb0bd55 | 167 | TVMET_DECLARE_MACRO(div) |
narshu | 1:cc2a9eb0bd55 | 168 | |
narshu | 1:cc2a9eb0bd55 | 169 | #undef TVMET_DECLARE_MACRO |
narshu | 1:cc2a9eb0bd55 | 170 | |
narshu | 1:cc2a9eb0bd55 | 171 | #endif // defined(TVMET_HAVE_COMPLEX) |
narshu | 1:cc2a9eb0bd55 | 172 | |
narshu | 1:cc2a9eb0bd55 | 173 | |
narshu | 1:cc2a9eb0bd55 | 174 | /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
narshu | 1:cc2a9eb0bd55 | 175 | * vector specific functions |
narshu | 1:cc2a9eb0bd55 | 176 | *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
narshu | 1:cc2a9eb0bd55 | 177 | |
narshu | 1:cc2a9eb0bd55 | 178 | |
narshu | 1:cc2a9eb0bd55 | 179 | template<class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 180 | typename NumericTraits<typename E::value_type>::sum_type |
narshu | 1:cc2a9eb0bd55 | 181 | sum(const XprVector<E, Sz>& v) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 182 | |
narshu | 1:cc2a9eb0bd55 | 183 | |
narshu | 1:cc2a9eb0bd55 | 184 | template<class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 185 | typename NumericTraits<typename E::value_type>::sum_type |
narshu | 1:cc2a9eb0bd55 | 186 | product(const XprVector<E, Sz>& v) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 187 | |
narshu | 1:cc2a9eb0bd55 | 188 | |
narshu | 1:cc2a9eb0bd55 | 189 | template<class E1, class E2, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 190 | typename PromoteTraits< |
narshu | 1:cc2a9eb0bd55 | 191 | typename E1::value_type, |
narshu | 1:cc2a9eb0bd55 | 192 | typename E2::value_type |
narshu | 1:cc2a9eb0bd55 | 193 | >::value_type |
narshu | 1:cc2a9eb0bd55 | 194 | dot(const XprVector<E1, Sz>& lhs, |
narshu | 1:cc2a9eb0bd55 | 195 | const XprVector<E2, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 196 | |
narshu | 1:cc2a9eb0bd55 | 197 | |
narshu | 1:cc2a9eb0bd55 | 198 | template<class T, class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 199 | typename PromoteTraits<T, typename E::value_type>::value_type |
narshu | 1:cc2a9eb0bd55 | 200 | dot(const Vector<T, Sz>& lhs, |
narshu | 1:cc2a9eb0bd55 | 201 | const XprVector<E, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 202 | |
narshu | 1:cc2a9eb0bd55 | 203 | |
narshu | 1:cc2a9eb0bd55 | 204 | template<class E, class T, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 205 | typename PromoteTraits<T, typename E::value_type>::value_type |
narshu | 1:cc2a9eb0bd55 | 206 | dot(const XprVector<E, Sz>& lhs, |
narshu | 1:cc2a9eb0bd55 | 207 | const Vector<T, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 208 | |
narshu | 1:cc2a9eb0bd55 | 209 | |
narshu | 1:cc2a9eb0bd55 | 210 | template<class E1, class E2> |
narshu | 1:cc2a9eb0bd55 | 211 | Vector< |
narshu | 1:cc2a9eb0bd55 | 212 | typename PromoteTraits< |
narshu | 1:cc2a9eb0bd55 | 213 | typename E1::value_type, |
narshu | 1:cc2a9eb0bd55 | 214 | typename E2::value_type |
narshu | 1:cc2a9eb0bd55 | 215 | >::value_type, |
narshu | 1:cc2a9eb0bd55 | 216 | 3 |
narshu | 1:cc2a9eb0bd55 | 217 | > |
narshu | 1:cc2a9eb0bd55 | 218 | cross(const XprVector<E1, 3>& lhs, |
narshu | 1:cc2a9eb0bd55 | 219 | const XprVector<E2, 3>& rhs) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 220 | |
narshu | 1:cc2a9eb0bd55 | 221 | |
narshu | 1:cc2a9eb0bd55 | 222 | template<class T, class E> |
narshu | 1:cc2a9eb0bd55 | 223 | Vector< |
narshu | 1:cc2a9eb0bd55 | 224 | typename PromoteTraits<T, typename E::value_type>::value_type, 3> |
narshu | 1:cc2a9eb0bd55 | 225 | cross(const Vector<T, 3>& lhs, |
narshu | 1:cc2a9eb0bd55 | 226 | const XprVector<E, 3>& rhs) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 227 | |
narshu | 1:cc2a9eb0bd55 | 228 | |
narshu | 1:cc2a9eb0bd55 | 229 | template<class E, class T> |
narshu | 1:cc2a9eb0bd55 | 230 | Vector< |
narshu | 1:cc2a9eb0bd55 | 231 | typename PromoteTraits<T, typename E::value_type>::value_type, 3> |
narshu | 1:cc2a9eb0bd55 | 232 | cross(const XprVector<E, 3>& lhs, |
narshu | 1:cc2a9eb0bd55 | 233 | const Vector<T, 3>& rhs) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 234 | |
narshu | 1:cc2a9eb0bd55 | 235 | |
narshu | 1:cc2a9eb0bd55 | 236 | template<class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 237 | typename NumericTraits<typename E::value_type>::sum_type |
narshu | 1:cc2a9eb0bd55 | 238 | norm1(const XprVector<E, Sz>& v) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 239 | |
narshu | 1:cc2a9eb0bd55 | 240 | |
narshu | 1:cc2a9eb0bd55 | 241 | template<class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 242 | typename NumericTraits<typename E::value_type>::sum_type |
narshu | 1:cc2a9eb0bd55 | 243 | norm2(const XprVector<E, Sz>& v) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 244 | |
narshu | 1:cc2a9eb0bd55 | 245 | |
narshu | 1:cc2a9eb0bd55 | 246 | template<class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 247 | XprVector< |
narshu | 1:cc2a9eb0bd55 | 248 | XprBinOp< |
narshu | 1:cc2a9eb0bd55 | 249 | Fcnl_div<typename E::value_type, typename E::value_type>, |
narshu | 1:cc2a9eb0bd55 | 250 | XprVector<E, Sz>, |
narshu | 1:cc2a9eb0bd55 | 251 | XprLiteral<typename E::value_type> |
narshu | 1:cc2a9eb0bd55 | 252 | >, |
narshu | 1:cc2a9eb0bd55 | 253 | Sz |
narshu | 1:cc2a9eb0bd55 | 254 | > |
narshu | 1:cc2a9eb0bd55 | 255 | normalize(const XprVector<E, Sz>& v) TVMET_CXX_ALWAYS_INLINE; |
narshu | 1:cc2a9eb0bd55 | 256 | |
narshu | 1:cc2a9eb0bd55 | 257 | |
narshu | 1:cc2a9eb0bd55 | 258 | /********************************************************* |
narshu | 1:cc2a9eb0bd55 | 259 | * PART II: IMPLEMENTATION |
narshu | 1:cc2a9eb0bd55 | 260 | *********************************************************/ |
narshu | 1:cc2a9eb0bd55 | 261 | |
narshu | 1:cc2a9eb0bd55 | 262 | |
narshu | 1:cc2a9eb0bd55 | 263 | /* |
narshu | 1:cc2a9eb0bd55 | 264 | * function(XprVector<E1, Sz>, XprVector<E2, Sz>) |
narshu | 1:cc2a9eb0bd55 | 265 | */ |
narshu | 1:cc2a9eb0bd55 | 266 | #define TVMET_IMPLEMENT_MACRO(NAME) \ |
narshu | 1:cc2a9eb0bd55 | 267 | template<class E1, class E2, std::size_t Sz> \ |
narshu | 1:cc2a9eb0bd55 | 268 | inline \ |
narshu | 1:cc2a9eb0bd55 | 269 | XprVector< \ |
narshu | 1:cc2a9eb0bd55 | 270 | XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 271 | Fcnl_##NAME<typename E1::value_type, typename E2::value_type>, \ |
narshu | 1:cc2a9eb0bd55 | 272 | XprVector<E1, Sz>, \ |
narshu | 1:cc2a9eb0bd55 | 273 | XprVector<E2, Sz> \ |
narshu | 1:cc2a9eb0bd55 | 274 | >, \ |
narshu | 1:cc2a9eb0bd55 | 275 | Sz \ |
narshu | 1:cc2a9eb0bd55 | 276 | > \ |
narshu | 1:cc2a9eb0bd55 | 277 | NAME (const XprVector<E1, Sz>& lhs, const XprVector<E2, Sz>& rhs) { \ |
narshu | 1:cc2a9eb0bd55 | 278 | typedef XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 279 | Fcnl_##NAME<typename E1::value_type, typename E2::value_type>, \ |
narshu | 1:cc2a9eb0bd55 | 280 | XprVector<E1, Sz>, \ |
narshu | 1:cc2a9eb0bd55 | 281 | XprVector<E2, Sz> \ |
narshu | 1:cc2a9eb0bd55 | 282 | > expr_type; \ |
narshu | 1:cc2a9eb0bd55 | 283 | return XprVector<expr_type, Sz>(expr_type(lhs, rhs)); \ |
narshu | 1:cc2a9eb0bd55 | 284 | } |
narshu | 1:cc2a9eb0bd55 | 285 | |
narshu | 1:cc2a9eb0bd55 | 286 | TVMET_IMPLEMENT_MACRO(add) // per se element wise |
narshu | 1:cc2a9eb0bd55 | 287 | TVMET_IMPLEMENT_MACRO(sub) // per se element wise |
narshu | 1:cc2a9eb0bd55 | 288 | TVMET_IMPLEMENT_MACRO(mul) // per se element wise |
narshu | 1:cc2a9eb0bd55 | 289 | namespace element_wise { |
narshu | 1:cc2a9eb0bd55 | 290 | TVMET_IMPLEMENT_MACRO(div) // not defined for vectors |
narshu | 1:cc2a9eb0bd55 | 291 | } |
narshu | 1:cc2a9eb0bd55 | 292 | |
narshu | 1:cc2a9eb0bd55 | 293 | #undef TVMET_IMPLEMENT_MACRO |
narshu | 1:cc2a9eb0bd55 | 294 | |
narshu | 1:cc2a9eb0bd55 | 295 | |
narshu | 1:cc2a9eb0bd55 | 296 | /* |
narshu | 1:cc2a9eb0bd55 | 297 | * function(XprVector<E, Sz>, POD) |
narshu | 1:cc2a9eb0bd55 | 298 | * function(POD, XprVector<E, Sz>) |
narshu | 1:cc2a9eb0bd55 | 299 | * Note: - operations +,-,*,/ are per se element wise |
narshu | 1:cc2a9eb0bd55 | 300 | */ |
narshu | 1:cc2a9eb0bd55 | 301 | #define TVMET_IMPLEMENT_MACRO(NAME, POD) \ |
narshu | 1:cc2a9eb0bd55 | 302 | template<class E, std::size_t Sz> \ |
narshu | 1:cc2a9eb0bd55 | 303 | inline \ |
narshu | 1:cc2a9eb0bd55 | 304 | XprVector< \ |
narshu | 1:cc2a9eb0bd55 | 305 | XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 306 | Fcnl_##NAME< typename E::value_type, POD >, \ |
narshu | 1:cc2a9eb0bd55 | 307 | XprVector<E, Sz>, \ |
narshu | 1:cc2a9eb0bd55 | 308 | XprLiteral< POD > \ |
narshu | 1:cc2a9eb0bd55 | 309 | >, \ |
narshu | 1:cc2a9eb0bd55 | 310 | Sz \ |
narshu | 1:cc2a9eb0bd55 | 311 | > \ |
narshu | 1:cc2a9eb0bd55 | 312 | NAME (const XprVector<E, Sz>& lhs, POD rhs) { \ |
narshu | 1:cc2a9eb0bd55 | 313 | typedef XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 314 | Fcnl_##NAME< typename E::value_type, POD >, \ |
narshu | 1:cc2a9eb0bd55 | 315 | XprVector<E, Sz>, \ |
narshu | 1:cc2a9eb0bd55 | 316 | XprLiteral< POD > \ |
narshu | 1:cc2a9eb0bd55 | 317 | > expr_type; \ |
narshu | 1:cc2a9eb0bd55 | 318 | return XprVector<expr_type, Sz>( \ |
narshu | 1:cc2a9eb0bd55 | 319 | expr_type(lhs, XprLiteral< POD >(rhs))); \ |
narshu | 1:cc2a9eb0bd55 | 320 | } \ |
narshu | 1:cc2a9eb0bd55 | 321 | \ |
narshu | 1:cc2a9eb0bd55 | 322 | template<class E, std::size_t Sz> \ |
narshu | 1:cc2a9eb0bd55 | 323 | inline \ |
narshu | 1:cc2a9eb0bd55 | 324 | XprVector< \ |
narshu | 1:cc2a9eb0bd55 | 325 | XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 326 | Fcnl_##NAME< POD, typename E::value_type>, \ |
narshu | 1:cc2a9eb0bd55 | 327 | XprLiteral< POD >, \ |
narshu | 1:cc2a9eb0bd55 | 328 | XprVector<E, Sz> \ |
narshu | 1:cc2a9eb0bd55 | 329 | >, \ |
narshu | 1:cc2a9eb0bd55 | 330 | Sz \ |
narshu | 1:cc2a9eb0bd55 | 331 | > \ |
narshu | 1:cc2a9eb0bd55 | 332 | NAME (POD lhs, const XprVector<E, Sz>& rhs) { \ |
narshu | 1:cc2a9eb0bd55 | 333 | typedef XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 334 | Fcnl_##NAME< POD, typename E::value_type>, \ |
narshu | 1:cc2a9eb0bd55 | 335 | XprLiteral< POD >, \ |
narshu | 1:cc2a9eb0bd55 | 336 | XprVector<E, Sz> \ |
narshu | 1:cc2a9eb0bd55 | 337 | > expr_type; \ |
narshu | 1:cc2a9eb0bd55 | 338 | return XprVector<expr_type, Sz>( \ |
narshu | 1:cc2a9eb0bd55 | 339 | expr_type(XprLiteral< POD >(lhs), rhs)); \ |
narshu | 1:cc2a9eb0bd55 | 340 | } |
narshu | 1:cc2a9eb0bd55 | 341 | |
narshu | 1:cc2a9eb0bd55 | 342 | TVMET_IMPLEMENT_MACRO(add, int) |
narshu | 1:cc2a9eb0bd55 | 343 | TVMET_IMPLEMENT_MACRO(sub, int) |
narshu | 1:cc2a9eb0bd55 | 344 | TVMET_IMPLEMENT_MACRO(mul, int) |
narshu | 1:cc2a9eb0bd55 | 345 | TVMET_IMPLEMENT_MACRO(div, int) |
narshu | 1:cc2a9eb0bd55 | 346 | |
narshu | 1:cc2a9eb0bd55 | 347 | #if defined(TVMET_HAVE_LONG_LONG) |
narshu | 1:cc2a9eb0bd55 | 348 | TVMET_IMPLEMENT_MACRO(add, long long int) |
narshu | 1:cc2a9eb0bd55 | 349 | TVMET_IMPLEMENT_MACRO(sub, long long int) |
narshu | 1:cc2a9eb0bd55 | 350 | TVMET_IMPLEMENT_MACRO(mul, long long int) |
narshu | 1:cc2a9eb0bd55 | 351 | TVMET_IMPLEMENT_MACRO(div, long long int) |
narshu | 1:cc2a9eb0bd55 | 352 | #endif |
narshu | 1:cc2a9eb0bd55 | 353 | |
narshu | 1:cc2a9eb0bd55 | 354 | TVMET_IMPLEMENT_MACRO(add, float) |
narshu | 1:cc2a9eb0bd55 | 355 | TVMET_IMPLEMENT_MACRO(sub, float) |
narshu | 1:cc2a9eb0bd55 | 356 | TVMET_IMPLEMENT_MACRO(mul, float) |
narshu | 1:cc2a9eb0bd55 | 357 | TVMET_IMPLEMENT_MACRO(div, float) |
narshu | 1:cc2a9eb0bd55 | 358 | |
narshu | 1:cc2a9eb0bd55 | 359 | TVMET_IMPLEMENT_MACRO(add, double) |
narshu | 1:cc2a9eb0bd55 | 360 | TVMET_IMPLEMENT_MACRO(sub, double) |
narshu | 1:cc2a9eb0bd55 | 361 | TVMET_IMPLEMENT_MACRO(mul, double) |
narshu | 1:cc2a9eb0bd55 | 362 | TVMET_IMPLEMENT_MACRO(div, double) |
narshu | 1:cc2a9eb0bd55 | 363 | |
narshu | 1:cc2a9eb0bd55 | 364 | #if defined(TVMET_HAVE_LONG_DOUBLE) |
narshu | 1:cc2a9eb0bd55 | 365 | TVMET_IMPLEMENT_MACRO(add, long double) |
narshu | 1:cc2a9eb0bd55 | 366 | TVMET_IMPLEMENT_MACRO(sub, long double) |
narshu | 1:cc2a9eb0bd55 | 367 | TVMET_IMPLEMENT_MACRO(mul, long double) |
narshu | 1:cc2a9eb0bd55 | 368 | TVMET_IMPLEMENT_MACRO(div, long double) |
narshu | 1:cc2a9eb0bd55 | 369 | #endif |
narshu | 1:cc2a9eb0bd55 | 370 | |
narshu | 1:cc2a9eb0bd55 | 371 | #undef TVMET_IMPLEMENT_MACRO |
narshu | 1:cc2a9eb0bd55 | 372 | |
narshu | 1:cc2a9eb0bd55 | 373 | |
narshu | 1:cc2a9eb0bd55 | 374 | #if defined(TVMET_HAVE_COMPLEX) |
narshu | 1:cc2a9eb0bd55 | 375 | /* |
narshu | 1:cc2a9eb0bd55 | 376 | * function(XprMatrix<E, Rows, Cols>, complex<T>) |
narshu | 1:cc2a9eb0bd55 | 377 | * function(complex<T>, XprMatrix<E, Rows, Cols>) |
narshu | 1:cc2a9eb0bd55 | 378 | * Note: - operations +,-,*,/ are per se element wise |
narshu | 1:cc2a9eb0bd55 | 379 | * \todo type promotion |
narshu | 1:cc2a9eb0bd55 | 380 | */ |
narshu | 1:cc2a9eb0bd55 | 381 | #define TVMET_IMPLEMENT_MACRO(NAME) \ |
narshu | 1:cc2a9eb0bd55 | 382 | template<class E, std::size_t Sz, class T> \ |
narshu | 1:cc2a9eb0bd55 | 383 | inline \ |
narshu | 1:cc2a9eb0bd55 | 384 | XprVector< \ |
narshu | 1:cc2a9eb0bd55 | 385 | XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 386 | Fcnl_##NAME< typename E::value_type, std::complex<T> >, \ |
narshu | 1:cc2a9eb0bd55 | 387 | XprVector<E, Sz>, \ |
narshu | 1:cc2a9eb0bd55 | 388 | XprLiteral< std::complex<T> > \ |
narshu | 1:cc2a9eb0bd55 | 389 | >, \ |
narshu | 1:cc2a9eb0bd55 | 390 | Sz \ |
narshu | 1:cc2a9eb0bd55 | 391 | > \ |
narshu | 1:cc2a9eb0bd55 | 392 | NAME (const XprVector<E, Sz>& lhs, const std::complex<T>& rhs) { \ |
narshu | 1:cc2a9eb0bd55 | 393 | typedef XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 394 | Fcnl_##NAME< typename E::value_type, std::complex<T> >, \ |
narshu | 1:cc2a9eb0bd55 | 395 | XprVector<E, Sz>, \ |
narshu | 1:cc2a9eb0bd55 | 396 | XprLiteral< std::complex<T> > \ |
narshu | 1:cc2a9eb0bd55 | 397 | > expr_type; \ |
narshu | 1:cc2a9eb0bd55 | 398 | return XprVector<expr_type, Sz>( \ |
narshu | 1:cc2a9eb0bd55 | 399 | expr_type(lhs, XprLiteral< std::complex<T> >(rhs))); \ |
narshu | 1:cc2a9eb0bd55 | 400 | } \ |
narshu | 1:cc2a9eb0bd55 | 401 | \ |
narshu | 1:cc2a9eb0bd55 | 402 | template<class E, std::size_t Sz, class T> \ |
narshu | 1:cc2a9eb0bd55 | 403 | inline \ |
narshu | 1:cc2a9eb0bd55 | 404 | XprVector< \ |
narshu | 1:cc2a9eb0bd55 | 405 | XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 406 | Fcnl_##NAME< std::complex<T>, typename E::value_type>, \ |
narshu | 1:cc2a9eb0bd55 | 407 | XprLiteral< std::complex<T> >, \ |
narshu | 1:cc2a9eb0bd55 | 408 | XprVector<E, Sz> \ |
narshu | 1:cc2a9eb0bd55 | 409 | >, \ |
narshu | 1:cc2a9eb0bd55 | 410 | Sz \ |
narshu | 1:cc2a9eb0bd55 | 411 | > \ |
narshu | 1:cc2a9eb0bd55 | 412 | NAME (const std::complex<T>& lhs, const XprVector<E, Sz>& rhs) { \ |
narshu | 1:cc2a9eb0bd55 | 413 | typedef XprBinOp< \ |
narshu | 1:cc2a9eb0bd55 | 414 | Fcnl_##NAME< std::complex<T>, typename E::value_type>, \ |
narshu | 1:cc2a9eb0bd55 | 415 | XprLiteral< std::complex<T> >, \ |
narshu | 1:cc2a9eb0bd55 | 416 | XprVector<E, Sz> \ |
narshu | 1:cc2a9eb0bd55 | 417 | > expr_type; \ |
narshu | 1:cc2a9eb0bd55 | 418 | return XprVector<expr_type, Sz>( \ |
narshu | 1:cc2a9eb0bd55 | 419 | expr_type(XprLiteral< std::complex<T> >(lhs), rhs)); \ |
narshu | 1:cc2a9eb0bd55 | 420 | } |
narshu | 1:cc2a9eb0bd55 | 421 | |
narshu | 1:cc2a9eb0bd55 | 422 | TVMET_IMPLEMENT_MACRO(add) |
narshu | 1:cc2a9eb0bd55 | 423 | TVMET_IMPLEMENT_MACRO(sub) |
narshu | 1:cc2a9eb0bd55 | 424 | TVMET_IMPLEMENT_MACRO(mul) |
narshu | 1:cc2a9eb0bd55 | 425 | TVMET_IMPLEMENT_MACRO(div) |
narshu | 1:cc2a9eb0bd55 | 426 | |
narshu | 1:cc2a9eb0bd55 | 427 | #undef TVMET_IMPLEMENT_MACRO |
narshu | 1:cc2a9eb0bd55 | 428 | |
narshu | 1:cc2a9eb0bd55 | 429 | #endif // defined(TVMET_HAVE_COMPLEX) |
narshu | 1:cc2a9eb0bd55 | 430 | |
narshu | 1:cc2a9eb0bd55 | 431 | |
narshu | 1:cc2a9eb0bd55 | 432 | /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
narshu | 1:cc2a9eb0bd55 | 433 | * vector specific functions |
narshu | 1:cc2a9eb0bd55 | 434 | *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
narshu | 1:cc2a9eb0bd55 | 435 | |
narshu | 1:cc2a9eb0bd55 | 436 | |
narshu | 1:cc2a9eb0bd55 | 437 | /** |
narshu | 1:cc2a9eb0bd55 | 438 | * \fn sum(const XprVector<E, Sz>& v) |
narshu | 1:cc2a9eb0bd55 | 439 | * \brief Compute the sum of the vector expression. |
narshu | 1:cc2a9eb0bd55 | 440 | * \ingroup _unary_function |
narshu | 1:cc2a9eb0bd55 | 441 | * |
narshu | 1:cc2a9eb0bd55 | 442 | * Simply compute the sum of the given vector as: |
narshu | 1:cc2a9eb0bd55 | 443 | * \f[ |
narshu | 1:cc2a9eb0bd55 | 444 | * \sum_{i = 0}^{Sz-1} v[i] |
narshu | 1:cc2a9eb0bd55 | 445 | * \f] |
narshu | 1:cc2a9eb0bd55 | 446 | */ |
narshu | 1:cc2a9eb0bd55 | 447 | template<class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 448 | inline |
narshu | 1:cc2a9eb0bd55 | 449 | typename NumericTraits<typename E::value_type>::sum_type |
narshu | 1:cc2a9eb0bd55 | 450 | sum(const XprVector<E, Sz>& v) { |
narshu | 1:cc2a9eb0bd55 | 451 | return meta::Vector<Sz>::sum(v); |
narshu | 1:cc2a9eb0bd55 | 452 | } |
narshu | 1:cc2a9eb0bd55 | 453 | |
narshu | 1:cc2a9eb0bd55 | 454 | |
narshu | 1:cc2a9eb0bd55 | 455 | /** |
narshu | 1:cc2a9eb0bd55 | 456 | * \fn product(const XprVector<E, Sz>& v) |
narshu | 1:cc2a9eb0bd55 | 457 | * \brief Compute the product of the vector elements. |
narshu | 1:cc2a9eb0bd55 | 458 | * \ingroup _unary_function |
narshu | 1:cc2a9eb0bd55 | 459 | * |
narshu | 1:cc2a9eb0bd55 | 460 | * Simply computer the product of the given vector expression as: |
narshu | 1:cc2a9eb0bd55 | 461 | * \f[ |
narshu | 1:cc2a9eb0bd55 | 462 | * \prod_{i = 0}^{Sz - 1} v[i] |
narshu | 1:cc2a9eb0bd55 | 463 | * \f] |
narshu | 1:cc2a9eb0bd55 | 464 | */ |
narshu | 1:cc2a9eb0bd55 | 465 | template<class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 466 | inline |
narshu | 1:cc2a9eb0bd55 | 467 | typename NumericTraits<typename E::value_type>::sum_type |
narshu | 1:cc2a9eb0bd55 | 468 | product(const XprVector<E, Sz>& v) { |
narshu | 1:cc2a9eb0bd55 | 469 | return meta::Vector<Sz>::product(v); |
narshu | 1:cc2a9eb0bd55 | 470 | } |
narshu | 1:cc2a9eb0bd55 | 471 | |
narshu | 1:cc2a9eb0bd55 | 472 | |
narshu | 1:cc2a9eb0bd55 | 473 | /** |
narshu | 1:cc2a9eb0bd55 | 474 | * \fn dot(const XprVector<E1, Sz>& lhs, const XprVector<E2, Sz>& rhs) |
narshu | 1:cc2a9eb0bd55 | 475 | * \brief Compute the dot/inner product |
narshu | 1:cc2a9eb0bd55 | 476 | * \ingroup _binary_function |
narshu | 1:cc2a9eb0bd55 | 477 | * |
narshu | 1:cc2a9eb0bd55 | 478 | * Compute the dot product as: |
narshu | 1:cc2a9eb0bd55 | 479 | * \f[ |
narshu | 1:cc2a9eb0bd55 | 480 | * \sum_{i = 0}^{Sz - 1} ( lhs[i] * rhs[i] ) |
narshu | 1:cc2a9eb0bd55 | 481 | * \f] |
narshu | 1:cc2a9eb0bd55 | 482 | * where lhs is a column vector and rhs is a row vector, both vectors |
narshu | 1:cc2a9eb0bd55 | 483 | * have the same dimension. |
narshu | 1:cc2a9eb0bd55 | 484 | */ |
narshu | 1:cc2a9eb0bd55 | 485 | template<class E1, class E2, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 486 | inline |
narshu | 1:cc2a9eb0bd55 | 487 | typename PromoteTraits< |
narshu | 1:cc2a9eb0bd55 | 488 | typename E1::value_type, |
narshu | 1:cc2a9eb0bd55 | 489 | typename E2::value_type |
narshu | 1:cc2a9eb0bd55 | 490 | >::value_type |
narshu | 1:cc2a9eb0bd55 | 491 | dot(const XprVector<E1, Sz>& lhs, const XprVector<E2, Sz>& rhs) { |
narshu | 1:cc2a9eb0bd55 | 492 | return meta::Vector<Sz>::dot(lhs, rhs); |
narshu | 1:cc2a9eb0bd55 | 493 | } |
narshu | 1:cc2a9eb0bd55 | 494 | |
narshu | 1:cc2a9eb0bd55 | 495 | |
narshu | 1:cc2a9eb0bd55 | 496 | /** |
narshu | 1:cc2a9eb0bd55 | 497 | * \fn dot(const Vector<T, Sz>& lhs, const XprVector<E, Sz>& rhs) |
narshu | 1:cc2a9eb0bd55 | 498 | * \brief Compute the dot/inner product |
narshu | 1:cc2a9eb0bd55 | 499 | * \ingroup _binary_function |
narshu | 1:cc2a9eb0bd55 | 500 | * |
narshu | 1:cc2a9eb0bd55 | 501 | * Compute the dot product as: |
narshu | 1:cc2a9eb0bd55 | 502 | * \f[ |
narshu | 1:cc2a9eb0bd55 | 503 | * \sum_{i = 0}^{Sz - 1} ( lhs[i] * rhs[i] ) |
narshu | 1:cc2a9eb0bd55 | 504 | * \f] |
narshu | 1:cc2a9eb0bd55 | 505 | * where lhs is a column vector and rhs is a row vector, both vectors |
narshu | 1:cc2a9eb0bd55 | 506 | * have the same dimension. |
narshu | 1:cc2a9eb0bd55 | 507 | */ |
narshu | 1:cc2a9eb0bd55 | 508 | template<class T, class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 509 | inline |
narshu | 1:cc2a9eb0bd55 | 510 | typename PromoteTraits<T, typename E::value_type>::value_type |
narshu | 1:cc2a9eb0bd55 | 511 | dot(const Vector<T, Sz>& lhs, const XprVector<E, Sz>& rhs) { |
narshu | 1:cc2a9eb0bd55 | 512 | return meta::Vector<Sz>::dot(lhs, rhs); |
narshu | 1:cc2a9eb0bd55 | 513 | } |
narshu | 1:cc2a9eb0bd55 | 514 | |
narshu | 1:cc2a9eb0bd55 | 515 | |
narshu | 1:cc2a9eb0bd55 | 516 | /** |
narshu | 1:cc2a9eb0bd55 | 517 | * \fn dot(const XprVector<E, Sz>& lhs, const Vector<T, Sz>& rhs) |
narshu | 1:cc2a9eb0bd55 | 518 | * \brief Compute the dot/inner product |
narshu | 1:cc2a9eb0bd55 | 519 | * \ingroup _binary_function |
narshu | 1:cc2a9eb0bd55 | 520 | * |
narshu | 1:cc2a9eb0bd55 | 521 | * Compute the dot product as: |
narshu | 1:cc2a9eb0bd55 | 522 | * \f[ |
narshu | 1:cc2a9eb0bd55 | 523 | * \sum_{i = 0}^{Sz - 1} ( lhs[i] * rhs[i] ) |
narshu | 1:cc2a9eb0bd55 | 524 | * \f] |
narshu | 1:cc2a9eb0bd55 | 525 | * where lhs is a column vector and rhs is a row vector, both vectors |
narshu | 1:cc2a9eb0bd55 | 526 | * have the same dimension. |
narshu | 1:cc2a9eb0bd55 | 527 | */ |
narshu | 1:cc2a9eb0bd55 | 528 | template<class E, class T, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 529 | inline |
narshu | 1:cc2a9eb0bd55 | 530 | typename PromoteTraits<T, typename E::value_type>::value_type |
narshu | 1:cc2a9eb0bd55 | 531 | dot(const XprVector<E, Sz>& lhs, const Vector<T, Sz>& rhs) { |
narshu | 1:cc2a9eb0bd55 | 532 | return meta::Vector<Sz>::dot(lhs, rhs); |
narshu | 1:cc2a9eb0bd55 | 533 | } |
narshu | 1:cc2a9eb0bd55 | 534 | |
narshu | 1:cc2a9eb0bd55 | 535 | |
narshu | 1:cc2a9eb0bd55 | 536 | /** |
narshu | 1:cc2a9eb0bd55 | 537 | * \fn cross(const XprVector<E1, 3>& lhs, const XprVector<E2, 3>& rhs) |
narshu | 1:cc2a9eb0bd55 | 538 | * \brief Compute the cross/outer product |
narshu | 1:cc2a9eb0bd55 | 539 | * \ingroup _binary_function |
narshu | 1:cc2a9eb0bd55 | 540 | * \note working only for vectors of size = 3 |
narshu | 1:cc2a9eb0bd55 | 541 | * \todo Implement vector outer product as ET and MT, returning a XprVector |
narshu | 1:cc2a9eb0bd55 | 542 | */ |
narshu | 1:cc2a9eb0bd55 | 543 | template<class E1, class E2> |
narshu | 1:cc2a9eb0bd55 | 544 | inline |
narshu | 1:cc2a9eb0bd55 | 545 | Vector< |
narshu | 1:cc2a9eb0bd55 | 546 | typename PromoteTraits< |
narshu | 1:cc2a9eb0bd55 | 547 | typename E1::value_type, |
narshu | 1:cc2a9eb0bd55 | 548 | typename E2::value_type |
narshu | 1:cc2a9eb0bd55 | 549 | >::value_type, |
narshu | 1:cc2a9eb0bd55 | 550 | 3 |
narshu | 1:cc2a9eb0bd55 | 551 | > |
narshu | 1:cc2a9eb0bd55 | 552 | cross(const XprVector<E1, 3>& lhs, const XprVector<E2, 3>& rhs) { |
narshu | 1:cc2a9eb0bd55 | 553 | typedef typename PromoteTraits< |
narshu | 1:cc2a9eb0bd55 | 554 | typename E1::value_type, |
narshu | 1:cc2a9eb0bd55 | 555 | typename E2::value_type |
narshu | 1:cc2a9eb0bd55 | 556 | >::value_type value_type; |
narshu | 1:cc2a9eb0bd55 | 557 | return Vector<value_type, 3>(lhs(1)*rhs(2) - rhs(1)*lhs(2), |
narshu | 1:cc2a9eb0bd55 | 558 | rhs(0)*lhs(2) - lhs(0)*rhs(2), |
narshu | 1:cc2a9eb0bd55 | 559 | lhs(0)*rhs(1) - rhs(0)*lhs(1)); |
narshu | 1:cc2a9eb0bd55 | 560 | } |
narshu | 1:cc2a9eb0bd55 | 561 | |
narshu | 1:cc2a9eb0bd55 | 562 | |
narshu | 1:cc2a9eb0bd55 | 563 | /** |
narshu | 1:cc2a9eb0bd55 | 564 | * \fn cross(const XprVector<E, 3>& lhs, const Vector<T, 3>& rhs) |
narshu | 1:cc2a9eb0bd55 | 565 | * \brief Compute the cross/outer product |
narshu | 1:cc2a9eb0bd55 | 566 | * \ingroup _binary_function |
narshu | 1:cc2a9eb0bd55 | 567 | * \note working only for vectors of size = 3 |
narshu | 1:cc2a9eb0bd55 | 568 | * \todo Implement vector outer product as ET and MT, returning a XprVector |
narshu | 1:cc2a9eb0bd55 | 569 | */ |
narshu | 1:cc2a9eb0bd55 | 570 | template<class E, class T> |
narshu | 1:cc2a9eb0bd55 | 571 | inline |
narshu | 1:cc2a9eb0bd55 | 572 | Vector< |
narshu | 1:cc2a9eb0bd55 | 573 | typename PromoteTraits<T, typename E::value_type>::value_type, 3> |
narshu | 1:cc2a9eb0bd55 | 574 | cross(const XprVector<E, 3>& lhs, const Vector<T, 3>& rhs) { |
narshu | 1:cc2a9eb0bd55 | 575 | typedef typename PromoteTraits< |
narshu | 1:cc2a9eb0bd55 | 576 | typename E::value_type, T>::value_type value_type; |
narshu | 1:cc2a9eb0bd55 | 577 | return Vector<value_type, 3>(lhs(1)*rhs(2) - rhs(1)*lhs(2), |
narshu | 1:cc2a9eb0bd55 | 578 | rhs(0)*lhs(2) - lhs(0)*rhs(2), |
narshu | 1:cc2a9eb0bd55 | 579 | lhs(0)*rhs(1) - rhs(0)*lhs(1)); |
narshu | 1:cc2a9eb0bd55 | 580 | } |
narshu | 1:cc2a9eb0bd55 | 581 | |
narshu | 1:cc2a9eb0bd55 | 582 | |
narshu | 1:cc2a9eb0bd55 | 583 | /** |
narshu | 1:cc2a9eb0bd55 | 584 | * \fn cross(const Vector<T, 3>& lhs, const XprVector<E, 3>& rhs) |
narshu | 1:cc2a9eb0bd55 | 585 | * \brief Compute the cross/outer product |
narshu | 1:cc2a9eb0bd55 | 586 | * \ingroup _binary_function |
narshu | 1:cc2a9eb0bd55 | 587 | * \note working only for vectors of size = 3 |
narshu | 1:cc2a9eb0bd55 | 588 | * \todo Implement vector outer product as ET and MT, returning a XprVector |
narshu | 1:cc2a9eb0bd55 | 589 | */ |
narshu | 1:cc2a9eb0bd55 | 590 | template<class T1, class E2> |
narshu | 1:cc2a9eb0bd55 | 591 | inline |
narshu | 1:cc2a9eb0bd55 | 592 | Vector< |
narshu | 1:cc2a9eb0bd55 | 593 | typename PromoteTraits<T1, typename E2::value_type>::value_type, 3> |
narshu | 1:cc2a9eb0bd55 | 594 | cross(const Vector<T1, 3>& lhs, const XprVector<E2, 3>& rhs) { |
narshu | 1:cc2a9eb0bd55 | 595 | typedef typename PromoteTraits< |
narshu | 1:cc2a9eb0bd55 | 596 | typename E2::value_type, T1>::value_type value_type; |
narshu | 1:cc2a9eb0bd55 | 597 | return Vector<value_type, 3>(lhs(1)*rhs(2) - rhs(1)*lhs(2), |
narshu | 1:cc2a9eb0bd55 | 598 | rhs(0)*lhs(2) - lhs(0)*rhs(2), |
narshu | 1:cc2a9eb0bd55 | 599 | lhs(0)*rhs(1) - rhs(0)*lhs(1)); |
narshu | 1:cc2a9eb0bd55 | 600 | } |
narshu | 1:cc2a9eb0bd55 | 601 | |
narshu | 1:cc2a9eb0bd55 | 602 | |
narshu | 1:cc2a9eb0bd55 | 603 | /** |
narshu | 1:cc2a9eb0bd55 | 604 | * \fn norm1(const XprVector<E, Sz>& v) |
narshu | 1:cc2a9eb0bd55 | 605 | * \brief The \f$l_1\f$ norm of a vector expression. |
narshu | 1:cc2a9eb0bd55 | 606 | * \ingroup _unary_function |
narshu | 1:cc2a9eb0bd55 | 607 | * The norm of any vector is just the square root of the dot product of |
narshu | 1:cc2a9eb0bd55 | 608 | * a vector with itself, or |
narshu | 1:cc2a9eb0bd55 | 609 | * |
narshu | 1:cc2a9eb0bd55 | 610 | * \f[ |
narshu | 1:cc2a9eb0bd55 | 611 | * |Vector<T, Sz> v| = |v| = \sum_{i=0}^{Sz-1}\,|v[i]| |
narshu | 1:cc2a9eb0bd55 | 612 | * \f] |
narshu | 1:cc2a9eb0bd55 | 613 | */ |
narshu | 1:cc2a9eb0bd55 | 614 | template<class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 615 | inline |
narshu | 1:cc2a9eb0bd55 | 616 | typename NumericTraits<typename E::value_type>::sum_type |
narshu | 1:cc2a9eb0bd55 | 617 | norm1(const XprVector<E, Sz>& v) { |
narshu | 1:cc2a9eb0bd55 | 618 | return sum(abs(v)); |
narshu | 1:cc2a9eb0bd55 | 619 | } |
narshu | 1:cc2a9eb0bd55 | 620 | |
narshu | 1:cc2a9eb0bd55 | 621 | |
narshu | 1:cc2a9eb0bd55 | 622 | /** |
narshu | 1:cc2a9eb0bd55 | 623 | * \fn norm2(const XprVector<E, Sz>& v) |
narshu | 1:cc2a9eb0bd55 | 624 | * \brief The euklidian norm (or \f$l_2\f$ norm) of a vector expression. |
narshu | 1:cc2a9eb0bd55 | 625 | * \ingroup _unary_function |
narshu | 1:cc2a9eb0bd55 | 626 | * The norm of any vector is just the square root of the dot product of |
narshu | 1:cc2a9eb0bd55 | 627 | * a vector with itself, or |
narshu | 1:cc2a9eb0bd55 | 628 | * |
narshu | 1:cc2a9eb0bd55 | 629 | * \f[ |
narshu | 1:cc2a9eb0bd55 | 630 | * |Vector<T, Sz> v| = |v| = \sqrt{ \sum_{i=0}^{Sz-1}\,v[i]^2 } |
narshu | 1:cc2a9eb0bd55 | 631 | * \f] |
narshu | 1:cc2a9eb0bd55 | 632 | * |
narshu | 1:cc2a9eb0bd55 | 633 | * \note The internal cast for Vector<int> avoids warnings on sqrt. |
narshu | 1:cc2a9eb0bd55 | 634 | */ |
narshu | 1:cc2a9eb0bd55 | 635 | template<class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 636 | inline |
narshu | 1:cc2a9eb0bd55 | 637 | typename NumericTraits<typename E::value_type>::sum_type |
narshu | 1:cc2a9eb0bd55 | 638 | norm2(const XprVector<E, Sz>& v) { |
narshu | 1:cc2a9eb0bd55 | 639 | typedef typename E::value_type value_type; |
narshu | 1:cc2a9eb0bd55 | 640 | return static_cast<value_type>( std::sqrt(static_cast<value_type>(dot(v, v))) ); |
narshu | 1:cc2a9eb0bd55 | 641 | } |
narshu | 1:cc2a9eb0bd55 | 642 | |
narshu | 1:cc2a9eb0bd55 | 643 | |
narshu | 1:cc2a9eb0bd55 | 644 | /** |
narshu | 1:cc2a9eb0bd55 | 645 | * \fn normalize(const XprVector<E, Sz>& v) |
narshu | 1:cc2a9eb0bd55 | 646 | * \brief Normalize the given vector expression. |
narshu | 1:cc2a9eb0bd55 | 647 | * \ingroup _unary_function |
narshu | 1:cc2a9eb0bd55 | 648 | * \sa norm2 |
narshu | 1:cc2a9eb0bd55 | 649 | * |
narshu | 1:cc2a9eb0bd55 | 650 | * using the equation: |
narshu | 1:cc2a9eb0bd55 | 651 | * \f[ |
narshu | 1:cc2a9eb0bd55 | 652 | * \frac{Vector<T, Sz> v}{\sqrt{ \sum_{i=0}^{Sz-1}\,v[i]^2 }} |
narshu | 1:cc2a9eb0bd55 | 653 | * \f] |
narshu | 1:cc2a9eb0bd55 | 654 | */ |
narshu | 1:cc2a9eb0bd55 | 655 | template<class E, std::size_t Sz> |
narshu | 1:cc2a9eb0bd55 | 656 | inline |
narshu | 1:cc2a9eb0bd55 | 657 | XprVector< |
narshu | 1:cc2a9eb0bd55 | 658 | XprBinOp< |
narshu | 1:cc2a9eb0bd55 | 659 | Fcnl_div<typename E::value_type, typename E::value_type>, |
narshu | 1:cc2a9eb0bd55 | 660 | XprVector<E, Sz>, |
narshu | 1:cc2a9eb0bd55 | 661 | XprLiteral<typename E::value_type> |
narshu | 1:cc2a9eb0bd55 | 662 | >, |
narshu | 1:cc2a9eb0bd55 | 663 | Sz |
narshu | 1:cc2a9eb0bd55 | 664 | > |
narshu | 1:cc2a9eb0bd55 | 665 | normalize(const XprVector<E, Sz>& v) { |
narshu | 1:cc2a9eb0bd55 | 666 | typedef typename E::value_type value_type; |
narshu | 1:cc2a9eb0bd55 | 667 | typedef XprBinOp< |
narshu | 1:cc2a9eb0bd55 | 668 | Fcnl_div<value_type, value_type>, |
narshu | 1:cc2a9eb0bd55 | 669 | XprVector<E, Sz>, |
narshu | 1:cc2a9eb0bd55 | 670 | XprLiteral<value_type> |
narshu | 1:cc2a9eb0bd55 | 671 | > expr_type; |
narshu | 1:cc2a9eb0bd55 | 672 | return XprVector<expr_type, Sz>( |
narshu | 1:cc2a9eb0bd55 | 673 | expr_type(v, XprLiteral< value_type >(norm2(v)))); |
narshu | 1:cc2a9eb0bd55 | 674 | } |
narshu | 1:cc2a9eb0bd55 | 675 | |
narshu | 1:cc2a9eb0bd55 | 676 | |
narshu | 1:cc2a9eb0bd55 | 677 | } // namespace tvmet |
narshu | 1:cc2a9eb0bd55 | 678 | |
narshu | 1:cc2a9eb0bd55 | 679 | #endif // TVMET_XPR_VECTOR_FUNCTIONS_H |
narshu | 1:cc2a9eb0bd55 | 680 | |
narshu | 1:cc2a9eb0bd55 | 681 | // Local Variables: |
narshu | 1:cc2a9eb0bd55 | 682 | // mode:C++ |
narshu | 1:cc2a9eb0bd55 | 683 | // tab-width:8 |
narshu | 1:cc2a9eb0bd55 | 684 | // End: |