working version with calibration done

Fork of Eurobot2013 by Oskar Weigl

Committer:
xiaxia686
Date:
Tue Apr 09 15:32:47 2013 +0000
Revision:
11:5ba926692210
Parent:
1:6799c07fe510
woking version (calibrated)

Who changed what in which revision?

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