multipers 2.3.3b6__cp311-cp311-manylinux_2_39_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of multipers might be problematic. Click here for more details.

Files changed (182) hide show
  1. multipers/__init__.py +33 -0
  2. multipers/_signed_measure_meta.py +453 -0
  3. multipers/_slicer_meta.py +211 -0
  4. multipers/array_api/__init__.py +45 -0
  5. multipers/array_api/numpy.py +41 -0
  6. multipers/array_api/torch.py +58 -0
  7. multipers/data/MOL2.py +458 -0
  8. multipers/data/UCR.py +18 -0
  9. multipers/data/__init__.py +1 -0
  10. multipers/data/graphs.py +466 -0
  11. multipers/data/immuno_regions.py +27 -0
  12. multipers/data/minimal_presentation_to_st_bf.py +0 -0
  13. multipers/data/pytorch2simplextree.py +91 -0
  14. multipers/data/shape3d.py +101 -0
  15. multipers/data/synthetic.py +113 -0
  16. multipers/distances.py +202 -0
  17. multipers/filtration_conversions.pxd +229 -0
  18. multipers/filtration_conversions.pxd.tp +84 -0
  19. multipers/filtrations/__init__.py +18 -0
  20. multipers/filtrations/density.py +574 -0
  21. multipers/filtrations/filtrations.py +361 -0
  22. multipers/filtrations.pxd +224 -0
  23. multipers/function_rips.cpython-311-x86_64-linux-gnu.so +0 -0
  24. multipers/function_rips.pyx +105 -0
  25. multipers/grids.cpython-311-x86_64-linux-gnu.so +0 -0
  26. multipers/grids.pyx +433 -0
  27. multipers/gudhi/Persistence_slices_interface.h +132 -0
  28. multipers/gudhi/Simplex_tree_interface.h +239 -0
  29. multipers/gudhi/Simplex_tree_multi_interface.h +551 -0
  30. multipers/gudhi/cubical_to_boundary.h +59 -0
  31. multipers/gudhi/gudhi/Bitmap_cubical_complex.h +450 -0
  32. multipers/gudhi/gudhi/Bitmap_cubical_complex_base.h +1070 -0
  33. multipers/gudhi/gudhi/Bitmap_cubical_complex_periodic_boundary_conditions_base.h +579 -0
  34. multipers/gudhi/gudhi/Debug_utils.h +45 -0
  35. multipers/gudhi/gudhi/Fields/Multi_field.h +484 -0
  36. multipers/gudhi/gudhi/Fields/Multi_field_operators.h +455 -0
  37. multipers/gudhi/gudhi/Fields/Multi_field_shared.h +450 -0
  38. multipers/gudhi/gudhi/Fields/Multi_field_small.h +531 -0
  39. multipers/gudhi/gudhi/Fields/Multi_field_small_operators.h +507 -0
  40. multipers/gudhi/gudhi/Fields/Multi_field_small_shared.h +531 -0
  41. multipers/gudhi/gudhi/Fields/Z2_field.h +355 -0
  42. multipers/gudhi/gudhi/Fields/Z2_field_operators.h +376 -0
  43. multipers/gudhi/gudhi/Fields/Zp_field.h +420 -0
  44. multipers/gudhi/gudhi/Fields/Zp_field_operators.h +400 -0
  45. multipers/gudhi/gudhi/Fields/Zp_field_shared.h +418 -0
  46. multipers/gudhi/gudhi/Flag_complex_edge_collapser.h +337 -0
  47. multipers/gudhi/gudhi/Matrix.h +2107 -0
  48. multipers/gudhi/gudhi/Multi_critical_filtration.h +1038 -0
  49. multipers/gudhi/gudhi/Multi_persistence/Box.h +174 -0
  50. multipers/gudhi/gudhi/Multi_persistence/Line.h +282 -0
  51. multipers/gudhi/gudhi/Off_reader.h +173 -0
  52. multipers/gudhi/gudhi/One_critical_filtration.h +1441 -0
  53. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix.h +769 -0
  54. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix_with_column_compression.h +686 -0
  55. multipers/gudhi/gudhi/Persistence_matrix/Boundary_matrix.h +842 -0
  56. multipers/gudhi/gudhi/Persistence_matrix/Chain_matrix.h +1350 -0
  57. multipers/gudhi/gudhi/Persistence_matrix/Id_to_index_overlay.h +1105 -0
  58. multipers/gudhi/gudhi/Persistence_matrix/Position_to_index_overlay.h +859 -0
  59. multipers/gudhi/gudhi/Persistence_matrix/RU_matrix.h +910 -0
  60. multipers/gudhi/gudhi/Persistence_matrix/allocators/entry_constructors.h +139 -0
  61. multipers/gudhi/gudhi/Persistence_matrix/base_pairing.h +230 -0
  62. multipers/gudhi/gudhi/Persistence_matrix/base_swap.h +211 -0
  63. multipers/gudhi/gudhi/Persistence_matrix/boundary_cell_position_to_id_mapper.h +60 -0
  64. multipers/gudhi/gudhi/Persistence_matrix/boundary_face_position_to_id_mapper.h +60 -0
  65. multipers/gudhi/gudhi/Persistence_matrix/chain_pairing.h +136 -0
  66. multipers/gudhi/gudhi/Persistence_matrix/chain_rep_cycles.h +190 -0
  67. multipers/gudhi/gudhi/Persistence_matrix/chain_vine_swap.h +616 -0
  68. multipers/gudhi/gudhi/Persistence_matrix/columns/chain_column_extra_properties.h +150 -0
  69. multipers/gudhi/gudhi/Persistence_matrix/columns/column_dimension_holder.h +106 -0
  70. multipers/gudhi/gudhi/Persistence_matrix/columns/column_utilities.h +219 -0
  71. multipers/gudhi/gudhi/Persistence_matrix/columns/entry_types.h +327 -0
  72. multipers/gudhi/gudhi/Persistence_matrix/columns/heap_column.h +1140 -0
  73. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_list_column.h +934 -0
  74. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_set_column.h +934 -0
  75. multipers/gudhi/gudhi/Persistence_matrix/columns/list_column.h +980 -0
  76. multipers/gudhi/gudhi/Persistence_matrix/columns/naive_vector_column.h +1092 -0
  77. multipers/gudhi/gudhi/Persistence_matrix/columns/row_access.h +192 -0
  78. multipers/gudhi/gudhi/Persistence_matrix/columns/set_column.h +921 -0
  79. multipers/gudhi/gudhi/Persistence_matrix/columns/small_vector_column.h +1093 -0
  80. multipers/gudhi/gudhi/Persistence_matrix/columns/unordered_set_column.h +1012 -0
  81. multipers/gudhi/gudhi/Persistence_matrix/columns/vector_column.h +1244 -0
  82. multipers/gudhi/gudhi/Persistence_matrix/matrix_dimension_holders.h +186 -0
  83. multipers/gudhi/gudhi/Persistence_matrix/matrix_row_access.h +164 -0
  84. multipers/gudhi/gudhi/Persistence_matrix/ru_pairing.h +156 -0
  85. multipers/gudhi/gudhi/Persistence_matrix/ru_rep_cycles.h +376 -0
  86. multipers/gudhi/gudhi/Persistence_matrix/ru_vine_swap.h +540 -0
  87. multipers/gudhi/gudhi/Persistent_cohomology/Field_Zp.h +118 -0
  88. multipers/gudhi/gudhi/Persistent_cohomology/Multi_field.h +173 -0
  89. multipers/gudhi/gudhi/Persistent_cohomology/Persistent_cohomology_column.h +128 -0
  90. multipers/gudhi/gudhi/Persistent_cohomology.h +745 -0
  91. multipers/gudhi/gudhi/Points_off_io.h +171 -0
  92. multipers/gudhi/gudhi/Simple_object_pool.h +69 -0
  93. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_iterators.h +463 -0
  94. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_node_explicit_storage.h +83 -0
  95. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_siblings.h +106 -0
  96. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_star_simplex_iterators.h +277 -0
  97. multipers/gudhi/gudhi/Simplex_tree/hooks_simplex_base.h +62 -0
  98. multipers/gudhi/gudhi/Simplex_tree/indexing_tag.h +27 -0
  99. multipers/gudhi/gudhi/Simplex_tree/serialization_utils.h +62 -0
  100. multipers/gudhi/gudhi/Simplex_tree/simplex_tree_options.h +157 -0
  101. multipers/gudhi/gudhi/Simplex_tree.h +2794 -0
  102. multipers/gudhi/gudhi/Simplex_tree_multi.h +152 -0
  103. multipers/gudhi/gudhi/distance_functions.h +62 -0
  104. multipers/gudhi/gudhi/graph_simplicial_complex.h +104 -0
  105. multipers/gudhi/gudhi/persistence_interval.h +253 -0
  106. multipers/gudhi/gudhi/persistence_matrix_options.h +170 -0
  107. multipers/gudhi/gudhi/reader_utils.h +367 -0
  108. multipers/gudhi/mma_interface_coh.h +256 -0
  109. multipers/gudhi/mma_interface_h0.h +223 -0
  110. multipers/gudhi/mma_interface_matrix.h +293 -0
  111. multipers/gudhi/naive_merge_tree.h +536 -0
  112. multipers/gudhi/scc_io.h +310 -0
  113. multipers/gudhi/truc.h +1403 -0
  114. multipers/io.cpython-311-x86_64-linux-gnu.so +0 -0
  115. multipers/io.pyx +644 -0
  116. multipers/ml/__init__.py +0 -0
  117. multipers/ml/accuracies.py +90 -0
  118. multipers/ml/invariants_with_persistable.py +79 -0
  119. multipers/ml/kernels.py +176 -0
  120. multipers/ml/mma.py +713 -0
  121. multipers/ml/one.py +472 -0
  122. multipers/ml/point_clouds.py +352 -0
  123. multipers/ml/signed_measures.py +1589 -0
  124. multipers/ml/sliced_wasserstein.py +461 -0
  125. multipers/ml/tools.py +113 -0
  126. multipers/mma_structures.cpython-311-x86_64-linux-gnu.so +0 -0
  127. multipers/mma_structures.pxd +128 -0
  128. multipers/mma_structures.pyx +2786 -0
  129. multipers/mma_structures.pyx.tp +1094 -0
  130. multipers/multi_parameter_rank_invariant/diff_helpers.h +84 -0
  131. multipers/multi_parameter_rank_invariant/euler_characteristic.h +97 -0
  132. multipers/multi_parameter_rank_invariant/function_rips.h +322 -0
  133. multipers/multi_parameter_rank_invariant/hilbert_function.h +769 -0
  134. multipers/multi_parameter_rank_invariant/persistence_slices.h +148 -0
  135. multipers/multi_parameter_rank_invariant/rank_invariant.h +369 -0
  136. multipers/multiparameter_edge_collapse.py +41 -0
  137. multipers/multiparameter_module_approximation/approximation.h +2330 -0
  138. multipers/multiparameter_module_approximation/combinatory.h +129 -0
  139. multipers/multiparameter_module_approximation/debug.h +107 -0
  140. multipers/multiparameter_module_approximation/euler_curves.h +0 -0
  141. multipers/multiparameter_module_approximation/format_python-cpp.h +286 -0
  142. multipers/multiparameter_module_approximation/heap_column.h +238 -0
  143. multipers/multiparameter_module_approximation/images.h +79 -0
  144. multipers/multiparameter_module_approximation/list_column.h +174 -0
  145. multipers/multiparameter_module_approximation/list_column_2.h +232 -0
  146. multipers/multiparameter_module_approximation/ru_matrix.h +347 -0
  147. multipers/multiparameter_module_approximation/set_column.h +135 -0
  148. multipers/multiparameter_module_approximation/structure_higher_dim_barcode.h +36 -0
  149. multipers/multiparameter_module_approximation/unordered_set_column.h +166 -0
  150. multipers/multiparameter_module_approximation/utilities.h +403 -0
  151. multipers/multiparameter_module_approximation/vector_column.h +223 -0
  152. multipers/multiparameter_module_approximation/vector_matrix.h +331 -0
  153. multipers/multiparameter_module_approximation/vineyards.h +464 -0
  154. multipers/multiparameter_module_approximation/vineyards_trajectories.h +649 -0
  155. multipers/multiparameter_module_approximation.cpython-311-x86_64-linux-gnu.so +0 -0
  156. multipers/multiparameter_module_approximation.pyx +235 -0
  157. multipers/pickle.py +90 -0
  158. multipers/plots.py +456 -0
  159. multipers/point_measure.cpython-311-x86_64-linux-gnu.so +0 -0
  160. multipers/point_measure.pyx +395 -0
  161. multipers/simplex_tree_multi.cpython-311-x86_64-linux-gnu.so +0 -0
  162. multipers/simplex_tree_multi.pxd +134 -0
  163. multipers/simplex_tree_multi.pyx +10840 -0
  164. multipers/simplex_tree_multi.pyx.tp +2009 -0
  165. multipers/slicer.cpython-311-x86_64-linux-gnu.so +0 -0
  166. multipers/slicer.pxd +3034 -0
  167. multipers/slicer.pxd.tp +234 -0
  168. multipers/slicer.pyx +20481 -0
  169. multipers/slicer.pyx.tp +1088 -0
  170. multipers/tensor/tensor.h +672 -0
  171. multipers/tensor.pxd +13 -0
  172. multipers/test.pyx +44 -0
  173. multipers/tests/__init__.py +62 -0
  174. multipers/torch/__init__.py +1 -0
  175. multipers/torch/diff_grids.py +240 -0
  176. multipers/torch/rips_density.py +310 -0
  177. multipers-2.3.3b6.dist-info/METADATA +128 -0
  178. multipers-2.3.3b6.dist-info/RECORD +182 -0
  179. multipers-2.3.3b6.dist-info/WHEEL +5 -0
  180. multipers-2.3.3b6.dist-info/licenses/LICENSE +21 -0
  181. multipers-2.3.3b6.dist-info/top_level.txt +1 -0
  182. multipers.libs/libtbb-ca48af5c.so.12.16 +0 -0
