summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Persistence_representations/example/persistence_weighted_gaussian.cpp4
-rw-r--r--src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h12
-rw-r--r--src/Persistence_representations/include/gudhi/Sliced_Wasserstein.h23
-rw-r--r--src/Persistence_representations/include/gudhi/common_persistence_representations.h2
-rw-r--r--src/cython/include/Kernels_interface.h6
5 files changed, 25 insertions, 22 deletions
diff --git a/src/Persistence_representations/example/persistence_weighted_gaussian.cpp b/src/Persistence_representations/example/persistence_weighted_gaussian.cpp
index d447f165..dea5dab6 100644
--- a/src/Persistence_representations/example/persistence_weighted_gaussian.cpp
+++ b/src/Persistence_representations/example/persistence_weighted_gaussian.cpp
@@ -91,8 +91,8 @@ int main(int argc, char** argv) {
PWG pwgex1(pd1, 2*std::sqrt(sigma), -1, PWG::pss_weight);
PWG pwgex2(pd2, 2*std::sqrt(sigma), -1, PWG::pss_weight);
- std::cout << "Approx PSS kernel: " << pwg1.compute_scalar_product (pwg2) / (16*pi*sigma) << std::endl;
- std::cout << "Exact PSS kernel: " << pwgex1.compute_scalar_product (pwgex2) / (16*pi*sigma) << std::endl;
+ 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;
diff --git a/src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h b/src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h
index d5c8e6d7..b30e0273 100644
--- a/src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h
+++ b/src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h
@@ -25,6 +25,7 @@
// gudhi include
#include <gudhi/read_persistence_from_file.h>
+#include <gudhi/common_persistence_representations.h>
// standard include
#include <cmath>
@@ -37,7 +38,6 @@
#include <string>
#include <utility>
#include <functional>
-#include <boost/math/constants/constants.hpp>
using PD = std::vector<std::pair<double,double> >;
using Weight = std::function<double (std::pair<double,double>) >;
@@ -87,8 +87,6 @@ class Persistence_weighted_gaussian{
int approx;
public:
-
- double pi = boost::math::constants::pi<double>();
/** \brief Persistence Weighted Gaussian Kernel constructor.
* \ingroup Persistence_weighted_gaussian
@@ -101,10 +99,10 @@ class Persistence_weighted_gaussian{
*/
Persistence_weighted_gaussian(PD _diagram, double _sigma = 1.0, int _approx = 1000, Weight _weight = arctan_weight){diagram = _diagram; sigma = _sigma; approx = _approx; weight = _weight;}
- PD get_diagram(){return this->diagram;}
- double get_sigma(){return this->sigma;}
- int get_approx(){return this->approx;}
- Weight get_weight(){return this->weight;}
+ PD get_diagram() const {return this->diagram;}
+ double get_sigma() const {return this->sigma;}
+ int get_approx() const {return this->approx;}
+ Weight get_weight() const {return this->weight;}
// **********************************
diff --git a/src/Persistence_representations/include/gudhi/Sliced_Wasserstein.h b/src/Persistence_representations/include/gudhi/Sliced_Wasserstein.h
index fc3cd142..6a9a607e 100644
--- a/src/Persistence_representations/include/gudhi/Sliced_Wasserstein.h
+++ b/src/Persistence_representations/include/gudhi/Sliced_Wasserstein.h
@@ -25,6 +25,8 @@
// gudhi include
#include <gudhi/read_persistence_from_file.h>
+#include <gudhi/common_persistence_representations.h>
+#include <gudhi/Debug_utils.h>
// standard include
#include <cmath>
@@ -37,7 +39,6 @@
#include <string>
#include <utility>
#include <functional>
-#include <boost/math/constants/constants.hpp>
using PD = std::vector<std::pair<double,double> >;
@@ -68,6 +69,7 @@ namespace Persistence_representations {
* It implements the following concepts: Topological_data_with_distances, Topological_data_with_scalar_product.
*
**/
+
class Sliced_Wasserstein {
protected:
@@ -78,8 +80,6 @@ class Sliced_Wasserstein {
public:
- double pi = boost::math::constants::pi<double>();
-
void build_rep(){
if(approx > 0){
@@ -117,9 +117,9 @@ class Sliced_Wasserstein {
*/
Sliced_Wasserstein(PD _diagram, double _sigma = 1.0, int _approx = 100){diagram = _diagram; approx = _approx; sigma = _sigma; build_rep();}
- PD get_diagram(){return this->diagram;}
- int get_approx(){return this->approx;}
- double get_sigma(){return this->sigma;}
+ PD get_diagram() const {return this->diagram;}
+ int get_approx() const {return this->approx;}
+ double get_sigma() const {return this->sigma;}
@@ -197,11 +197,15 @@ class Sliced_Wasserstein {
/** \brief Evaluation of the Sliced Wasserstein Distance between a pair of diagrams.
* \ingroup Sliced_Wasserstein
*
- * @param[in] second other instance of class Sliced_Wasserstein. Warning: approx parameter needs to be the same for both instances!!!
+ * @param[in] second other instance of class Sliced_Wasserstein.
+ * For warning in red:
+ * @warning approx parameter needs to be the same for both instances.
*
*/
double compute_sliced_wasserstein_distance(Sliced_Wasserstein second) {
+ GUDHI_CHECK(this->approx != second.approx, std::invalid_argument("Error: different approx values for representations"));
+
PD diagram1 = this->diagram; PD diagram2 = second.diagram; double sw = 0;
if(this->approx == -1){
@@ -321,6 +325,7 @@ class Sliced_Wasserstein {
*
*/
double compute_scalar_product(Sliced_Wasserstein second){
+ 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));
}
@@ -331,8 +336,8 @@ class Sliced_Wasserstein {
*
*/
double distance(Sliced_Wasserstein second) {
- if(this->sigma != second.sigma || this->approx != second.approx){std::cout << "Error: different representations!" << std::endl; return 0;}
- else return std::pow(this->compute_scalar_product(*this) + second.compute_scalar_product(second)-2*this->compute_scalar_product(second), 0.5);
+ 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);
}
diff --git a/src/Persistence_representations/include/gudhi/common_persistence_representations.h b/src/Persistence_representations/include/gudhi/common_persistence_representations.h
index 44e125a7..90f2626d 100644
--- a/src/Persistence_representations/include/gudhi/common_persistence_representations.h
+++ b/src/Persistence_representations/include/gudhi/common_persistence_representations.h
@@ -26,11 +26,13 @@
#include <utility>
#include <string>
#include <cmath>
+#include <boost/math/constants/constants.hpp>
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;
diff --git a/src/cython/include/Kernels_interface.h b/src/cython/include/Kernels_interface.h
index 33cd6e35..1742d016 100644
--- a/src/cython/include/Kernels_interface.h
+++ b/src/cython/include/Kernels_interface.h
@@ -53,24 +53,22 @@ namespace persistence_diagram {
double pss(const std::vector<std::pair<double, double>>& diag1,
const std::vector<std::pair<double, double>>& diag2,
double sigma, int N) {
- double pi = boost::math::constants::pi<double>();
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::Persistence_weighted_gaussian::pss_weight);
Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg2(pd2, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::Persistence_weighted_gaussian::pss_weight);
- return pwg1.compute_scalar_product (pwg2) / (16*pi*sigma);
+ 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) {
- double pi = boost::math::constants::pi<double>();
Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg1(diag1, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::Persistence_weighted_gaussian::pss_weight);
Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg2(diag2, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::Persistence_weighted_gaussian::pss_weight);
- return pwg1.compute_scalar_product (pwg2) / (16*pi*sigma);
+ return pwg1.compute_scalar_product (pwg2) / (16*Gudhi::Persistence_representations::pi*sigma);
}