Loading...
Searching...
No Matches
Witness_complex.h
/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
* See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
* Author(s): Siargey Kachanovich
*
* Copyright (C) 2015 Inria
*
* Modification(s):
* - YYYY/MM Author: Description of the modification
*/
#ifndef WITNESS_COMPLEX_H_
#define WITNESS_COMPLEX_H_
#include <gudhi/Active_witness/Active_witness.h>
#include <gudhi/Witness_complex/all_faces_in.h>
#include <utility>
#include <vector>
#include <list>
#include <limits>
namespace Gudhi {
namespace witness_complex {
template< class Nearest_landmark_table_ >
class Witness_complex {
private:
typedef typename Nearest_landmark_table_::value_type Nearest_landmark_range;
typedef std::size_t Witness_id;
typedef std::size_t Landmark_id;
typedef std::pair<Landmark_id, double> Id_distance_pair;
typedef Active_witness<Id_distance_pair, Nearest_landmark_range> ActiveWitness;
typedef std::list< ActiveWitness > ActiveWitnessList;
typedef std::vector< Landmark_id > typeVectorVertex;
typedef std::vector<Nearest_landmark_range> Nearest_landmark_table_internal;
typedef Landmark_id Vertex_handle;
protected:
Nearest_landmark_table_internal nearest_landmark_table_;
public:
/* @name Constructor
*/
Witness_complex() {
}
Witness_complex(Nearest_landmark_table_ const & nearest_landmark_table)
: nearest_landmark_table_(std::begin(nearest_landmark_table), std::end(nearest_landmark_table)) {
}
template < typename SimplicialComplexForWitness >
bool create_complex(SimplicialComplexForWitness& complex,
double max_alpha_square,
std::size_t limit_dimension = std::numeric_limits<std::size_t>::max()) const {
if (complex.num_vertices() > 0) {
std::cerr << "Witness complex cannot create complex - complex is not empty.\n";
return false;
}
if (max_alpha_square < 0) {
std::cerr << "Witness complex cannot create complex - squared relaxation parameter must be non-negative.\n";
return false;
}
ActiveWitnessList active_witnesses;
Landmark_id k = 0; /* current dimension in iterative construction */
for (auto&& w : nearest_landmark_table_)
active_witnesses.emplace_back(w);
while (!active_witnesses.empty() && k <= limit_dimension) {
typename ActiveWitnessList::iterator aw_it = active_witnesses.begin();
std::vector<Landmark_id> simplex;
simplex.reserve(k+1);
while (aw_it != active_witnesses.end()) {
bool ok = add_all_faces_of_dimension(k,
max_alpha_square,
std::numeric_limits<double>::infinity(),
aw_it->begin(),
simplex,
complex,
aw_it->end());
assert(simplex.empty());
if (!ok)
active_witnesses.erase(aw_it++); // First increase the iterator and then erase the previous element
else
aw_it++;
}
k++;
}
return true;
}
private:
template < typename SimplicialComplexForWitness >
bool add_all_faces_of_dimension(int dim,
double alpha2,
double norelax_dist2,
typename ActiveWitness::iterator curr_l,
std::vector<Landmark_id>& simplex,
SimplicialComplexForWitness& sc,
typename ActiveWitness::iterator end) const {
if (curr_l == end)
return false;
bool will_be_active = false;
typename ActiveWitness::iterator l_it = curr_l;
if (dim > 0) {
for (; l_it != end && l_it->second - alpha2 <= norelax_dist2; ++l_it) {
simplex.push_back(l_it->first);
if (sc.find(simplex) != sc.null_simplex()) {
typename ActiveWitness::iterator next_it = l_it;
will_be_active = add_all_faces_of_dimension(dim-1,
alpha2,
norelax_dist2,
++next_it,
simplex,
sc,
end) || will_be_active;
}
assert(!simplex.empty());
simplex.pop_back();
// If norelax_dist is infinity, change to first omitted distance
if (l_it->second <= norelax_dist2)
norelax_dist2 = l_it->second;
}
} else if (dim == 0) {
for (; l_it != end && l_it->second - alpha2 <= norelax_dist2; ++l_it) {
simplex.push_back(l_it->first);
double filtration_value = 0;
// if norelax_dist is infinite, relaxation is 0.
if (l_it->second > norelax_dist2)
filtration_value = l_it->second - norelax_dist2;
if (all_faces_in(simplex, &filtration_value, sc)) {
will_be_active = true;
sc.insert_simplex(simplex, filtration_value);
}
assert(!simplex.empty());
simplex.pop_back();
// If norelax_dist is infinity, change to first omitted distance
if (l_it->second < norelax_dist2)
norelax_dist2 = l_it->second;
}
}
return will_be_active;
}
};
} // namespace witness_complex
} // namespace Gudhi
#endif // WITNESS_COMPLEX_H_
Gudhi::witness_complex::Active_witness
Class representing a list of nearest neighbors to a given witness.
Definition Active_witness.h:27
Gudhi::witness_complex::Witness_complex::create_complex
bool create_complex(SimplicialComplexForWitness &complex, double max_alpha_square, std::size_t limit_dimension=std::numeric_limits< std::size_t >::max()) const
Outputs the (weak) witness complex of relaxation 'max_alpha_square' in a simplicial complex data stru...
Definition Witness_complex.h:89
Gudhi::witness_complex::Witness_complex::Witness_complex
Witness_complex(Nearest_landmark_table_ const &nearest_landmark_table)
Initializes member variables before constructing simplicial complex.
Definition Witness_complex.h:75
Gudhi
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14
std
STL namespace.
Gudhi::witness_complex::SimplicialComplexForWitness
The concept SimplicialComplexForWitness describes the requirements for a type to implement a simplici...
Definition SimplicialComplexForWitness.h:22
Gudhi::witness_complex::SimplicialComplexForWitness::num_vertices
std::size_t num_vertices()
Gudhi::witness_complex::SimplicialComplexForWitness::insert_simplex
Insertion_result_type insert_simplex(Input_vertex_range const &vertex_range, Filtration_value filtration)
Inserts a simplex with vertices from a given range 'vertex_range' in the simplicial complex....
Gudhi::witness_complex::SimplicialComplexForWitness::find
Simplex_handle find(Input_vertex_range const &vertex_range)
Finds a simplex with vertices given by a range.
Gudhi::witness_complex::SimplicialComplexForWitness::null_simplex
Simplex_handle null_simplex()
Returns a Simplex_hanlde that is different from all simplex handles of the simplices.