jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
util.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2013 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #include "util.hpp"
18 
19 #include <cfloat>
20 #include <cmath>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 using std::pair;
26 using std::string;
27 using std::vector;
28 
29 namespace jubatus {
30 namespace core {
31 namespace clustering {
32 
33 void concat(const wplist& src, wplist& dst) {
34  dst.insert(dst.end(), src.begin(), src.end());
35 }
36 
37 char digit(int num, int r, int n) {
38  if (r < 0) {
39  return 0;
40  }
41  for (int i = 0; i < r; ++i) {
42  num /= n;
43  }
44  return num % n;
45 }
46 
47 double sum(const common::sfv_t& p) {
48  double s = 0;
49  for (common::sfv_t::const_iterator it = p.begin(); it != p.end(); ++it) {
50  s += (*it).second;
51  }
52  return s;
53 }
54 double sum2(const common::sfv_t& p) {
55  double s = 0;
56  for (common::sfv_t::const_iterator it = p.begin(); it != p.end(); ++it) {
57  s += std::pow((*it).second, 2);
58  }
59  return s;
60 }
61 
63  const common::sfv_t& left,
64  float s,
65  common::sfv_t& right) {
66  common::sfv_t::const_iterator l = left.begin();
67  common::sfv_t::iterator r = right.begin();
68  while (l != left.end() && r != right.end()) {
69  if (l->first < r->first) {
70  std::pair<std::string, float> p = *l;
71  p.second *= s;
72  r = right.insert(r, p);
73  ++l;
74  } else if (l->first > r->first) {
75  ++r;
76  } else {
77  r->second += l->second * s;
78  ++l;
79  ++r;
80  }
81  }
82  for (; l != left.end(); ++l) {
83  std::pair<std::string, float> p = *l;
84  p.second *= s;
85  right.push_back(p);
86  }
87 }
88 
90  common::sfv_t ret;
91  common::sfv_t::const_iterator it1 = p1.begin();
92  common::sfv_t::const_iterator it2 = p2.begin();
93  while (it1 != p1.end() && it2 != p2.end()) {
94  if ((*it1).first < (*it2).first) {
95  ret.push_back((*it1));
96  ++it1;
97  } else if ((*it1).first > (*it2).first) {
98  ret.push_back((*it2));
99  ++it2;
100  } else {
101  ret.push_back(make_pair((*it1).first, (*it1).second + (*it2).second));
102  ++it1;
103  ++it2;
104  }
105  }
106  for (; it1 != p1.end(); ++it1) {
107  ret.push_back((*it1));
108  }
109  for (; it2 != p2.end(); ++it2) {
110  ret.push_back((*it2));
111  }
112 
113  return ret;
114 }
115 
117  common::sfv_t ret;
118  common::sfv_t::const_iterator it1 = p1.begin();
119  common::sfv_t::const_iterator it2 = p2.begin();
120  while (it1 != p1.end() && it2 != p2.end()) {
121  if ((*it1).first < (*it2).first) {
122  ret.push_back((*it1));
123  ++it1;
124  } else if ((*it1).first > (*it2).first) {
125  ret.push_back(make_pair((*it2).first, -(*it2).second));
126  ++it2;
127  } else {
128  ret.push_back(make_pair((*it1).first, (*it1).second - (*it2).second));
129  ++it1;
130  ++it2;
131  }
132  }
133  for (; it1 != p1.end(); ++it1) {
134  ret.push_back((*it1));
135  }
136  for (; it2 != p2.end(); ++it2) {
137  ret.push_back(make_pair((*it2).first, -(*it2).second));
138  }
139 
140  return ret;
141 }
142 
144  common::sfv_t ret;
145  for (common::sfv_t::const_iterator it = p.begin(); it != p.end(); ++it) {
146  ret.push_back(make_pair((*it).first, (*it).second*s));
147  }
148  return ret;
149 }
150 
151 double dist(const common::sfv_t& p1, const common::sfv_t& p2) {
152  double ret = 0;
153  common::sfv_t::const_iterator it1 = p1.begin();
154  common::sfv_t::const_iterator it2 = p2.begin();
155  while (it1 != p1.end() && it2 != p2.end()) {
156  int cmp = strcmp(it1->first.c_str(), it2->first.c_str());
157  if (cmp < 0) {
158  ret += it1->second * it1->second;
159  ++it1;
160  } else if (cmp > 0) {
161  ret += it2->second * it2->second;
162  ++it2;
163  } else {
164  ret += (it1->second - it2->second) * (it1->second - it2->second);
165  ++it1;
166  ++it2;
167  }
168  }
169  for (; it1 != p1.end(); ++it1) {
170  ret += std::pow(it1->second, 2);
171  }
172  for (; it2 != p2.end(); ++it2) {
173  ret += std::pow(it2->second, 2);
174  }
175  return std::sqrt(ret);
176 }
177 
178 double dist(const weighted_point &d1, const weighted_point &d2) {
179  return dist(d1.data, d2.data);
180 }
181 
182 pair<size_t, double> min_dist(
183  const common::sfv_t& p,
184  const vector<common::sfv_t>& P) {
185  size_t idx = 0;
186  double mindist = DBL_MAX;
187  for (vector<common::sfv_t>::const_iterator it = P.begin();
188  it != P.end(); ++it) {
189  double d = dist(p, *it);
190  if (mindist > d) {
191  idx = it - P.begin();
192  mindist = d;
193  }
194  }
195  return std::make_pair(idx, mindist);
196 }
197 
198 std::pair<size_t, double> min_dist(const weighted_point& d1, const wplist& P) {
199  double md = DBL_MAX;
200  size_t midx = 0;
201  for (wplist::const_iterator it = P.begin(); it != P.end(); ++it) {
202  double d = dist((*it), d1);
203  if (md > d) {
204  midx = it - P.begin();
205  md = d;
206  }
207  }
208  return std::make_pair(midx, md);
209 }
210 
211 } // namespace clustering
212 } // namespace core
213 } // namespace jubatus
void scalar_mul_and_add(const common::sfv_t &left, float s, common::sfv_t &right)
Definition: util.cpp:62
common::sfv_t scalar_dot(const common::sfv_t &p, double s)
Definition: util.cpp:143
char digit(int num, int r, int n)
Definition: util.cpp:37
double dist(const common::sfv_t &p1, const common::sfv_t &p2)
Definition: util.cpp:151
void concat(const wplist &src, wplist &dst)
Definition: util.cpp:33
pair< size_t, double > min_dist(const common::sfv_t &p, const vector< common::sfv_t > &P)
Definition: util.cpp:182
common::sfv_t sub(const common::sfv_t &p1, const common::sfv_t &p2)
Definition: util.cpp:116
double sum(const common::sfv_t &p)
Definition: util.cpp:47
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
double sum2(const common::sfv_t &p)
Definition: util.cpp:54
std::vector< weighted_point > wplist
Definition: types.hpp:55
common::sfv_t add(const common::sfv_t &p1, const common::sfv_t &p2)
Definition: util.cpp:89