Skip to content

Commit

Permalink
Optional num_parameters, vincent's tests of simplextree multi
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLapous committed Oct 5, 2023
1 parent 96056a3 commit 6d5ad8b
Show file tree
Hide file tree
Showing 3 changed files with 383 additions and 78 deletions.
13 changes: 8 additions & 5 deletions src/Simplex_tree/include/gudhi/Simplex_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,20 +400,23 @@ class Simplex_tree {
: null_vertex_(-1),
root_(nullptr, null_vertex_),
filtration_vect_(),
dimension_(-1) { }
dimension_(-1) {
if constexpr (Options::is_multi_parameter) number_of_parameters_ = 2;
}

/** \brief User-defined copy constructor reproduces the whole tree structure. */
Simplex_tree(const Simplex_tree& complex_source) {
#ifdef DEBUG_TRACES
std::clog << "Simplex_tree copy constructor" << std::endl;
#endif // DEBUG_TRACES
copy_from(complex_source);
if constexpr (Options::is_multi_parameter) number_of_parameters_ = complex_source.number_of_parameters_;
}

/** \brief User-defined move constructor relocates the whole tree structure.
* \exception std::invalid_argument In debug mode, if the complex_source is invalid.
*/
Simplex_tree(Simplex_tree && complex_source) {
Simplex_tree(Simplex_tree && complex_source) : number_of_parameters_(std::move(complex_source.number_of_parameters_)) {
#ifdef DEBUG_TRACES
std::clog << "Simplex_tree move constructor" << std::endl;
#endif // DEBUG_TRACES
Expand Down Expand Up @@ -1627,7 +1630,7 @@ class Simplex_tree {
Filtration_value max_filt_border_value;
if constexpr (SimplexTreeOptions::is_multi_parameter) {
// in that case, we assume that Filtration_value has a `push_to` member to handle this.
max_filt_border_value = Filtration_value(this->number_of_parameters_);
max_filt_border_value = Filtration_value(*number_of_parameters_); // is multiparam
for (auto& face_sh : boundary) {
max_filt_border_value.push_to(
filtration(face_sh)); // pushes the value of max_filt_border_value to reach simplex' filtration
Expand Down Expand Up @@ -2286,7 +2289,7 @@ class Simplex_tree {
* */
int get_number_of_parameters() const {
if constexpr (SimplexTreeOptions::is_multi_parameter)
return number_of_parameters_;
return *number_of_parameters_;
else
return 1;
}
Expand All @@ -2296,7 +2299,7 @@ class Simplex_tree {
: std::numeric_limits<Filtration_value>::max(); /**< Default infinite value. */

private:
int number_of_parameters_; /**< Number of parameters of the multi-filtrations when SimplexTreeOptions::is_multi_parameter.-*/
std::optional<int> number_of_parameters_; /**< Number of parameters of the multi-filtrations when SimplexTreeOptions::is_multi_parameter.-*/
};

// Print a Simplex_tree in os.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <algorithm>
#include <limits>
#include <vector>
#include <cmath>

namespace Gudhi::multiparameter::multi_filtrations {

Expand Down Expand Up @@ -53,7 +54,20 @@ class Finitely_critical_multi_filtration : public std::vector<T> {
operator std::vector<T>&() const { return *this; }
std::vector<T> get_vector() const { return static_cast<std::vector<T>>(*this); }

friend bool operator<(const Finitely_critical_multi_filtration& a, const Finitely_critical_multi_filtration& b) {
inline bool is_inf() const {
if (this->size() != 1) return false;
return *(this->begin()) == std::numeric_limits<T>::infinity();
}
inline bool is_minus_inf() const {
if (this->size() != 1) return false;
return *(this->begin()) == - std::numeric_limits<T>::infinity();
}
inline bool is_nan() const {
if (this->size() != 1) return false;
return std::isnan(*(this->begin()));
}
inline friend bool operator<(const Finitely_critical_multi_filtration& a, const Finitely_critical_multi_filtration& b) {
if (a.is_inf() || b.is_inf() || a.is_nan() || b.is_nan()) return false;
bool isSame = true;
int n = std::min(a.size(), b.size());
for (int i = 0; i < n; ++i) {
Expand All @@ -63,7 +77,9 @@ class Finitely_critical_multi_filtration : public std::vector<T> {
if (isSame) return false;
return true;
}
friend bool operator<=(const Finitely_critical_multi_filtration& a, const Finitely_critical_multi_filtration& b) {
inline friend bool operator<=(const Finitely_critical_multi_filtration& a, const Finitely_critical_multi_filtration& b) {
if (a.is_nan() || b.is_nan()) return false;
if (b.is_inf()) return a.is_inf();
int n = std::min(a.size(), b.size());
for (int i = 0; i < n; ++i) {
if (a[i] > b[i]) return false;
Expand All @@ -72,40 +88,40 @@ class Finitely_critical_multi_filtration : public std::vector<T> {
}

// GREATER THAN OPERATORS
friend bool operator>(const Finitely_critical_multi_filtration& a, const Finitely_critical_multi_filtration& b) {
inline friend bool operator>(const Finitely_critical_multi_filtration& a, const Finitely_critical_multi_filtration& b) {
return b < a;
}
friend bool operator>=(const Finitely_critical_multi_filtration& a, const Finitely_critical_multi_filtration& b) {
inline friend bool operator>=(const Finitely_critical_multi_filtration& a, const Finitely_critical_multi_filtration& b) {
return b <= a;
}

Finitely_critical_multi_filtration& operator=(const Finitely_critical_multi_filtration& a) {
inline Finitely_critical_multi_filtration& operator=(const Finitely_critical_multi_filtration& a) {
std::vector<T>::operator=(a);
return *this;
}

std::vector<T>& _convert_back() { return *this; }

friend Finitely_critical_multi_filtration& operator-=(Finitely_critical_multi_filtration& result,
inline friend Finitely_critical_multi_filtration& operator-=(Finitely_critical_multi_filtration& result,
const Finitely_critical_multi_filtration& to_substract) {
std::transform(result.begin(), result.end(), to_substract.begin(), result.begin(), std::minus<T>());
return result;
}
friend Finitely_critical_multi_filtration<T>& operator+=(Finitely_critical_multi_filtration<T>& result,
inline friend Finitely_critical_multi_filtration<T>& operator+=(Finitely_critical_multi_filtration<T>& result,
const Finitely_critical_multi_filtration& to_add) {
std::transform(result.begin(), result.end(), to_add.begin(), result.begin(), std::plus<T>());
return result;
}

friend Finitely_critical_multi_filtration& operator-=(Finitely_critical_multi_filtration& result,
inline friend Finitely_critical_multi_filtration& operator-=(Finitely_critical_multi_filtration& result,
const T& to_substract) {
// std::transform(result.begin(), result.end(), to_substract.begin(),result.begin(), std::minus<T>());
for (auto& truc : result) {
truc -= to_substract;
}
return result;
}
friend Finitely_critical_multi_filtration<T>& operator+=(Finitely_critical_multi_filtration<T>& result,
inline friend Finitely_critical_multi_filtration<T>& operator+=(Finitely_critical_multi_filtration<T>& result,
const T& to_add) {
for (auto& truc : result) {
truc += to_add;
Expand All @@ -114,7 +130,7 @@ class Finitely_critical_multi_filtration : public std::vector<T> {
}

// template<class array_like>
friend bool operator==(Finitely_critical_multi_filtration<T>& self,
inline friend bool operator==(Finitely_critical_multi_filtration<T>& self,
const Finitely_critical_multi_filtration<T>& to_compare) {
if (self.size() != to_compare.size()) return false;
auto it = to_compare.begin();
Expand All @@ -124,36 +140,57 @@ class Finitely_critical_multi_filtration : public std::vector<T> {
return true;
}

static std::vector<std::vector<T>> to_python(const std::vector<Finitely_critical_multi_filtration<T>>& to_convert) {
inline static std::vector<std::vector<T>> to_python(const std::vector<Finitely_critical_multi_filtration<T>>& to_convert) {
return std::vector<std::vector<T>>(to_convert.begin(), to_convert.end());
}

static std::vector<Finitely_critical_multi_filtration<T>> from_python(const std::vector<std::vector<T>>& to_convert) {
inline static std::vector<Finitely_critical_multi_filtration<T>> from_python(const std::vector<std::vector<T>>& to_convert) {
return std::vector<Finitely_critical_multi_filtration<T>>(to_convert.begin(), to_convert.end());
}
void push_to(const Finitely_critical_multi_filtration<T>& x) {
inline void push_to(const Finitely_critical_multi_filtration<T>& x) {
if (this->is_inf() || this->is_nan() || x.is_nan() || x.is_minus_inf())
return;
if (x.is_inf() || this->is_minus_inf()) {
*this = x;
return;
}
if (this->size() != x.size()) {
std::cerr << "Does only work with 1-critical filtrations ! Sizes " << this->size() << " and " << x.size()
<< "are different !" << std::endl;
std::cerr << "Does only work with 1-critical filtrations ! Sizes "
<< this->size() << " and " << x.size()
<< "are different !" << std::endl;
std::cerr << "This : " << *this << std::endl;
std::cerr << "arg : " << x << std::endl;
throw std::logic_error("Bad sizes");
}
for (unsigned int i = 0; i < x.size(); i++) this->at(i) = this->at(i) > x[i] ? this->at(i) : x[i];
}
// Warning, this function assumes that the comparisons checks have already been made !
void insert_new(Finitely_critical_multi_filtration to_concatenate) {
inline void insert_new(Finitely_critical_multi_filtration to_concatenate) {
this->insert(this->end(), std::move_iterator(to_concatenate.begin()), std::move_iterator(to_concatenate.end()));
}

// scalar product of a filtration value with x.
T linear_projection(const std::vector<T>& x) {
inline T linear_projection(const std::vector<T>& x) {
T projection = 0;
unsigned int size = std::min(x.size(), this->size());
for (auto i = 0u; i < size; i++) projection += x[i] * this->at(i);
return projection;
}

// easy debug
friend std::ostream& operator<<(std::ostream& stream, const Finitely_critical_multi_filtration<T>& truc) {
inline friend std::ostream& operator<<(std::ostream& stream, const Finitely_critical_multi_filtration<T>& truc) {
if (truc.is_inf()) {
stream << "[inf, ..., inf]";
return stream;
}
if (truc.is_minus_inf()) {
stream << "[-inf, ..., -inf]";
return stream;
}
if (truc.is_nan()) {
stream << "[NaN]";
return stream;
}
if (truc.empty()) {
stream << "[]";
return stream;
Expand Down Expand Up @@ -192,4 +229,7 @@ static Gudhi::multiparameter::multi_filtrations::Finitely_critical_multi_filtrat

}




#endif // FINITELY_CRITICAL_FILTRATIONS_H_
Loading

0 comments on commit 6d5ad8b

Please sign in to comment.