Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
/home/msk/ArduinoMin/libraries/CustomHeaderLibs/numerics.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 04.07.2013 17:27:20 EDT
3 // File: numerics.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __NUMERICS_HEADER
12 #define __NUMERICS_HEADER
13 
14 #define OPERATOR(op) \
15  template<typename T> \
16  T operator op(T a, T const & b) { \
17  ASSERT(a.size() == b.size()) \
18  for(typename T::size_type i = 0; i < a.size(); ++i) { \
19  a[i] op##= b[i]; \
20  } \
21  return a; \
22 }
23 
24 OPERATOR(+)
25 OPERATOR(-)
26 OPERATOR(*)
27 OPERATOR(/)
28 
29 #undef OPERATOR
30 
31 template<typename T>
32 typename T::value_type scalar(T const & a, T const & b) {
33  ASSERT(a.size() == b.size())
34 
35  typename T::value_type res = typename T::value_type();
36  for(typename T::size_type i = 0; i < a.size(); ++i) {
37  res += a[i] * b[i];
38  }
39  return res;
40 }
41 
42 template<typename T>
43 typename T::value_type d1(T const & a, T const & b) {
44  ASSERT(a.size() == b.size())
45 
46  typename T::value_type res = typename T::value_type();
47  for(typename T::size_type i = 0; i < a.size(); ++i) {
48  if(a[i] > b[i]) //bc of unsigned
49  res += (a[i] - b[i]);
50  else
51  res += (b[i] - a[i]);
52  }
53  return res;
54 }
55 
56 template<typename T>
57 typename T::value_type d2(T const & a, T const & b) {
58  ASSERT(a.size() == b.size())
59 
60  typename T::value_type res = typename T::value_type();
61  for(typename T::size_type i = 0; i < a.size(); ++i) {
62  if(a[i] > b[i]) //bc of unsigned
63  res += (a[i] - b[i])*(a[i] - b[i]);
64  else
65  res += (b[i] - a[i])*(b[i] - a[i]);
66  }
67  return sqrt(res);
68 }
69 
70 template<typename T>
71 typename T::value_type accumulate(T const & a) {
72  typename T::value_type res = typename T::value_type();
73  for(typename T::size_type i = 0; i < a.size(); ++i) {
74  res += a[i];
75  }
76  return res;
77 }
78 
79 
80 //=================== custom returntype versions ===================
81 template<typename R, typename T>
82 R scalar(T const & a, T const & b) {
83  ASSERT(a.size() == b.size())
84 
85  R res = R();
86  for(typename T::size_type i = 0; i < a.size(); ++i) {
87  res += a[i] * b[i];
88  }
89  return res;
90 }
91 
92 template<typename R, typename T>
93 R d1(T const & a, T const & b) {
94  ASSERT(a.size() == b.size())
95 
96  R res = R();
97  for(typename T::size_type i = 0; i < a.size(); ++i) {
98  if(a[i] > b[i]) //bc of unsigned
99  res += (a[i] - b[i]);
100  else
101  res += (b[i] - a[i]);
102  }
103  return res;
104 }
105 
106 template<typename R, typename T>
107 R d2(T const & a, T const & b) {
108  ASSERT(a.size() == b.size())
109 
110  R res = R();
111  for(typename T::size_type i = 0; i < a.size(); ++i) {
112  if(a[i] > b[i]) //bc of unsigned
113  res += (a[i] - b[i])*(a[i] - b[i]);
114  else
115  res += (b[i] - a[i])*(b[i] - a[i]);
116  }
117  return sqrt(res);
118 }
119 
120 template<typename R, typename T>
121 R accumulate(T const & a) {
122  R res = R();
123  for(typename T::size_type i = 0; i < a.size(); ++i) {
124  res += a[i];
125  }
126  return res;
127 }
128 
129 #endif //__NUMERICS_HEADER
T::value_type accumulate(T const &a)
Definition: numerics.hpp:71
#define OPERATOR(op)
Definition: numerics.hpp:14
T::value_type d2(T const &a, T const &b)
Definition: numerics.hpp:57
T::value_type d1(T const &a, T const &b)
Definition: numerics.hpp:43
#define ASSERT(exp)
Definition: ard_assert.hpp:33
T::value_type scalar(T const &a, T const &b)
Definition: numerics.hpp:32