12#ifndef PERSISTENCE_VECTORS_H_
13#define PERSISTENCE_VECTORS_H_
31#include <gudhi/read_persistence_from_file.h>
33#include <gudhi/Debug_utils.h>
36namespace Persistence_representations {
71 : intervals_(intervals), where_to_cut_(where_to_cut)
73 this->_compute_sorted_vector_of_distances_via_vector_sorting();
74 this->_set_up_numbers_of_functions_for_vectorization_and_projections_to_reals();
82 std::size_t where_to_cut,
83 unsigned int dimension = std::numeric_limits<unsigned int>::max())
86 dimension ==
std::numeric_limits<unsigned int>::max() ? -1 : static_cast<int>(dimension))),
87 where_to_cut_(where_to_cut)
89 this->_compute_sorted_vector_of_distances_via_heap();
90 _set_up_numbers_of_functions_for_vectorization_and_projections_to_reals();
98 for (std::size_t i = 0; i != std::min(d.sorted_vector_of_distances_.size(), d.where_to_cut_); ++i) {
99 out << d.sorted_vector_of_distances_[i] <<
" ";
109 if (position >= this->sorted_vector_of_distances_.size())
110 throw(
"Wrong position in accessing Vector_distances_in_diagram::sorted_vector_of_distances_\n");
111 return this->sorted_vector_of_distances_[position];
117 std::size_t
size()
const {
return this->sorted_vector_of_distances_.size(); }
139 if (this->sorted_vector_of_distances_.size() != second.sorted_vector_of_distances_.size())
return false;
140 for (std::size_t i = 0; i != this->sorted_vector_of_distances_.size(); ++i) {
141 if (!
almost_equal(this->sorted_vector_of_distances_[i], second.sorted_vector_of_distances_[i]))
return false;
166 std::vector<double>
vectorize(
int number_of_function)
const;
201 void plot(
const char* filename)
const
203 std::stringstream gnuplot_script;
204 gnuplot_script << filename <<
"_GnuplotScript";
206 out.open(gnuplot_script.str().c_str());
207 out <<
"set style data histogram" << std::endl;
208 out <<
"set style histogram cluster gap 1" << std::endl;
209 out <<
"set style fill solid border -1" << std::endl;
210 out <<
"plot '-' notitle" << std::endl;
211 for (std::size_t i = 0; i != this->sorted_vector_of_distances_.size(); ++i) {
212 out << this->sorted_vector_of_distances_[i] << std::endl;
217 std::clog <<
"To visualize, install gnuplot and type the command: gnuplot -persist -e \"load \'"
218 << gnuplot_script.str().c_str() <<
"\'\"" << std::endl;
225 std::pair<double, double>
get_x_range()
const {
return std::make_pair(0, this->sorted_vector_of_distances_.size()); }
232 if (this->sorted_vector_of_distances_.size() == 0)
return std::make_pair(0, 0);
233 return std::make_pair(this->sorted_vector_of_distances_[0], 0);
237 template <
typename Operation>
240 Operation&& operation)
243 result.sorted_vector_of_distances_.reserve(
244 std::max(first.sorted_vector_of_distances_.size(), second.sorted_vector_of_distances_.size()));
245 for (std::size_t i = 0;
246 i != std::min(first.sorted_vector_of_distances_.size(), second.sorted_vector_of_distances_.size());
248 result.sorted_vector_of_distances_.push_back(
249 operation(first.sorted_vector_of_distances_[i], second.sorted_vector_of_distances_[i]));
251 if (first.sorted_vector_of_distances_.size() ==
252 std::min(first.sorted_vector_of_distances_.size(), second.sorted_vector_of_distances_.size())) {
254 std::min(first.sorted_vector_of_distances_.size(), second.sorted_vector_of_distances_.size());
255 i != std::max(first.sorted_vector_of_distances_.size(), second.sorted_vector_of_distances_.size());
257 result.sorted_vector_of_distances_.push_back(operation(0, second.sorted_vector_of_distances_[i]));
261 std::min(first.sorted_vector_of_distances_.size(), second.sorted_vector_of_distances_.size());
262 i != std::max(first.sorted_vector_of_distances_.size(), second.sorted_vector_of_distances_.size());
264 result.sorted_vector_of_distances_.push_back(operation(first.sorted_vector_of_distances_[i], 0));
276 result.sorted_vector_of_distances_.reserve(this->sorted_vector_of_distances_.size());
277 for (std::size_t i = 0; i != this->sorted_vector_of_distances_.size(); ++i) {
278 result.sorted_vector_of_distances_.push_back(scalar * this->sorted_vector_of_distances_[i]);
289 return operation_on_pair_of_vectors(first, second, std::plus<double>());
298 return operation_on_pair_of_vectors(first, second, std::minus<double>());
354 if (x == 0)
throw(
"In operator /=, division by 0. Program terminated.");
355 *
this = *
this * (1 / x);
360 std::vector<std::pair<double, double> > intervals_;
361 std::vector<double> sorted_vector_of_distances_;
362 std::size_t number_of_functions_for_vectorization_;
363 std::size_t number_of_functions_for_projections_to_reals_;
364 std::size_t where_to_cut_;
366 void _compute_sorted_vector_of_distances_via_heap();
367 void _compute_sorted_vector_of_distances_via_vector_sorting();
370 : sorted_vector_of_distances_(sorted_vector_of_distances)
372 this->_set_up_numbers_of_functions_for_vectorization_and_projections_to_reals();
376 void _set_up_numbers_of_functions_for_vectorization_and_projections_to_reals()
378 this->number_of_functions_for_vectorization_ = this->sorted_vector_of_distances_.size();
379 this->number_of_functions_for_projections_to_reals_ = this->sorted_vector_of_distances_.size();
384void Vector_distances_in_diagram<F>::_compute_sorted_vector_of_distances_via_heap()
387 std::clog <<
"Here are the intervals : \n";
388 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
389 std::clog << this->intervals_[i].first <<
" , " << this->intervals_[i].second << std::endl;
394 std::min(where_to_cut_,
395 (std::size_t)(0.5 * this->intervals_.size() * (this->intervals_.size() - 1) + this->intervals_.size()));
397 std::vector<double> heap(where_to_cut, std::numeric_limits<int>::max());
398 std::make_heap(heap.begin(), heap.end());
403 for (std::size_t i = 0; i < this->intervals_.size(); ++i) {
404 for (std::size_t j = i + 1; j < this->intervals_.size(); ++j) {
406 std::min(f(this->intervals_[i], this->intervals_[j]),
407 std::min(f(this->intervals_[i],
408 std::make_pair(0.5 * (this->intervals_[i].first + this->intervals_[i].second),
409 0.5 * (this->intervals_[i].first + this->intervals_[i].second))),
410 f(this->intervals_[j],
411 std::make_pair(0.5 * (this->intervals_[j].first + this->intervals_[j].second),
412 0.5 * (this->intervals_[j].first + this->intervals_[j].second)))));
415 std::clog <<
"Value : " << value << std::endl;
416 std::clog <<
"heap.front() : " << heap.front() << std::endl;
419 if (-value < heap.front()) {
421 std::clog <<
"Replacing : " << heap.front() <<
" with : " << -value << std::endl;
424 std::pop_heap(heap.begin(), heap.end());
426 heap[where_to_cut - 1] = -value;
427 std::push_heap(heap.begin(), heap.end());
433 for (std::size_t i = 0; i < this->intervals_.size(); ++i) {
434 double value = f(this->intervals_[i],
435 std::make_pair(0.5 * (this->intervals_[i].first + this->intervals_[i].second),
436 0.5 * (this->intervals_[i].first + this->intervals_[i].second)));
437 if (-value < heap.front()) {
439 std::pop_heap(heap.begin(), heap.end());
441 heap[where_to_cut - 1] = -value;
442 std::push_heap(heap.begin(), heap.end());
446 std::sort_heap(heap.begin(), heap.end());
447 for (std::size_t i = 0; i != heap.size(); ++i) {
448 if (heap[i] == std::numeric_limits<int>::max()) {
456 std::clog <<
"This is the heap after all the operations :\n";
457 for (std::size_t i = 0; i != heap.size(); ++i) {
458 std::clog << heap[i] <<
" ";
460 std::clog << std::endl;
463 this->sorted_vector_of_distances_.swap(heap);
467void Vector_distances_in_diagram<F>::_compute_sorted_vector_of_distances_via_vector_sorting()
469 std::vector<double> distances;
471 (std::size_t)(0.5 * this->intervals_.size() * (this->intervals_.size() - 1) + this->intervals_.size()));
476 for (std::size_t i = 0; i < this->intervals_.size(); ++i) {
478 distances.push_back(f(this->intervals_[i],
479 std::make_pair(0.5 * (this->intervals_[i].first + this->intervals_[i].second),
480 0.5 * (this->intervals_[i].first + this->intervals_[i].second))));
481 for (std::size_t j = i + 1; j < this->intervals_.size(); ++j) {
483 std::min(f(this->intervals_[i], this->intervals_[j]),
484 std::min(f(this->intervals_[i],
485 std::make_pair(0.5 * (this->intervals_[i].first + this->intervals_[i].second),
486 0.5 * (this->intervals_[i].first + this->intervals_[i].second))),
487 f(this->intervals_[j],
488 std::make_pair(0.5 * (this->intervals_[j].first + this->intervals_[j].second),
489 0.5 * (this->intervals_[j].first + this->intervals_[j].second)))));
490 distances.push_back(value);
493 std::sort(distances.begin(), distances.end(), std::greater<double>());
494 if (distances.size() > where_to_cut_) distances.resize(where_to_cut_);
496 this->sorted_vector_of_distances_.swap(distances);
505 if (
static_cast<std::size_t
>(number_of_function) > this->number_of_functions_for_projections_to_reals_)
506 throw std::invalid_argument(
"Wrong index of a function in a method Vector_distances_in_diagram<F>::project_to_R");
507 if (number_of_function < 0)
508 throw std::invalid_argument(
"Wrong index of a function in a method Vector_distances_in_diagram<F>::project_to_R");
511 for (std::size_t i = 0; i !=
static_cast<std::size_t
>(number_of_function); ++i) {
512 result += sorted_vector_of_distances_[i];
520 if (to_average.size() == 0) {
525 std::size_t maximal_length_of_vector = 0;
526 for (std::size_t i = 0; i != to_average.size(); ++i) {
527 if (to_average[i]->sorted_vector_of_distances_.size() > maximal_length_of_vector) {
528 maximal_length_of_vector = to_average[i]->sorted_vector_of_distances_.size();
532 std::vector<double> av(maximal_length_of_vector, 0);
533 for (std::size_t i = 0; i != to_average.size(); ++i) {
534 for (std::size_t j = 0; j != to_average[i]->sorted_vector_of_distances_.size(); ++j) {
535 av[j] += to_average[i]->sorted_vector_of_distances_[j];
539 for (std::size_t i = 0; i != maximal_length_of_vector; ++i) {
540 av[i] /=
static_cast<double>(to_average.size());
542 this->sorted_vector_of_distances_.swap(av);
543 this->where_to_cut_ = av.size();
550 std::clog <<
"Entering double Vector_distances_in_diagram<F>::distance( const Abs_Topological_data_with_distances* "
551 "second , double power ) procedure \n";
552 std::clog <<
"Power : " << power << std::endl;
553 std::clog <<
"This : " << *
this << std::endl;
554 std::clog <<
"second : " << second_ << std::endl;
558 for (std::size_t i = 0;
559 i != std::min(this->sorted_vector_of_distances_.size(), second_.sorted_vector_of_distances_.size());
563 std::clog <<
"|" << this->sorted_vector_of_distances_[i] <<
" - " << second_.sorted_vector_of_distances_[i]
564 <<
" | : " << std::fabs(this->sorted_vector_of_distances_[i] - second_.sorted_vector_of_distances_[i])
567 result += std::fabs(this->sorted_vector_of_distances_[i] - second_.sorted_vector_of_distances_[i]);
569 if (power < std::numeric_limits<double>::max()) {
571 std::pow(std::fabs(this->sorted_vector_of_distances_[i] - second_.sorted_vector_of_distances_[i]), power);
574 if (result < std::fabs(this->sorted_vector_of_distances_[i] - second_.sorted_vector_of_distances_[i]))
575 result = std::fabs(this->sorted_vector_of_distances_[i] - second_.sorted_vector_of_distances_[i]);
578 std::clog <<
"| " << this->sorted_vector_of_distances_[i] <<
" - " << second_.sorted_vector_of_distances_[i]
579 <<
" : " << std::fabs(this->sorted_vector_of_distances_[i] - second_.sorted_vector_of_distances_[i])
584 if (this->sorted_vector_of_distances_.size() != second_.sorted_vector_of_distances_.size()) {
585 if (this->sorted_vector_of_distances_.size() > second_.sorted_vector_of_distances_.size()) {
586 for (std::size_t i = second_.sorted_vector_of_distances_.size(); i != this->sorted_vector_of_distances_.size();
588 result += std::fabs(this->sorted_vector_of_distances_[i]);
592 for (std::size_t i = this->sorted_vector_of_distances_.size(); i != second_.sorted_vector_of_distances_.size();
594 result += std::fabs(second_.sorted_vector_of_distances_[i]);
600 result = std::pow(result, (1.0 / power));
610 if (
static_cast<std::size_t
>(number_of_function) > this->number_of_functions_for_vectorization_)
611 throw std::invalid_argument(
"Wrong index of a function in a method Vector_distances_in_diagram<F>::vectorize");
612 if (number_of_function < 0)
613 throw std::invalid_argument(
"Wrong index of a function in a method Vector_distances_in_diagram<F>::vectorize");
615 std::vector<double> result(
616 std::min(
static_cast<std::size_t
>(number_of_function), this->sorted_vector_of_distances_.size()));
617 for (std::size_t i = 0;
618 i != std::min(
static_cast<std::size_t
>(number_of_function), this->sorted_vector_of_distances_.size());
620 result[i] = this->sorted_vector_of_distances_[i];
631 for (std::size_t i = 0; i != this->sorted_vector_of_distances_.size(); ++i) {
632 out << this->sorted_vector_of_distances_[i] <<
" ";
646 std::cerr <<
"The file : " << filename <<
" do not exist. The program will now terminate \n";
648 throw std::invalid_argument(
"The persistence landscape file do not exist.");
652 while (in >> number) {
653 this->sorted_vector_of_distances_.push_back(number);
662 for (std::size_t i = 0;
663 i != std::min(this->sorted_vector_of_distances_.size(), second_vector.sorted_vector_of_distances_.size());
665 result += this->sorted_vector_of_distances_[i] * second_vector.sorted_vector_of_distances_[i];
A class implementing persistence vectors.
Definition Persistence_vectors.h:59
bool operator==(const Vector_distances_in_diagram &second) const
Definition Persistence_vectors.h:137
const std::vector< double > & output_for_visualization() const
Definition Persistence_vectors.h:196
void plot(const char *filename) const
Definition Persistence_vectors.h:201
double project_to_R(int number_of_function) const
Definition Persistence_vectors.h:501
Vector_distances_in_diagram operator+=(const Vector_distances_in_diagram &rhs)
Definition Persistence_vectors.h:325
void compute_average(const std::vector< Vector_distances_in_diagram * > &to_average)
Definition Persistence_vectors.h:518
double compute_scalar_product(const Vector_distances_in_diagram &second) const
Definition Persistence_vectors.h:659
std::pair< double, double > get_x_range() const
Definition Persistence_vectors.h:225
void load_from_file(const char *filename)
Definition Persistence_vectors.h:639
Vector_distances_in_diagram operator*=(double x)
Definition Persistence_vectors.h:343
std::size_t size() const
Definition Persistence_vectors.h:117
friend std::ostream & operator<<(std::ostream &out, const Vector_distances_in_diagram &d)
Definition Persistence_vectors.h:96
void write_to_file(const char *filename) const
Definition Persistence_vectors.h:626
std::pair< double, double > get_y_range() const
Definition Persistence_vectors.h:230
std::size_t number_of_vectorize_functions() const
Definition Persistence_vectors.h:172
void print_to_file(const char *filename) const
Definition Persistence_vectors.h:127
double distance(const Vector_distances_in_diagram &second, double power=1) const
Definition Persistence_vectors.h:547
Vector_distances_in_diagram operator/=(double x)
Definition Persistence_vectors.h:352
Vector_distances_in_diagram(const std::vector< std::pair< double, double > > &intervals, std::size_t where_to_cut)
Definition Persistence_vectors.h:70
double vector_in_position(std::size_t position) const
Definition Persistence_vectors.h:107
friend Vector_distances_in_diagram operator*(const Vector_distances_in_diagram &A, double scalar)
Definition Persistence_vectors.h:312
Vector_distances_in_diagram operator*(double scalar)
Definition Persistence_vectors.h:320
friend Vector_distances_in_diagram operator*(double scalar, const Vector_distances_in_diagram &A)
Definition Persistence_vectors.h:304
friend Vector_distances_in_diagram operator+(const Vector_distances_in_diagram &first, const Vector_distances_in_diagram &second)
Definition Persistence_vectors.h:286
Vector_distances_in_diagram()
Definition Persistence_vectors.h:64
std::size_t number_of_projections_to_R() const
Definition Persistence_vectors.h:161
Vector_distances_in_diagram operator-=(const Vector_distances_in_diagram &rhs)
Definition Persistence_vectors.h:334
Vector_distances_in_diagram multiply_by_scalar(double scalar) const
Definition Persistence_vectors.h:273
Vector_distances_in_diagram(const char *filename, std::size_t where_to_cut, unsigned int dimension=std::numeric_limits< unsigned int >::max())
Definition Persistence_vectors.h:81
std::vector< double > vectorize(int number_of_function) const
Definition Persistence_vectors.h:606
friend Vector_distances_in_diagram operator-(const Vector_distances_in_diagram &first, const Vector_distances_in_diagram &second)
Definition Persistence_vectors.h:295
This file contain an implementation of some common procedures used in the Persistence_representations...
bool almost_equal(double a, double b)
Definition common_persistence_representations.h:65
std::vector< std::pair< double, double > > read_persistence_intervals_in_one_dimension_from_file(std::string const &filename, int dimension=-1, double what_to_substitute_for_infinite_bar=-1)
Definition read_persistence_from_file.h:44
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14