78 : diagram_(diagram), approx_(approx), sigma_(sigma)
91 GUDHI_CHECK(this->sigma_ == second.sigma_,
92 std::invalid_argument(
"Error: different sigma values for representations"));
93 return std::exp(-_compute_sliced_wasserstein_distance(second) / (2 * this->sigma_ * this->sigma_));
104 GUDHI_CHECK(this->sigma_ == second.sigma_,
105 std::invalid_argument(
"Error: different sigma values for representations"));
107 2 * this->compute_scalar_product(second));
114 std::vector<std::vector<double> > projections_, projections_diagonal_;
123 double step =
pi / this->approx_;
124 int n = diagram_.size();
126 for (
int i = 0; i < this->approx_; i++) {
127 std::vector<double> l, l_diag;
128 for (
int j = 0; j < n; j++) {
129 double px = diagram_[j].first;
130 double py = diagram_[j].second;
131 double proj_diag = (px + py) / 2;
133 l.push_back(px * cos(-
pi / 2 + i * step) + py * sin(-
pi / 2 + i * step));
134 l_diag.push_back(proj_diag * cos(-
pi / 2 + i * step) + proj_diag * sin(-
pi / 2 + i * step));
137 std::sort(l.begin(), l.end());
138 std::sort(l_diag.begin(), l_diag.end());
139 projections_.push_back(std::move(l));
140 projections_diagonal_.push_back(std::move(l_diag));
150 if (diag[i].second == diag[j].second)
153 return atan((diag[j].first - diag[i].first) / (diag[i].second - diag[j].second));
158 double _compute_int_cos(
double alpha,
double beta)
const
161 if (alpha >= 0 && alpha <=
pi) {
162 if (cos(alpha) >= 0) {
163 if (
pi / 2 <= beta) {
164 res = 2 - sin(alpha) - sin(beta);
166 res = sin(beta) - sin(alpha);
169 if (1.5 *
pi <= beta) {
170 res = 2 + sin(alpha) + sin(beta);
172 res = sin(alpha) - sin(beta);
176 if (alpha >= -
pi && alpha <= 0) {
177 if (cos(alpha) <= 0) {
178 if (-
pi / 2 <= beta) {
179 res = 2 + sin(alpha) + sin(beta);
181 res = sin(alpha) - sin(beta);
184 if (
pi / 2 <= beta) {
185 res = 2 - sin(alpha) - sin(beta);
187 res = sin(beta) - sin(alpha);
194 double _compute_int(
double theta1,
201 double norm = std::sqrt((diag1[p].first - diag2[q].first) * (diag1[p].first - diag2[q].first) +
202 (diag1[p].second - diag2[q].second) * (diag1[p].second - diag2[q].second));
204 if (diag1[p].first == diag2[q].first)
205 angle1 = theta1 -
pi / 2;
207 angle1 = theta1 - atan((diag1[p].second - diag2[q].second) / (diag1[p].first - diag2[q].first));
208 double angle2 = angle1 + theta2 - theta1;
209 double integral = _compute_int_cos(angle1, angle2);
210 return norm * integral;
217 GUDHI_CHECK(this->approx_ == second.approx_,
218 std::invalid_argument(
"Error: different approx values for representations"));
224 if (this->approx_ == -1) {
227 n1 = diagram1.size();
228 n2 = diagram2.size();
229 double min_ordinate = std::numeric_limits<double>::max();
230 double min_abscissa = std::numeric_limits<double>::max();
231 double max_ordinate = std::numeric_limits<double>::lowest();
232 double max_abscissa = std::numeric_limits<double>::lowest();
233 for (
int i = 0; i < n2; i++) {
234 min_ordinate = std::min(min_ordinate, diagram2[i].second);
235 min_abscissa = std::min(min_abscissa, diagram2[i].first);
236 max_ordinate = std::max(max_ordinate, diagram2[i].second);
237 max_abscissa = std::max(max_abscissa, diagram2[i].first);
238 diagram1.emplace_back((diagram2[i].first + diagram2[i].second) / 2,
239 (diagram2[i].first + diagram2[i].second) / 2);
241 for (
int i = 0; i < n1; i++) {
242 min_ordinate = std::min(min_ordinate, diagram1[i].second);
243 min_abscissa = std::min(min_abscissa, diagram1[i].first);
244 max_ordinate = std::max(max_ordinate, diagram1[i].second);
245 max_abscissa = std::max(max_abscissa, diagram1[i].first);
246 diagram2.emplace_back((diagram1[i].first + diagram1[i].second) / 2,
247 (diagram1[i].first + diagram1[i].second) / 2);
249 int num_pts_dgm = diagram1.size();
252 double epsilon = 0.0001;
253 double thresh_y = (max_ordinate - min_ordinate) * epsilon;
254 double thresh_x = (max_abscissa - min_abscissa) * epsilon;
255 for (
int i = 0; i < num_pts_dgm; i++) {
257 diagram1[i].first += u * thresh_x;
258 diagram1[i].second += u * thresh_y;
259 diagram2[i].first += u * thresh_x;
260 diagram2[i].second += u * thresh_y;
264 std::vector<std::pair<double, std::pair<int, int> > > angles1, angles2;
265 for (
int i = 0; i < num_pts_dgm; i++) {
266 for (
int j = i + 1; j < num_pts_dgm; j++) {
267 double theta1 = _compute_angle(diagram1, i, j);
268 double theta2 = _compute_angle(diagram2, i, j);
269 angles1.emplace_back(theta1, std::pair<int, int>(i, j));
270 angles2.emplace_back(theta2, std::pair<int, int>(i, j));
275 std::sort(angles1.begin(),
277 [](
const std::pair<
double, std::pair<int, int> >& p1,
278 const std::pair<
double, std::pair<int, int> >& p2) { return (p1.first < p2.first); });
279 std::sort(angles2.begin(),
281 [](
const std::pair<
double, std::pair<int, int> >& p1,
282 const std::pair<
double, std::pair<int, int> >& p2) { return (p1.first < p2.first); });
285 std::vector<int> orderp1, orderp2;
286 for (
int i = 0; i < num_pts_dgm; i++) {
287 orderp1.push_back(i);
288 orderp2.push_back(i);
290 std::sort(orderp1.begin(), orderp1.end(), [&](
int i,
int j) {
291 if (diagram1[i].second != diagram1[j].second)
292 return (diagram1[i].second < diagram1[j].second);
294 return (diagram1[i].first > diagram1[j].first);
296 std::sort(orderp2.begin(), orderp2.end(), [&](
int i,
int j) {
297 if (diagram2[i].second != diagram2[j].second)
298 return (diagram2[i].second < diagram2[j].second);
300 return (diagram2[i].first > diagram2[j].first);
304 std::vector<int> order1(num_pts_dgm);
305 std::vector<int> order2(num_pts_dgm);
306 for (
int i = 0; i < num_pts_dgm; i++) {
307 order1[orderp1[i]] = i;
308 order2[orderp2[i]] = i;
312 std::vector<std::vector<std::pair<int, double> > > anglePerm1(num_pts_dgm);
313 std::vector<std::vector<std::pair<int, double> > > anglePerm2(num_pts_dgm);
315 int m1 = angles1.size();
316 for (
int i = 0; i < m1; i++) {
317 double theta = angles1[i].first;
318 int p = angles1[i].second.first;
319 int q = angles1[i].second.second;
320 anglePerm1[order1[p]].emplace_back(p, theta);
321 anglePerm1[order1[q]].emplace_back(q, theta);
328 int m2 = angles2.size();
329 for (
int i = 0; i < m2; i++) {
330 double theta = angles2[i].first;
331 int p = angles2[i].second.first;
332 int q = angles2[i].second.second;
333 anglePerm2[order2[p]].emplace_back(p, theta);
334 anglePerm2[order2[q]].emplace_back(q, theta);
341 for (
int i = 0; i < num_pts_dgm; i++) {
342 anglePerm1[order1[i]].emplace_back(i,
pi / 2);
343 anglePerm2[order2[i]].emplace_back(i,
pi / 2);
347 for (
int i = 0; i < num_pts_dgm; i++) {
348 std::vector<std::pair<int, double> > u, v;
351 double theta1, theta2;
356 theta2 = std::min(u[ku].second, v[kv].second);
357 while (theta1 !=
pi / 2) {
358 if (diagram1[u[ku].first].first != diagram2[v[kv].first].first ||
359 diagram1[u[ku].first].second != diagram2[v[kv].first].second)
360 if (theta1 != theta2) sw += _compute_int(theta1, theta2, u[ku].first, v[kv].first, diagram1, diagram2);
362 if ((theta2 == u[ku].second) && ku < u.size() - 1) ku++;
363 if ((theta2 == v[kv].second) && kv < v.size() - 1) kv++;
364 theta2 = std::min(u[ku].second, v[kv].second);
368 double step =
pi / this->approx_;
369 std::vector<double> v1, v2;
370 for (
int i = 0; i < this->approx_; i++) {
373 std::merge(this->projections_[i].begin(),
374 this->projections_[i].end(),
375 second.projections_diagonal_[i].begin(),
376 second.projections_diagonal_[i].end(),
377 std::back_inserter(v1));
378 std::merge(second.projections_[i].begin(),
379 second.projections_[i].end(),
380 this->projections_diagonal_[i].begin(),
381 this->projections_diagonal_[i].end(),
382 std::back_inserter(v2));
386 for (
int j = 0; j < n; j++) f += std::abs(v1[j] - v2[j]);