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