summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--biblio/bibliography.bib44
-rw-r--r--data/persistence_diagram/PD1.pers3
-rw-r--r--data/persistence_diagram/PD2.pers2
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/Doxyfile3
-rw-r--r--src/Persistence_representations/doc/Persistence_representations_doc.h63
-rw-r--r--src/Persistence_representations/example/CMakeLists.txt19
-rw-r--r--src/Persistence_representations/example/landscape.cpp51
-rw-r--r--src/Persistence_representations/example/persistence_image.cpp54
-rw-r--r--src/Persistence_representations/example/persistence_weighted_gaussian.cpp98
-rw-r--r--src/Persistence_representations/example/sliced_wasserstein.cpp60
-rw-r--r--src/Persistence_representations/include/gudhi/Landscape.h108
-rw-r--r--src/Persistence_representations/include/gudhi/Persistence_image.h126
-rw-r--r--src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h181
-rw-r--r--src/Persistence_representations/include/gudhi/Sliced_Wasserstein.h340
-rw-r--r--src/Persistence_representations/include/gudhi/Weight_functions.h81
-rw-r--r--src/Persistence_representations/include/gudhi/common_persistence_representations.h24
-rw-r--r--src/Persistence_representations/test/CMakeLists.txt5
-rw-r--r--src/Persistence_representations/test/kernels.cpp54
-rw-r--r--src/cmake/modules/GUDHI_modules.cmake4
-rw-r--r--src/cython/cython/kernels.pyx126
-rw-r--r--src/cython/cython/vectors.pyx65
-rw-r--r--src/cython/gudhi.pyx.in2
-rw-r--r--src/cython/include/Kernels_interface.h124
-rw-r--r--src/cython/include/Vectors_interface.h59
26 files changed, 1664 insertions, 34 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 10373f75..b28dcbf2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -50,6 +50,7 @@ add_gudhi_module(Subsampling)
add_gudhi_module(Tangential_complex)
add_gudhi_module(Witness_complex)
add_gudhi_module(Nerve_GIC)
+add_gudhi_module(Kernels)
message("++ GUDHI_MODULES list is:\"${GUDHI_MODULES}\"")
diff --git a/biblio/bibliography.bib b/biblio/bibliography.bib
index 16f43d6f..24c0f718 100644
--- a/biblio/bibliography.bib
+++ b/biblio/bibliography.bib
@@ -1079,27 +1079,25 @@ language={English}
year = {2016}
}
-@inproceedings{cavanna15geometric,
- author = {Nicholas J. Cavanna and Mahmoodreza Jahanseir and Donald R. Sheehy},
- booktitle = {Proceedings of the Canadian Conference on Computational Geometry},
- title = {A Geometric Perspective on Sparse Filtrations},
- year = {2015}
-}
-
-@inproceedings{cavanna15visualizing,
- author = {Nicholas J. Cavanna and Mahmoodreza Jahanseir and Donald R. Sheehy},
- booktitle = {Proceedings of the 31st International Symposium on Computational Geometry},
- title = {Visualizing Sparse Filtrations},
- year = {2015},
- doi = {10.4230/LIPIcs.SOCG.2015.23}
-}
-
-@article{sheehy13linear,
- title = {Linear-Size Approximations to the {V}ietoris-{R}ips Filtration},
- author = {Donald R. Sheehy},
- journal = {Discrete \& Computational Geometry},
- volume = {49},
- number = {4},
- pages = {778--796},
- year = {2013}
+
+@InProceedings{pmlr-v70-carriere17a,
+ title = {Sliced {W}asserstein Kernel for Persistence Diagrams},
+ author = {Mathieu Carri{\`e}re and Marco Cuturi and Steve Oudot},
+ booktitle = {Proceedings of the 34th International Conference on Machine Learning},
+ pages = {664--673},
+ year = {2017},
+ editor = {Doina Precup and Yee Whye Teh},
+ volume = {70},
+ series = {Proceedings of Machine Learning Research},
+ address = {International Convention Centre, Sydney, Australia},
+ month = {06--11 Aug},
+ publisher = {PMLR},
+}
+
+@INPROCEEDINGS{Rahimi07randomfeatures,
+ author = {Ali Rahimi and Ben Recht},
+ title = {Random features for large-scale kernel machines},
+ booktitle = {In Neural Information Processing Systems},
+ year = {2007}
}
+
diff --git a/data/persistence_diagram/PD1.pers b/data/persistence_diagram/PD1.pers
new file mode 100644
index 00000000..404199b4
--- /dev/null
+++ b/data/persistence_diagram/PD1.pers
@@ -0,0 +1,3 @@
+2.7 3.7
+9.6 14
+34.2 34.974 \ No newline at end of file
diff --git a/data/persistence_diagram/PD2.pers b/data/persistence_diagram/PD2.pers
new file mode 100644
index 00000000..125d8e4b
--- /dev/null
+++ b/data/persistence_diagram/PD2.pers
@@ -0,0 +1,2 @@
+2.8 4.45
+9.5 14.1 \ No newline at end of file
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 94587044..0ae26081 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -26,6 +26,7 @@ add_gudhi_module(Subsampling)
add_gudhi_module(Tangential_complex)
add_gudhi_module(Witness_complex)
add_gudhi_module(Nerve_GIC)
+add_gudhi_module(Kernels)
message("++ GUDHI_MODULES list is:\"${GUDHI_MODULES}\"")
diff --git a/src/Doxyfile b/src/Doxyfile
index f1981e2e..2348b290 100644
--- a/src/Doxyfile
+++ b/src/Doxyfile
@@ -854,7 +854,8 @@ IMAGE_PATH = doc/Skeleton_blocker/ \
doc/Tangential_complex/ \
doc/Bottleneck_distance/ \
doc/Nerve_GIC/ \
- doc/Persistence_representations/
+ doc/Persistence_representations/ \
+ doc/Kernels/
# The INPUT_FILTER tag can be used to specify a program that doxygen should
# invoke to filter for each input file. Doxygen will invoke the filter program
diff --git a/src/Persistence_representations/doc/Persistence_representations_doc.h b/src/Persistence_representations/doc/Persistence_representations_doc.h
index 38bd3a21..ca283017 100644
--- a/src/Persistence_representations/doc/Persistence_representations_doc.h
+++ b/src/Persistence_representations/doc/Persistence_representations_doc.h
@@ -24,7 +24,6 @@
#define DOC_GUDHI_STAT_H_
namespace Gudhi {
-
namespace Persistence_representations {
/** \defgroup Persistence_representations Persistence representations
@@ -250,6 +249,68 @@ namespace Persistence_representations {
absolute value of differences between coordinates. A scalar product is a sum of products of
values at the corresponding positions of two vectors.
+
+
+
+
+\section sec_persistence_kernels Kernels on persistence diagrams
+ <b>Reference manual:</b> \ref Gudhi::Persistence_representations::Sliced_Wasserstein <br>
+ <b>Reference manual:</b> \ref Gudhi::Persistence_representations::Persistence_weighted_gaussian <br>
+
+ Kernels for persistence diagrams can be regarded as infinite-dimensional vectorizations. More specifically,
+ they are similarity functions whose evaluations on pairs of persistence diagrams equals the scalar products
+ between images of these pairs under a map \f$\Phi\f$ taking values in a specific (possibly non Euclidean) Hilbert space \f$k(D_i, D_j) = \langle \Phi(D_i),\Phi(D_j)\rangle\f$.
+ Reciprocally, classical results of learning theory ensure that such a \f$\Phi\f$ exists for a given similarity function \f$k\f$ if and only if \f$k\f$ is <i>positive semi-definite</i>.
+ Kernels are designed for algorithms that can be <i>kernelized</i>, i.e., algorithms that only require to know scalar products between instances in order to run.
+ Examples of such algorithms include Support Vector Machines, Principal Component Analysis and Ridge Regression.
+
+ There have been several attempts at defining kernels, i.e., positive semi-definite functions, between persistence diagrams within the last few years. We provide implementation
+ for three of them:
+
+ \li the <i>Persistence Scale Space Kernel</i>---see \cite Reininghaus_Huber_ALL_PSSK, which is the classical scalar product between \f$L^2\f$ functions, where persistence diagrams
+ are turned into functions by centering and summing Gaussian functions over the diagram points and their symmetric counterparts w.r.t. the diagonal: \f$k(D_1,D_2)=\int \Phi(D_1)\Phi(D_2)\f$,
+ where \f$\Phi(D)=\sum_{p\in D} {\rm exp}\left(-\frac{\|p-\cdot\|_2^2}{2\sigma^2}\right)\f$.
+
+ \li the <i>Persistence Weighted Gaussian Kernel</i>---see \cite Kusano_Fukumizu_Hiraoka_PWGK, which is a slight generalization of the previous kernel, is the scalar product between
+ weighted Kernel Mean Embeddings of persistence diagrams w.r.t. the Gaussian Kernel \f$k_G\f$ (with corresponding map \f$\Phi_G\f$) in \f$\mathbb{R}^2\f$:
+ \f$k(D_1,D_2)=\langle\sum_{p\in D_1} w(p)\Phi_G(p), \sum_{q\in D_2} w(q)\Phi_G(q)\rangle\f$
+
+ \li the <i>Sliced Wasserstein Kernel</i>---see \cite pmlr-v70-carriere17a, which takes the form of a Gaussian kernel with a specific distance between persistence diagrams
+ called the <i>Sliced Wasserstein Distance</i>: \f$k(D_1,D_2)={\rm exp}\left(-\frac{SW(D_1,D_2)}{2\sigma^2}\right)\f$
+
+ When launching:
+
+ \code $> ./Sliced_Wasserstein
+ \endcode
+
+ the program output is:
+
+ \code $> Approx SW distance: 5.33648
+ $> Exact SW distance: 5.33798
+ $> Approx SW kernel: 0.0693743
+ $> Exact SW kernel: 0.0693224
+ $> Distance induced by approx SW kernel: 1.36428
+ $> Distance induced by exact SW kernel: 1.3643
+ \endcode
+
+
+ and when launching:
+
+ \code $> ./Persistence_weighted_gaussian
+ \endcode
+
+ the program output is:
+
+ \code $> Approx PWG kernel: 1.21509
+ $> Exact PWG kernel: 1.13628
+ $> Distance induced by approx PWG kernel: 3.23354
+ $> Distance induced by exact PWG kernel: 3.25697
+ $> Approx Gaussian PWG kernel: 0.0194222
+ $> Exact Gaussian PWG kernel: 0.0192524
+ $> Approx PSS kernel: 0.134413
+ $> Exact PSS kernel: 0.133394
+ \endcode
+
*/
/** @} */ // end defgroup Persistence_representations
diff --git a/src/Persistence_representations/example/CMakeLists.txt b/src/Persistence_representations/example/CMakeLists.txt
index 54d719ac..89284e38 100644
--- a/src/Persistence_representations/example/CMakeLists.txt
+++ b/src/Persistence_representations/example/CMakeLists.txt
@@ -27,3 +27,22 @@ add_test(NAME Persistence_representations_example_heat_maps
COMMAND $<TARGET_FILE:Persistence_representations_example_heat_maps>)
install(TARGETS Persistence_representations_example_heat_maps DESTINATION bin)
+add_executable ( Sliced_Wasserstein sliced_wasserstein.cpp )
+add_test(NAME Sliced_Wasserstein
+ COMMAND $<TARGET_FILE:Sliced_Wasserstein>)
+install(TARGETS Sliced_Wasserstein DESTINATION bin)
+
+add_executable ( Persistence_weighted_gaussian persistence_weighted_gaussian.cpp )
+add_test(NAME Persistence_weighted_gaussian
+ COMMAND $<TARGET_FILE:Persistence_weighted_gaussian>)
+install(TARGETS Persistence_weighted_gaussian DESTINATION bin)
+
+add_executable ( Persistence_image persistence_image.cpp )
+add_test(NAME Persistence_image
+ COMMAND $<TARGET_FILE:Persistence_image>)
+install(TARGETS Persistence_image DESTINATION bin)
+
+add_executable ( Landscape landscape.cpp )
+add_test(NAME Landscape
+ COMMAND $<TARGET_FILE:Landscape>)
+install(TARGETS Landscape DESTINATION bin)
diff --git a/src/Persistence_representations/example/landscape.cpp b/src/Persistence_representations/example/landscape.cpp
new file mode 100644
index 00000000..5fa84a7c
--- /dev/null
+++ b/src/Persistence_representations/example/landscape.cpp
@@ -0,0 +1,51 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carriere
+ *
+ * Copyright (C) 2018 INRIA (France)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gudhi/Landscape.h>
+
+#include <iostream>
+#include <vector>
+#include <utility>
+
+using LS = Gudhi::Persistence_representations::Landscape;
+
+int main(int argc, char** argv) {
+
+ std::vector<std::pair<double, double> > persistence;
+
+ persistence.push_back(std::make_pair(1, 2));
+ persistence.push_back(std::make_pair(6, 8));
+ persistence.push_back(std::make_pair(0, 4));
+ persistence.push_back(std::make_pair(3, 8));
+
+ int nb_ls = 3; double min_x = 0.0; double max_x = 10.0; int res_x = 100;
+
+ LS ls(persistence, nb_ls, min_x, max_x, res_x);
+ std::vector<std::vector<double> > L = ls.vectorize();
+
+ for(int i = 0; i < nb_ls; i++){
+ for(int j = 0; j < res_x; j++) std::cout << L[i][j] << " ";
+ std::cout << std::endl;
+ }
+
+ return 0;
+}
diff --git a/src/Persistence_representations/example/persistence_image.cpp b/src/Persistence_representations/example/persistence_image.cpp
new file mode 100644
index 00000000..cdce3bbf
--- /dev/null
+++ b/src/Persistence_representations/example/persistence_image.cpp
@@ -0,0 +1,54 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carriere
+ *
+ * Copyright (C) 2018 INRIA (France)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gudhi/Persistence_image.h>
+#include <gudhi/Persistence_weighted_gaussian.h>
+
+#include <iostream>
+#include <vector>
+#include <utility>
+#include <string>
+
+using PI = Gudhi::Persistence_representations::Persistence_image;
+using Weight = std::function<double (std::pair<double,double>) >;
+
+int main(int argc, char** argv) {
+
+ std::vector<std::pair<double, double> > persistence;
+
+ persistence.push_back(std::make_pair(1, 2));
+ persistence.push_back(std::make_pair(6, 8));
+ persistence.push_back(std::make_pair(0, 4));
+ persistence.push_back(std::make_pair(3, 8));
+
+ double min_x = 0.0; double max_x = 10.0; int res_x = 100; double min_y = 0.0; double max_y = 10.0; int res_y = 100; double sigma = 1.0; Weight weight = Gudhi::Persistence_representations::linear_weight;
+
+ PI pim(persistence, min_x, max_x, res_x, min_y, max_y, res_y, weight, sigma);
+ std::vector<std::vector<double> > P = pim.vectorize();
+
+ for(int i = 0; i < res_y; i++){
+ for(int j = 0; j < res_x; j++) std::cout << P[i][j] << " ";
+ std::cout << std::endl;
+ }
+
+ return 0;
+}
diff --git a/src/Persistence_representations/example/persistence_weighted_gaussian.cpp b/src/Persistence_representations/example/persistence_weighted_gaussian.cpp
new file mode 100644
index 00000000..db60755f
--- /dev/null
+++ b/src/Persistence_representations/example/persistence_weighted_gaussian.cpp
@@ -0,0 +1,98 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carriere
+ *
+ * Copyright (C) 2018 INRIA (France)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gudhi/Persistence_weighted_gaussian.h>
+
+#include <iostream>
+#include <vector>
+#include <utility>
+
+using PWG = Gudhi::Persistence_representations::Persistence_weighted_gaussian;
+
+int main(int argc, char** argv) {
+
+ Persistence_diagram persistence1, persistence2;
+
+ persistence1.push_back(std::make_pair(1, 2));
+ persistence1.push_back(std::make_pair(6, 8));
+ persistence1.push_back(std::make_pair(0, 4));
+ persistence1.push_back(std::make_pair(3, 8));
+
+ persistence2.push_back(std::make_pair(2, 9));
+ persistence2.push_back(std::make_pair(1, 6));
+ persistence2.push_back(std::make_pair(3, 5));
+ persistence2.push_back(std::make_pair(6, 10));
+
+ double sigma = 1;
+ double tau = 1;
+ int m = 10000;
+
+ PWG PWG1(persistence1, sigma, m, Gudhi::Persistence_representations::arctan_weight(1,1));
+ PWG PWG2(persistence2, sigma, m, Gudhi::Persistence_representations::arctan_weight(1,1));
+
+ PWG PWGex1(persistence1, sigma, -1, Gudhi::Persistence_representations::arctan_weight(1,1));
+ PWG PWGex2(persistence2, sigma, -1, Gudhi::Persistence_representations::arctan_weight(1,1));
+
+
+ // Linear PWG
+
+ std::cout << "Approx PWG kernel: " << PWG1.compute_scalar_product (PWG2) << std::endl;
+ std::cout << "Exact PWG kernel: " << PWGex1.compute_scalar_product (PWGex2) << std::endl;
+
+ std::cout << "Distance induced by approx PWG kernel: " << PWG1.distance (PWG2) << std::endl;
+ std::cout << "Distance induced by exact PWG kernel: " << PWGex1.distance (PWGex2) << std::endl;
+
+
+
+
+
+
+
+ // Gaussian PWG
+
+ std::cout << "Approx Gaussian PWG kernel: " << std::exp( -PWG1.distance (PWG2) ) / (2*tau*tau) << std::endl;
+ std::cout << "Exact Gaussian PWG kernel: " << std::exp( -PWGex1.distance (PWGex2) ) / (2*tau*tau) << std::endl;
+
+
+
+
+
+
+
+ // PSS
+
+ Persistence_diagram pd1 = persistence1; int numpts = persistence1.size(); for(int i = 0; i < numpts; i++) pd1.emplace_back(persistence1[i].second,persistence1[i].first);
+ Persistence_diagram pd2 = persistence2; numpts = persistence2.size(); for(int i = 0; i < numpts; i++) pd2.emplace_back(persistence2[i].second,persistence2[i].first);
+
+ PWG pwg1(pd1, 2*std::sqrt(sigma), m, Gudhi::Persistence_representations::pss_weight);
+ PWG pwg2(pd2, 2*std::sqrt(sigma), m, Gudhi::Persistence_representations::pss_weight);
+
+ PWG pwgex1(pd1, 2*std::sqrt(sigma), -1, Gudhi::Persistence_representations::pss_weight);
+ PWG pwgex2(pd2, 2*std::sqrt(sigma), -1, Gudhi::Persistence_representations::pss_weight);
+
+ std::cout << "Approx PSS kernel: " << pwg1.compute_scalar_product (pwg2) / (16*Gudhi::Persistence_representations::pi*sigma) << std::endl;
+ std::cout << "Exact PSS kernel: " << pwgex1.compute_scalar_product (pwgex2) / (16*Gudhi::Persistence_representations::pi*sigma) << std::endl;
+
+
+
+ return 0;
+}
diff --git a/src/Persistence_representations/example/sliced_wasserstein.cpp b/src/Persistence_representations/example/sliced_wasserstein.cpp
new file mode 100644
index 00000000..d37cb23c
--- /dev/null
+++ b/src/Persistence_representations/example/sliced_wasserstein.cpp
@@ -0,0 +1,60 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carriere
+ *
+ * Copyright (C) 2018 INRIA (France)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gudhi/Sliced_Wasserstein.h>
+
+#include <iostream>
+#include <vector>
+#include <utility>
+
+using SW = Gudhi::Persistence_representations::Sliced_Wasserstein;
+
+int main(int argc, char** argv) {
+
+ Persistence_diagram persistence1, persistence2;
+
+ persistence1.push_back(std::make_pair(1, 2));
+ persistence1.push_back(std::make_pair(6, 8));
+ persistence1.push_back(std::make_pair(0, 4));
+ persistence1.push_back(std::make_pair(3, 8));
+
+ persistence2.push_back(std::make_pair(2, 9));
+ persistence2.push_back(std::make_pair(1, 6));
+ persistence2.push_back(std::make_pair(3, 5));
+ persistence2.push_back(std::make_pair(6, 10));
+
+
+ SW sw1(persistence1, 1, 100);
+ SW sw2(persistence2, 1, 100);
+
+ SW swex1(persistence1, 1, -1);
+ SW swex2(persistence2, 1, -1);
+
+ std::cout << "Approx SW distance: " << sw1.compute_sliced_wasserstein_distance(sw2) << std::endl;
+ std::cout << "Exact SW distance: " << swex1.compute_sliced_wasserstein_distance(swex2) << std::endl;
+ std::cout << "Approx SW kernel: " << sw1.compute_scalar_product(sw2) << std::endl;
+ std::cout << "Exact SW kernel: " << swex1.compute_scalar_product(swex2) << std::endl;
+ std::cout << "Distance induced by approx SW kernel: " << sw1.distance(sw2) << std::endl;
+ std::cout << "Distance induced by exact SW kernel: " << swex1.distance(swex2) << std::endl;
+
+ return 0;
+}
diff --git a/src/Persistence_representations/include/gudhi/Landscape.h b/src/Persistence_representations/include/gudhi/Landscape.h
new file mode 100644
index 00000000..bbbca36b
--- /dev/null
+++ b/src/Persistence_representations/include/gudhi/Landscape.h
@@ -0,0 +1,108 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carriere
+ *
+ * Copyright (C) 2018 INRIA (France)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LANDSCAPE_H_
+#define LANDSCAPE_H_
+
+// gudhi include
+#include <gudhi/read_persistence_from_file.h>
+#include <gudhi/common_persistence_representations.h>
+#include <gudhi/Debug_utils.h>
+
+// standard include
+#include <cmath>
+#include <iostream>
+#include <vector>
+#include <limits>
+#include <fstream>
+#include <sstream>
+#include <algorithm>
+#include <string>
+#include <utility>
+#include <functional>
+
+namespace Gudhi {
+namespace Persistence_representations {
+
+/**
+ * \class Landscape gudhi/Landscape.h
+ * \brief A class implementing landscapes.
+ *
+ * \ingroup Persistence_representations
+ *
+ * \details
+ *
+ * The landscape is a way to turn a persistence diagram into \f$L^2\f$ functions. Roughly, the idea is to see the boundaries of the rank functions as scalar functions taking values on the diagonal.
+ * See \cite bubenik_landscapes_2015 for more details. Here we provide a way to approximate such functions by computing their values on a set of samples.
+ *
+**/
+
+class Landscape {
+
+ protected:
+ Persistence_diagram diagram;
+ int res_x, nb_ls;
+ double min_x, max_x;
+
+ public:
+
+ /** \brief Landscape constructor.
+ * \ingroup Landscape
+ *
+ * @param[in] _diagram persistence diagram.
+ * @param[in] _nb_ls number of landscape functions.
+ * @param[in] _min_x minimum value of samples.
+ * @param[in] _max_x maximum value of samples.
+ * @param[in] _res_x number of samples.
+ *
+ */
+ Landscape(const Persistence_diagram & _diagram, int _nb_ls = 5, double _min_x = 0.0, double _max_x = 1.0, int _res_x = 10){diagram = _diagram; nb_ls = _nb_ls; min_x = _min_x; max_x = _max_x; res_x = _res_x;}
+
+ /** \brief Computes the landscape of a diagram.
+ * \ingroup Landscape
+ *
+ */
+ std::vector<std::vector<double> > vectorize() const {
+ std::vector<std::vector<double> > ls; for(int i = 0; i < nb_ls; i++) ls.emplace_back();
+ int num_pts = diagram.size(); double step = (max_x - min_x)/res_x;
+
+ for(int i = 0; i < res_x; i++){
+ double x = min_x + i*step; double t = x / std::sqrt(2); std::vector<double> events;
+ for(int j = 0; j < num_pts; j++){
+ double px = diagram[j].first; double py = diagram[j].second;
+ if(t >= px && t <= py){ if(t >= (px+py)/2) events.push_back(std::sqrt(2)*(py-t)); else events.push_back(std::sqrt(2)*(t-px)); }
+ }
+
+ std::sort(events.begin(), events.end(), [](const double & a, const double & b){return a > b;}); int nb_events = events.size();
+ for (int j = 0; j < nb_ls; j++){ if(j < nb_events) ls[j].push_back(events[j]); else ls[j].push_back(0); }
+ }
+ return ls;
+ }
+
+
+
+
+}; // class Landscape
+} // namespace Persistence_representations
+} // namespace Gudhi
+
+#endif // LANDSCAPE_H_
diff --git a/src/Persistence_representations/include/gudhi/Persistence_image.h b/src/Persistence_representations/include/gudhi/Persistence_image.h
new file mode 100644
index 00000000..76b34d8d
--- /dev/null
+++ b/src/Persistence_representations/include/gudhi/Persistence_image.h
@@ -0,0 +1,126 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carriere
+ *
+ * Copyright (C) 2018 INRIA (France)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PERSISTENCE_IMAGE_H_
+#define PERSISTENCE_IMAGE_H_
+
+// gudhi include
+#include <gudhi/read_persistence_from_file.h>
+#include <gudhi/common_persistence_representations.h>
+#include <gudhi/Weight_functions.h>
+#include <gudhi/Debug_utils.h>
+
+// standard include
+#include <cmath>
+#include <iostream>
+#include <vector>
+#include <limits>
+#include <fstream>
+#include <sstream>
+#include <algorithm>
+#include <string>
+#include <utility>
+#include <functional>
+
+namespace Gudhi {
+namespace Persistence_representations {
+
+/**
+ * \class Persistence_image gudhi/Persistence_image.h
+ * \brief A class implementing the persistence images.
+ *
+ * \ingroup Persistence_representations
+ *
+ * \details
+ *
+ * Persistence images are a way to build images from persistence diagrams. Roughly, the idea is to center Gaussians on each diagram point, with a weight that usually depends on
+ * the distance to the diagonal, so that the diagram is turned into a function, and then to discretize the plane into pixels, and integrate this function on each pixel.
+ * See \cite Persistence_Images_2017 for more details.
+ *
+**/
+
+class Persistence_image {
+
+ protected:
+ Persistence_diagram diagram;
+ int res_x, res_y;
+ double min_x, max_x, min_y, max_y;
+ Weight weight;
+ double sigma;
+
+ public:
+
+ /** \brief Persistence Image constructor.
+ * \ingroup Persistence_image
+ *
+ * @param[in] _diagram persistence diagram.
+ * @param[in] _min_x minimum value of pixel abscissa.
+ * @param[in] _max_x maximum value of pixel abscissa.
+ * @param[in] _res_x number of pixels for the x-direction.
+ * @param[in] _min_y minimum value of pixel ordinate.
+ * @param[in] _max_y maximum value of pixel ordinate.
+ * @param[in] _res_y number of pixels for the y-direction.
+ * @param[in] _weight weight function for the Gaussians.
+ * @param[in] _sigma bandwidth parameter for the Gaussians.
+ *
+ */
+ Persistence_image(const Persistence_diagram & _diagram, double _min_x = 0.0, double _max_x = 1.0, int _res_x = 10, double _min_y = 0.0, double _max_y = 1.0, int _res_y = 10, const Weight & _weight = arctan_weight(1,1), double _sigma = 1.0){
+ diagram = _diagram; min_x = _min_x; max_x = _max_x; res_x = _res_x; min_y = _min_y; max_y = _max_y; res_y = _res_y, weight = _weight; sigma = _sigma;
+ }
+
+ /** \brief Computes the persistence image of a diagram.
+ * \ingroup Persistence_image
+ *
+ */
+ std::vector<std::vector<double> > vectorize() const {
+ std::vector<std::vector<double> > im; for(int i = 0; i < res_y; i++) im.emplace_back();
+ double step_x = (max_x - min_x)/res_x; double step_y = (max_y - min_y)/res_y;
+
+ int num_pts = diagram.size();
+
+ for(int i = 0; i < res_y; i++){
+ double y = min_y + i*step_y;
+ for(int j = 0; j < res_x; j++){
+ double x = min_x + j*step_x;
+
+ double pixel_value = 0;
+ for(int k = 0; k < num_pts; k++){
+ double px = diagram[k].first; double py = diagram[k].second;
+ pixel_value += weight(std::pair<double,double>(px,py)) * std::exp( -((x-px)*(x-px) + (y-(py-px))*(y-(py-px))) / (2*sigma*sigma) ) / (sigma*std::sqrt(2*pi));
+ }
+ im[i].push_back(pixel_value);
+
+ }
+ }
+
+ return im;
+
+ }
+
+
+
+
+}; // class Persistence_image
+} // namespace Persistence_representations
+} // namespace Gudhi
+
+#endif // PERSISTENCE_IMAGE_H_
diff --git a/src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h b/src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h
new file mode 100644
index 00000000..76c43e65
--- /dev/null
+++ b/src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h
@@ -0,0 +1,181 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carriere
+ *
+ * Copyright (C) 2018 INRIA (France)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PERSISTENCE_WEIGHTED_GAUSSIAN_H_
+#define PERSISTENCE_WEIGHTED_GAUSSIAN_H_
+
+// gudhi include
+#include <gudhi/read_persistence_from_file.h>
+#include <gudhi/common_persistence_representations.h>
+#include <gudhi/Weight_functions.h>
+
+// standard include
+#include <cmath>
+#include <iostream>
+#include <vector>
+#include <limits>
+#include <fstream>
+#include <sstream>
+#include <algorithm>
+#include <string>
+#include <utility>
+#include <functional>
+
+namespace Gudhi {
+namespace Persistence_representations {
+/**
+ * \class Persistence_weighted_gaussian gudhi/Persistence_weighted_gaussian.h
+ * \brief A class implementing the Persistence Weighted Gaussian kernel and a specific case thereof called the Persistence Scale Space kernel.
+ *
+ * \ingroup Persistence_representations
+ *
+ * \details
+ * The Persistence Weighted Gaussian kernel is built with Gaussian Kernel Mean Embedding, meaning that each persistence diagram is first
+ * sent to the Hilbert space of a Gaussian kernel with bandwidth parameter \f$\sigma >0\f$ using a weighted mean embedding \f$\Phi\f$:
+ *
+ * \f$ \Phi\,:\,D\,\rightarrow\,\sum_{p\in D}\,w(p)\,{\rm exp}\left(-\frac{\|p-\cdot\|_2^2}{2\sigma^2}\right) \f$,
+ *
+ * Usually, the weight function is chosen to be an arctan function of the distance of the point to the diagonal:
+ * \f$w(p) = {\rm arctan}(C\,|y-x|^\alpha)\f$, for some parameters \f$C,\alpha >0\f$.
+ * Then, their scalar product in this space is computed:
+ *
+ * \f$ k(D_1,D_2)=\langle\Phi(D_1),\Phi(D_2)\rangle
+ * \,=\,\sum_{p\in D_1}\,\sum_{q\in D_2}\,w(p)\,w(q)\,{\rm exp}\left(-\frac{\|p-q\|_2^2}{2\sigma^2}\right).\f$
+ *
+ * Note that one may apply a second Gaussian kernel to their distance in this space and still get a kernel.
+ *
+ * It follows that the computation time is \f$O(n^2)\f$ where \f$n\f$ is the number of points
+ * in the diagrams. This time can be improved by computing approximations of the kernel
+ * with \f$m\f$ Fourier features \cite Rahimi07randomfeatures. In that case, the computation time becomes \f$O(mn)\f$.
+ *
+ * The Persistence Scale Space kernel is a Persistence Weighted Gaussian kernel between modified diagrams:
+ * the symmetric of each point with respect to the diagonal is first added in each diagram, and then the weight function
+ * is set to be +1 if the point is above the diagonal and -1 otherwise.
+ *
+ * For more details, please see \cite Kusano_Fukumizu_Hiraoka_PWGK
+ * and \cite Reininghaus_Huber_ALL_PSSK .
+ *
+**/
+class Persistence_weighted_gaussian{
+
+ protected:
+ Persistence_diagram diagram;
+ Weight weight;
+ double sigma;
+ int approx;
+
+ public:
+
+ /** \brief Persistence Weighted Gaussian kernel constructor.
+ * \ingroup Persistence_weighted_gaussian
+ *
+ * @param[in] _diagram persistence diagram.
+ * @param[in] _sigma bandwidth parameter of the Gaussian kernel used for the Kernel Mean Embedding of the diagrams.
+ * @param[in] _approx number of random Fourier features in case of approximate computation, set to -1 for exact computation.
+ * @param[in] _weight weight function for the points in the diagrams.
+ *
+ */
+ Persistence_weighted_gaussian(const Persistence_diagram & _diagram, double _sigma = 1.0, int _approx = 1000, const Weight & _weight = arctan_weight(1,1)){diagram = _diagram; sigma = _sigma; approx = _approx; weight = _weight;}
+
+
+ // **********************************
+ // Utils.
+ // **********************************
+
+ std::vector<std::pair<double,double> > Fourier_feat(const Persistence_diagram & diag, const std::vector<std::pair<double,double> > & z, const Weight & weight = arctan_weight(1,1)) const {
+ int md = diag.size(); std::vector<std::pair<double,double> > b; int mz = z.size();
+ for(int i = 0; i < mz; i++){
+ double d1 = 0; double d2 = 0; double zx = z[i].first; double zy = z[i].second;
+ for(int j = 0; j < md; j++){
+ double x = diag[j].first; double y = diag[j].second;
+ d1 += weight(diag[j])*cos(x*zx + y*zy);
+ d2 += weight(diag[j])*sin(x*zx + y*zy);
+ }
+ b.emplace_back(d1,d2);
+ }
+ return b;
+ }
+
+ std::vector<std::pair<double,double> > random_Fourier(double sigma, int m = 1000) const {
+ std::normal_distribution<double> distrib(0,1); std::vector<std::pair<double,double> > z; std::random_device rd;
+ for(int i = 0; i < m; i++){
+ std::mt19937 e1(rd()); std::mt19937 e2(rd());
+ double zx = distrib(e1); double zy = distrib(e2);
+ z.emplace_back(zx/sigma,zy/sigma);
+ }
+ return z;
+ }
+
+
+
+ // **********************************
+ // Scalar product + distance.
+ // **********************************
+
+ /** \brief Evaluation of the kernel on a pair of diagrams.
+ * \ingroup Persistence_weighted_gaussian
+ *
+ * @pre sigma, approx and weight attributes need to be the same for both instances.
+ * @param[in] second other instance of class Persistence_weighted_gaussian.
+ *
+ */
+ double compute_scalar_product(const Persistence_weighted_gaussian & second) const {
+
+ GUDHI_CHECK(this->sigma != second.sigma || this->approx != second.approx || this->weight != second.weight, std::invalid_argument("Error: different values for representations"));
+ Persistence_diagram diagram1 = this->diagram; Persistence_diagram diagram2 = second.diagram;
+
+ if(this->approx == -1){
+ int num_pts1 = diagram1.size(); int num_pts2 = diagram2.size(); double k = 0;
+ for(int i = 0; i < num_pts1; i++)
+ for(int j = 0; j < num_pts2; j++)
+ k += this->weight(diagram1[i])*this->weight(diagram2[j])*exp(-((diagram1[i].first - diagram2[j].first) * (diagram1[i].first - diagram2[j].first) +
+ (diagram1[i].second - diagram2[j].second) * (diagram1[i].second - diagram2[j].second))
+ /(2*this->sigma*this->sigma));
+ return k;
+ }
+ else{
+ std::vector<std::pair<double,double> > z = random_Fourier(this->sigma, this->approx);
+ std::vector<std::pair<double,double> > b1 = Fourier_feat(diagram1,z,this->weight);
+ std::vector<std::pair<double,double> > b2 = Fourier_feat(diagram2,z,this->weight);
+ double d = 0; for(int i = 0; i < this->approx; i++) d += b1[i].first*b2[i].first + b1[i].second*b2[i].second;
+ return d/this->approx;
+ }
+ }
+
+ /** \brief Evaluation of the distance between images of diagrams in the Hilbert space of the kernel.
+ * \ingroup Persistence_weighted_gaussian
+ *
+ * @pre sigma, approx and weight attributes need to be the same for both instances.
+ * @param[in] second other instance of class Persistence_weighted_gaussian.
+ *
+ */
+ double distance(const Persistence_weighted_gaussian & second) const {
+ GUDHI_CHECK(this->sigma != second.sigma || this->approx != second.approx || this->weight != second.weight, std::invalid_argument("Error: different values for representations"));
+ return std::pow(this->compute_scalar_product(*this) + second.compute_scalar_product(second)-2*this->compute_scalar_product(second), 0.5);
+ }
+
+
+}; // class Persistence_weighted_gaussian
+} // namespace Persistence_representations
+} // namespace Gudhi
+
+#endif // PERSISTENCE_WEIGHTED_GAUSSIAN_H_
diff --git a/src/Persistence_representations/include/gudhi/Sliced_Wasserstein.h b/src/Persistence_representations/include/gudhi/Sliced_Wasserstein.h
new file mode 100644
index 00000000..235918fe
--- /dev/null
+++ b/src/Persistence_representations/include/gudhi/Sliced_Wasserstein.h
@@ -0,0 +1,340 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carriere
+ *
+ * Copyright (C) 2018 INRIA (France)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SLICED_WASSERSTEIN_H_
+#define SLICED_WASSERSTEIN_H_
+
+// gudhi include
+#include <gudhi/read_persistence_from_file.h>
+#include <gudhi/common_persistence_representations.h>
+#include <gudhi/Debug_utils.h>
+
+// standard include
+#include <cmath>
+#include <iostream>
+#include <vector>
+#include <limits>
+#include <fstream>
+#include <sstream>
+#include <algorithm>
+#include <string>
+#include <utility>
+#include <functional>
+
+namespace Gudhi {
+namespace Persistence_representations {
+
+/**
+ * \class Sliced_Wasserstein gudhi/Sliced_Wasserstein.h
+ * \brief A class implementing the Sliced Wasserstein kernel.
+ *
+ * \ingroup Persistence_representations
+ *
+ * \details
+ * The Sliced Wasserstein kernel is defined as a Gaussian-like kernel between persistence diagrams, where the distance used for
+ * comparison is the Sliced Wasserstein distance \f$SW\f$ between persistence diagrams, defined as the integral of the 1-norm
+ * between the sorted projections of the diagrams onto all lines passing through the origin:
+ *
+ * \f$ SW(D_1,D_2)=\int_{\theta\in\mathbb{S}}\,\|\pi_\theta(D_1\cup\pi_\Delta(D_2))-\pi_\theta(D_2\cup\pi_\Delta(D_1))\|_1{\rm d}\theta\f$,
+ *
+ * where \f$\pi_\theta\f$ is the projection onto the line defined with angle \f$\theta\f$ in the unit circle \f$\mathbb{S}\f$,
+ * and \f$\pi_\Delta\f$ is the projection onto the diagonal.
+ * The integral can be either computed exactly in \f$O(n^2{\rm log}(n))\f$ time, where \f$n\f$ is the number of points
+ * in the diagrams, or approximated by sampling \f$N\f$ lines in the circle in \f$O(Nn{\rm log}(n))\f$ time. The Sliced Wasserstein Kernel is then computed as:
+ *
+ * \f$ k(D_1,D_2) = {\rm exp}\left(-\frac{SW(D_1,D_2)}{2\sigma^2}\right).\f$
+ *
+ * For more details, please see \cite pmlr-v70-carriere17a .
+ *
+**/
+
+class Sliced_Wasserstein {
+
+ protected:
+ Persistence_diagram diagram;
+ int approx;
+ double sigma;
+ std::vector<std::vector<double> > projections, projections_diagonal;
+
+ public:
+
+ void build_rep(){
+
+ if(approx > 0){
+
+ double step = pi/this->approx;
+ int n = diagram.size();
+
+ for (int i = 0; i < this->approx; i++){
+ std::vector<double> l,l_diag;
+ for (int j = 0; j < n; j++){
+
+ double px = diagram[j].first; double py = diagram[j].second;
+ double proj_diag = (px+py)/2;
+
+ l.push_back ( px * cos(-pi/2+i*step) + py * sin(-pi/2+i*step) );
+ l_diag.push_back ( proj_diag * cos(-pi/2+i*step) + proj_diag * sin(-pi/2+i*step) );
+ }
+
+ std::sort(l.begin(), l.end()); std::sort(l_diag.begin(), l_diag.end());
+ projections.push_back(l); projections_diagonal.push_back(l_diag);
+
+ }
+
+ }
+
+ }
+
+ /** \brief Sliced Wasserstein kernel constructor.
+ * \ingroup Sliced_Wasserstein
+ *
+ * @param[in] _diagram persistence diagram.
+ * @param[in] _sigma bandwidth parameter.
+ * @param[in] _approx number of directions used to approximate the integral in the Sliced Wasserstein distance, set to -1 for exact computation.
+ *
+ */
+ Sliced_Wasserstein(const Persistence_diagram & _diagram, double _sigma = 1.0, int _approx = 100){diagram = _diagram; approx = _approx; sigma = _sigma; build_rep();}
+
+ // **********************************
+ // Utils.
+ // **********************************
+
+ // Compute the angle formed by two points of a PD
+ double compute_angle(const Persistence_diagram & diag, int i, int j) const {
+ std::pair<double,double> vect; double x1,y1, x2,y2;
+ x1 = diag[i].first; y1 = diag[i].second;
+ x2 = diag[j].first; y2 = diag[j].second;
+ if (y1 - y2 > 0){
+ vect.first = y1 - y2;
+ vect.second = x2 - x1;}
+ else{
+ if(y1 - y2 < 0){
+ vect.first = y2 - y1;
+ vect.second = x1 - x2;
+ }
+ else{
+ vect.first = 0;
+ vect.second = abs(x1 - x2);}
+ }
+ double norm = std::sqrt(vect.first*vect.first + vect.second*vect.second);
+ return asin(vect.second/norm);
+ }
+
+ // Compute the integral of |cos()| between alpha and beta, valid only if alpha is in [-pi,pi] and beta-alpha is in [0,pi]
+ double compute_int_cos(double alpha, double beta) const {
+ double res = 0;
+ if (alpha >= 0 && alpha <= pi){
+ if (cos(alpha) >= 0){
+ if(pi/2 <= beta){res = 2-sin(alpha)-sin(beta);}
+ else{res = sin(beta)-sin(alpha);}
+ }
+ else{
+ if(1.5*pi <= beta){res = 2+sin(alpha)+sin(beta);}
+ else{res = sin(alpha)-sin(beta);}
+ }
+ }
+ if (alpha >= -pi && alpha <= 0){
+ if (cos(alpha) <= 0){
+ if(-pi/2 <= beta){res = 2+sin(alpha)+sin(beta);}
+ else{res = sin(alpha)-sin(beta);}
+ }
+ else{
+ if(pi/2 <= beta){res = 2-sin(alpha)-sin(beta);}
+ else{res = sin(beta)-sin(alpha);}
+ }
+ }
+ return res;
+ }
+
+ double compute_int(double theta1, double theta2, int p, int q, const Persistence_diagram & diag1, const Persistence_diagram & diag2) const {
+ double norm = std::sqrt( (diag1[p].first-diag2[q].first)*(diag1[p].first-diag2[q].first) + (diag1[p].second-diag2[q].second)*(diag1[p].second-diag2[q].second) );
+ double angle1;
+ if (diag1[p].first > diag2[q].first)
+ angle1 = theta1 - asin( (diag1[p].second-diag2[q].second)/norm );
+ else
+ angle1 = theta1 - asin( (diag2[q].second-diag1[p].second)/norm );
+ double angle2 = angle1 + theta2 - theta1;
+ double integral = compute_int_cos(angle1,angle2);
+ return norm*integral;
+ }
+
+
+
+
+ // **********************************
+ // Scalar product + distance.
+ // **********************************
+
+ /** \brief Evaluation of the Sliced Wasserstein Distance between a pair of diagrams.
+ * \ingroup Sliced_Wasserstein
+ *
+ * @pre approx attribute needs to be the same for both instances.
+ * @param[in] second other instance of class Sliced_Wasserstein.
+ *
+ *
+ */
+ double compute_sliced_wasserstein_distance(const Sliced_Wasserstein & second) const {
+
+ GUDHI_CHECK(this->approx != second.approx, std::invalid_argument("Error: different approx values for representations"));
+
+ Persistence_diagram diagram1 = this->diagram; Persistence_diagram diagram2 = second.diagram; double sw = 0;
+
+ if(this->approx == -1){
+
+ // Add projections onto diagonal.
+ int n1, n2; n1 = diagram1.size(); n2 = diagram2.size(); double max_ordinate = std::numeric_limits<double>::lowest();
+ for (int i = 0; i < n2; i++){
+ max_ordinate = std::max(max_ordinate, diagram2[i].second);
+ diagram1.emplace_back( (diagram2[i].first+diagram2[i].second)/2, (diagram2[i].first+diagram2[i].second)/2 );
+ }
+ for (int i = 0; i < n1; i++){
+ max_ordinate = std::max(max_ordinate, diagram1[i].second);
+ diagram2.emplace_back( (diagram1[i].first+diagram1[i].second)/2, (diagram1[i].first+diagram1[i].second)/2 );
+ }
+ int num_pts_dgm = diagram1.size();
+
+ // Slightly perturb the points so that the PDs are in generic positions.
+ int mag = 0; while(max_ordinate > 10){mag++; max_ordinate/=10;}
+ double thresh = pow(10,-5+mag);
+ srand(time(NULL));
+ for (int i = 0; i < num_pts_dgm; i++){
+ diagram1[i].first += thresh*(1.0-2.0*rand()/RAND_MAX); diagram1[i].second += thresh*(1.0-2.0*rand()/RAND_MAX);
+ diagram2[i].first += thresh*(1.0-2.0*rand()/RAND_MAX); diagram2[i].second += thresh*(1.0-2.0*rand()/RAND_MAX);
+ }
+
+ // Compute all angles in both PDs.
+ std::vector<std::pair<double, std::pair<int,int> > > angles1, angles2;
+ for (int i = 0; i < num_pts_dgm; i++){
+ for (int j = i+1; j < num_pts_dgm; j++){
+ double theta1 = compute_angle(diagram1,i,j); double theta2 = compute_angle(diagram2,i,j);
+ angles1.emplace_back(theta1, std::pair<int,int>(i,j));
+ angles2.emplace_back(theta2, std::pair<int,int>(i,j));
+ }
+ }
+
+ // Sort angles.
+ std::sort(angles1.begin(), angles1.end(), [=](std::pair<double, std::pair<int,int> >& p1, const std::pair<double, std::pair<int,int> >& p2){return (p1.first < p2.first);});
+ std::sort(angles2.begin(), angles2.end(), [=](std::pair<double, std::pair<int,int> >& p1, const std::pair<double, std::pair<int,int> >& p2){return (p1.first < p2.first);});
+
+ // Initialize orders of the points of both PDs (given by ordinates when theta = -pi/2).
+ std::vector<int> orderp1, orderp2;
+ for (int i = 0; i < num_pts_dgm; i++){ orderp1.push_back(i); orderp2.push_back(i); }
+ std::sort( orderp1.begin(), orderp1.end(), [=](int i, int j){ if(diagram1[i].second != diagram1[j].second) return (diagram1[i].second < diagram1[j].second); else return (diagram1[i].first > diagram1[j].first); } );
+ std::sort( orderp2.begin(), orderp2.end(), [=](int i, int j){ if(diagram2[i].second != diagram2[j].second) return (diagram2[i].second < diagram2[j].second); else return (diagram2[i].first > diagram2[j].first); } );
+
+ // Find the inverses of the orders.
+ std::vector<int> order1(num_pts_dgm); std::vector<int> order2(num_pts_dgm);
+ for(int i = 0; i < num_pts_dgm; i++) for (int j = 0; j < num_pts_dgm; j++) if(orderp1[j] == i){ order1[i] = j; break; }
+ for(int i = 0; i < num_pts_dgm; i++) for (int j = 0; j < num_pts_dgm; j++) if(orderp2[j] == i){ order2[i] = j; break; }
+
+ // Record all inversions of points in the orders as theta varies along the positive half-disk.
+ std::vector<std::vector<std::pair<int,double> > > anglePerm1(num_pts_dgm);
+ std::vector<std::vector<std::pair<int,double> > > anglePerm2(num_pts_dgm);
+
+ int m1 = angles1.size();
+ for (int i = 0; i < m1; i++){
+ double theta = angles1[i].first; int p = angles1[i].second.first; int q = angles1[i].second.second;
+ anglePerm1[order1[p]].emplace_back(p,theta);
+ anglePerm1[order1[q]].emplace_back(q,theta);
+ int a = order1[p]; int b = order1[q]; order1[p] = b; order1[q] = a;
+ }
+
+ int m2 = angles2.size();
+ for (int i = 0; i < m2; i++){
+ double theta = angles2[i].first; int p = angles2[i].second.first; int q = angles2[i].second.second;
+ anglePerm2[order2[p]].emplace_back(p,theta);
+ anglePerm2[order2[q]].emplace_back(q,theta);
+ int a = order2[p]; int b = order2[q]; order2[p] = b; order2[q] = a;
+ }
+
+ for (int i = 0; i < num_pts_dgm; i++){
+ anglePerm1[order1[i]].emplace_back(i,pi/2);
+ anglePerm2[order2[i]].emplace_back(i,pi/2);
+ }
+
+ // Compute the SW distance with the list of inversions.
+ for (int i = 0; i < num_pts_dgm; i++){
+ std::vector<std::pair<int,double> > u,v; u = anglePerm1[i]; v = anglePerm2[i];
+ double theta1, theta2; theta1 = -pi/2;
+ unsigned int ku, kv; ku = 0; kv = 0; theta2 = std::min(u[ku].second,v[kv].second);
+ while(theta1 != pi/2){
+ if(diagram1[u[ku].first].first != diagram2[v[kv].first].first || diagram1[u[ku].first].second != diagram2[v[kv].first].second)
+ if(theta1 != theta2)
+ sw += compute_int(theta1, theta2, u[ku].first, v[kv].first, diagram1, diagram2);
+ theta1 = theta2;
+ if ( (theta2 == u[ku].second) && ku < u.size()-1 ) ku++;
+ if ( (theta2 == v[kv].second) && kv < v.size()-1 ) kv++;
+ theta2 = std::min(u[ku].second, v[kv].second);
+ }
+ }
+ }
+
+
+ else{
+
+ double step = pi/this->approx;
+ for (int i = 0; i < this->approx; i++){
+
+ std::vector<double> v1; std::vector<double> l1 = this->projections[i]; std::vector<double> l1bis = second.projections_diagonal[i]; std::merge(l1.begin(), l1.end(), l1bis.begin(), l1bis.end(), std::back_inserter(v1));
+ std::vector<double> v2; std::vector<double> l2 = second.projections[i]; std::vector<double> l2bis = this->projections_diagonal[i]; std::merge(l2.begin(), l2.end(), l2bis.begin(), l2bis.end(), std::back_inserter(v2));
+ int n = v1.size(); double f = 0;
+ for (int j = 0; j < n; j++) f += std::abs(v1[j] - v2[j]);
+ sw += f*step;
+
+ }
+ }
+
+ return sw/pi;
+ }
+
+ /** \brief Evaluation of the kernel on a pair of diagrams.
+ * \ingroup Sliced_Wasserstein
+ *
+ * @pre approx and sigma attributes need to be the same for both instances.
+ * @param[in] second other instance of class Sliced_Wasserstein.
+ *
+ */
+ double compute_scalar_product(const Sliced_Wasserstein & second) const {
+ GUDHI_CHECK(this->sigma != second.sigma, std::invalid_argument("Error: different sigma values for representations"));
+ return std::exp(-compute_sliced_wasserstein_distance(second)/(2*this->sigma*this->sigma));
+ }
+
+ /** \brief Evaluation of the distance between images of diagrams in the Hilbert space of the kernel.
+ * \ingroup Sliced_Wasserstein
+ *
+ * @pre approx and sigma attributes need to be the same for both instances.
+ * @param[in] second other instance of class Sliced_Wasserstein.
+ *
+ */
+ double distance(const Sliced_Wasserstein & second) const {
+ GUDHI_CHECK(this->sigma != second.sigma, std::invalid_argument("Error: different sigma values for representations"));
+ return std::pow(this->compute_scalar_product(*this) + second.compute_scalar_product(second)-2*this->compute_scalar_product(second), 0.5);
+ }
+
+
+
+
+}; // class Sliced_Wasserstein
+} // namespace Persistence_representations
+} // namespace Gudhi
+
+#endif // SLICED_WASSERSTEIN_H_
diff --git a/src/Persistence_representations/include/gudhi/Weight_functions.h b/src/Persistence_representations/include/gudhi/Weight_functions.h
new file mode 100644
index 00000000..78de406d
--- /dev/null
+++ b/src/Persistence_representations/include/gudhi/Weight_functions.h
@@ -0,0 +1,81 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carriere
+ *
+ * Copyright (C) 2018 INRIA (France)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef WEIGHT_FUNCTIONS_H_
+#define WEIGHT_FUNCTIONS_H_
+
+// gudhi include
+#include <gudhi/read_persistence_from_file.h>
+#include <gudhi/common_persistence_representations.h>
+
+// standard include
+#include <cmath>
+#include <iostream>
+#include <vector>
+#include <limits>
+#include <fstream>
+#include <sstream>
+#include <algorithm>
+#include <string>
+#include <utility>
+#include <functional>
+
+namespace Gudhi {
+namespace Persistence_representations {
+
+/** \fn static double pss_weight(std::pair<double,double> p)
+ * \brief Persistence Scale Space kernel weight function.
+ * \ingroup Persistence_representations
+ *
+ * @param[in] p point in 2D.
+ */
+static double pss_weight(std::pair<double,double> p) {if(p.second > p.first) return 1; else return -1;}
+
+/** \fn static double linear_weight(std::pair<double,double> p)
+ * \brief Linear weight function.
+ * \ingroup Persistence_representations
+ *
+ * @param[in] p point in 2D.
+ */
+static double linear_weight(std::pair<double,double> p) {return std::abs(p.second - p.first);}
+
+/** \fn static double const_weight(std::pair<double,double> p)
+ * \brief Constant weight function.
+ * \ingroup Persistence_representations
+ *
+ * @param[in] p point in 2D.
+ */
+static double const_weight(std::pair<double,double> p) {return 1;}
+
+/** \fn static std::function<double (std::pair<double,double>) > arctan_weight(double C, double alpha)
+ * \brief Returns the arctan weight function with parameters C and alpha.
+ * \ingroup Persistence_representations
+ *
+ * @param[in] C positive constant.
+ * @param[in] alpha positive power.
+ */
+static std::function<double (std::pair<double,double>) > arctan_weight(double C, double alpha) {return [=](std::pair<double,double> p){return C * atan(std::pow(std::abs(p.second - p.first), alpha));};}
+
+} // namespace Persistence_representations
+} // namespace Gudhi
+
+#endif // WEIGHT_FUNCTIONS_H_
diff --git a/src/Persistence_representations/include/gudhi/common_persistence_representations.h b/src/Persistence_representations/include/gudhi/common_persistence_representations.h
index 44e125a7..884fce58 100644
--- a/src/Persistence_representations/include/gudhi/common_persistence_representations.h
+++ b/src/Persistence_representations/include/gudhi/common_persistence_representations.h
@@ -26,17 +26,30 @@
#include <utility>
#include <string>
#include <cmath>
+#include <boost/math/constants/constants.hpp>
+
+/**
+ * In this module, we use the name Persistence_diagram for the representation of a diagram in a vector of pairs of two double.
+ */
+using Persistence_diagram = std::vector<std::pair<double,double> >;
+
+/**
+ * In this module, we use the name Weight for the representation of a function taking a pair of two double and returning a double.
+ */
+using Weight = std::function<double (std::pair<double,double>) >;
namespace Gudhi {
namespace Persistence_representations {
// this file contain an implementation of some common procedures used in Persistence_representations.
+static constexpr double pi = boost::math::constants::pi<double>();
+
// double epsi = std::numeric_limits<double>::epsilon();
double epsi = 0.000005;
/**
* A procedure used to compare doubles. Typically given two doubles A and B, comparing A == B is not good idea. In this
- *case, we use the procedure almostEqual with the epsi defined at
+ * case, we use the procedure almostEqual with the epsi defined at
* the top of the file. Setting up the epsi gives the user a tolerance on what should be consider equal.
**/
inline bool almost_equal(double a, double b) {
@@ -53,8 +66,7 @@ double birth_plus_deaths(std::pair<double, double> a) { return a.first + a.secon
// landscapes
/**
- * Given two points in R^2, the procedure compute the parameters A and B of the line y = Ax + B that crosses those two
- *points.
+ * Given two points in R^2, the procedure compute the parameters A and B of the line y = Ax + B that crosses those two points.
**/
std::pair<double, double> compute_parameters_of_a_line(std::pair<double, double> p1, std::pair<double, double> p2) {
double a = (p2.second - p1.second) / (p2.first - p1.first);
@@ -64,8 +76,7 @@ std::pair<double, double> compute_parameters_of_a_line(std::pair<double, double>
// landscapes
/**
- * This procedure given two points which lies on the opposite sides of x axis, compute x for which the line connecting
- *those two points crosses x axis.
+ * This procedure given two points which lies on the opposite sides of x axis, compute x for which the line connecting those two points crosses x axis.
**/
double find_zero_of_a_line_segment_between_those_two_points(std::pair<double, double> p1,
std::pair<double, double> p2) {
@@ -89,8 +100,7 @@ double find_zero_of_a_line_segment_between_those_two_points(std::pair<double, do
// landscapes
/**
* This method provides a comparison of points that is used in construction of persistence landscapes. The ordering is
- *lexicographical for the first coordinate, and reverse-lexicographical for the
- * second coordinate.
+ * lexicographical for the first coordinate, and reverse-lexicographical for the second coordinate.
**/
bool compare_points_sorting(std::pair<double, double> f, std::pair<double, double> s) {
if (f.first < s.first) {
diff --git a/src/Persistence_representations/test/CMakeLists.txt b/src/Persistence_representations/test/CMakeLists.txt
index 335a71ef..ca1713c3 100644
--- a/src/Persistence_representations/test/CMakeLists.txt
+++ b/src/Persistence_representations/test/CMakeLists.txt
@@ -35,6 +35,11 @@ target_link_libraries(Read_persistence_from_file_test_unit ${Boost_UNIT_TEST_FRA
gudhi_add_coverage_test(Read_persistence_from_file_test_unit)
+add_executable ( kernels_unit kernels.cpp )
+target_link_libraries(kernels_unit ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
+
+gudhi_add_coverage_test(kernels_unit)
+
if (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.8.1)
add_executable (Persistence_intervals_with_distances_test_unit persistence_intervals_with_distances_test.cpp )
target_link_libraries(Persistence_intervals_with_distances_test_unit ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
diff --git a/src/Persistence_representations/test/kernels.cpp b/src/Persistence_representations/test/kernels.cpp
new file mode 100644
index 00000000..9db19123
--- /dev/null
+++ b/src/Persistence_representations/test/kernels.cpp
@@ -0,0 +1,54 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carrière
+ *
+ * Copyright (C) 2018 INRIA
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_MODULE "kernel"
+
+#include <boost/test/unit_test.hpp>
+#include <cmath> // float comparison
+#include <limits>
+#include <string>
+#include <vector>
+#include <algorithm> // std::max
+#include <gudhi/common_persistence_representations.h>
+#include <gudhi/Weight_functions.h>
+#include <gudhi/Persistence_weighted_gaussian.h>
+#include <gudhi/Sliced_Wasserstein.h>
+#include <gudhi/distance_functions.h>
+#include <gudhi/reader_utils.h>
+
+using SW = Gudhi::Persistence_representations::Sliced_Wasserstein;
+using PWG = Gudhi::Persistence_representations::Persistence_weighted_gaussian;
+
+BOOST_AUTO_TEST_CASE(check_PWG) {
+ Persistence_diagram v1, v2; v1.emplace_back(0,1); v2.emplace_back(0,2);
+ PWG pwg1(v1, 1.0, 1000, Gudhi::Persistence_representations::arctan_weight(1,1)); PWG pwgex1(v1, 1.0, -1, Gudhi::Persistence_representations::arctan_weight(1,1));
+ PWG pwg2(v2, 1.0, 1000, Gudhi::Persistence_representations::arctan_weight(1,1)); PWG pwgex2(v2, 1.0, -1, Gudhi::Persistence_representations::arctan_weight(1,1));
+ BOOST_CHECK(std::abs(pwg1.compute_scalar_product(pwg2) - pwgex1.compute_scalar_product(pwgex2)) <= 1e-1);
+}
+
+BOOST_AUTO_TEST_CASE(check_SW) {
+ Persistence_diagram v1, v2; v1.emplace_back(0,1); v2.emplace_back(0,2);
+ SW sw1(v1, 1.0, 100); SW swex1(v1, 1.0, -1);
+ SW sw2(v2, 1.0, 100); SW swex2(v2, 1.0, -1);
+ BOOST_CHECK(std::abs(sw1.compute_scalar_product(sw2) - swex1.compute_scalar_product(swex2)) <= 1e-1);
+}
diff --git a/src/cmake/modules/GUDHI_modules.cmake b/src/cmake/modules/GUDHI_modules.cmake
index f95d0c34..205ee8a1 100644
--- a/src/cmake/modules/GUDHI_modules.cmake
+++ b/src/cmake/modules/GUDHI_modules.cmake
@@ -16,8 +16,8 @@ function(add_gudhi_module file_path)
endfunction(add_gudhi_module)
-option(WITH_GUDHI_BENCHMARK "Activate/desactivate benchmark compilation" OFF)
-option(WITH_GUDHI_EXAMPLE "Activate/desactivate examples compilation and installation" OFF)
+option(WITH_GUDHI_BENCHMARK "Activate/desactivate benchmark compilation" ON)
+option(WITH_GUDHI_EXAMPLE "Activate/desactivate examples compilation and installation" ON)
option(WITH_GUDHI_PYTHON "Activate/desactivate python module compilation and installation" ON)
option(WITH_GUDHI_TEST "Activate/desactivate examples compilation and installation" ON)
option(WITH_GUDHI_UTILITIES "Activate/desactivate utilities compilation and installation" ON)
diff --git a/src/cython/cython/kernels.pyx b/src/cython/cython/kernels.pyx
new file mode 100644
index 00000000..0cb296ec
--- /dev/null
+++ b/src/cython/cython/kernels.pyx
@@ -0,0 +1,126 @@
+from cython cimport numeric
+from libcpp.vector cimport vector
+from libcpp.utility cimport pair
+import os
+
+"""This file is part of the Gudhi Library. The Gudhi library
+ (Geometric Understanding in Higher Dimensions) is a generic C++
+ library for computational topology.
+
+ Author(s): Mathieu Carriere
+
+ Copyright (C) 2018 INRIA
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+"""
+
+__author__ = "Mathieu Carriere"
+__copyright__ = "Copyright (C) 2018 INRIA"
+__license__ = "GPL v3"
+
+cdef extern from "Kernels_interface.h" namespace "Gudhi::persistence_diagram":
+ double sw (vector[pair[double, double]], vector[pair[double, double]], double, int)
+ vector[vector[double]] sw_matrix (vector[vector[pair[double, double]]], vector[vector[pair[double, double]]], double, int)
+ double pss (vector[pair[double, double]], vector[pair[double, double]], double, int)
+ vector[vector[double]] pss_matrix (vector[vector[pair[double, double]]], vector[vector[pair[double, double]]], double, int)
+ double pwg (vector[pair[double, double]], vector[pair[double, double]], double, int, double, double)
+ vector[vector[double]] pwg_matrix (vector[vector[pair[double, double]]], vector[vector[pair[double, double]]], double, int, double, double)
+
+def sliced_wasserstein(diagram_1, diagram_2, sigma = 1, N = 100):
+ """
+
+ :param diagram_1: The first diagram.
+ :type diagram_1: vector[pair[double, double]]
+ :param diagram_2: The second diagram.
+ :type diagram_2: vector[pair[double, double]]
+ :param sigma: bandwidth of Gaussian
+ :param N: number of directions
+
+ :returns: the sliced wasserstein kernel.
+ """
+ return sw(diagram_1, diagram_2, sigma, N)
+
+def sliced_wasserstein_matrix(diagrams_1, diagrams_2, sigma = 1, N = 100):
+ """
+
+ :param diagram_1: The first set of diagrams.
+ :type diagram_1: vector[vector[pair[double, double]]]
+ :param diagram_2: The second set of diagrams.
+ :type diagram_2: vector[vector[pair[double, double]]]
+ :param sigma: bandwidth of Gaussian
+ :param N: number of directions
+
+ :returns: the sliced wasserstein kernel matrix.
+ """
+ return sw_matrix(diagrams_1, diagrams_2, sigma, N)
+
+def persistence_weighted_gaussian(diagram_1, diagram_2, sigma = 1, N = 100, C = 1, p = 1):
+ """
+
+ :param diagram_1: The first diagram.
+ :type diagram_1: vector[pair[double, double]]
+ :param diagram_2: The second diagram.
+ :type diagram_2: vector[pair[double, double]]
+ :param sigma: bandwidth of Gaussian
+ :param N: number of Fourier features
+ :param C: cost of persistence weight
+ :param p: power of persistence weight
+
+ :returns: the persistence weighted gaussian kernel.
+ """
+ return pwg(diagram_1, diagram_2, sigma, N, C, p)
+
+def persistence_weighted_gaussian_matrix(diagrams_1, diagrams_2, sigma = 1, N = 100, C = 1, p = 1):
+ """
+
+ :param diagram_1: The first set of diagrams.
+ :type diagram_1: vector[vector[pair[double, double]]]
+ :param diagram_2: The second set of diagrams.
+ :type diagram_2: vector[vector[pair[double, double]]]
+ :param sigma: bandwidth of Gaussian
+ :param N: number of Fourier features
+ :param C: cost of persistence weight
+ :param p: power of persistence weight
+
+ :returns: the persistence weighted gaussian kernel matrix.
+ """
+ return pwg_matrix(diagrams_1, diagrams_2, sigma, N, C, p)
+
+def persistence_scale_space(diagram_1, diagram_2, sigma = 1, N = 100):
+ """
+
+ :param diagram_1: The first diagram.
+ :type diagram_1: vector[pair[double, double]]
+ :param diagram_2: The second diagram.
+ :type diagram_2: vector[pair[double, double]]
+ :param sigma: bandwidth of Gaussian
+ :param N: number of Fourier features
+
+ :returns: the persistence scale space kernel.
+ """
+ return pss(diagram_1, diagram_2, sigma, N)
+
+def persistence_scale_space_matrix(diagrams_1, diagrams_2, sigma = 1, N = 100):
+ """
+
+ :param diagram_1: The first set of diagrams.
+ :type diagram_1: vector[vector[pair[double, double]]]
+ :param diagram_2: The second set of diagrams.
+ :type diagram_2: vector[vector[pair[double, double]]]
+ :param sigma: bandwidth of Gaussian
+ :param N: number of Fourier features
+
+ :returns: the persistence scale space kernel matrix.
+ """
+ return pss_matrix(diagrams_1, diagrams_2, sigma, N)
diff --git a/src/cython/cython/vectors.pyx b/src/cython/cython/vectors.pyx
new file mode 100644
index 00000000..42390ae6
--- /dev/null
+++ b/src/cython/cython/vectors.pyx
@@ -0,0 +1,65 @@
+from cython cimport numeric
+from libcpp.vector cimport vector
+from libcpp.utility cimport pair
+import os
+
+"""This file is part of the Gudhi Library. The Gudhi library
+ (Geometric Understanding in Higher Dimensions) is a generic C++
+ library for computational topology.
+
+ Author(s): Mathieu Carriere
+
+ Copyright (C) 2018 INRIA
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+"""
+
+__author__ = "Mathieu Carriere"
+__copyright__ = "Copyright (C) 2018 INRIA"
+__license__ = "GPL v3"
+
+cdef extern from "Vectors_interface.h" namespace "Gudhi::persistence_diagram":
+ vector[vector[double]] compute_ls (vector[pair[double, double]], int, double, double, int)
+ vector[vector[double]] compute_pim (vector[pair[double, double]], double, double, int, double, double, int, string, double, double, double)
+
+def landscape(diagram, nb_ls = 10, min_x = 0.0, max_x = 1.0, res_x = 100):
+ """
+
+ :param diagram: The diagram
+ :type diagram: vector[pair[double, double]]
+ :param nb_ls: Number of landscapes
+ :param min_x: Minimum abscissa
+ :param max_x: Maximum abscissa
+ :param res_x: Number of samples
+
+ :returns: the landscape
+ """
+ return compute_ls(diagram, nb_ls, min_x, max_x, res_x)
+
+def persistence_image(diagram, min_x = 0.0, max_x = 1.0, res_x = 10, min_y = 0.0, max_y = 1.0, res_y = 10, weight = "linear", sigma = 1.0, C = 1.0, p = 1.0):
+ """
+
+ :param diagram: The diagram
+ :type diagram: vector[vector[pair[double, double]]]
+ :param min_x: Minimum abscissa
+ :param max_x: Maximum abscissa
+ :param res_x: Number of abscissa pixels
+ :param min_x: Minimum ordinate
+ :param max_x: Maximum ordinate
+ :param res_x: Number of ordinate pixels
+ :param sigma: bandwidth of Gaussian
+
+ :returns: the persistence image
+ """
+ return compute_pim(diagram, min_x, max_x, res_x, min_y, max_y, res_y, weight, sigma, C, p)
diff --git a/src/cython/gudhi.pyx.in b/src/cython/gudhi.pyx.in
index a8dd9f80..3ff68085 100644
--- a/src/cython/gudhi.pyx.in
+++ b/src/cython/gudhi.pyx.in
@@ -36,6 +36,8 @@ include '@CMAKE_CURRENT_SOURCE_DIR@/cython/persistence_graphical_tools.py'
include '@CMAKE_CURRENT_SOURCE_DIR@/cython/reader_utils.pyx'
include '@CMAKE_CURRENT_SOURCE_DIR@/cython/witness_complex.pyx'
include '@CMAKE_CURRENT_SOURCE_DIR@/cython/strong_witness_complex.pyx'
+include '@CMAKE_CURRENT_SOURCE_DIR@/cython/kernels.pyx'
+include '@CMAKE_CURRENT_SOURCE_DIR@/cython/vectors.pyx'
@GUDHI_CYTHON_ALPHA_COMPLEX@
@GUDHI_CYTHON_EUCLIDEAN_WITNESS_COMPLEX@
@GUDHI_CYTHON_SUBSAMPLING@
diff --git a/src/cython/include/Kernels_interface.h b/src/cython/include/Kernels_interface.h
new file mode 100644
index 00000000..03050408
--- /dev/null
+++ b/src/cython/include/Kernels_interface.h
@@ -0,0 +1,124 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carriere
+ *
+ * Copyright (C) 2018 INRIA
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef INCLUDE_KERNELS_INTERFACE_H_
+#define INCLUDE_KERNELS_INTERFACE_H_
+
+#include <gudhi/Sliced_Wasserstein.h>
+#include <gudhi/Persistence_weighted_gaussian.h>
+
+#include <iostream>
+#include <vector>
+#include <utility> // for std::pair
+
+namespace Gudhi {
+
+namespace persistence_diagram {
+
+
+ // *******************
+ // Kernel evaluations.
+ // *******************
+
+ double sw(const std::vector<std::pair<double, double>>& diag1, const std::vector<std::pair<double, double>>& diag2, double sigma, int N) {
+ Gudhi::Persistence_representations::Sliced_Wasserstein sw1(diag1, sigma, N);
+ Gudhi::Persistence_representations::Sliced_Wasserstein sw2(diag2, sigma, N);
+ return sw1.compute_scalar_product(sw2);
+ }
+
+ double pwg(const std::vector<std::pair<double, double>>& diag1, const std::vector<std::pair<double, double>>& diag2, double sigma, int N, double C, double p) {
+ Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg1(diag1, sigma, N, Gudhi::Persistence_representations::arctan_weight(C,p));
+ Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg2(diag2, sigma, N, Gudhi::Persistence_representations::arctan_weight(C,p));
+ return pwg1.compute_scalar_product(pwg2);
+ }
+
+ double pss(const std::vector<std::pair<double, double>>& diag1, const std::vector<std::pair<double, double>>& diag2, double sigma, int N) {
+ std::vector<std::pair<double, double>> pd1 = diag1; int numpts = diag1.size(); for(int i = 0; i < numpts; i++) pd1.emplace_back(diag1[i].second,diag1[i].first);
+ std::vector<std::pair<double, double>> pd2 = diag2; numpts = diag2.size(); for(int i = 0; i < numpts; i++) pd2.emplace_back(diag2[i].second,diag2[i].first);
+
+ Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg1(pd1, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::pss_weight);
+ Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg2(pd2, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::pss_weight);
+
+ return pwg1.compute_scalar_product (pwg2) / (16*Gudhi::Persistence_representations::pi*sigma);
+ }
+
+ double pss_sym(const std::vector<std::pair<double, double>>& diag1, const std::vector<std::pair<double, double>>& diag2, double sigma, int N) {
+ Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg1(diag1, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::pss_weight);
+ Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg2(diag2, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::pss_weight);
+
+ return pwg1.compute_scalar_product (pwg2) / (16*Gudhi::Persistence_representations::pi*sigma);
+ }
+
+
+ // ****************
+ // Kernel matrices.
+ // ****************
+
+ std::vector<std::vector<double> > sw_matrix(const std::vector<std::vector<std::pair<double, double> > >& s1, const std::vector<std::vector<std::pair<double, double> > >& s2, double sigma, int N){
+ std::vector<std::vector<double> > matrix;
+ std::vector<Gudhi::Persistence_representations::Sliced_Wasserstein> ss1;
+ int num_diag_1 = s1.size(); for(int i = 0; i < num_diag_1; i++){Gudhi::Persistence_representations::Sliced_Wasserstein sw1(s1[i], sigma, N); ss1.push_back(sw1);}
+ std::vector<Gudhi::Persistence_representations::Sliced_Wasserstein> ss2;
+ int num_diag_2 = s2.size(); for(int i = 0; i < num_diag_2; i++){Gudhi::Persistence_representations::Sliced_Wasserstein sw2(s2[i], sigma, N); ss2.push_back(sw2);}
+ for(int i = 0; i < num_diag_1; i++){
+ std::cout << 100.0*i/num_diag_1 << " %" << std::endl;
+ std::vector<double> ps; for(int j = 0; j < num_diag_2; j++) ps.push_back(ss1[i].compute_scalar_product(ss2[j])); matrix.push_back(ps);
+ }
+ return matrix;
+ }
+
+ std::vector<std::vector<double> > pwg_matrix(const std::vector<std::vector<std::pair<double, double> > >& s1, const std::vector<std::vector<std::pair<double, double> > >& s2, double sigma, int N, double C, double p){
+ std::vector<std::vector<double> > matrix; int num_diag_1 = s1.size(); int num_diag_2 = s2.size();
+ for(int i = 0; i < num_diag_1; i++){
+ std::cout << 100.0*i/num_diag_1 << " %" << std::endl;
+ std::vector<double> ps; for(int j = 0; j < num_diag_2; j++) ps.push_back(pwg(s1[i], s2[j], sigma, N, C, p)); matrix.push_back(ps);
+ }
+ return matrix;
+ }
+
+ std::vector<std::vector<double> > pss_matrix(const std::vector<std::vector<std::pair<double, double> > >& s1, const std::vector<std::vector<std::pair<double, double> > >& s2, double sigma, int N){
+ std::vector<std::vector<std::pair<double, double> > > ss1, ss2; std::vector<std::vector<double> > matrix; int num_diag_1 = s1.size(); int num_diag_2 = s2.size();
+ for(int i = 0; i < num_diag_1; i++){
+ std::vector<std::pair<double, double>> pd1 = s1[i]; int numpts = s1[i].size();
+ for(int j = 0; j < numpts; j++) pd1.emplace_back(s1[i][j].second,s1[i][j].first);
+ ss1.push_back(pd1);
+ }
+
+ for(int i = 0; i < num_diag_2; i++){
+ std::vector<std::pair<double, double>> pd2 = s2[i]; int numpts = s2[i].size();
+ for(int j = 0; j < numpts; j++) pd2.emplace_back(s2[i][j].second,s2[i][j].first);
+ ss2.push_back(pd2);
+ }
+
+ for(int i = 0; i < num_diag_1; i++){
+ std::cout << 100.0*i/num_diag_1 << " %" << std::endl;
+ std::vector<double> ps; for(int j = 0; j < num_diag_2; j++) ps.push_back(pss_sym(ss1[i], ss2[j], sigma, N)); matrix.push_back(ps);
+ }
+ return matrix;
+ }
+
+} // namespace persistence_diagram
+
+} // namespace Gudhi
+
+
+#endif // INCLUDE_KERNELS_INTERFACE_H_
diff --git a/src/cython/include/Vectors_interface.h b/src/cython/include/Vectors_interface.h
new file mode 100644
index 00000000..7e191f2a
--- /dev/null
+++ b/src/cython/include/Vectors_interface.h
@@ -0,0 +1,59 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Mathieu Carriere
+ *
+ * Copyright (C) 2018 INRIA
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef INCLUDE_VECTORS_INTERFACE_H_
+#define INCLUDE_VECTORS_INTERFACE_H_
+
+#include <gudhi/Landscape.h>
+#include <gudhi/Persistence_image.h>
+#include <gudhi/Persistence_weighted_gaussian.h>
+
+#include <iostream>
+#include <vector>
+#include <utility> // for std::pair
+
+using Weight = std::function<double (std::pair<double,double>) >;
+
+namespace Gudhi {
+
+namespace persistence_diagram {
+
+ std::vector<std::vector<double> > compute_ls(const std::vector<std::pair<double, double> >& diag, int nb_ls, double min_x, double max_x, int res_x) {
+ Gudhi::Persistence_representations::Landscape L(diag, nb_ls, min_x, max_x, res_x);
+ return L.vectorize();
+ }
+
+ std::vector<std::vector<double> > compute_pim(const std::vector<std::pair<double, double> >& diag, double min_x, double max_x, int res_x, double min_y, double max_y, int res_y, std::string weight, double sigma, double C, double p) {
+ Weight weight_fn;
+ if(weight.compare("linear") == 0) weight_fn = Gudhi::Persistence_representations::linear_weight;
+ if(weight.compare("arctan") == 0) weight_fn = Gudhi::Persistence_representations::arctan_weight(C,p);
+ if(weight.compare("const") == 0) weight_fn = Gudhi::Persistence_representations::const_weight;
+ Gudhi::Persistence_representations::Persistence_image P(diag, min_x, max_x, res_x, min_y, max_y, res_y, weight_fn, sigma);
+ return P.vectorize();
+ }
+
+} // namespace persistence_diagram
+
+} // namespace Gudhi
+
+
+#endif // INCLUDE_VECTORS_INTERFACE_H_