Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Wed Oct 17 22:22:47 2012 +0000
Revision:
26:0995f61cb7b8
Parent:
25:143b19c1fb05
Eurobot 2012 Primary;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
narshu 25:143b19c1fb05 1 /*
narshu 25:143b19c1fb05 2 * Tiny Vector Matrix Library
narshu 25:143b19c1fb05 3 * Dense Vector Matrix Libary of Tiny size using Expression Templates
narshu 25:143b19c1fb05 4 *
narshu 25:143b19c1fb05 5 * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
narshu 25:143b19c1fb05 6 *
narshu 25:143b19c1fb05 7 * This library is free software; you can redistribute it and/or
narshu 25:143b19c1fb05 8 * modify it under the terms of the GNU Lesser General Public
narshu 25:143b19c1fb05 9 * License as published by the Free Software Foundation; either
narshu 25:143b19c1fb05 10 * version 2.1 of the License, or (at your option) any later version.
narshu 25:143b19c1fb05 11 *
narshu 25:143b19c1fb05 12 * This library is distributed in the hope that it will be useful,
narshu 25:143b19c1fb05 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
narshu 25:143b19c1fb05 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
narshu 25:143b19c1fb05 15 * Lesser General Public License for more details.
narshu 25:143b19c1fb05 16 *
narshu 25:143b19c1fb05 17 * You should have received a copy of the GNU Lesser General Public
narshu 25:143b19c1fb05 18 * License along with this library; if not, write to the Free Software
narshu 25:143b19c1fb05 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
narshu 25:143b19c1fb05 20 *
narshu 25:143b19c1fb05 21 * $Id: VectorFunctions.h,v 1.37 2007-06-23 15:58:58 opetzold Exp $
narshu 25:143b19c1fb05 22 */
narshu 25:143b19c1fb05 23
narshu 25:143b19c1fb05 24 #ifndef TVMET_VECTOR_FUNCTIONS_H
narshu 25:143b19c1fb05 25 #define TVMET_VECTOR_FUNCTIONS_H
narshu 25:143b19c1fb05 26
narshu 25:143b19c1fb05 27 #include <tvmet/Extremum.h>
narshu 25:143b19c1fb05 28
narshu 25:143b19c1fb05 29 namespace tvmet {
narshu 25:143b19c1fb05 30
narshu 25:143b19c1fb05 31
narshu 25:143b19c1fb05 32 /*********************************************************
narshu 25:143b19c1fb05 33 * PART I: DECLARATION
narshu 25:143b19c1fb05 34 *********************************************************/
narshu 25:143b19c1fb05 35
narshu 25:143b19c1fb05 36
narshu 25:143b19c1fb05 37 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
narshu 25:143b19c1fb05 38 * Vector arithmetic functions add, sub, mul and div
narshu 25:143b19c1fb05 39 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
narshu 25:143b19c1fb05 40
narshu 25:143b19c1fb05 41
narshu 25:143b19c1fb05 42 /*
narshu 25:143b19c1fb05 43 * function(Vector<T1, Sz>, Vector<T2, Sz>)
narshu 25:143b19c1fb05 44 * function(Vector<T, Sz>, XprVector<E, Sz>)
narshu 25:143b19c1fb05 45 * function(XprVector<E, Sz>, Vector<T, Sz>)
narshu 25:143b19c1fb05 46 */
narshu 25:143b19c1fb05 47 #define TVMET_DECLARE_MACRO(NAME) \
narshu 25:143b19c1fb05 48 template<class T1, class T2, std::size_t Sz> \
narshu 25:143b19c1fb05 49 XprVector< \
narshu 25:143b19c1fb05 50 XprBinOp< \
narshu 25:143b19c1fb05 51 Fcnl_##NAME<T1, T2>, \
narshu 25:143b19c1fb05 52 VectorConstReference<T1, Sz>, \
narshu 25:143b19c1fb05 53 VectorConstReference<T2, Sz> \
narshu 25:143b19c1fb05 54 >, \
narshu 25:143b19c1fb05 55 Sz \
narshu 25:143b19c1fb05 56 > \
narshu 25:143b19c1fb05 57 NAME (const Vector<T1, Sz>& lhs, \
narshu 25:143b19c1fb05 58 const Vector<T2, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE; \
narshu 25:143b19c1fb05 59 \
narshu 25:143b19c1fb05 60 template<class E, class T, std::size_t Sz> \
narshu 25:143b19c1fb05 61 XprVector< \
narshu 25:143b19c1fb05 62 XprBinOp< \
narshu 25:143b19c1fb05 63 Fcnl_##NAME<typename E::value_type, T>, \
narshu 25:143b19c1fb05 64 XprVector<E, Sz>, \
narshu 25:143b19c1fb05 65 VectorConstReference<T, Sz> \
narshu 25:143b19c1fb05 66 >, \
narshu 25:143b19c1fb05 67 Sz \
narshu 25:143b19c1fb05 68 > \
narshu 25:143b19c1fb05 69 NAME (const XprVector<E, Sz>& lhs, \
narshu 25:143b19c1fb05 70 const Vector<T, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE; \
narshu 25:143b19c1fb05 71 \
narshu 25:143b19c1fb05 72 template<class E, class T, std::size_t Sz> \
narshu 25:143b19c1fb05 73 XprVector< \
narshu 25:143b19c1fb05 74 XprBinOp< \
narshu 25:143b19c1fb05 75 Fcnl_##NAME<T, typename E::value_type>, \
narshu 25:143b19c1fb05 76 VectorConstReference<T, Sz>, \
narshu 25:143b19c1fb05 77 XprVector<E, Sz> \
narshu 25:143b19c1fb05 78 >, \
narshu 25:143b19c1fb05 79 Sz \
narshu 25:143b19c1fb05 80 > \
narshu 25:143b19c1fb05 81 NAME (const Vector<T, Sz>& lhs, \
narshu 25:143b19c1fb05 82 const XprVector<E, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 83
narshu 25:143b19c1fb05 84 TVMET_DECLARE_MACRO(add) // per se element wise
narshu 25:143b19c1fb05 85 TVMET_DECLARE_MACRO(sub) // per se element wise
narshu 25:143b19c1fb05 86 TVMET_DECLARE_MACRO(mul) // per se element wise
narshu 25:143b19c1fb05 87 namespace element_wise {
narshu 25:143b19c1fb05 88 TVMET_DECLARE_MACRO(div) // not defined for vectors
narshu 25:143b19c1fb05 89 }
narshu 25:143b19c1fb05 90
narshu 25:143b19c1fb05 91 #undef TVMET_DECLARE_MACRO
narshu 25:143b19c1fb05 92
narshu 25:143b19c1fb05 93
narshu 25:143b19c1fb05 94 /*
narshu 25:143b19c1fb05 95 * function(Vector<T, Sz>, POD)
narshu 25:143b19c1fb05 96 * function(POD, Vector<T, Sz>)
narshu 25:143b19c1fb05 97 * Note: - operations +,-,*,/ are per se element wise
narshu 25:143b19c1fb05 98 */
narshu 25:143b19c1fb05 99 #define TVMET_DECLARE_MACRO(NAME, POD) \
narshu 25:143b19c1fb05 100 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 101 XprVector< \
narshu 25:143b19c1fb05 102 XprBinOp< \
narshu 25:143b19c1fb05 103 Fcnl_##NAME< T, POD >, \
narshu 25:143b19c1fb05 104 VectorConstReference<T, Sz>, \
narshu 25:143b19c1fb05 105 XprLiteral< POD > \
narshu 25:143b19c1fb05 106 >, \
narshu 25:143b19c1fb05 107 Sz \
narshu 25:143b19c1fb05 108 > \
narshu 25:143b19c1fb05 109 NAME (const Vector<T, Sz>& lhs, \
narshu 25:143b19c1fb05 110 POD rhs) TVMET_CXX_ALWAYS_INLINE; \
narshu 25:143b19c1fb05 111 \
narshu 25:143b19c1fb05 112 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 113 XprVector< \
narshu 25:143b19c1fb05 114 XprBinOp< \
narshu 25:143b19c1fb05 115 Fcnl_##NAME< POD, T>, \
narshu 25:143b19c1fb05 116 XprLiteral< POD >, \
narshu 25:143b19c1fb05 117 VectorConstReference<T, Sz> \
narshu 25:143b19c1fb05 118 >, \
narshu 25:143b19c1fb05 119 Sz \
narshu 25:143b19c1fb05 120 > \
narshu 25:143b19c1fb05 121 NAME (POD lhs, \
narshu 25:143b19c1fb05 122 const Vector<T, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 123
narshu 25:143b19c1fb05 124 TVMET_DECLARE_MACRO(add, int)
narshu 25:143b19c1fb05 125 TVMET_DECLARE_MACRO(sub, int)
narshu 25:143b19c1fb05 126 TVMET_DECLARE_MACRO(mul, int)
narshu 25:143b19c1fb05 127 TVMET_DECLARE_MACRO(div, int)
narshu 25:143b19c1fb05 128
narshu 25:143b19c1fb05 129 #if defined(TVMET_HAVE_LONG_LONG)
narshu 25:143b19c1fb05 130 TVMET_DECLARE_MACRO(add, long long int)
narshu 25:143b19c1fb05 131 TVMET_DECLARE_MACRO(sub, long long int)
narshu 25:143b19c1fb05 132 TVMET_DECLARE_MACRO(mul, long long int)
narshu 25:143b19c1fb05 133 TVMET_DECLARE_MACRO(div, long long int)
narshu 25:143b19c1fb05 134 #endif
narshu 25:143b19c1fb05 135
narshu 25:143b19c1fb05 136 TVMET_DECLARE_MACRO(add, float)
narshu 25:143b19c1fb05 137 TVMET_DECLARE_MACRO(sub, float)
narshu 25:143b19c1fb05 138 TVMET_DECLARE_MACRO(mul, float)
narshu 25:143b19c1fb05 139 TVMET_DECLARE_MACRO(div, float)
narshu 25:143b19c1fb05 140
narshu 25:143b19c1fb05 141 TVMET_DECLARE_MACRO(add, double)
narshu 25:143b19c1fb05 142 TVMET_DECLARE_MACRO(sub, double)
narshu 25:143b19c1fb05 143 TVMET_DECLARE_MACRO(mul, double)
narshu 25:143b19c1fb05 144 TVMET_DECLARE_MACRO(div, double)
narshu 25:143b19c1fb05 145
narshu 25:143b19c1fb05 146 #if defined(TVMET_HAVE_LONG_DOUBLE)
narshu 25:143b19c1fb05 147 TVMET_DECLARE_MACRO(add, long double)
narshu 25:143b19c1fb05 148 TVMET_DECLARE_MACRO(sub, long double)
narshu 25:143b19c1fb05 149 TVMET_DECLARE_MACRO(mul, long double)
narshu 25:143b19c1fb05 150 TVMET_DECLARE_MACRO(div, long double)
narshu 25:143b19c1fb05 151 #endif
narshu 25:143b19c1fb05 152
narshu 25:143b19c1fb05 153 #undef TVMET_DECLARE_MACRO
narshu 25:143b19c1fb05 154
narshu 25:143b19c1fb05 155
narshu 25:143b19c1fb05 156 #if defined(TVMET_HAVE_COMPLEX)
narshu 25:143b19c1fb05 157 /*
narshu 25:143b19c1fb05 158 * function(Vector<std::complex<T>, Sz>, std::complex<T>)
narshu 25:143b19c1fb05 159 * function(std::complex<T>, Vector<std::complex<T>, Sz>)
narshu 25:143b19c1fb05 160 * Note: per se element wise
narshu 25:143b19c1fb05 161 * \todo type promotion
narshu 25:143b19c1fb05 162 */
narshu 25:143b19c1fb05 163 #define TVMET_DECLARE_MACRO(NAME) \
narshu 25:143b19c1fb05 164 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 165 XprVector< \
narshu 25:143b19c1fb05 166 XprBinOp< \
narshu 25:143b19c1fb05 167 Fcnl_##NAME< std::complex<T>, std::complex<T> >, \
narshu 25:143b19c1fb05 168 VectorConstReference< std::complex<T>, Sz>, \
narshu 25:143b19c1fb05 169 XprLiteral< std::complex<T> > \
narshu 25:143b19c1fb05 170 >, \
narshu 25:143b19c1fb05 171 Sz \
narshu 25:143b19c1fb05 172 > \
narshu 25:143b19c1fb05 173 NAME (const Vector<std::complex<T>, Sz>& lhs, \
narshu 25:143b19c1fb05 174 const std::complex<T>& rhs) TVMET_CXX_ALWAYS_INLINE; \
narshu 25:143b19c1fb05 175 \
narshu 25:143b19c1fb05 176 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 177 XprVector< \
narshu 25:143b19c1fb05 178 XprBinOp< \
narshu 25:143b19c1fb05 179 Fcnl_##NAME< std::complex<T>, std::complex<T> >, \
narshu 25:143b19c1fb05 180 XprLiteral< std::complex<T> >, \
narshu 25:143b19c1fb05 181 VectorConstReference< std::complex<T>, Sz> \
narshu 25:143b19c1fb05 182 >, \
narshu 25:143b19c1fb05 183 Sz \
narshu 25:143b19c1fb05 184 > \
narshu 25:143b19c1fb05 185 NAME (const std::complex<T>& lhs, \
narshu 25:143b19c1fb05 186 const Vector< std::complex<T>, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 187
narshu 25:143b19c1fb05 188 TVMET_DECLARE_MACRO(add)
narshu 25:143b19c1fb05 189 TVMET_DECLARE_MACRO(sub)
narshu 25:143b19c1fb05 190 TVMET_DECLARE_MACRO(mul)
narshu 25:143b19c1fb05 191 TVMET_DECLARE_MACRO(div)
narshu 25:143b19c1fb05 192
narshu 25:143b19c1fb05 193 #undef TVMET_DECLARE_MACRO
narshu 25:143b19c1fb05 194
narshu 25:143b19c1fb05 195 #endif // defined(TVMET_HAVE_COMPLEX)
narshu 25:143b19c1fb05 196
narshu 25:143b19c1fb05 197
narshu 25:143b19c1fb05 198 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
narshu 25:143b19c1fb05 199 * vector specific functions
narshu 25:143b19c1fb05 200 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
narshu 25:143b19c1fb05 201
narshu 25:143b19c1fb05 202
narshu 25:143b19c1fb05 203 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 204 typename NumericTraits<T>::sum_type
narshu 25:143b19c1fb05 205 sum(const Vector<T, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 206
narshu 25:143b19c1fb05 207
narshu 25:143b19c1fb05 208 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 209 typename NumericTraits<T>::sum_type
narshu 25:143b19c1fb05 210 product(const Vector<T, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 211
narshu 25:143b19c1fb05 212
narshu 25:143b19c1fb05 213 template<class T1, class T2, std::size_t Sz>
narshu 25:143b19c1fb05 214 typename PromoteTraits<T1, T2>::value_type
narshu 25:143b19c1fb05 215 dot(const Vector<T1, Sz>& lhs,
narshu 25:143b19c1fb05 216 const Vector<T2, Sz>& rhs) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 217
narshu 25:143b19c1fb05 218
narshu 25:143b19c1fb05 219 template<class T1, class T2>
narshu 25:143b19c1fb05 220 Vector<typename PromoteTraits<T1, T2>::value_type, 3>
narshu 25:143b19c1fb05 221 cross(const Vector<T1, 3>& lhs,
narshu 25:143b19c1fb05 222 const Vector<T2, 3>& rhs) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 223
narshu 25:143b19c1fb05 224
narshu 25:143b19c1fb05 225 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 226 typename NumericTraits<T>::sum_type
narshu 25:143b19c1fb05 227 norm1(const Vector<T, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 228
narshu 25:143b19c1fb05 229
narshu 25:143b19c1fb05 230 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 231 typename NumericTraits<T>::sum_type
narshu 25:143b19c1fb05 232 norm2(const Vector<T, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 233
narshu 25:143b19c1fb05 234
narshu 25:143b19c1fb05 235 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 236 XprVector<
narshu 25:143b19c1fb05 237 XprBinOp<
narshu 25:143b19c1fb05 238 Fcnl_div<T, T>,
narshu 25:143b19c1fb05 239 VectorConstReference<T, Sz>,
narshu 25:143b19c1fb05 240 XprLiteral< T >
narshu 25:143b19c1fb05 241 >,
narshu 25:143b19c1fb05 242 Sz
narshu 25:143b19c1fb05 243 >
narshu 25:143b19c1fb05 244 normalize(const Vector<T, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 245
narshu 25:143b19c1fb05 246
narshu 25:143b19c1fb05 247 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
narshu 25:143b19c1fb05 248 * min/max unary functions
narshu 25:143b19c1fb05 249 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
narshu 25:143b19c1fb05 250
narshu 25:143b19c1fb05 251 template<class E, std::size_t Sz>
narshu 25:143b19c1fb05 252 Extremum<typename E::value_type, std::size_t, vector_tag>
narshu 25:143b19c1fb05 253 maximum(const XprVector<E, Sz>& e); // NOT TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 254
narshu 25:143b19c1fb05 255
narshu 25:143b19c1fb05 256 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 257 Extremum<T, std::size_t, vector_tag>
narshu 25:143b19c1fb05 258 maximum(const Vector<T, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 259
narshu 25:143b19c1fb05 260
narshu 25:143b19c1fb05 261 template<class E, std::size_t Sz>
narshu 25:143b19c1fb05 262 Extremum<typename E::value_type, std::size_t, vector_tag>
narshu 25:143b19c1fb05 263 minimum(const XprVector<E, Sz>& e); // NOT TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 264
narshu 25:143b19c1fb05 265
narshu 25:143b19c1fb05 266 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 267 Extremum<T, std::size_t, vector_tag>
narshu 25:143b19c1fb05 268 minimum(const Vector<T, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 269
narshu 25:143b19c1fb05 270
narshu 25:143b19c1fb05 271 template<class E, std::size_t Sz>
narshu 25:143b19c1fb05 272 typename E::value_type
narshu 25:143b19c1fb05 273 max(const XprVector<E, Sz>& e); // NOT TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 274
narshu 25:143b19c1fb05 275
narshu 25:143b19c1fb05 276 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 277 T max(const Vector<T, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 278
narshu 25:143b19c1fb05 279
narshu 25:143b19c1fb05 280 template<class E, std::size_t Sz>
narshu 25:143b19c1fb05 281 typename E::value_type
narshu 25:143b19c1fb05 282 min(const XprVector<E, Sz>& e); // NOT TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 283
narshu 25:143b19c1fb05 284
narshu 25:143b19c1fb05 285 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 286 T min(const Vector<T, Sz>& v) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 287
narshu 25:143b19c1fb05 288
narshu 25:143b19c1fb05 289 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 290 XprVector<
narshu 25:143b19c1fb05 291 VectorConstReference<T, Sz>,
narshu 25:143b19c1fb05 292 Sz
narshu 25:143b19c1fb05 293 >
narshu 25:143b19c1fb05 294 cvector_ref(const T* mem) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 295
narshu 25:143b19c1fb05 296
narshu 25:143b19c1fb05 297 /*********************************************************
narshu 25:143b19c1fb05 298 * PART II: IMPLEMENTATION
narshu 25:143b19c1fb05 299 *********************************************************/
narshu 25:143b19c1fb05 300
narshu 25:143b19c1fb05 301
narshu 25:143b19c1fb05 302 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
narshu 25:143b19c1fb05 303 * Vector arithmetic functions add, sub, mul and div
narshu 25:143b19c1fb05 304 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
narshu 25:143b19c1fb05 305
narshu 25:143b19c1fb05 306
narshu 25:143b19c1fb05 307 /*
narshu 25:143b19c1fb05 308 * function(Vector<T1, Sz>, Vector<T2, Sz>)
narshu 25:143b19c1fb05 309 * function(Vector<T, Sz>, XprVector<E, Sz>)
narshu 25:143b19c1fb05 310 * function(XprVector<E, Sz>, Vector<T, Sz>)
narshu 25:143b19c1fb05 311 */
narshu 25:143b19c1fb05 312 #define TVMET_IMPLEMENT_MACRO(NAME) \
narshu 25:143b19c1fb05 313 template<class T1, class T2, std::size_t Sz> \
narshu 25:143b19c1fb05 314 inline \
narshu 25:143b19c1fb05 315 XprVector< \
narshu 25:143b19c1fb05 316 XprBinOp< \
narshu 25:143b19c1fb05 317 Fcnl_##NAME<T1, T2>, \
narshu 25:143b19c1fb05 318 VectorConstReference<T1, Sz>, \
narshu 25:143b19c1fb05 319 VectorConstReference<T2, Sz> \
narshu 25:143b19c1fb05 320 >, \
narshu 25:143b19c1fb05 321 Sz \
narshu 25:143b19c1fb05 322 > \
narshu 25:143b19c1fb05 323 NAME (const Vector<T1, Sz>& lhs, const Vector<T2, Sz>& rhs) { \
narshu 25:143b19c1fb05 324 typedef XprBinOp < \
narshu 25:143b19c1fb05 325 Fcnl_##NAME<T1, T2>, \
narshu 25:143b19c1fb05 326 VectorConstReference<T1, Sz>, \
narshu 25:143b19c1fb05 327 VectorConstReference<T2, Sz> \
narshu 25:143b19c1fb05 328 > expr_type; \
narshu 25:143b19c1fb05 329 return XprVector<expr_type, Sz>( \
narshu 25:143b19c1fb05 330 expr_type(lhs.const_ref(), rhs.const_ref())); \
narshu 25:143b19c1fb05 331 } \
narshu 25:143b19c1fb05 332 \
narshu 25:143b19c1fb05 333 template<class E, class T, std::size_t Sz> \
narshu 25:143b19c1fb05 334 inline \
narshu 25:143b19c1fb05 335 XprVector< \
narshu 25:143b19c1fb05 336 XprBinOp< \
narshu 25:143b19c1fb05 337 Fcnl_##NAME<typename E::value_type, T>, \
narshu 25:143b19c1fb05 338 XprVector<E, Sz>, \
narshu 25:143b19c1fb05 339 VectorConstReference<T, Sz> \
narshu 25:143b19c1fb05 340 >, \
narshu 25:143b19c1fb05 341 Sz \
narshu 25:143b19c1fb05 342 > \
narshu 25:143b19c1fb05 343 NAME (const XprVector<E, Sz>& lhs, const Vector<T, Sz>& rhs) { \
narshu 25:143b19c1fb05 344 typedef XprBinOp< \
narshu 25:143b19c1fb05 345 Fcnl_##NAME<typename E::value_type, T>, \
narshu 25:143b19c1fb05 346 XprVector<E, Sz>, \
narshu 25:143b19c1fb05 347 VectorConstReference<T, Sz> \
narshu 25:143b19c1fb05 348 > expr_type; \
narshu 25:143b19c1fb05 349 return XprVector<expr_type, Sz>( \
narshu 25:143b19c1fb05 350 expr_type(lhs, rhs.const_ref())); \
narshu 25:143b19c1fb05 351 } \
narshu 25:143b19c1fb05 352 \
narshu 25:143b19c1fb05 353 template<class E, class T, std::size_t Sz> \
narshu 25:143b19c1fb05 354 inline \
narshu 25:143b19c1fb05 355 XprVector< \
narshu 25:143b19c1fb05 356 XprBinOp< \
narshu 25:143b19c1fb05 357 Fcnl_##NAME<T, typename E::value_type>, \
narshu 25:143b19c1fb05 358 VectorConstReference<T, Sz>, \
narshu 25:143b19c1fb05 359 XprVector<E, Sz> \
narshu 25:143b19c1fb05 360 >, \
narshu 25:143b19c1fb05 361 Sz \
narshu 25:143b19c1fb05 362 > \
narshu 25:143b19c1fb05 363 NAME (const Vector<T, Sz>& lhs, const XprVector<E, Sz>& rhs) { \
narshu 25:143b19c1fb05 364 typedef XprBinOp< \
narshu 25:143b19c1fb05 365 Fcnl_##NAME<T, typename E::value_type>, \
narshu 25:143b19c1fb05 366 VectorConstReference<T, Sz>, \
narshu 25:143b19c1fb05 367 XprVector<E, Sz> \
narshu 25:143b19c1fb05 368 > expr_type; \
narshu 25:143b19c1fb05 369 return XprVector<expr_type, Sz>( \
narshu 25:143b19c1fb05 370 expr_type(lhs.const_ref(), rhs)); \
narshu 25:143b19c1fb05 371 }
narshu 25:143b19c1fb05 372
narshu 25:143b19c1fb05 373 TVMET_IMPLEMENT_MACRO(add) // per se element wise
narshu 25:143b19c1fb05 374 TVMET_IMPLEMENT_MACRO(sub) // per se element wise
narshu 25:143b19c1fb05 375 TVMET_IMPLEMENT_MACRO(mul) // per se element wise
narshu 25:143b19c1fb05 376 namespace element_wise {
narshu 25:143b19c1fb05 377 TVMET_IMPLEMENT_MACRO(div) // not defined for vectors
narshu 25:143b19c1fb05 378 }
narshu 25:143b19c1fb05 379
narshu 25:143b19c1fb05 380 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 381
narshu 25:143b19c1fb05 382
narshu 25:143b19c1fb05 383 /*
narshu 25:143b19c1fb05 384 * function(Vector<T, Sz>, POD)
narshu 25:143b19c1fb05 385 * function(POD, Vector<T, Sz>)
narshu 25:143b19c1fb05 386 * Note: - operations +,-,*,/ are per se element wise
narshu 25:143b19c1fb05 387 */
narshu 25:143b19c1fb05 388 #define TVMET_IMPLEMENT_MACRO(NAME, POD) \
narshu 25:143b19c1fb05 389 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 390 inline \
narshu 25:143b19c1fb05 391 XprVector< \
narshu 25:143b19c1fb05 392 XprBinOp< \
narshu 25:143b19c1fb05 393 Fcnl_##NAME< T, POD >, \
narshu 25:143b19c1fb05 394 VectorConstReference<T, Sz>, \
narshu 25:143b19c1fb05 395 XprLiteral< POD > \
narshu 25:143b19c1fb05 396 >, \
narshu 25:143b19c1fb05 397 Sz \
narshu 25:143b19c1fb05 398 > \
narshu 25:143b19c1fb05 399 NAME (const Vector<T, Sz>& lhs, POD rhs) { \
narshu 25:143b19c1fb05 400 typedef XprBinOp< \
narshu 25:143b19c1fb05 401 Fcnl_##NAME<T, POD >, \
narshu 25:143b19c1fb05 402 VectorConstReference<T, Sz>, \
narshu 25:143b19c1fb05 403 XprLiteral< POD > \
narshu 25:143b19c1fb05 404 > expr_type; \
narshu 25:143b19c1fb05 405 return XprVector<expr_type, Sz>( \
narshu 25:143b19c1fb05 406 expr_type(lhs.const_ref(), XprLiteral< POD >(rhs))); \
narshu 25:143b19c1fb05 407 } \
narshu 25:143b19c1fb05 408 \
narshu 25:143b19c1fb05 409 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 410 inline \
narshu 25:143b19c1fb05 411 XprVector< \
narshu 25:143b19c1fb05 412 XprBinOp< \
narshu 25:143b19c1fb05 413 Fcnl_##NAME< POD, T>, \
narshu 25:143b19c1fb05 414 XprLiteral< POD >, \
narshu 25:143b19c1fb05 415 VectorConstReference<T, Sz> \
narshu 25:143b19c1fb05 416 >, \
narshu 25:143b19c1fb05 417 Sz \
narshu 25:143b19c1fb05 418 > \
narshu 25:143b19c1fb05 419 NAME (POD lhs, const Vector<T, Sz>& rhs) { \
narshu 25:143b19c1fb05 420 typedef XprBinOp< \
narshu 25:143b19c1fb05 421 Fcnl_##NAME< POD, T>, \
narshu 25:143b19c1fb05 422 XprLiteral< POD >, \
narshu 25:143b19c1fb05 423 VectorConstReference<T, Sz> \
narshu 25:143b19c1fb05 424 > expr_type; \
narshu 25:143b19c1fb05 425 return XprVector<expr_type, Sz>( \
narshu 25:143b19c1fb05 426 expr_type(XprLiteral< POD >(lhs), rhs.const_ref())); \
narshu 25:143b19c1fb05 427 }
narshu 25:143b19c1fb05 428
narshu 25:143b19c1fb05 429 TVMET_IMPLEMENT_MACRO(add, int)
narshu 25:143b19c1fb05 430 TVMET_IMPLEMENT_MACRO(sub, int)
narshu 25:143b19c1fb05 431 TVMET_IMPLEMENT_MACRO(mul, int)
narshu 25:143b19c1fb05 432 TVMET_IMPLEMENT_MACRO(div, int)
narshu 25:143b19c1fb05 433
narshu 25:143b19c1fb05 434 #if defined(TVMET_HAVE_LONG_LONG)
narshu 25:143b19c1fb05 435 TVMET_IMPLEMENT_MACRO(add, long long int)
narshu 25:143b19c1fb05 436 TVMET_IMPLEMENT_MACRO(sub, long long int)
narshu 25:143b19c1fb05 437 TVMET_IMPLEMENT_MACRO(mul, long long int)
narshu 25:143b19c1fb05 438 TVMET_IMPLEMENT_MACRO(div, long long int)
narshu 25:143b19c1fb05 439 #endif
narshu 25:143b19c1fb05 440
narshu 25:143b19c1fb05 441 TVMET_IMPLEMENT_MACRO(add, float)
narshu 25:143b19c1fb05 442 TVMET_IMPLEMENT_MACRO(sub, float)
narshu 25:143b19c1fb05 443 TVMET_IMPLEMENT_MACRO(mul, float)
narshu 25:143b19c1fb05 444 TVMET_IMPLEMENT_MACRO(div, float)
narshu 25:143b19c1fb05 445
narshu 25:143b19c1fb05 446 TVMET_IMPLEMENT_MACRO(add, double)
narshu 25:143b19c1fb05 447 TVMET_IMPLEMENT_MACRO(sub, double)
narshu 25:143b19c1fb05 448 TVMET_IMPLEMENT_MACRO(mul, double)
narshu 25:143b19c1fb05 449 TVMET_IMPLEMENT_MACRO(div, double)
narshu 25:143b19c1fb05 450
narshu 25:143b19c1fb05 451 #if defined(TVMET_HAVE_LONG_DOUBLE)
narshu 25:143b19c1fb05 452 TVMET_IMPLEMENT_MACRO(add, long double)
narshu 25:143b19c1fb05 453 TVMET_IMPLEMENT_MACRO(sub, long double)
narshu 25:143b19c1fb05 454 TVMET_IMPLEMENT_MACRO(mul, long double)
narshu 25:143b19c1fb05 455 TVMET_IMPLEMENT_MACRO(div, long double)
narshu 25:143b19c1fb05 456 #endif
narshu 25:143b19c1fb05 457
narshu 25:143b19c1fb05 458 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 459
narshu 25:143b19c1fb05 460
narshu 25:143b19c1fb05 461 #if defined(TVMET_HAVE_COMPLEX)
narshu 25:143b19c1fb05 462 /*
narshu 25:143b19c1fb05 463 * function(Vector<std::complex<T>, Sz>, std::complex<T>)
narshu 25:143b19c1fb05 464 * function(std::complex<T>, Vector<std::complex<T>, Sz>)
narshu 25:143b19c1fb05 465 * Note: per se element wise
narshu 25:143b19c1fb05 466 * \todo type promotion
narshu 25:143b19c1fb05 467 */
narshu 25:143b19c1fb05 468 #define TVMET_IMPLEMENT_MACRO(NAME) \
narshu 25:143b19c1fb05 469 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 470 inline \
narshu 25:143b19c1fb05 471 XprVector< \
narshu 25:143b19c1fb05 472 XprBinOp< \
narshu 25:143b19c1fb05 473 Fcnl_##NAME< std::complex<T>, std::complex<T> >, \
narshu 25:143b19c1fb05 474 VectorConstReference< std::complex<T>, Sz>, \
narshu 25:143b19c1fb05 475 XprLiteral< std::complex<T> > \
narshu 25:143b19c1fb05 476 >, \
narshu 25:143b19c1fb05 477 Sz \
narshu 25:143b19c1fb05 478 > \
narshu 25:143b19c1fb05 479 NAME (const Vector<std::complex<T>, Sz>& lhs, const std::complex<T>& rhs) { \
narshu 25:143b19c1fb05 480 typedef XprBinOp< \
narshu 25:143b19c1fb05 481 Fcnl_##NAME< std::complex<T>, std::complex<T> >, \
narshu 25:143b19c1fb05 482 VectorConstReference< std::complex<T>, Sz>, \
narshu 25:143b19c1fb05 483 XprLiteral< std::complex<T> > \
narshu 25:143b19c1fb05 484 > expr_type; \
narshu 25:143b19c1fb05 485 return XprVector<expr_type, Sz>( \
narshu 25:143b19c1fb05 486 expr_type(lhs.const_ref(), XprLiteral< std::complex<T> >(rhs))); \
narshu 25:143b19c1fb05 487 } \
narshu 25:143b19c1fb05 488 \
narshu 25:143b19c1fb05 489 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 490 inline \
narshu 25:143b19c1fb05 491 XprVector< \
narshu 25:143b19c1fb05 492 XprBinOp< \
narshu 25:143b19c1fb05 493 Fcnl_##NAME< std::complex<T>, std::complex<T> >, \
narshu 25:143b19c1fb05 494 XprLiteral< std::complex<T> >, \
narshu 25:143b19c1fb05 495 VectorConstReference< std::complex<T>, Sz> \
narshu 25:143b19c1fb05 496 >, \
narshu 25:143b19c1fb05 497 Sz \
narshu 25:143b19c1fb05 498 > \
narshu 25:143b19c1fb05 499 NAME (const std::complex<T>& lhs, const Vector< std::complex<T>, Sz>& rhs) { \
narshu 25:143b19c1fb05 500 typedef XprBinOp< \
narshu 25:143b19c1fb05 501 Fcnl_##NAME< std::complex<T>, std::complex<T> >, \
narshu 25:143b19c1fb05 502 XprLiteral< std::complex<T> >, \
narshu 25:143b19c1fb05 503 VectorConstReference< std::complex<T>, Sz> \
narshu 25:143b19c1fb05 504 > expr_type; \
narshu 25:143b19c1fb05 505 return XprVector<expr_type, Sz>( \
narshu 25:143b19c1fb05 506 expr_type(XprLiteral< std::complex<T> >(lhs), rhs.const_ref())); \
narshu 25:143b19c1fb05 507 }
narshu 25:143b19c1fb05 508
narshu 25:143b19c1fb05 509 TVMET_IMPLEMENT_MACRO(add)
narshu 25:143b19c1fb05 510 TVMET_IMPLEMENT_MACRO(sub)
narshu 25:143b19c1fb05 511 TVMET_IMPLEMENT_MACRO(mul)
narshu 25:143b19c1fb05 512 TVMET_IMPLEMENT_MACRO(div)
narshu 25:143b19c1fb05 513
narshu 25:143b19c1fb05 514 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 515
narshu 25:143b19c1fb05 516 #endif // defined(TVMET_HAVE_COMPLEX)
narshu 25:143b19c1fb05 517
narshu 25:143b19c1fb05 518
narshu 25:143b19c1fb05 519 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
narshu 25:143b19c1fb05 520 * vector specific functions
narshu 25:143b19c1fb05 521 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
narshu 25:143b19c1fb05 522
narshu 25:143b19c1fb05 523
narshu 25:143b19c1fb05 524 /**
narshu 25:143b19c1fb05 525 * \fn sum(const Vector<T, Sz>& v)
narshu 25:143b19c1fb05 526 * \brief Compute the sum of the vector.
narshu 25:143b19c1fb05 527 * \ingroup _unary_function
narshu 25:143b19c1fb05 528 *
narshu 25:143b19c1fb05 529 * Simply compute the sum of the given vector as:
narshu 25:143b19c1fb05 530 * \f[
narshu 25:143b19c1fb05 531 * \sum_{i = 0}^{Sz-1} v[i]
narshu 25:143b19c1fb05 532 * \f]
narshu 25:143b19c1fb05 533 */
narshu 25:143b19c1fb05 534 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 535 inline
narshu 25:143b19c1fb05 536 typename NumericTraits<T>::sum_type
narshu 25:143b19c1fb05 537 sum(const Vector<T, Sz>& v) {
narshu 25:143b19c1fb05 538 return meta::Vector<Sz>::sum(v);
narshu 25:143b19c1fb05 539 }
narshu 25:143b19c1fb05 540
narshu 25:143b19c1fb05 541
narshu 25:143b19c1fb05 542 /**
narshu 25:143b19c1fb05 543 * \fn product(const Vector<T, Sz>& v)
narshu 25:143b19c1fb05 544 * \brief Compute the product of the vector elements.
narshu 25:143b19c1fb05 545 * \ingroup _unary_function
narshu 25:143b19c1fb05 546 *
narshu 25:143b19c1fb05 547 * Simply computer the product of the given vector as:
narshu 25:143b19c1fb05 548 * \f[
narshu 25:143b19c1fb05 549 * \prod_{i = 0}^{Sz - 1} v[i]
narshu 25:143b19c1fb05 550 * \f]
narshu 25:143b19c1fb05 551 */
narshu 25:143b19c1fb05 552 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 553 inline
narshu 25:143b19c1fb05 554 typename NumericTraits<T>::sum_type
narshu 25:143b19c1fb05 555 product(const Vector<T, Sz>& v) {
narshu 25:143b19c1fb05 556 return meta::Vector<Sz>::product(v);
narshu 25:143b19c1fb05 557 }
narshu 25:143b19c1fb05 558
narshu 25:143b19c1fb05 559
narshu 25:143b19c1fb05 560 /**
narshu 25:143b19c1fb05 561 * \fn dot(const Vector<T1, Sz>& lhs, const Vector<T2, Sz>& rhs)
narshu 25:143b19c1fb05 562 * \brief Compute the dot/inner product
narshu 25:143b19c1fb05 563 * \ingroup _binary_function
narshu 25:143b19c1fb05 564 *
narshu 25:143b19c1fb05 565 * Compute the dot product as:
narshu 25:143b19c1fb05 566 * \f[
narshu 25:143b19c1fb05 567 * \sum_{i = 0}^{Sz - 1} ( lhs[i] * rhs[i] )
narshu 25:143b19c1fb05 568 * \f]
narshu 25:143b19c1fb05 569 * where lhs is a column vector and rhs is a row vector, both vectors
narshu 25:143b19c1fb05 570 * have the same dimension.
narshu 25:143b19c1fb05 571 */
narshu 25:143b19c1fb05 572 template<class T1, class T2, std::size_t Sz>
narshu 25:143b19c1fb05 573 inline
narshu 25:143b19c1fb05 574 typename PromoteTraits<T1, T2>::value_type
narshu 25:143b19c1fb05 575 dot(const Vector<T1, Sz>& lhs, const Vector<T2, Sz>& rhs) {
narshu 25:143b19c1fb05 576 return meta::Vector<Sz>::dot(lhs, rhs);
narshu 25:143b19c1fb05 577 }
narshu 25:143b19c1fb05 578
narshu 25:143b19c1fb05 579
narshu 25:143b19c1fb05 580 /**
narshu 25:143b19c1fb05 581 * \fn cross(const Vector<T1, 3>& lhs, const Vector<T2, 3>& rhs)
narshu 25:143b19c1fb05 582 * \brief Compute the cross/outer product
narshu 25:143b19c1fb05 583 * \ingroup _binary_function
narshu 25:143b19c1fb05 584 * \note working only for vectors of size = 3
narshu 25:143b19c1fb05 585 * \todo Implement vector outer product as ET and MT, returning a XprVector
narshu 25:143b19c1fb05 586 */
narshu 25:143b19c1fb05 587 template<class T1, class T2>
narshu 25:143b19c1fb05 588 inline
narshu 25:143b19c1fb05 589 Vector<typename PromoteTraits<T1, T2>::value_type, 3>
narshu 25:143b19c1fb05 590 cross(const Vector<T1, 3>& lhs, const Vector<T2, 3>& rhs) {
narshu 25:143b19c1fb05 591 typedef typename PromoteTraits<T1, T2>::value_type value_type;
narshu 25:143b19c1fb05 592 return Vector<value_type, 3>(lhs(1)*rhs(2) - rhs(1)*lhs(2),
narshu 25:143b19c1fb05 593 rhs(0)*lhs(2) - lhs(0)*rhs(2),
narshu 25:143b19c1fb05 594 lhs(0)*rhs(1) - rhs(0)*lhs(1));
narshu 25:143b19c1fb05 595 }
narshu 25:143b19c1fb05 596
narshu 25:143b19c1fb05 597
narshu 25:143b19c1fb05 598 /**
narshu 25:143b19c1fb05 599 * \fn norm1(const Vector<T, Sz>& v)
narshu 25:143b19c1fb05 600 * \brief The \f$l_1\f$ norm of a vector v.
narshu 25:143b19c1fb05 601 * \ingroup _unary_function
narshu 25:143b19c1fb05 602 * The norm of any vector is just the square root of the dot product of
narshu 25:143b19c1fb05 603 * a vector with itself, or
narshu 25:143b19c1fb05 604 *
narshu 25:143b19c1fb05 605 * \f[
narshu 25:143b19c1fb05 606 * |Vector<T, Sz> v| = |v| = \sum_{i=0}^{Sz-1}\,|v[i]|
narshu 25:143b19c1fb05 607 * \f]
narshu 25:143b19c1fb05 608 */
narshu 25:143b19c1fb05 609 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 610 inline
narshu 25:143b19c1fb05 611 typename NumericTraits<T>::sum_type
narshu 25:143b19c1fb05 612 norm1(const Vector<T, Sz>& v) {
narshu 25:143b19c1fb05 613 return sum(abs(v));
narshu 25:143b19c1fb05 614 }
narshu 25:143b19c1fb05 615
narshu 25:143b19c1fb05 616
narshu 25:143b19c1fb05 617 /**
narshu 25:143b19c1fb05 618 * \fn norm2(const Vector<T, Sz>& v)
narshu 25:143b19c1fb05 619 * \brief The euklidian norm (or \f$l_2\f$ norm) of a vector v.
narshu 25:143b19c1fb05 620 * \ingroup _unary_function
narshu 25:143b19c1fb05 621 * The norm of any vector is just the square root of the dot product of
narshu 25:143b19c1fb05 622 * a vector with itself, or
narshu 25:143b19c1fb05 623 *
narshu 25:143b19c1fb05 624 * \f[
narshu 25:143b19c1fb05 625 * |Vector<T, Sz> v| = |v| = \sqrt{ \sum_{i=0}^{Sz-1}\,v[i]^2 }
narshu 25:143b19c1fb05 626 * \f]
narshu 25:143b19c1fb05 627 *
narshu 25:143b19c1fb05 628 * \note The internal cast for Vector<int> avoids warnings on sqrt.
narshu 25:143b19c1fb05 629 */
narshu 25:143b19c1fb05 630 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 631 inline
narshu 25:143b19c1fb05 632 typename NumericTraits<T>::sum_type
narshu 25:143b19c1fb05 633 norm2(const Vector<T, Sz>& v) {
narshu 25:143b19c1fb05 634 return static_cast<T>( std::sqrt(static_cast<typename NumericTraits<T>::float_type>(dot(v, v))) );
narshu 25:143b19c1fb05 635 }
narshu 25:143b19c1fb05 636
narshu 25:143b19c1fb05 637
narshu 25:143b19c1fb05 638 /**
narshu 25:143b19c1fb05 639 * \fn normalize(const Vector<T, Sz>& v)
narshu 25:143b19c1fb05 640 * \brief Normalize the given vector.
narshu 25:143b19c1fb05 641 * \ingroup _unary_function
narshu 25:143b19c1fb05 642 * \sa norm2
narshu 25:143b19c1fb05 643 *
narshu 25:143b19c1fb05 644 * using the equation:
narshu 25:143b19c1fb05 645 * \f[
narshu 25:143b19c1fb05 646 * \frac{Vector<T, Sz> v}{\sqrt{ \sum_{i=0}^{Sz-1}\,v[i]^2 }}
narshu 25:143b19c1fb05 647 * \f]
narshu 25:143b19c1fb05 648 */
narshu 25:143b19c1fb05 649 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 650 inline
narshu 25:143b19c1fb05 651 XprVector<
narshu 25:143b19c1fb05 652 XprBinOp<
narshu 25:143b19c1fb05 653 Fcnl_div<T, T>,
narshu 25:143b19c1fb05 654 VectorConstReference<T, Sz>,
narshu 25:143b19c1fb05 655 XprLiteral< T >
narshu 25:143b19c1fb05 656 >,
narshu 25:143b19c1fb05 657 Sz
narshu 25:143b19c1fb05 658 >
narshu 25:143b19c1fb05 659 normalize(const Vector<T, Sz>& v) {
narshu 25:143b19c1fb05 660 typedef XprBinOp<
narshu 25:143b19c1fb05 661 Fcnl_div<T, T>,
narshu 25:143b19c1fb05 662 VectorConstReference<T, Sz>,
narshu 25:143b19c1fb05 663 XprLiteral< T >
narshu 25:143b19c1fb05 664 > expr_type;
narshu 25:143b19c1fb05 665 return XprVector<expr_type, Sz>(
narshu 25:143b19c1fb05 666 expr_type(v.const_ref(), XprLiteral< T >(norm2(v))));
narshu 25:143b19c1fb05 667 }
narshu 25:143b19c1fb05 668
narshu 25:143b19c1fb05 669
narshu 25:143b19c1fb05 670 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
narshu 25:143b19c1fb05 671 * min/max unary functions
narshu 25:143b19c1fb05 672 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
narshu 25:143b19c1fb05 673
narshu 25:143b19c1fb05 674
narshu 25:143b19c1fb05 675 /**
narshu 25:143b19c1fb05 676 * \fn maximum(const XprVector<E, Sz>& e)
narshu 25:143b19c1fb05 677 * \brief Find the maximum of a vector expression
narshu 25:143b19c1fb05 678 * \ingroup _unary_function
narshu 25:143b19c1fb05 679 */
narshu 25:143b19c1fb05 680 template<class E, std::size_t Sz>
narshu 25:143b19c1fb05 681 inline
narshu 25:143b19c1fb05 682 Extremum<typename E::value_type, std::size_t, vector_tag>
narshu 25:143b19c1fb05 683 maximum(const XprVector<E, Sz>& e) {
narshu 25:143b19c1fb05 684 typedef typename E::value_type value_type;
narshu 25:143b19c1fb05 685
narshu 25:143b19c1fb05 686 value_type m_max(e(0));
narshu 25:143b19c1fb05 687 std::size_t m_idx(0);
narshu 25:143b19c1fb05 688
narshu 25:143b19c1fb05 689 // this loop is faster than meta templates!
narshu 25:143b19c1fb05 690 for(std::size_t i = 1; i != Sz; ++i) {
narshu 25:143b19c1fb05 691 if(e(i) > m_max) {
narshu 25:143b19c1fb05 692 m_max = e(i);
narshu 25:143b19c1fb05 693 m_idx = i;
narshu 25:143b19c1fb05 694 }
narshu 25:143b19c1fb05 695 }
narshu 25:143b19c1fb05 696
narshu 25:143b19c1fb05 697 return Extremum<value_type, std::size_t, vector_tag>(m_max, m_idx);
narshu 25:143b19c1fb05 698 }
narshu 25:143b19c1fb05 699
narshu 25:143b19c1fb05 700
narshu 25:143b19c1fb05 701 /**
narshu 25:143b19c1fb05 702 * \fn maximum(const Vector<T, Sz>& v)
narshu 25:143b19c1fb05 703 * \brief Find the maximum of a vector
narshu 25:143b19c1fb05 704 * \ingroup _unary_function
narshu 25:143b19c1fb05 705 */
narshu 25:143b19c1fb05 706 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 707 inline
narshu 25:143b19c1fb05 708 Extremum<T, std::size_t, vector_tag>
narshu 25:143b19c1fb05 709 maximum(const Vector<T, Sz>& v) { return maximum(v.as_expr()); }
narshu 25:143b19c1fb05 710
narshu 25:143b19c1fb05 711
narshu 25:143b19c1fb05 712 /**
narshu 25:143b19c1fb05 713 * \fn minimum(const XprVector<E, Sz>& e)
narshu 25:143b19c1fb05 714 * \brief Find the minimum of a vector expression
narshu 25:143b19c1fb05 715 * \ingroup _unary_function
narshu 25:143b19c1fb05 716 */
narshu 25:143b19c1fb05 717 template<class E, std::size_t Sz>
narshu 25:143b19c1fb05 718 inline
narshu 25:143b19c1fb05 719 Extremum<typename E::value_type, std::size_t, vector_tag>
narshu 25:143b19c1fb05 720 minimum(const XprVector<E, Sz>& e) {
narshu 25:143b19c1fb05 721 typedef typename E::value_type value_type;
narshu 25:143b19c1fb05 722
narshu 25:143b19c1fb05 723 value_type m_min(e(0));
narshu 25:143b19c1fb05 724 std::size_t m_idx(0);
narshu 25:143b19c1fb05 725
narshu 25:143b19c1fb05 726 // this loop is faster than meta templates!
narshu 25:143b19c1fb05 727 for(std::size_t i = 1; i != Sz; ++i) {
narshu 25:143b19c1fb05 728 if(e(i) < m_min) {
narshu 25:143b19c1fb05 729 m_min = e(i);
narshu 25:143b19c1fb05 730 m_idx = i;
narshu 25:143b19c1fb05 731 }
narshu 25:143b19c1fb05 732 }
narshu 25:143b19c1fb05 733
narshu 25:143b19c1fb05 734 return Extremum<value_type, std::size_t, vector_tag>(m_min, m_idx);
narshu 25:143b19c1fb05 735 }
narshu 25:143b19c1fb05 736
narshu 25:143b19c1fb05 737
narshu 25:143b19c1fb05 738 /**
narshu 25:143b19c1fb05 739 * \fn minimum(const Vector<T, Sz>& v)
narshu 25:143b19c1fb05 740 * \brief Find the minimum of a vector
narshu 25:143b19c1fb05 741 * \ingroup _unary_function
narshu 25:143b19c1fb05 742 */
narshu 25:143b19c1fb05 743 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 744 inline
narshu 25:143b19c1fb05 745 Extremum<T, std::size_t, vector_tag>
narshu 25:143b19c1fb05 746 minimum(const Vector<T, Sz>& v) { return minimum(v.as_expr()); }
narshu 25:143b19c1fb05 747
narshu 25:143b19c1fb05 748
narshu 25:143b19c1fb05 749 /**
narshu 25:143b19c1fb05 750 * \fn max(const XprVector<E, Sz>& e)
narshu 25:143b19c1fb05 751 * \brief Find the maximum of a vector expression
narshu 25:143b19c1fb05 752 * \ingroup _unary_function
narshu 25:143b19c1fb05 753 */
narshu 25:143b19c1fb05 754 template<class E, std::size_t Sz>
narshu 25:143b19c1fb05 755 inline
narshu 25:143b19c1fb05 756 typename E::value_type
narshu 25:143b19c1fb05 757 max(const XprVector<E, Sz>& e) {
narshu 25:143b19c1fb05 758 typedef typename E::value_type value_type;
narshu 25:143b19c1fb05 759
narshu 25:143b19c1fb05 760 value_type m_max(e(0));
narshu 25:143b19c1fb05 761
narshu 25:143b19c1fb05 762 // this loop is faster than meta templates!
narshu 25:143b19c1fb05 763 for(std::size_t i = 1; i != Sz; ++i)
narshu 25:143b19c1fb05 764 if(e(i) > m_max)
narshu 25:143b19c1fb05 765 m_max = e(i);
narshu 25:143b19c1fb05 766
narshu 25:143b19c1fb05 767 return m_max;
narshu 25:143b19c1fb05 768 }
narshu 25:143b19c1fb05 769
narshu 25:143b19c1fb05 770
narshu 25:143b19c1fb05 771 /**
narshu 25:143b19c1fb05 772 * \fn max(const Vector<T, Sz>& v)
narshu 25:143b19c1fb05 773 * \brief Find the maximum of a vector
narshu 25:143b19c1fb05 774 * \ingroup _unary_function
narshu 25:143b19c1fb05 775 */
narshu 25:143b19c1fb05 776 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 777 inline
narshu 25:143b19c1fb05 778 T max(const Vector<T, Sz>& v) {
narshu 25:143b19c1fb05 779 typedef T value_type;
narshu 25:143b19c1fb05 780 typedef typename Vector<T, Sz>::const_iterator const_iterator;
narshu 25:143b19c1fb05 781
narshu 25:143b19c1fb05 782 const_iterator iter(v.begin());
narshu 25:143b19c1fb05 783 const_iterator last(v.end());
narshu 25:143b19c1fb05 784 value_type temp(*iter);
narshu 25:143b19c1fb05 785
narshu 25:143b19c1fb05 786 for( ; iter != last; ++iter)
narshu 25:143b19c1fb05 787 if(*iter > temp)
narshu 25:143b19c1fb05 788 temp = *iter;
narshu 25:143b19c1fb05 789
narshu 25:143b19c1fb05 790 return temp;
narshu 25:143b19c1fb05 791 }
narshu 25:143b19c1fb05 792
narshu 25:143b19c1fb05 793
narshu 25:143b19c1fb05 794 /**
narshu 25:143b19c1fb05 795 * \fn min(const XprVector<E, Sz>& e)
narshu 25:143b19c1fb05 796 * \brief Find the minimum of a vector expression
narshu 25:143b19c1fb05 797 * \ingroup _unary_function
narshu 25:143b19c1fb05 798 */
narshu 25:143b19c1fb05 799 template<class E, std::size_t Sz>
narshu 25:143b19c1fb05 800 inline
narshu 25:143b19c1fb05 801 typename E::value_type
narshu 25:143b19c1fb05 802 min(const XprVector<E, Sz>& e) {
narshu 25:143b19c1fb05 803 typedef typename E::value_type value_type;
narshu 25:143b19c1fb05 804
narshu 25:143b19c1fb05 805 value_type m_min(e(0));
narshu 25:143b19c1fb05 806
narshu 25:143b19c1fb05 807 // this loop is faster than meta templates!
narshu 25:143b19c1fb05 808 for(std::size_t i = 1; i != Sz; ++i)
narshu 25:143b19c1fb05 809 if(e(i) < m_min)
narshu 25:143b19c1fb05 810 m_min = e(i);
narshu 25:143b19c1fb05 811
narshu 25:143b19c1fb05 812 return m_min;
narshu 25:143b19c1fb05 813 }
narshu 25:143b19c1fb05 814
narshu 25:143b19c1fb05 815
narshu 25:143b19c1fb05 816 /**
narshu 25:143b19c1fb05 817 * \fn min(const Vector<T, Sz>& v)
narshu 25:143b19c1fb05 818 * \brief Find the minimum of a vector
narshu 25:143b19c1fb05 819 * \ingroup _unary_function
narshu 25:143b19c1fb05 820 */
narshu 25:143b19c1fb05 821 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 822 inline
narshu 25:143b19c1fb05 823 T min(const Vector<T, Sz>& v) {
narshu 25:143b19c1fb05 824 typedef T value_type;
narshu 25:143b19c1fb05 825 typedef typename Vector<T, Sz>::const_iterator const_iterator;
narshu 25:143b19c1fb05 826
narshu 25:143b19c1fb05 827 const_iterator iter(v.begin());
narshu 25:143b19c1fb05 828 const_iterator last(v.end());
narshu 25:143b19c1fb05 829 value_type temp(*iter);
narshu 25:143b19c1fb05 830
narshu 25:143b19c1fb05 831 for( ; iter != last; ++iter)
narshu 25:143b19c1fb05 832 if(*iter < temp)
narshu 25:143b19c1fb05 833 temp = *iter;
narshu 25:143b19c1fb05 834
narshu 25:143b19c1fb05 835 return temp;
narshu 25:143b19c1fb05 836 }
narshu 25:143b19c1fb05 837
narshu 25:143b19c1fb05 838
narshu 25:143b19c1fb05 839 /**
narshu 25:143b19c1fb05 840 * \fn cvector_ref(const T* mem)
narshu 25:143b19c1fb05 841 * \brief Creates an expression wrapper for a C like vector arrays.
narshu 25:143b19c1fb05 842 * \ingroup _unary_function
narshu 25:143b19c1fb05 843 *
narshu 25:143b19c1fb05 844 * This is like creating a vector of external data, as described
narshu 25:143b19c1fb05 845 * at \ref construct. With this function you wrap an expression
narshu 25:143b19c1fb05 846 * around a C style vector array and you can operate directly with it
narshu 25:143b19c1fb05 847 * as usual.
narshu 25:143b19c1fb05 848 *
narshu 25:143b19c1fb05 849 * \par Example:
narshu 25:143b19c1fb05 850 * \code
narshu 25:143b19c1fb05 851 * static float vertices[N][3] = {
narshu 25:143b19c1fb05 852 * {-1, 0, 1}, { 1, 0, 1}, ...
narshu 25:143b19c1fb05 853 * };
narshu 25:143b19c1fb05 854 * ...
narshu 25:143b19c1fb05 855 * typedef Vector<float, 3> vector_type;
narshu 25:143b19c1fb05 856 * ...
narshu 25:143b19c1fb05 857 * vector_type V( cross(cvector_ref<float, 3>(&vertices[0][0]),
narshu 25:143b19c1fb05 858 * cvector_ref<float, 3>(&vertices[1][0])) );
narshu 25:143b19c1fb05 859 * \endcode
narshu 25:143b19c1fb05 860 *
narshu 25:143b19c1fb05 861 * \since release 1.6.0
narshu 25:143b19c1fb05 862 */
narshu 25:143b19c1fb05 863 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 864 inline
narshu 25:143b19c1fb05 865 XprVector<
narshu 25:143b19c1fb05 866 VectorConstReference<T, Sz>,
narshu 25:143b19c1fb05 867 Sz
narshu 25:143b19c1fb05 868 >
narshu 25:143b19c1fb05 869 cvector_ref(const T* mem) {
narshu 25:143b19c1fb05 870 typedef VectorConstReference<T, Sz> expr_type;
narshu 25:143b19c1fb05 871
narshu 25:143b19c1fb05 872 return XprVector<expr_type, Sz>(expr_type(mem));
narshu 25:143b19c1fb05 873 }
narshu 25:143b19c1fb05 874
narshu 25:143b19c1fb05 875
narshu 25:143b19c1fb05 876 } // namespace tvmet
narshu 25:143b19c1fb05 877
narshu 25:143b19c1fb05 878 #endif // TVMET_VECTOR_FUNCTIONS_H
narshu 25:143b19c1fb05 879
narshu 25:143b19c1fb05 880 // Local Variables:
narshu 25:143b19c1fb05 881 // mode:C++
narshu 25:143b19c1fb05 882 // tab-width:8
narshu 25:143b19c1fb05 883 // End: