Colour sensors calibrated

Dependencies:   mbed-rtos mbed Servo QEI

Fork of ICRSEurobot13 by Thomas Branch

Committer:
xiaxia686
Date:
Fri Apr 12 20:59:18 2013 +0000
Revision:
46:adcd57a5e402
Parent:
15:9c5aaeda36dc
Colours Sensors fixed

Who changed what in which revision?

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