multipers/gudhi/truc.h ADDED
@@ -0,0 +1,1403 @@
1
+ #pragma once
2
+ #include "gudhi/Matrix.h"
3
+ #include "gudhi/mma_interface_matrix.h"
4
+ #include "gudhi/Multi_persistence/Line.h"
5
+ #include "multiparameter_module_approximation/format_python-cpp.h"
6
+ #include <gudhi/One_critical_filtration.h>
7
+ #include <gudhi/Multi_critical_filtration.h>
8
+ #include <algorithm>
9
+ #include <cassert>
10
+ #include <csignal>
11
+ #include <cstddef>
12
+ #include <cstdint>
13
+ #include <iostream>
14
+ #include <limits>
15
+ #include <numeric>
16
+ #include <oneapi/tbb/enumerable_thread_specific.h>
17
+ #include <oneapi/tbb/parallel_for.h>
18
+ #include <oneapi/tbb/parallel_sort.h>
19
+ #include <oneapi/tbb/task_group.h>
20
+ #include <oneapi/tbb/mutex.h>
21
+ #include <ostream>
22
+ #include <ranges>
23
+ #include <sstream>
24
+ #include <stdexcept>
25
+ #include <string>
26
+ #include <utility>
27
+ #include <vector>
28
+ #include <type_traits> //std::invoke_result
29
+ #include "scc_io.h"
30
+
31
+ namespace Gudhi {
32
+ namespace multiparameter {
33
+ namespace truc_interface {
34
+ using index_type = std::uint32_t;
35
+
36
+ template <typename T, typename = void>
37
+ struct has_columns : std::false_type {};
38
+
39
+ template <typename T>
40
+ struct has_columns<T, std::void_t<typename T::options>> : std::true_type {};
41
+
42
+ class PresentationStructure {
43
+ public:
44
+ PresentationStructure() {}
45
+
46
+ /* SimplicialStructure &operator=(const SimplicialStructure &) = default; */
47
+
48
+ PresentationStructure(const std::vector<std::vector<index_type>> &generators,
49
+ const std::vector<int> &generator_dimensions)
50
+ : generators(generators), generator_dimensions(generator_dimensions), num_vertices_(0) {
51
+ for (const auto &stuff : generator_dimensions) {
52
+ if (stuff == 0) num_vertices_++;
53
+ }
54
+ max_dimension_ = generator_dimensions.size() > 0
55
+ ? *std::max_element(generator_dimensions.begin(), generator_dimensions.end())
56
+ : 0;
57
+ };
58
+
59
+ PresentationStructure(const PresentationStructure &other)
60
+ : generators(other.generators),
61
+ generator_dimensions(other.generator_dimensions),
62
+ num_vertices_(other.num_vertices_),
63
+ max_dimension_(other.max_dimension_) {}
64
+
65
+ /* PresentationStructure &operator=(const PresentationStructure &other) { */
66
+ /* generators = other.generators; */
67
+ /* generator_dimensions = other.generator_dimensions; */
68
+ /* num_vertices_ = other.num_vertices_; */
69
+ /* max_dimension_ = other.max_dimension_; */
70
+ /**/
71
+ /* return *this; */
72
+ /* } */
73
+
74
+ const std::vector<unsigned int> &operator[](std::size_t i) const {
75
+ return generators[i];
76
+ } // needs to be iterable (begin, end, size)
77
+
78
+ std::vector<unsigned int> &operator[](std::size_t i) {
79
+ return generators[i];
80
+ } // needs to be iterable (begin, end, size)
81
+
82
+ inline int dimension(std::size_t i) const { return generator_dimensions[i]; };
83
+
84
+ inline friend std::ostream &operator<<(std::ostream &stream, const PresentationStructure &structure) {
85
+ stream << "Boundary:\n";
86
+ stream << "{\n";
87
+ for (auto i : std::views::iota(0u, structure.size())) {
88
+ const auto &stuff = structure.generators[i];
89
+ stream << i << ": {";
90
+ for (auto truc : stuff) stream << truc << ", ";
91
+
92
+ if (!stuff.empty()) stream << "\b" << "\b ";
93
+
94
+ stream << "},\n";
95
+ }
96
+ stream << "}\n";
97
+ stream << "Degrees: (max " << structure.max_dimension() << ")\n";
98
+ stream << "{";
99
+ for (const auto &stuff : structure.generator_dimensions) stream << stuff << ", ";
100
+ if (structure.size() > 0) {
101
+ stream << "\b" << "\b";
102
+ }
103
+ stream << "}\n";
104
+ return stream;
105
+ }
106
+
107
+ inline void to_stream(std::ostream &stream, const std::vector<index_type> &order) {
108
+ for (const auto &i : order) {
109
+ const auto &stuff = this->operator[](i);
110
+ stream << i << " : [";
111
+ for (const auto &truc : stuff) stream << truc << ", ";
112
+ stream << "]\n";
113
+ }
114
+ /* return stream; */
115
+ }
116
+
117
+ inline std::size_t size() const { return generators.size(); };
118
+
119
+ unsigned int num_vertices() const { return num_vertices_; };
120
+
121
+ unsigned int max_dimension() const { return max_dimension_; }
122
+
123
+ int prune_above_dimension(int dim) {
124
+ int idx = std::lower_bound(generator_dimensions.begin(), generator_dimensions.end(), dim + 1) -
125
+ generator_dimensions.begin();
126
+ generators.resize(idx);
127
+ generator_dimensions.resize(idx);
128
+ max_dimension_ = generator_dimensions.size() ? generator_dimensions.back() : -1;
129
+ return idx;
130
+ }
131
+
132
+ PresentationStructure permute(const std::vector<index_type> &order) const {
133
+ if (order.size() > generators.size()) {
134
+ throw std::invalid_argument("Permutation order must have the same size as the number of generators.");
135
+ }
136
+ index_type flag = -1;
137
+ std::vector<index_type> inverse_order(generators.size(), flag);
138
+ for (std::size_t i = 0; i < order.size(); i++) {
139
+ inverse_order[order[i]] = i;
140
+ }
141
+ std::vector<std::vector<index_type>> new_generators(order.size());
142
+ std::vector<int> new_generator_dimensions(order.size());
143
+
144
+ for (auto i : std::views::iota(0u, order.size())) {
145
+ new_generators[i].reserve(generators[order[i]].size());
146
+ for (std::size_t j = 0; j < generators[order[i]].size(); j++) {
147
+ index_type stuff = inverse_order[generators[order[i]][j]];
148
+ if (stuff != flag) new_generators[i].push_back(stuff);
149
+ }
150
+ std::sort(new_generators[i].begin(), new_generators[i].end());
151
+ new_generator_dimensions[i] = generator_dimensions[order[i]];
152
+ }
153
+ return PresentationStructure(new_generators, new_generator_dimensions);
154
+ }
155
+
156
+ void update_matrix(std::vector<std::vector<index_type>> &new_gens) { std::swap(generators, new_gens); }
157
+
158
+ private:
159
+ std::vector<std::vector<index_type>> generators;
160
+ std::vector<int> generator_dimensions;
161
+ unsigned int num_vertices_;
162
+ int max_dimension_ = -1;
163
+ };
164
+
165
+ class SimplicialStructure {
166
+ public:
167
+ template <typename SimplexTree>
168
+ void from_simplextree(SimplexTree &st) {
169
+ auto [filtration, boundary] = Gudhi::multiparameter::mma::simplextree_to_ordered_bf(st);
170
+ this->boundaries = boundary;
171
+ this->num_vertices_ = st.num_vertices();
172
+ this->max_dimension_ = st.dimension();
173
+ }
174
+
175
+ SimplicialStructure() {}
176
+
177
+ /* SimplicialStructure &operator=(const SimplicialStructure &) = default; */
178
+
179
+ SimplicialStructure(const std::vector<std::vector<index_type>> &boundaries,
180
+ unsigned int num_vertices,
181
+ unsigned int max_dimension)
182
+ : boundaries(boundaries), num_vertices_(num_vertices), max_dimension_(max_dimension) {
183
+
184
+ };
185
+
186
+ const std::vector<unsigned int> &operator[](std::size_t i) const {
187
+ return boundaries[i];
188
+ } // needs to be iterable (begin, end, size)
189
+
190
+ std::vector<unsigned int> &operator[](std::size_t i) {
191
+ return boundaries[i];
192
+ } // needs to be iterable (begin, end, size)
193
+
194
+ int dimension(std::size_t i) const { return boundaries[i].size() == 0 ? 0 : boundaries[i].size() - 1; };
195
+
196
+ inline friend std::ostream &operator<<(std::ostream &stream, const SimplicialStructure &structure) {
197
+ stream << "{";
198
+ for (const auto &stuff : structure.boundaries) {
199
+ stream << "{";
200
+ for (auto truc : stuff) stream << truc << ", ";
201
+
202
+ if (!stuff.empty()) stream << "\b" << "\b ";
203
+
204
+ stream << "},\n";
205
+ }
206
+ stream << "}\n";
207
+ return stream;
208
+ }
209
+
210
+ inline void to_stream(std::ostream &stream, const std::vector<index_type> &order) {
211
+ for (const auto &i : order) {
212
+ const auto &stuff = this->operator[](i);
213
+ stream << i << " : [";
214
+ for (const auto &truc : stuff) stream << truc << ", ";
215
+ stream << "]\n";
216
+ }
217
+ /* return stream; */
218
+ }
219
+
220
+ inline std::size_t size() const { return boundaries.size(); };
221
+
222
+ inline unsigned int num_vertices() const { return num_vertices_; }
223
+
224
+ inline unsigned int max_dimension() const { return max_dimension_; }
225
+
226
+ int prune_above_dimension([[maybe_unused]] int dim) { throw "Not implemented"; }
227
+
228
+ private:
229
+ std::vector<std::vector<index_type>> boundaries;
230
+ unsigned int num_vertices_;
231
+ unsigned int max_dimension_;
232
+ };
233
+
234
+ template <class PersBackend, class Structure, class MultiFiltration>
235
+ class Truc {
236
+ public:
237
+ using Filtration_value = MultiFiltration;
238
+ using MultiFiltrations = std::vector<MultiFiltration>;
239
+ using value_type = typename MultiFiltration::value_type;
240
+ using split_barcode =
241
+ std::vector<std::vector<std::pair<typename MultiFiltration::value_type, typename MultiFiltration::value_type>>>;
242
+ using split_barcode_idx = std::vector<std::vector<std::pair<int, int>>>;
243
+ template <typename value_type = value_type>
244
+ using flat_barcode = std::vector<std::pair<int, std::pair<value_type, value_type>>>;
245
+
246
+ template <typename value_type = value_type>
247
+ using flat_nodim_barcode = std::vector<std::pair<value_type, value_type>>;
248
+
249
+ // CONSTRUCTORS.
250
+ // - Need everything of the same size, generator order is a PERMUTATION
251
+ //
252
+ Truc(const Structure &structure, const std::vector<MultiFiltration> &generator_filtration_values)
253
+ : generator_filtration_values(generator_filtration_values),
254
+ generator_order(structure.size()),
255
+ structure(structure),
256
+ filtration_container(structure.size()) {
257
+ std::iota(generator_order.begin(), generator_order.end(), 0); // range
258
+ };
259
+
260
+ template <class SimplexTree>
261
+ Truc(SimplexTree *simplextree) {
262
+ auto [filtration, boundary] = mma::simplextree_to_ordered_bf(*simplextree);
263
+ structure = SimplicialStructure(boundary, (*simplextree).num_vertices(), (*simplextree).dimension());
264
+ generator_filtration_values.resize(filtration.size());
265
+ for (auto i = 0u; i < filtration.size(); i++)
266
+ generator_filtration_values[i] = filtration[i]; // there is a copy here. TODO : deal with it.
267
+ generator_order = std::vector<index_type>(structure.size());
268
+ std::iota(generator_order.begin(), generator_order.end(), 0); // range
269
+ filtration_container.resize(structure.size());
270
+ }
271
+
272
+ Truc(const std::vector<std::vector<index_type>> &generator_maps,
273
+ const std::vector<int> &generator_dimensions,
274
+ const std::vector<MultiFiltration> &generator_filtrations)
275
+ : generator_filtration_values(generator_filtrations),
276
+ generator_order(generator_filtrations.size(), 0),
277
+ structure(PresentationStructure(generator_maps, generator_dimensions)),
278
+ filtration_container(generator_filtrations.size()) {
279
+ std::iota(generator_order.begin(), generator_order.end(), 0); // range
280
+ }
281
+
282
+ Truc(const Truc &other)
283
+ : generator_filtration_values(other.generator_filtration_values),
284
+ generator_order(other.generator_order),
285
+ structure(other.structure),
286
+ filtration_container(other.filtration_container),
287
+ persistence(other.persistence) {
288
+ persistence._update_permutation_ptr(generator_order);
289
+ }
290
+
291
+ Truc &operator=(Truc other) {
292
+ if (this != &other) {
293
+ generator_filtration_values = other.generator_filtration_values;
294
+ generator_order = other.generator_order;
295
+ structure = other.structure;
296
+ filtration_container = other.filtration_container;
297
+ persistence = other.persistence;
298
+ persistence._update_permutation_ptr(generator_order);
299
+ }
300
+ return *this;
301
+ }
302
+
303
+ Truc() {};
304
+
305
+ inline bool dimension_order(const index_type &i, const index_type &j) const {
306
+ return structure.dimension(i) < structure.dimension(j);
307
+ };
308
+
309
+ inline bool colexical_order(const index_type &i, const index_type &j) const {
310
+ if (structure.dimension(i) > structure.dimension(j)) return false;
311
+ if (structure.dimension(i) < structure.dimension(j)) return true;
312
+ if constexpr (MultiFiltration::is_multicritical()) // TODO : this may not be the best
313
+ throw "Not implemented in the multicritical case";
314
+
315
+ for (int idx = generator_filtration_values[i].num_parameters() - 1; idx >= 0; --idx) {
316
+ if (generator_filtration_values[i][idx] < generator_filtration_values[j][idx])
317
+ return true;
318
+ else if (generator_filtration_values[i][idx] > generator_filtration_values[j][idx])
319
+ return false;
320
+ }
321
+ return false;
322
+ };
323
+
324
+ // TODO : inside of MultiFiltration
325
+ inline static bool lexical_order(const MultiFiltration &a, const MultiFiltration &b) {
326
+ if constexpr (MultiFiltration::is_multicritical()) // TODO : this may not be the best
327
+ throw "Not implemented in the multicritical case";
328
+ if (a.is_plus_inf() || a.is_nan() || b.is_minus_inf()) return false;
329
+ if (b.is_plus_inf() || b.is_nan() || a.is_minus_inf()) return true;
330
+ for (auto idx = 0u; idx < a.num_parameters(); ++idx) {
331
+ if (a[idx] < b[idx])
332
+ return true;
333
+ else if (a[idx] > b[idx])
334
+ return false;
335
+ }
336
+ return false;
337
+ };
338
+
339
+ inline bool lexical_order(const index_type &i, const index_type &j) const {
340
+ if (structure.dimension(i) > structure.dimension(j)) return false;
341
+ if (structure.dimension(i) < structure.dimension(j)) return true;
342
+ if constexpr (MultiFiltration::is_multicritical()) // TODO : this may not be the best
343
+ throw "Not implemented in the multicritical case";
344
+
345
+ for (int idx = 0; idx < generator_filtration_values[i].num_parameters(); ++idx) {
346
+ if (generator_filtration_values[i][idx] < generator_filtration_values[j][idx])
347
+ return true;
348
+ else if (generator_filtration_values[i][idx] > generator_filtration_values[j][idx])
349
+ return false;
350
+ }
351
+ return false;
352
+ };
353
+
354
+ inline Truc permute(const std::vector<index_type> &permutation) const {
355
+ auto num_new_gen = permutation.size();
356
+ if (permutation.size() > this->num_generators()) {
357
+ throw std::invalid_argument("Invalid permutation size. Got " + std::to_string(num_new_gen) + " expected " +
358
+ std::to_string(this->num_generators()) + ".");
359
+ }
360
+ std::vector<MultiFiltration> new_filtration(num_new_gen);
361
+ for (auto i : std::views::iota(0u, num_new_gen)) { // assumes permutation has correct indices.
362
+ new_filtration[i] = generator_filtration_values[permutation[i]];
363
+ }
364
+ return Truc(structure.permute(permutation), new_filtration);
365
+ }
366
+
367
+ template <class Fun>
368
+ inline std::pair<Truc, std::vector<index_type>> rearange_sort(const Fun &&fun) const {
369
+ std::vector<index_type> permutation(generator_order.size());
370
+ std::iota(permutation.begin(), permutation.end(), 0);
371
+ tbb::parallel_sort(permutation.begin(), permutation.end(), [&](std::size_t i, std::size_t j) { return fun(i, j); });
372
+ return {permute(permutation), permutation};
373
+ }
374
+
375
+ std::pair<Truc, std::vector<index_type>> colexical_rearange() const {
376
+ return rearange_sort([this](std::size_t i, std::size_t j) { return this->colexical_order(i, j); });
377
+ }
378
+
379
+ template <bool generator_only = false>
380
+ std::conditional_t<generator_only, std::pair<std::vector<std::vector<index_type>>, MultiFiltrations>, Truc>
381
+ projective_cover_kernel(int dim) {
382
+ if constexpr (MultiFiltration::is_multicritical() || !std::is_same_v<Structure, PresentationStructure> ||
383
+ !has_columns<PersBackend>::value) // TODO : this may not be the best
384
+ {
385
+ throw std::invalid_argument("Not implemented for this Truc");
386
+ } else {
387
+ // TODO : this only works for 2 parameter modules. Optimize w.r.t. this.
388
+ const bool verbose = false;
389
+ // filtration values are assumed to be dim + colexicographically sorted
390
+ // vector seem to be good here
391
+ using SmallMatrix = Gudhi::persistence_matrix::Matrix<
392
+ Gudhi::multiparameter::truc_interface::fix_presentation_options<PersBackend::options::column_type, false>>;
393
+
394
+ int nd = 0;
395
+ int ndpp = 0;
396
+ for (auto i : std::views::iota(0u, structure.size())) {
397
+ if (structure.dimension(i) == dim) {
398
+ nd++;
399
+ } else if (structure.dimension(i) == dim + 1) {
400
+ ndpp++;
401
+ } else {
402
+ throw std::invalid_argument("This truc contains bad dimensions. Got " +
403
+ std::to_string(structure.dimension(i)) + " expected " + std::to_string(dim) +
404
+ " or " + std::to_string(dim + 1) + " in position " + std::to_string(i) + " .");
405
+ }
406
+ }
407
+ if (ndpp == 0)
408
+ throw std::invalid_argument("Given dimension+1 has no simplices. Got " + std::to_string(nd) + " " +
409
+ std::to_string(ndpp) + ".");
410
+ // lexico iterator
411
+ auto lex_cmp = [&](const MultiFiltration &a, const MultiFiltration &b) { return lexical_order(a, b); };
412
+
413
+ struct SmallQueue {
414
+ SmallQueue() {};
415
+
416
+ struct MFWrapper {
417
+ MFWrapper(const MultiFiltration &g) : g(g) {};
418
+
419
+ MFWrapper(const MultiFiltration &g, int col) : g(g) { some_cols.insert(col); }
420
+
421
+ MFWrapper(const MultiFiltration &g, std::initializer_list<int> cols)
422
+ : g(g), some_cols(cols.begin(), cols.end()) {}
423
+
424
+ inline void insert(int col) const { some_cols.insert(col); }
425
+
426
+ inline bool operator<(const MFWrapper &other) const { return lexical_order(g, other.g); }
427
+
428
+ public:
429
+ MultiFiltration g;
430
+ mutable std::set<int> some_cols;
431
+ };
432
+
433
+ inline void insert(const MultiFiltration &g, int col) {
434
+ auto it = queue.find(g);
435
+ if (it != queue.end()) {
436
+ it->insert(col);
437
+ } else {
438
+ queue.emplace(g, col);
439
+ }
440
+ };
441
+
442
+ inline void insert(const MultiFiltration &g, const std::initializer_list<int> &cols) {
443
+ auto it = queue.find(g);
444
+ if (it != queue.end()) {
445
+ for (int c : cols) it->insert(c);
446
+ } else {
447
+ queue.emplace(g, cols);
448
+ }
449
+ };
450
+
451
+ inline bool empty() const { return queue.empty(); }
452
+
453
+ inline MultiFiltration pop() {
454
+ if (queue.empty()) [[unlikely]]
455
+ throw std::runtime_error("Queue is empty");
456
+
457
+ auto out = std::move(*queue.begin());
458
+ queue.erase(queue.begin());
459
+ std::swap(last_cols, out.some_cols);
460
+ return out.g;
461
+ }
462
+
463
+ const auto &get_current_cols() const { return last_cols; }
464
+
465
+ private:
466
+ std::set<MFWrapper> queue;
467
+ std::set<int> last_cols;
468
+ };
469
+
470
+ SmallQueue lexico_it;
471
+ SmallMatrix M(nd + ndpp);
472
+ for (int i = 0; i < nd + ndpp; i++) {
473
+ const auto &b = structure[i];
474
+ M.insert_boundary(b);
475
+ }
476
+ SmallMatrix N(nd + ndpp); // slave
477
+ for (auto i : std::views::iota(0u, static_cast<unsigned int>(nd + ndpp))) {
478
+ N.insert_boundary({i});
479
+ };
480
+
481
+ auto get_fil = [&](int i) -> MultiFiltration & { return generator_filtration_values[i]; };
482
+ auto get_pivot = [&](int j) -> int {
483
+ const auto &col = M.get_column(j);
484
+ return col.size() > 0 ? (*col.rbegin()).get_row_index() : -1;
485
+ };
486
+
487
+ if constexpr (verbose) {
488
+ std::cout << "Initial matrix (" << nd << " + " << ndpp << "):" << std::endl;
489
+ for (int i = 0; i < nd + ndpp; i++) {
490
+ std::cout << "Column " << i << " : {";
491
+ for (const auto &j : M.get_column(i)) std::cout << j << " ";
492
+ std::cout << "} | " << get_fil(i) << std::endl;
493
+ }
494
+ }
495
+
496
+ // TODO : pivot caches are small : maybe use a flat container instead ?
497
+ std::vector<std::set<int>> pivot_cache(nd + ndpp); // this[pivot] = cols of given pivot (<=nd)
498
+ std::vector<bool> reduced_columns(nd + ndpp); // small cache
499
+ MultiFiltration grid_value;
500
+
501
+ std::vector<std::vector<index_type>> out_structure;
502
+ out_structure.reserve(2 * ndpp);
503
+ std::vector<MultiFiltration> out_filtration;
504
+ out_filtration.reserve(2 * ndpp);
505
+ std::vector<int> out_dimension;
506
+ out_dimension.reserve(2 * ndpp);
507
+ if constexpr (!generator_only) {
508
+ for (auto i : std::views::iota(nd, nd + ndpp)) {
509
+ out_structure.push_back({});
510
+ out_filtration.push_back(this->get_filtration_values()[i]);
511
+ out_dimension.push_back(this->structure.dimension(i));
512
+ }
513
+ }
514
+ // pivot cache
515
+ if constexpr (verbose) {
516
+ std::cout << "Initial pivot cache:\n";
517
+ }
518
+ for (int j : std::views::iota(nd, nd + ndpp)) {
519
+ int col_pivot = get_pivot(j);
520
+ if (col_pivot < 0) {
521
+ reduced_columns[j] = true;
522
+ continue;
523
+ };
524
+ auto &current_pivot_cache = pivot_cache[col_pivot];
525
+ current_pivot_cache.emplace_hint(current_pivot_cache.cend(), j); // j is increasing
526
+ }
527
+ if constexpr (verbose) {
528
+ int i = 0;
529
+ for (const auto &cols : pivot_cache) {
530
+ std::cout << " - " << i++ << " : ";
531
+ for (const auto &col : cols) {
532
+ std::cout << col << " ";
533
+ }
534
+ std::cout << std::endl;
535
+ }
536
+ }
537
+
538
+ // if constexpr (!use_grid) {
539
+ if constexpr (verbose) std::cout << "Initial grid queue:\n";
540
+ for (int j : std::views::iota(nd, nd + ndpp)) {
541
+ int col_pivot = get_pivot(j);
542
+ if (col_pivot < 0) continue;
543
+ lexico_it.insert(get_fil(j), j);
544
+ auto it = pivot_cache[col_pivot].find(j);
545
+ if (it == pivot_cache[col_pivot].end()) [[unlikely]]
546
+ throw std::runtime_error("Column " + std::to_string(j) + " not in pivot cache");
547
+ it++;
548
+ // for (int k : pivot_cache[col_pivot]) {
549
+ for (auto _k = it; _k != pivot_cache[col_pivot].end(); ++_k) {
550
+ int k = *_k;
551
+ if (k <= j) [[unlikely]]
552
+ throw std::runtime_error("Column " + std::to_string(k) + " is not a future column");
553
+ auto prev = get_fil(k);
554
+ prev.push_to_least_common_upper_bound(get_fil(j));
555
+ if constexpr (verbose) std::cout << " - (" << j << ", " << k << ") are interacting at " << prev << "\n";
556
+ lexico_it.insert(std::move(prev), k);
557
+ }
558
+ }
559
+ // TODO : check poset cache ?
560
+ if constexpr (verbose) std::cout << std::flush;
561
+ // }
562
+ auto reduce_column = [&](int j) -> bool {
563
+ int pivot = get_pivot(j);
564
+ if constexpr (verbose) std::cout << "Reducing column " << j << " with pivot " << pivot << "\n";
565
+ if (pivot < 0) {
566
+ if (!reduced_columns[j]) {
567
+ std::vector<index_type> _b(N.get_column(j).begin(), N.get_column(j).end());
568
+ for (auto &stuff : _b) stuff -= nd;
569
+ out_structure.push_back(std::move(_b));
570
+ out_filtration.emplace_back(grid_value.begin(), grid_value.end());
571
+ if constexpr (!generator_only) out_dimension.emplace_back(this->structure.dimension(j) + 1);
572
+ reduced_columns[j] = true;
573
+ }
574
+ return false;
575
+ }
576
+ if constexpr (verbose) std::cout << "Previous registered pivot : " << *pivot_cache[pivot].begin() << std::endl;
577
+ // WARN : we lazy update variables linked with col j...
578
+ if (pivot_cache[pivot].size() == 0) {
579
+ return false;
580
+ }
581
+ for (int k : pivot_cache[pivot]) {
582
+ if (k >= j) { // cannot reduce more here. this is a (local) pivot.
583
+ return false;
584
+ }
585
+ if (get_fil(k) <= grid_value) {
586
+ M.add_to(k, j);
587
+ N.add_to(k, j);
588
+ // std::cout << "Adding " << k << " to " << j << " at grid time " << grid_value << std::endl;
589
+ pivot_cache[pivot].erase(j);
590
+ // WARN : we update the pivot cache after the update loop
591
+ if (get_pivot(j) >= pivot) {
592
+ throw std::runtime_error("Addition failed ? current " + std::to_string(get_pivot(j)) + " previous " +
593
+ std::to_string(pivot));
594
+ }
595
+ return true; // pivot has changed
596
+ }
597
+ }
598
+ return false; // for loop exhausted (j may not be there because of lazy)
599
+ };
600
+ auto chores_after_new_pivot = [&](int j) {
601
+ int col_pivot = get_pivot(j);
602
+ if (col_pivot < 0) {
603
+ if (!reduced_columns[j]) throw std::runtime_error("Empty column should have been detected before");
604
+ return;
605
+ };
606
+ auto [it, was_there] = pivot_cache[col_pivot].insert(j);
607
+ it++;
608
+ // if constexpr (!use_grid) {
609
+ for (auto _k = it; _k != pivot_cache[col_pivot].end(); ++_k) {
610
+ int k = *_k;
611
+ if (k <= j) [[unlikely]]
612
+ throw std::runtime_error("(chores) col " + std::to_string(k) + " is not a future column");
613
+ if (get_fil(k) >= get_fil(j)) continue;
614
+ auto prev = get_fil(k);
615
+ prev.push_to_least_common_upper_bound(get_fil(j));
616
+ if (lex_cmp(grid_value, prev)) {
617
+ if constexpr (verbose)
618
+ std::cout << "(chores) Updating grid queue, (" << j << ", " << k << ") are interacting at " << prev
619
+ << std::endl;
620
+ lexico_it.insert(prev, k);
621
+ }
622
+ }
623
+ // }
624
+ };
625
+ if constexpr (verbose) {
626
+ std::cout << "Initially reduced columns: [";
627
+ for (int i = 0; i < nd + ndpp; i++) {
628
+ std::cout << reduced_columns[i] << ", ";
629
+ }
630
+ std::cout << "]" << std::endl;
631
+ }
632
+ while (!lexico_it.empty()) {
633
+ // if constexpr (use_grid) {
634
+ // grid_value = lexico_it.next();
635
+ // } else {
636
+ grid_value = std::move(lexico_it.pop());
637
+ // }
638
+ if constexpr (verbose) {
639
+ std::cout << "Grid value: " << grid_value << std::endl;
640
+ std::cout << " Reduced cols: ";
641
+ for (int i = 0; i < nd + ndpp; i++) {
642
+ std::cout << reduced_columns[i] << ", ";
643
+ }
644
+ std::cout << "]" << std::endl;
645
+ }
646
+
647
+ for (int i : lexico_it.get_current_cols()) {
648
+ if constexpr (false) {
649
+ if ((reduced_columns[i] || !(get_fil(i) <= grid_value))) continue;
650
+ if ((get_fil(i) > grid_value)) break;
651
+ }
652
+ while (reduce_column(i));
653
+ chores_after_new_pivot(i);
654
+ }
655
+ }
656
+ // std::cout<< grid_.str() << std::endl;
657
+ if constexpr (generator_only)
658
+ return {out_structure, out_dimension};
659
+ else {
660
+ return Truc(out_structure, out_dimension, out_filtration);
661
+ }
662
+ }
663
+ }
664
+
665
+ template <bool ignore_inf>
666
+ std::vector<std::pair<int, std::vector<index_type>>> get_current_boundary_matrix() {
667
+ std::vector<index_type> permutation(generator_order.size());
668
+ std::iota(permutation.begin(), permutation.end(), 0);
669
+ if constexpr (ignore_inf) {
670
+ permutation.erase(std::remove_if(permutation.begin(),
671
+ permutation.end(),
672
+ [&](std::size_t val) {
673
+ return filtration_container[val] == MultiFiltration::Generator::T_inf;
674
+ }),
675
+ permutation.end());
676
+ tbb::parallel_sort(permutation.begin(), permutation.end());
677
+ }
678
+ tbb::parallel_sort(permutation.begin(), permutation.end(), [&](std::size_t i, std::size_t j) {
679
+ if (structure.dimension(i) > structure.dimension(j)) return false;
680
+ if (structure.dimension(i) < structure.dimension(j)) return true;
681
+ return filtration_container[i] < filtration_container[j];
682
+ });
683
+
684
+ std::vector<std::pair<int, std::vector<index_type>>> matrix(permutation.size());
685
+
686
+ std::vector<index_type> permutationInv(generator_order.size());
687
+ std::size_t newPos = 0;
688
+ for (std::size_t oldPos : permutation) {
689
+ permutationInv[oldPos] = newPos;
690
+ auto &boundary = matrix[newPos].second;
691
+ boundary.resize(structure[oldPos].size());
692
+ for (std::size_t j = 0; j < structure[oldPos].size(); ++j) {
693
+ boundary[j] = permutationInv[structure[oldPos][j]];
694
+ }
695
+ std::sort(boundary.begin(), boundary.end());
696
+ matrix[newPos].first = structure.dimension(oldPos);
697
+ ++newPos;
698
+ }
699
+
700
+ return matrix;
701
+ }
702
+
703
+ inline std::size_t num_generators() const { return structure.size(); }
704
+
705
+ inline std::size_t num_parameters() const {
706
+ return num_generators() == 0 ? 0 : this->generator_filtration_values[0].num_parameters();
707
+ }
708
+
709
+ inline const Structure &get_structure() const { return structure; }
710
+
711
+ template <class SubFiltration, bool original_order = true>
712
+ inline void push_to_out(const SubFiltration &f,
713
+ std::vector<typename MultiFiltration::value_type> &filtration_container,
714
+ const std::vector<index_type> &generator_order) const {
715
+ /* std::vector<NewFilrationType> out(this->num_generators()); */
716
+
717
+ // filtration_container.resize(
718
+ // this->num_generators()); // for some reasons it is necessary FIXME
719
+ for (std::size_t i = 0u; i < this->num_generators(); i++) {
720
+ if constexpr (original_order) {
721
+ filtration_container[i] = f.compute_forward_intersection(generator_filtration_values[i]);
722
+ } else {
723
+ filtration_container[i] = f.compute_forward_intersection(generator_filtration_values[generator_order[i]]);
724
+ }
725
+ }
726
+ }
727
+
728
+ template <class SubFiltration, bool original_order = true>
729
+ inline void push_to(const SubFiltration &f) {
730
+ this->push_to_out<SubFiltration, original_order>(f, this->filtration_container, this->generator_order);
731
+ }
732
+
733
+ template <class array1d>
734
+ inline void set_one_filtration(const array1d &truc) {
735
+ if (truc.size() != this->num_generators())
736
+ throw std::invalid_argument("(setting one filtration) Bad size. Got " + std::to_string(truc.size()) +
737
+ " expected " + std::to_string(this->num_generators()));
738
+ this->filtration_container = truc;
739
+ }
740
+
741
+ inline const std::vector<typename MultiFiltration::value_type> &get_one_filtration() const {
742
+ return this->filtration_container;
743
+ }
744
+
745
+ inline PersBackend compute_persistence_out(
746
+ const std::vector<typename MultiFiltration::value_type> &one_filtration,
747
+ std::vector<index_type> &out_gen_order,
748
+ const bool ignore_inf) const { // needed ftm as PersBackend only points there
749
+ constexpr const bool verbose = false;
750
+ if (one_filtration.size() != this->num_generators()) {
751
+ throw std::runtime_error("The one parameter filtration doesn't have a proper size.");
752
+ }
753
+ out_gen_order.resize(this->num_generators());
754
+ std::iota(out_gen_order.begin(),
755
+ out_gen_order.end(),
756
+ 0); // we have to reset here, even though we're already doing this
757
+ std::sort(out_gen_order.begin(), out_gen_order.end(), [&](index_type i, index_type j) {
758
+ if (structure.dimension(i) > structure.dimension(j)) return false;
759
+ if (structure.dimension(i) < structure.dimension(j)) return true;
760
+ return one_filtration[i] < one_filtration[j];
761
+ });
762
+ if (!PersBackend::is_vine && ignore_inf) {
763
+ if constexpr (verbose) {
764
+ std::cout << "Removing infinite simplices" << std::endl;
765
+ }
766
+ for (auto &i : out_gen_order)
767
+ if (one_filtration[i] == MultiFiltration::Generator::T_inf) {
768
+ // TODO : later
769
+ // int d = structure.dimension(i);
770
+ // d = d == 0 ? 1 : 0;
771
+ // if (degrees.size()>d || degrees[d] || degrees[d-1])
772
+ // continue;
773
+ i = std::numeric_limits<typename std::remove_reference_t<decltype(out_gen_order)>::value_type>::max();
774
+ }
775
+ }
776
+ if constexpr (false) {
777
+ std::cout << structure << std::endl;
778
+ std::cout << "[";
779
+ for (auto i : out_gen_order) {
780
+ std::cout << i << ", ";
781
+ }
782
+ std::cout << "]" << std::endl;
783
+ std::cout << "[";
784
+ for (auto i : one_filtration) {
785
+ std::cout << i << ",";
786
+ }
787
+ std::cout << "]" << std::endl;
788
+ }
789
+ return PersBackend(structure, out_gen_order);
790
+ }
791
+
792
+ inline bool has_persistence() const { return this->persistence.size(); };
793
+
794
+ inline void compute_persistence(const bool ignore_inf = true) {
795
+ this->persistence = this->compute_persistence_out(
796
+ // this->filtration_container, this->generator_order, degrees); // TODO
797
+ // : later
798
+ this->filtration_container,
799
+ this->generator_order,
800
+ ignore_inf);
801
+ };
802
+
803
+ // TODO : static ?
804
+ inline void vineyard_update(PersBackend &persistence,
805
+ const std::vector<typename MultiFiltration::value_type> &one_filtration,
806
+ std::vector<index_type> &generator_order) const {
807
+ constexpr const bool verbose = false;
808
+ /* static_assert(PersBackend::has_vine_update); */
809
+ // the first false is to get the generator order
810
+ // insertion sort
811
+ auto n = this->num_generators();
812
+ if constexpr (verbose) {
813
+ std::cout << "Vine updates : ";
814
+ }
815
+ for (std::size_t i = 0; i < n; i++) {
816
+ auto j = i;
817
+ while (j > 0 && persistence.get_dimension(j) == persistence.get_dimension(j - 1) &&
818
+ one_filtration[generator_order[j]] < one_filtration[generator_order[j - 1]]) {
819
+ if constexpr (verbose) {
820
+ std::cout << j - 1 << ", ";
821
+ }
822
+ persistence.vine_swap(j - 1);
823
+ std::swap(generator_order[j - 1], generator_order[j]);
824
+ j--;
825
+ }
826
+ }
827
+ if constexpr (verbose) {
828
+ std::cout << std::endl;
829
+ }
830
+ }
831
+
832
+ inline void vineyard_update() {
833
+ vineyard_update(this->persistence, this->filtration_container, this->generator_order);
834
+ }
835
+
836
+ inline split_barcode_idx get_barcode_idx(
837
+ PersBackend &persistence) const {
838
+ auto barcode_indices = persistence.get_barcode();
839
+ split_barcode_idx out(this->structure.max_dimension() + 1); // TODO : This doesn't allow for negative dimensions
840
+ for (const auto &bar : barcode_indices) {
841
+ int death = bar.death == static_cast<typename PersBackend::pos_index>(-1) ? -1 : bar.death;
842
+ out[bar.dim].push_back({bar.birth, death});
843
+ }
844
+ return out;
845
+ }
846
+
847
+ // puts the degree-ordered bc starting out_ptr, and returns the "next" pointer.
848
+ // corresond to an array of shape (num_bar, 2);
849
+ template <bool return_shape = false>
850
+ inline std::conditional_t<return_shape, std::pair<std::vector<int>, int*>, int*> get_barcode_idx(
851
+ PersBackend &persistence,
852
+ int *start_ptr) const {
853
+ const auto &bc = persistence.barcode();
854
+ if (bc.size() == 0) return start_ptr;
855
+ std::vector<int> shape(this->structure.max_dimension());
856
+ for (const auto &b : bc) shape[b.dim]++;
857
+ // dim in barcode may be unsorted...
858
+ std::vector<int *> ptr_shifts(shape.size());
859
+ int shape_cumsum = 0;
860
+ for (auto i : std::views::iota(0u, bc.size())) {
861
+ if (i != 0u) shape_cumsum += shape[i - 1];
862
+ // 2 for (birth, death)
863
+ ptr_shifts[i] = 2 * shape_cumsum + start_ptr;
864
+ }
865
+ for (const auto &b : bc) {
866
+ int *current_loc = ptr_shifts[b.dim];
867
+ *(current_loc++) = b.birth;
868
+ *(current_loc++) = b.death == static_cast<typename PersBackend::pos_index>(-1) ? -1 : b.death;
869
+ }
870
+
871
+ if constexpr (return_shape)
872
+ return {shape, ptr_shifts.back()};
873
+ else
874
+ return ptr_shifts.back();
875
+ }
876
+
877
+
878
+
879
+ inline split_barcode get_barcode(
880
+ PersBackend &persistence,
881
+ const std::vector<typename MultiFiltration::value_type> &filtration_container) const {
882
+ auto barcode_indices = persistence.get_barcode();
883
+ split_barcode out(this->structure.max_dimension() + 1); // TODO : This doesn't allow for negative dimensions
884
+ constexpr const bool verbose = false;
885
+ constexpr const bool debug = false;
886
+ const auto inf = MultiFiltration::Generator::T_inf;
887
+ for (const auto &bar : barcode_indices) {
888
+ if constexpr (verbose) std::cout << "BAR : " << bar.birth << " " << bar.death << "\n";
889
+ if constexpr (debug) {
890
+ if (bar.birth >= filtration_container.size() || bar.birth < 0) {
891
+ std::cout << "Trying to add an incompatible birth... ";
892
+ std::cout << bar.birth << std::endl;
893
+ std::cout << "Death is " << bar.death << std::endl;
894
+ std::cout << "Max size is " << filtration_container.size() << std::endl;
895
+ continue;
896
+ }
897
+ if (bar.dim > static_cast<int>(this->structure.max_dimension())) {
898
+ std::cout << "Incompatible dimension detected... " << bar.dim << std::endl;
899
+ std::cout << "While max dim is " << this->structure.max_dimension() << std::endl;
900
+ continue;
901
+ }
902
+ }
903
+
904
+ auto birth_filtration = filtration_container[bar.birth];
905
+ auto death_filtration = inf;
906
+ if (bar.death != static_cast<typename PersBackend::pos_index>(-1))
907
+ death_filtration = filtration_container[bar.death];
908
+
909
+ if constexpr (verbose) {
910
+ std::cout << "BAR: " << bar.birth << "(" << birth_filtration << ")"
911
+ << " --" << bar.death << "(" << death_filtration << ")"
912
+ << " dim " << bar.dim << std::endl;
913
+ }
914
+ if (birth_filtration <= death_filtration)
915
+ out[bar.dim].push_back({birth_filtration, death_filtration});
916
+ else {
917
+ out[bar.dim].push_back({inf, inf});
918
+ }
919
+ }
920
+ return out;
921
+ }
922
+
923
+ inline split_barcode get_barcode() { return get_barcode(this->persistence, this->filtration_container); }
924
+
925
+ inline split_barcode_idx get_barcode_idx() { return get_barcode_idx(this->persistence); }
926
+
927
+ template <typename value_type = value_type>
928
+ static inline flat_nodim_barcode<value_type> get_flat_nodim_barcode(
929
+ PersBackend &persistence,
930
+ std::vector<typename MultiFiltration::value_type> &filtration_container) {
931
+ constexpr const bool verbose = false;
932
+ const auto &barcode_indices = persistence.get_barcode();
933
+ auto num_bars = barcode_indices.size();
934
+ flat_nodim_barcode<value_type> out(num_bars);
935
+ if (num_bars <= 0) return out;
936
+ auto idx = 0u;
937
+ const value_type inf = MultiFiltration::Generator::T_inf;
938
+ for (const auto &bar : barcode_indices) {
939
+ value_type birth_filtration = inf;
940
+ value_type death_filtration = -birth_filtration;
941
+ if (bar.death == static_cast<typename PersBackend::pos_index>(-1))
942
+ death_filtration = inf;
943
+ else
944
+ death_filtration = static_cast<value_type>(filtration_container[bar.death]);
945
+ birth_filtration = static_cast<value_type>(filtration_container[bar.birth]);
946
+ if constexpr (verbose) {
947
+ std::cout << "PAIRING : " << bar.birth << " / " << bar.death << " dim " << bar.dim << std::endl;
948
+ }
949
+ if constexpr (verbose) {
950
+ std::cout << "PAIRING filtration : " << birth_filtration << " " << death_filtration << " dim " << bar.dim
951
+ << std::endl;
952
+ }
953
+
954
+ if (birth_filtration <= death_filtration)
955
+ out[idx] = {birth_filtration, death_filtration};
956
+ else {
957
+ out[idx] = {inf, inf};
958
+ }
959
+ idx++;
960
+ }
961
+ return out;
962
+ }
963
+
964
+ template <typename value_type = value_type>
965
+ static inline flat_barcode<value_type> get_flat_barcode(
966
+ PersBackend &persistence,
967
+ std::vector<typename MultiFiltration::value_type> &filtration_container) {
968
+ constexpr const bool verbose = false;
969
+ const auto &barcode_indices = persistence.get_barcode();
970
+ auto num_bars = barcode_indices.size();
971
+ flat_barcode<value_type> out(num_bars);
972
+ if (num_bars <= 0) return out;
973
+ auto idx = 0u;
974
+ const value_type inf = MultiFiltration::Generator::T_inf;
975
+ for (const auto &bar : barcode_indices) {
976
+ value_type birth_filtration = inf;
977
+ value_type death_filtration = -birth_filtration;
978
+ if (bar.death == static_cast<typename PersBackend::pos_index>(-1))
979
+ death_filtration = inf;
980
+ else
981
+ death_filtration = static_cast<value_type>(filtration_container[bar.death]);
982
+ birth_filtration = static_cast<value_type>(filtration_container[bar.birth]);
983
+ if constexpr (verbose) {
984
+ std::cout << "PAIRING : " << bar.birth << " / " << bar.death << " dim " << bar.dim << std::endl;
985
+ }
986
+ if constexpr (verbose) {
987
+ std::cout << "PAIRING filtration : " << birth_filtration << " " << death_filtration << " dim " << bar.dim
988
+ << std::endl;
989
+ }
990
+
991
+ if (birth_filtration <= death_filtration)
992
+ out[idx] = {bar.dim, {birth_filtration, death_filtration}};
993
+ else {
994
+ out[idx] = {bar.dim, {inf, inf}};
995
+ }
996
+ idx++;
997
+ }
998
+ return out;
999
+ }
1000
+
1001
+ template <typename value_type = value_type>
1002
+ inline flat_barcode<value_type> get_flat_barcode() {
1003
+ return get_flat_barcode(this->persistence, this->filtration_container);
1004
+ }
1005
+
1006
+ template <typename value_type = value_type>
1007
+ inline flat_nodim_barcode<value_type> get_flat_nodim_barcode() {
1008
+ return get_flat_nodim_barcode(this->persistence, this->filtration_container);
1009
+ }
1010
+
1011
+ inline friend std::ostream &operator<<(std::ostream &stream, Truc &truc) {
1012
+ stream << "-------------------- Truc \n";
1013
+ stream << "--- Structure \n";
1014
+ stream << truc.structure;
1015
+ /* stream << "-- Dimensions (max " << truc.structure.max_dimension() <<
1016
+ * ")\n"; */
1017
+ /* stream << "{"; */
1018
+ /* for (auto i = 0u; i < truc.num_generators(); i++) */
1019
+ /* stream << truc.structure.dimension(i) << ", "; */
1020
+ /* stream << "\b" */
1021
+ /* << "\b"; */
1022
+ /* stream << "}" << std::endl; */
1023
+ stream << "--- Order \n";
1024
+ stream << "{";
1025
+ for (const auto &idx : truc.generator_order) stream << idx << ", ";
1026
+ stream << "}" << std::endl;
1027
+
1028
+ stream << "--- Current slice filtration\n";
1029
+ stream << "{";
1030
+ for (const auto &stuff : truc.filtration_container) stream << stuff << ", ";
1031
+ stream << "\b" << "\b";
1032
+ stream << "}" << std::endl;
1033
+
1034
+ stream << "--- Filtrations \n";
1035
+ for (const auto &i : truc.generator_order) {
1036
+ stream << i << " : ";
1037
+ const auto &stuff = truc.generator_filtration_values[i];
1038
+ stream << stuff << "\n";
1039
+ }
1040
+ stream << "--- PersBackend \n";
1041
+ stream << truc.persistence;
1042
+
1043
+ return stream;
1044
+ }
1045
+
1046
+ inline std::string to_str() {
1047
+ std::stringstream stream;
1048
+ stream << *this;
1049
+ return stream.str();
1050
+ }
1051
+
1052
+ inline std::pair<typename MultiFiltration::Generator, typename MultiFiltration::Generator> get_bounding_box() const {
1053
+ using OC = typename MultiFiltration::Generator;
1054
+ // assert(!generator_filtration_values.empty());
1055
+ OC a = OC::inf();
1056
+ OC b = -1 * a;
1057
+ for (const auto &filtration_value : generator_filtration_values) {
1058
+ if constexpr (MultiFiltration::is_multi_critical) {
1059
+ a.pull_to_greatest_common_lower_bound(factorize_below(filtration_value));
1060
+ b.push_to_least_common_upper_bound(factorize_above(filtration_value));
1061
+ } else {
1062
+ a.pull_to_greatest_common_lower_bound(filtration_value);
1063
+ b.push_to_least_common_upper_bound(filtration_value);
1064
+ }
1065
+ }
1066
+ return {a, b};
1067
+ }
1068
+
1069
+ inline std::vector<typename MultiFiltration::Generator> get_filtration_values() const {
1070
+ if constexpr (MultiFiltration::is_multi_critical) {
1071
+ std::vector<typename MultiFiltration::Generator> out;
1072
+ out.reserve(generator_filtration_values.size()); // at least this, will dooble later
1073
+ for (std::size_t i = 0; i < generator_filtration_values.size(); ++i) {
1074
+ for (const auto &f : generator_filtration_values[i]) {
1075
+ out.push_back(f);
1076
+ }
1077
+ }
1078
+ return out;
1079
+ } else {
1080
+ return generator_filtration_values; // copy not necessary for Generator
1081
+ } // (could return const&)
1082
+ }
1083
+
1084
+ inline std::vector<MultiFiltration> &get_filtrations() { return generator_filtration_values; }
1085
+
1086
+ inline const std::vector<MultiFiltration> &get_filtrations() const { return generator_filtration_values; }
1087
+
1088
+ inline const std::vector<int> get_dimensions() const {
1089
+ std::size_t n = this->num_generators();
1090
+ std::vector<int> out(n);
1091
+ for (std::size_t i = 0; i < n; ++i) {
1092
+ out[i] = structure.dimension(i);
1093
+ }
1094
+ return out;
1095
+ }
1096
+
1097
+ inline int get_dimension(int i) const { return structure.dimension(i); }
1098
+
1099
+ inline void prune_above_dimension(int max_dim) {
1100
+ int idx = structure.prune_above_dimension(max_dim);
1101
+ generator_filtration_values.resize(idx);
1102
+ generator_order.resize(idx);
1103
+ filtration_container.resize(idx);
1104
+ }
1105
+
1106
+ inline const std::vector<std::vector<index_type>> get_boundaries() {
1107
+ std::size_t n = this->num_generators();
1108
+ std::vector<std::vector<index_type>> out(n);
1109
+ for (auto i = 0u; i < n; ++i) {
1110
+ out[i] = this->structure[i];
1111
+ }
1112
+ return out;
1113
+ }
1114
+
1115
+ auto coarsen_on_grid(const std::vector<std::vector<typename MultiFiltration::value_type>>& grid) {
1116
+ using return_type = decltype(std::declval<MultiFiltration>().template as_type<std::int32_t>());
1117
+ std::vector<return_type> coords(this->num_generators());
1118
+ // for (std::size_t gen = 0u; gen < coords.size(); ++gen) { // TODO : parallelize
1119
+ // coords[gen] = compute_coordinates_in_grid<int32_t>(generator_filtration_values[gen], grid);
1120
+ // }
1121
+ tbb::parallel_for(static_cast<std::size_t>(0u), coords.size(), [&](std::size_t gen){
1122
+ coords[gen] = compute_coordinates_in_grid<int32_t>(generator_filtration_values[gen], grid);
1123
+ });
1124
+ return Truc<PersBackend, Structure, return_type>(structure, coords);
1125
+ }
1126
+
1127
+ inline void coarsen_on_grid_inplace(const std::vector<std::vector<typename MultiFiltration::value_type>> &grid,
1128
+ bool coordinate = true) {
1129
+ for (auto gen = 0u; gen < this->num_generators(); ++gen) {
1130
+ generator_filtration_values[gen].project_onto_grid(grid, coordinate);
1131
+ }
1132
+ }
1133
+
1134
+ // dim, num_cycle_of_dim, num_faces_in_cycle, vertices_in_face
1135
+ inline std::vector<std::vector<std::vector<std::vector<index_type>>>> get_representative_cycles(
1136
+ bool update = true,
1137
+ bool detailed = false) {
1138
+ // iterable iterable simplex key
1139
+ auto cycles_key = persistence.get_representative_cycles(update, detailed);
1140
+ auto num_cycles = cycles_key.size();
1141
+ std::vector<std::vector<std::vector<std::vector<index_type>>>> out(structure.max_dimension() + 1);
1142
+ for (auto &cycles_of_dim : out) cycles_of_dim.reserve(num_cycles);
1143
+ for (const auto &cycle : cycles_key) {
1144
+ int cycle_dim = 0; // for more generality, should be minimal dimension instead
1145
+ if (!cycle[0].empty()) { // if empty, cycle has no border -> assumes dimension 0 even if it could be min dim
1146
+ cycle_dim = structure.dimension(cycle[0][0]) + 1; // all faces have the same dim
1147
+ }
1148
+ out[cycle_dim].push_back(cycle);
1149
+ }
1150
+ return out;
1151
+ }
1152
+
1153
+ const std::vector<index_type> &get_current_order() const { return generator_order; }
1154
+
1155
+ const PersBackend &get_persistence() const { return persistence; }
1156
+
1157
+ PersBackend &get_persistence() { return persistence; }
1158
+
1159
+ // TrucThread get_safe_thread() { return TrucThread(*this); }
1160
+
1161
+ class TrucThread {
1162
+ public:
1163
+ using Filtration_value = MultiFiltration;
1164
+ using value_type = typename MultiFiltration::value_type;
1165
+ using ThreadSafe = TrucThread;
1166
+
1167
+ inline TrucThread(const Truc &truc)
1168
+ : truc_ptr(&truc),
1169
+ generator_order(truc.get_current_order()),
1170
+ filtration_container(truc.get_one_filtration()),
1171
+ persistence(truc.get_persistence()) {
1172
+ persistence._update_permutation_ptr(generator_order);
1173
+ };
1174
+
1175
+ inline TrucThread(const TrucThread &truc)
1176
+ : truc_ptr(truc.truc_ptr),
1177
+ generator_order(truc.get_current_order()),
1178
+ filtration_container(truc.get_one_filtration()),
1179
+ persistence(truc.get_persistence()) {
1180
+ persistence._update_permutation_ptr(generator_order);
1181
+ };
1182
+
1183
+ inline TrucThread weak_copy() const { return TrucThread(*truc_ptr); }
1184
+
1185
+ inline bool has_persistence() const { return this->persistence.size(); };
1186
+
1187
+ inline const PersBackend &get_persistence() const { return persistence; }
1188
+
1189
+ inline PersBackend &get_persistence() { return persistence; }
1190
+
1191
+ inline std::pair<MultiFiltration, MultiFiltration> get_bounding_box() const { return truc_ptr->get_bounding_box(); }
1192
+
1193
+ inline const std::vector<index_type> &get_current_order() const { return generator_order; }
1194
+
1195
+ inline const std::vector<MultiFiltration> &get_filtrations() const { return truc_ptr->get_filtrations(); }
1196
+
1197
+ inline const std::vector<int> &get_dimensions() const { return truc_ptr->get_dimensions(); }
1198
+
1199
+ inline const std::vector<std::vector<index_type>> &get_boundaries() const { return truc_ptr->get_boundaries(); }
1200
+
1201
+ inline void coarsen_on_grid_inplace(const std::vector<std::vector<typename MultiFiltration::value_type>> &grid,
1202
+ bool coordinate = true) {
1203
+ truc_ptr->coarsen_on_grid_inplace(grid, coordinate);
1204
+ }
1205
+
1206
+ template <typename Subfiltration>
1207
+ inline void push_to(const Subfiltration &f) {
1208
+ truc_ptr->push_to_out(f, this->filtration_container, this->generator_order);
1209
+ }
1210
+
1211
+ inline std::vector<typename PersBackend::cycle_type> get_representative_cycles(bool update = true) {
1212
+ return truc_ptr->get_representative_cycles(update);
1213
+ }
1214
+
1215
+ inline void compute_persistence(const bool ignore_inf = true) {
1216
+ this->persistence =
1217
+ this->truc_ptr->compute_persistence_out(this->filtration_container, this->generator_order, ignore_inf);
1218
+ };
1219
+
1220
+ inline void vineyard_update() {
1221
+ truc_ptr->vineyard_update(this->persistence, this->filtration_container, this->generator_order);
1222
+ }
1223
+
1224
+ template <typename value_type = value_type>
1225
+ inline flat_barcode<value_type> get_flat_barcode() {
1226
+ return truc_ptr->get_flat_barcode(this->persistence, this->filtration_container);
1227
+ }
1228
+
1229
+ template <typename value_type = value_type>
1230
+ inline flat_nodim_barcode<value_type> get_flat_nodim_barcode() {
1231
+ return truc_ptr->get_flat_nodim_barcode(this->persistence, this->filtration_container);
1232
+ }
1233
+
1234
+ inline split_barcode get_barcode() { return truc_ptr->get_barcode(this->persistence, this->filtration_container); }
1235
+
1236
+ inline split_barcode_idx get_barcode_idx() {
1237
+ return truc_ptr->get_barcode_idx(this->persistence);
1238
+ }
1239
+
1240
+ inline std::size_t num_generators() const { return this->truc_ptr->structure.size(); }
1241
+
1242
+ inline std::size_t num_parameters() const {
1243
+ return num_generators() == 0 ? 0 : this->get_filtrations()[0].num_parameters();
1244
+ }
1245
+
1246
+ inline const std::vector<typename MultiFiltration::value_type> &get_one_filtration() const {
1247
+ return this->filtration_container;
1248
+ }
1249
+
1250
+ inline std::vector<typename MultiFiltration::value_type> &get_one_filtration() {
1251
+ return this->filtration_container;
1252
+ }
1253
+
1254
+ template <class array1d>
1255
+ inline void set_one_filtration(const array1d &truc) {
1256
+ if (truc.size() != this->num_generators())
1257
+ throw std::invalid_argument("(setting one filtration) Bad size. Got " + std::to_string(truc.size()) +
1258
+ " expected " + std::to_string(this->num_generators()));
1259
+ this->filtration_container = truc;
1260
+ }
1261
+
1262
+ private:
1263
+ const Truc *truc_ptr;
1264
+ std::vector<index_type> generator_order; // size fixed at construction time,
1265
+ std::vector<typename MultiFiltration::value_type> filtration_container; // filtration of the current slice
1266
+ PersBackend persistence; // generated by the structure, and generator_order.
1267
+
1268
+ }; // class TrucThread
1269
+
1270
+ /*
1271
+ * returns barcodes of the f(multipers)
1272
+ *
1273
+ */
1274
+ template <typename Fun, typename Fun_arg, bool idx = false, bool custom = false>
1275
+ inline std::conditional_t<idx, std::vector<split_barcode_idx>, std::vector<split_barcode>>
1276
+ barcodes(Fun &&f, const std::vector<Fun_arg> &args, const bool ignore_inf = true) {
1277
+ if (args.size() == 0) {
1278
+ return {};
1279
+ }
1280
+ std::conditional_t<idx, std::vector<split_barcode_idx>, std::vector<split_barcode>> out(args.size());
1281
+
1282
+ if constexpr (PersBackend::is_vine) {
1283
+ if constexpr (custom)
1284
+ this->set_one_filtration(f(args[0]));
1285
+ else
1286
+ this->push_to(f(args[0]));
1287
+ this->compute_persistence();
1288
+ if constexpr (idx)
1289
+ out[0] = this->get_barcode_idx();
1290
+ else
1291
+ out[0] = this->get_barcode();
1292
+ for (auto i = 1u; i < args.size(); ++i) {
1293
+ if constexpr (custom)
1294
+ this->set_one_filtration(f(args[i]));
1295
+ else
1296
+ this->push_to(f(args[i]));
1297
+ this->vineyard_update();
1298
+ if constexpr (idx)
1299
+ out[i] = this->get_barcode_idx();
1300
+ else
1301
+ out[i] = this->get_barcode();
1302
+ }
1303
+
1304
+ } else {
1305
+ ThreadSafe local_template = this->weak_copy();
1306
+ tbb::enumerable_thread_specific<ThreadSafe> thread_locals(local_template);
1307
+ tbb::parallel_for(static_cast<std::size_t>(0), args.size(), [&](const std::size_t &i) {
1308
+ ThreadSafe &s = thread_locals.local();
1309
+ if constexpr (custom)
1310
+ s.set_one_filtration(f(args[i]));
1311
+ else
1312
+ s.push_to(f(args[i]));
1313
+ s.compute_persistence(ignore_inf);
1314
+ if constexpr (idx) {
1315
+ out[i] = s.get_barcode_idx();
1316
+ } else
1317
+ out[i] = s.get_barcode();
1318
+ });
1319
+ }
1320
+ return out;
1321
+ }
1322
+
1323
+ // FOR Python interface, but I'm not fan. Todo: do the lambda function in
1324
+ // cython?
1325
+ inline std::vector<split_barcode> persistence_on_lines(const std::vector<std::vector<value_type>> &basepoints,
1326
+ bool ignore_inf) {
1327
+ return barcodes(
1328
+ [](const std::vector<value_type> &basepoint) { return Gudhi::multi_persistence::Line<value_type>(basepoint); },
1329
+ basepoints,
1330
+ ignore_inf);
1331
+ }
1332
+
1333
+ inline std::vector<split_barcode_idx> custom_persistences(const value_type *filtrations, int size, bool ignore_inf) {
1334
+ std::vector<const value_type *> args(size);
1335
+ for (auto i = 0; i < size; ++i) args[i] = filtrations + this->num_generators() * i;
1336
+
1337
+ auto fun = [&](const value_type *one_filtration_ptr) {
1338
+ std::vector<value_type> fil(this->num_generators());
1339
+ for (auto i : std::views::iota(0u, this->num_generators())) {
1340
+ fil[i] = *(one_filtration_ptr + i);
1341
+ }
1342
+ return std::move(fil);
1343
+ };
1344
+ return barcodes<decltype(fun), const value_type *, true, true>(std::move(fun), args, ignore_inf);
1345
+ }
1346
+
1347
+ inline std::vector<split_barcode> persistence_on_lines(
1348
+ const std::vector<std::pair<std::vector<value_type>, std::vector<value_type>>> &bp_dirs,
1349
+ bool ignore_inf) {
1350
+ return barcodes(
1351
+ [](const std::pair<std::vector<value_type>, std::vector<value_type>> &bpdir) {
1352
+ return Gudhi::multi_persistence::Line<value_type>(bpdir.first, bpdir.second);
1353
+ },
1354
+ bp_dirs,
1355
+ ignore_inf);
1356
+ }
1357
+
1358
+ void build_from_scc_file(const std::string &inFilePath,
1359
+ bool isRivetCompatible = false,
1360
+ bool isReversed = false,
1361
+ int shiftDimensions = 0) {
1362
+ *this = read_scc_file<Truc>(inFilePath, isRivetCompatible, isReversed, shiftDimensions);
1363
+ }
1364
+
1365
+ void write_to_scc_file(const std::string &outFilePath,
1366
+ int numberOfParameters = -1,
1367
+ int degree = -1,
1368
+ bool rivetCompatible = false,
1369
+ bool IgnoreLastGenerators = false,
1370
+ bool stripComments = false,
1371
+ bool reverse = false) {
1372
+ write_scc_file<Truc>(
1373
+ outFilePath, *this, numberOfParameters, degree, rivetCompatible, IgnoreLastGenerators, stripComments, reverse);
1374
+ }
1375
+
1376
+ public:
1377
+ using ThreadSafe = TrucThread; // for outside
1378
+
1379
+ TrucThread weak_copy() const { return TrucThread(*this); }
1380
+
1381
+ // TODO: declare method here instead of scc_io.h
1382
+ // it is just temporary, until Truc is cleaned up
1383
+ // friend void write_scc_file<Truc>(const std::string &outFilePath,
1384
+ // const Truc &slicer,
1385
+ // int numberOfParameters,
1386
+ // int degree,
1387
+ // bool rivetCompatible,
1388
+ // bool IgnoreLastGenerators,
1389
+ // bool stripComments,
1390
+ // bool reverse);
1391
+
1392
+ private:
1393
+ MultiFiltrations generator_filtration_values; // defined at construction time. Const
1394
+ std::vector<index_type> generator_order; // size fixed at construction time
1395
+ Structure structure; // defined at construction time. Const
1396
+ std::vector<typename MultiFiltration::value_type> filtration_container; // filtration of the current slice
1397
+ PersBackend persistence; // generated by the structure, and generator_order.
1398
+
1399
+ }; // class Truc
1400
+
1401
+ } // namespace truc_interface
1402
+ } // namespace multiparameter
1403
+ } // namespace Gudhi