WeightedVector.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef WVECTOR_H_
32 #define WVECTOR_H_
33 
34 
35 /*
36  g++ drain/examples/WeightedVector-example.cpp drain/util/{Caster,Variable,Debug}.cpp drain/image/{Geometry,Coordinates,Image,FilePng}.cpp -lpng -o WeightedVector-example
37  */
38 #include <stdlib.h>
39 
40 
41 #include <iostream>
42 #include <vector>
43 #include <list>
44 #include <limits>
45 #include <sstream>
46 
47 //
48 
49 
50 // using namespace std;
51 
52 namespace drain {
53 
56 template <class T>
57 class WeightedVector : public std::vector<T> {
58 
59 public:
60 
61  WeightedVector(typename std::vector<T>::size_type size = 0){
62  resize(size);
63  }
64 
65  std::vector<T> weight;
66 
67 
68  // Overrides the original.
69  void resize(typename std::vector<T>::size_type n, typename std::vector<T>::value_type t = typename std::vector<T>::value_type()){ // const & value ?
70  std::vector<T>::resize(n, t);
71  weight.resize(n, 1.0); // (n,t)
72  }
73 
74  void toStream(std::ostream &ostr) const {
75 
76  char separator;
77 
79  separator = 0;
80  for (size_t i=0; i < this->size(); i++){
81  if (separator)
82  ostr << separator;
83  else
84  separator = ',';
85  ostr << (*this)[i];
86  };
87 
88  // weights
89  ostr << ' ' << '{';
90  separator = 0;
91  for (size_t i=0; i < this->size(); i++){
92  if (separator)
93  ostr << separator;
94  else
95  separator = ',';
96  ostr << (*this).weight[i];
97  };
98  ostr << '}';
99 
100  };
101 
102 
103  //template <class T>
104  static
105  double defaultDistance2(const WeightedVector<T> & a, const WeightedVector<T> & b) {
106 
107  double d;
108  double w;
109  double sumD2 = 0.0;
110  double sumW = 0.0;
111 
112  for (int i = 0; i < a.size(); ++i) {
113  d = a[i] - b[i];
114  w = a.weight[i] * b.weight[i];
115  sumD2 += w*d*d;
116  sumW += w;
117  }
118  if (sumW > 0.0)
119  return sumD2/sumW;
120  else
121  return std::numeric_limits<double>::max();
122  }
123 
125 
129  //template <class T>
130  static
131  void euclideanMixingFunction(const WeightedVector<T> & a, const WeightedVector<T> & b, double coeff, WeightedVector<T> & m) {
132 
133  double alpha;
134  double beta;
135  const double coeff2 = 1.0 - coeff;
136  double w;
137  for (int i = 0; i < a.size(); ++i){
138  alpha = coeff2 * a.weight[i];
139  beta = coeff * b.weight[i];
140  w = alpha + beta;
141  if ( w > 0.0 ){
142  m[i] = (alpha*a[i] + beta*b[i]) / w;
143  m.weight[i] = w; // consider sqrt?
144  }
145  else { // else ? 0, max, min ?
146  m.weight[i] = 0.0;
147  }
148 
149  }
150 
151  }
152 
153 
154 
155 };
156 
157 
158 
159 template <class T>
160 std::ostream & operator<<(std::ostream & ostr, const WeightedVector<T> & WeightedVector){
161  WeightedVector.toStream(ostr);
162  return ostr;
163 };
164 
165 
166 } // drain
167 
168 
169 
170 /* namespace drain */
171 
172 #endif /* WeightedVector_H_ */
173 
174 // Drain
Definition: WeightedVector.h:57
void toStream(std::ostream &ostr) const
Definition: WeightedVector.h:74
static void euclideanMixingFunction(const WeightedVector< T > &a, const WeightedVector< T > &b, double coeff, WeightedVector< T > &m)
Given vectors a and a and a mixing coefficient coeff , outputs mixture vector m.
Definition: WeightedVector.h:131
Definition: DataSelector.cpp:1277