77 : diagram_(diagram), approx_(approx), sigma_(sigma)
90 GUDHI_CHECK(this->sigma_ == second.sigma_,
91 std::invalid_argument(
"Error: different sigma values for representations"));
92 return std::exp(-_compute_sliced_wasserstein_distance(second) / (2 * this->sigma_ * this->sigma_));
103 GUDHI_CHECK(this->sigma_ == second.sigma_,
104 std::invalid_argument(
"Error: different sigma values for representations"));
106 2 * this->compute_scalar_product(second));
113 std::vector<std::vector<double> > projections_, projections_diagonal_;
122 double step =
pi / this->approx_;
123 int n = diagram_.size();
125 for (
int i = 0; i < this->approx_; i++) {
126 std::vector<double> l, l_diag;
127 for (
int j = 0; j < n; j++) {
128 double px = diagram_[j].first;
129 double py = diagram_[j].second;
130 double proj_diag = (px + py) / 2;
132 l.push_back(px * cos(-
pi / 2 + i * step) + py * sin(-
pi / 2 + i * step));
133 l_diag.push_back(proj_diag * cos(-
pi / 2 + i * step) + proj_diag * sin(-
pi / 2 + i * step));
136 std::sort(l.begin(), l.end());
137 std::sort(l_diag.begin(), l_diag.end());
138 projections_.push_back(std::move(l));
139 projections_diagonal_.push_back(std::move(l_diag));
149 if (diag[i].second == diag[j].second)
152 return atan((diag[j].first - diag[i].first) / (diag[i].second - diag[j].second));
157 double _compute_int_cos(
double alpha,
double beta)
const
160 if (alpha >= 0 && alpha <=
pi) {
161 if (cos(alpha) >= 0) {
162 if (
pi / 2 <= beta) {
163 res = 2 - sin(alpha) - sin(beta);
165 res = sin(beta) - sin(alpha);
168 if (1.5 *
pi <= beta) {
169 res = 2 + sin(alpha) + sin(beta);
171 res = sin(alpha) - sin(beta);
175 if (alpha >= -
pi && alpha <= 0) {
176 if (cos(alpha) <= 0) {
177 if (-
pi / 2 <= beta) {
178 res = 2 + sin(alpha) + sin(beta);
180 res = sin(alpha) - sin(beta);
183 if (
pi / 2 <= beta) {
184 res = 2 - sin(alpha) - sin(beta);
186 res = sin(beta) - sin(alpha);
193 double _compute_int(
double theta1,
200 double norm = std::sqrt((diag1[p].first - diag2[q].first) * (diag1[p].first - diag2[q].first) +
201 (diag1[p].second - diag2[q].second) * (diag1[p].second - diag2[q].second));
203 if (diag1[p].first == diag2[q].first)
204 angle1 = theta1 -
pi / 2;
206 angle1 = theta1 - atan((diag1[p].second - diag2[q].second) / (diag1[p].first - diag2[q].first));
207 double angle2 = angle1 + theta2 - theta1;
208 double integral = _compute_int_cos(angle1, angle2);
209 return norm * integral;
216 GUDHI_CHECK(this->approx_ == second.approx_,
217 std::invalid_argument(
"Error: different approx values for representations"));
223 if (this->approx_ == -1) {
226 n1 = diagram1.size();
227 n2 = diagram2.size();
228 double min_ordinate = std::numeric_limits<double>::max();
229 double min_abscissa = std::numeric_limits<double>::max();
230 double max_ordinate = std::numeric_limits<double>::lowest();
231 double max_abscissa = std::numeric_limits<double>::lowest();
232 for (
int i = 0; i < n2; i++) {
233 min_ordinate = std::min(min_ordinate, diagram2[i].second);
234 min_abscissa = std::min(min_abscissa, diagram2[i].first);
235 max_ordinate = std::max(max_ordinate, diagram2[i].second);
236 max_abscissa = std::max(max_abscissa, diagram2[i].first);
237 diagram1.emplace_back((diagram2[i].first + diagram2[i].second) / 2,
238 (diagram2[i].first + diagram2[i].second) / 2);
240 for (
int i = 0; i < n1; i++) {
241 min_ordinate = std::min(min_ordinate, diagram1[i].second);
242 min_abscissa = std::min(min_abscissa, diagram1[i].first);
243 max_ordinate = std::max(max_ordinate, diagram1[i].second);
244 max_abscissa = std::max(max_abscissa, diagram1[i].first);
245 diagram2.emplace_back((diagram1[i].first + diagram1[i].second) / 2,
246 (diagram1[i].first + diagram1[i].second) / 2);
248 int num_pts_dgm = diagram1.size();
251 double epsilon = 0.0001;
252 double thresh_y = (max_ordinate - min_ordinate) * epsilon;
253 double thresh_x = (max_abscissa - min_abscissa) * epsilon;
254 std::random_device rd;
255 std::default_random_engine re(rd());
256 std::uniform_real_distribution<double> uni(-1, 1);
257 for (
int i = 0; i < num_pts_dgm; i++) {
259 diagram1[i].first += u * thresh_x;
260 diagram1[i].second += u * thresh_y;
261 diagram2[i].first += u * thresh_x;
262 diagram2[i].second += u * thresh_y;
266 std::vector<std::pair<double, std::pair<int, int> > > angles1, angles2;
267 for (
int i = 0; i < num_pts_dgm; i++) {
268 for (
int j = i + 1; j < num_pts_dgm; j++) {
269 double theta1 = _compute_angle(diagram1, i, j);
270 double theta2 = _compute_angle(diagram2, i, j);
271 angles1.emplace_back(theta1, std::pair<int, int>(i, j));
272 angles2.emplace_back(theta2, std::pair<int, int>(i, j));
277 std::sort(angles1.begin(),
279 [](
const std::pair<
double, std::pair<int, int> >& p1,
280 const std::pair<
double, std::pair<int, int> >& p2) { return (p1.first < p2.first); });
281 std::sort(angles2.begin(),
283 [](
const std::pair<
double, std::pair<int, int> >& p1,
284 const std::pair<
double, std::pair<int, int> >& p2) { return (p1.first < p2.first); });
287 std::vector<int> orderp1, orderp2;
288 for (
int i = 0; i < num_pts_dgm; i++) {
289 orderp1.push_back(i);
290 orderp2.push_back(i);
292 std::sort(orderp1.begin(), orderp1.end(), [&](
int i,
int j) {
293 if (diagram1[i].second != diagram1[j].second)
294 return (diagram1[i].second < diagram1[j].second);
296 return (diagram1[i].first > diagram1[j].first);
298 std::sort(orderp2.begin(), orderp2.end(), [&](
int i,
int j) {
299 if (diagram2[i].second != diagram2[j].second)
300 return (diagram2[i].second < diagram2[j].second);
302 return (diagram2[i].first > diagram2[j].first);
306 std::vector<int> order1(num_pts_dgm);
307 std::vector<int> order2(num_pts_dgm);
308 for (
int i = 0; i < num_pts_dgm; i++) {
309 order1[orderp1[i]] = i;
310 order2[orderp2[i]] = i;
314 std::vector<std::vector<std::pair<int, double> > > anglePerm1(num_pts_dgm);
315 std::vector<std::vector<std::pair<int, double> > > anglePerm2(num_pts_dgm);
317 int m1 = angles1.size();
318 for (
int i = 0; i < m1; i++) {
319 double theta = angles1[i].first;
320 int p = angles1[i].second.first;
321 int q = angles1[i].second.second;
322 anglePerm1[order1[p]].emplace_back(p, theta);
323 anglePerm1[order1[q]].emplace_back(q, theta);
330 int m2 = angles2.size();
331 for (
int i = 0; i < m2; i++) {
332 double theta = angles2[i].first;
333 int p = angles2[i].second.first;
334 int q = angles2[i].second.second;
335 anglePerm2[order2[p]].emplace_back(p, theta);
336 anglePerm2[order2[q]].emplace_back(q, theta);
343 for (
int i = 0; i < num_pts_dgm; i++) {
344 anglePerm1[order1[i]].emplace_back(i,
pi / 2);
345 anglePerm2[order2[i]].emplace_back(i,
pi / 2);
349 for (
int i = 0; i < num_pts_dgm; i++) {
350 std::vector<std::pair<int, double> > u, v;
353 double theta1, theta2;
358 theta2 = std::min(u[ku].second, v[kv].second);
359 while (theta1 !=
pi / 2) {
360 if (diagram1[u[ku].first].first != diagram2[v[kv].first].first ||
361 diagram1[u[ku].first].second != diagram2[v[kv].first].second)
362 if (theta1 != theta2) sw += _compute_int(theta1, theta2, u[ku].first, v[kv].first, diagram1, diagram2);
364 if ((theta2 == u[ku].second) && ku < u.size() - 1) ku++;
365 if ((theta2 == v[kv].second) && kv < v.size() - 1) kv++;
366 theta2 = std::min(u[ku].second, v[kv].second);
370 double step =
pi / this->approx_;
371 std::vector<double> v1, v2;
372 for (
int i = 0; i < this->approx_; i++) {
375 std::merge(this->projections_[i].begin(),
376 this->projections_[i].end(),
377 second.projections_diagonal_[i].begin(),
378 second.projections_diagonal_[i].end(),
379 std::back_inserter(v1));
380 std::merge(second.projections_[i].begin(),
381 second.projections_[i].end(),
382 this->projections_diagonal_[i].begin(),
383 this->projections_diagonal_[i].end(),
384 std::back_inserter(v2));
388 for (
int j = 0; j < n; j++) f += std::abs(v1[j] - v2[j]);