multipers 2.2.3__cp310-cp310-win_amd64.whl → 2.3.0__cp310-cp310-win_amd64.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 -31
  2. multipers/_signed_measure_meta.py +430 -430
  3. multipers/_slicer_meta.py +211 -212
  4. multipers/data/MOL2.py +458 -458
  5. multipers/data/UCR.py +18 -18
  6. multipers/data/graphs.py +466 -466
  7. multipers/data/immuno_regions.py +27 -27
  8. multipers/data/pytorch2simplextree.py +90 -90
  9. multipers/data/shape3d.py +101 -101
  10. multipers/data/synthetic.py +113 -111
  11. multipers/distances.py +198 -198
  12. multipers/filtration_conversions.pxd.tp +84 -84
  13. multipers/filtrations/__init__.py +18 -0
  14. multipers/filtrations/filtrations.py +289 -0
  15. multipers/filtrations.pxd +224 -224
  16. multipers/function_rips.cp310-win_amd64.pyd +0 -0
  17. multipers/function_rips.pyx +105 -105
  18. multipers/grids.cp310-win_amd64.pyd +0 -0
  19. multipers/grids.pyx +350 -350
  20. multipers/gudhi/Persistence_slices_interface.h +132 -132
  21. multipers/gudhi/Simplex_tree_interface.h +239 -245
  22. multipers/gudhi/Simplex_tree_multi_interface.h +516 -561
  23. multipers/gudhi/cubical_to_boundary.h +59 -59
  24. multipers/gudhi/gudhi/Bitmap_cubical_complex.h +450 -450
  25. multipers/gudhi/gudhi/Bitmap_cubical_complex_base.h +1070 -1070
  26. multipers/gudhi/gudhi/Bitmap_cubical_complex_periodic_boundary_conditions_base.h +579 -579
  27. multipers/gudhi/gudhi/Debug_utils.h +45 -45
  28. multipers/gudhi/gudhi/Fields/Multi_field.h +484 -484
  29. multipers/gudhi/gudhi/Fields/Multi_field_operators.h +455 -455
  30. multipers/gudhi/gudhi/Fields/Multi_field_shared.h +450 -450
  31. multipers/gudhi/gudhi/Fields/Multi_field_small.h +531 -531
  32. multipers/gudhi/gudhi/Fields/Multi_field_small_operators.h +507 -507
  33. multipers/gudhi/gudhi/Fields/Multi_field_small_shared.h +531 -531
  34. multipers/gudhi/gudhi/Fields/Z2_field.h +355 -355
  35. multipers/gudhi/gudhi/Fields/Z2_field_operators.h +376 -376
  36. multipers/gudhi/gudhi/Fields/Zp_field.h +420 -420
  37. multipers/gudhi/gudhi/Fields/Zp_field_operators.h +400 -400
  38. multipers/gudhi/gudhi/Fields/Zp_field_shared.h +418 -418
  39. multipers/gudhi/gudhi/Flag_complex_edge_collapser.h +337 -337
  40. multipers/gudhi/gudhi/Matrix.h +2107 -2107
  41. multipers/gudhi/gudhi/Multi_critical_filtration.h +1038 -1038
  42. multipers/gudhi/gudhi/Multi_persistence/Box.h +171 -171
  43. multipers/gudhi/gudhi/Multi_persistence/Line.h +282 -282
  44. multipers/gudhi/gudhi/Off_reader.h +173 -173
  45. multipers/gudhi/gudhi/One_critical_filtration.h +1432 -1431
  46. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix.h +769 -769
  47. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix_with_column_compression.h +686 -686
  48. multipers/gudhi/gudhi/Persistence_matrix/Boundary_matrix.h +842 -842
  49. multipers/gudhi/gudhi/Persistence_matrix/Chain_matrix.h +1350 -1350
  50. multipers/gudhi/gudhi/Persistence_matrix/Id_to_index_overlay.h +1105 -1105
  51. multipers/gudhi/gudhi/Persistence_matrix/Position_to_index_overlay.h +859 -859
  52. multipers/gudhi/gudhi/Persistence_matrix/RU_matrix.h +910 -910
  53. multipers/gudhi/gudhi/Persistence_matrix/allocators/entry_constructors.h +139 -139
  54. multipers/gudhi/gudhi/Persistence_matrix/base_pairing.h +230 -230
  55. multipers/gudhi/gudhi/Persistence_matrix/base_swap.h +211 -211
  56. multipers/gudhi/gudhi/Persistence_matrix/boundary_cell_position_to_id_mapper.h +60 -60
  57. multipers/gudhi/gudhi/Persistence_matrix/boundary_face_position_to_id_mapper.h +60 -60
  58. multipers/gudhi/gudhi/Persistence_matrix/chain_pairing.h +136 -136
  59. multipers/gudhi/gudhi/Persistence_matrix/chain_rep_cycles.h +190 -190
  60. multipers/gudhi/gudhi/Persistence_matrix/chain_vine_swap.h +616 -616
  61. multipers/gudhi/gudhi/Persistence_matrix/columns/chain_column_extra_properties.h +150 -150
  62. multipers/gudhi/gudhi/Persistence_matrix/columns/column_dimension_holder.h +106 -106
  63. multipers/gudhi/gudhi/Persistence_matrix/columns/column_utilities.h +219 -219
  64. multipers/gudhi/gudhi/Persistence_matrix/columns/entry_types.h +327 -327
  65. multipers/gudhi/gudhi/Persistence_matrix/columns/heap_column.h +1140 -1140
  66. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_list_column.h +934 -934
  67. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_set_column.h +934 -934
  68. multipers/gudhi/gudhi/Persistence_matrix/columns/list_column.h +980 -980
  69. multipers/gudhi/gudhi/Persistence_matrix/columns/naive_vector_column.h +1092 -1092
  70. multipers/gudhi/gudhi/Persistence_matrix/columns/row_access.h +192 -192
  71. multipers/gudhi/gudhi/Persistence_matrix/columns/set_column.h +921 -921
  72. multipers/gudhi/gudhi/Persistence_matrix/columns/small_vector_column.h +1093 -1093
  73. multipers/gudhi/gudhi/Persistence_matrix/columns/unordered_set_column.h +1012 -1012
  74. multipers/gudhi/gudhi/Persistence_matrix/columns/vector_column.h +1244 -1244
  75. multipers/gudhi/gudhi/Persistence_matrix/matrix_dimension_holders.h +186 -186
  76. multipers/gudhi/gudhi/Persistence_matrix/matrix_row_access.h +164 -164
  77. multipers/gudhi/gudhi/Persistence_matrix/ru_pairing.h +156 -156
  78. multipers/gudhi/gudhi/Persistence_matrix/ru_rep_cycles.h +376 -376
  79. multipers/gudhi/gudhi/Persistence_matrix/ru_vine_swap.h +540 -540
  80. multipers/gudhi/gudhi/Persistent_cohomology/Field_Zp.h +118 -118
  81. multipers/gudhi/gudhi/Persistent_cohomology/Multi_field.h +173 -173
  82. multipers/gudhi/gudhi/Persistent_cohomology/Persistent_cohomology_column.h +128 -128
  83. multipers/gudhi/gudhi/Persistent_cohomology.h +745 -745
  84. multipers/gudhi/gudhi/Points_off_io.h +171 -171
  85. multipers/gudhi/gudhi/Simple_object_pool.h +69 -69
  86. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_iterators.h +463 -463
  87. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_node_explicit_storage.h +83 -83
  88. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_siblings.h +106 -106
  89. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_star_simplex_iterators.h +277 -277
  90. multipers/gudhi/gudhi/Simplex_tree/hooks_simplex_base.h +62 -62
  91. multipers/gudhi/gudhi/Simplex_tree/indexing_tag.h +27 -27
  92. multipers/gudhi/gudhi/Simplex_tree/serialization_utils.h +62 -62
  93. multipers/gudhi/gudhi/Simplex_tree/simplex_tree_options.h +157 -157
  94. multipers/gudhi/gudhi/Simplex_tree.h +2794 -2794
  95. multipers/gudhi/gudhi/Simplex_tree_multi.h +152 -163
  96. multipers/gudhi/gudhi/distance_functions.h +62 -62
  97. multipers/gudhi/gudhi/graph_simplicial_complex.h +104 -104
  98. multipers/gudhi/gudhi/persistence_interval.h +253 -253
  99. multipers/gudhi/gudhi/persistence_matrix_options.h +170 -170
  100. multipers/gudhi/gudhi/reader_utils.h +367 -367
  101. multipers/gudhi/mma_interface_coh.h +256 -255
  102. multipers/gudhi/mma_interface_h0.h +223 -231
  103. multipers/gudhi/mma_interface_matrix.h +284 -282
  104. multipers/gudhi/naive_merge_tree.h +536 -575
  105. multipers/gudhi/scc_io.h +310 -289
  106. multipers/gudhi/truc.h +890 -888
  107. multipers/io.cp310-win_amd64.pyd +0 -0
  108. multipers/io.pyx +711 -711
  109. multipers/ml/accuracies.py +90 -90
  110. multipers/ml/convolutions.py +520 -520
  111. multipers/ml/invariants_with_persistable.py +79 -79
  112. multipers/ml/kernels.py +176 -176
  113. multipers/ml/mma.py +713 -714
  114. multipers/ml/one.py +472 -472
  115. multipers/ml/point_clouds.py +352 -346
  116. multipers/ml/signed_measures.py +1589 -1589
  117. multipers/ml/sliced_wasserstein.py +461 -461
  118. multipers/ml/tools.py +113 -113
  119. multipers/mma_structures.cp310-win_amd64.pyd +0 -0
  120. multipers/mma_structures.pxd +127 -127
  121. multipers/mma_structures.pyx +4 -4
  122. multipers/mma_structures.pyx.tp +1085 -1085
  123. multipers/multi_parameter_rank_invariant/diff_helpers.h +84 -93
  124. multipers/multi_parameter_rank_invariant/euler_characteristic.h +97 -97
  125. multipers/multi_parameter_rank_invariant/function_rips.h +322 -322
  126. multipers/multi_parameter_rank_invariant/hilbert_function.h +769 -769
  127. multipers/multi_parameter_rank_invariant/persistence_slices.h +148 -148
  128. multipers/multi_parameter_rank_invariant/rank_invariant.h +369 -369
  129. multipers/multiparameter_edge_collapse.py +41 -41
  130. multipers/multiparameter_module_approximation/approximation.h +2296 -2295
  131. multipers/multiparameter_module_approximation/combinatory.h +129 -129
  132. multipers/multiparameter_module_approximation/debug.h +107 -107
  133. multipers/multiparameter_module_approximation/format_python-cpp.h +286 -286
  134. multipers/multiparameter_module_approximation/heap_column.h +238 -238
  135. multipers/multiparameter_module_approximation/images.h +79 -79
  136. multipers/multiparameter_module_approximation/list_column.h +174 -174
  137. multipers/multiparameter_module_approximation/list_column_2.h +232 -232
  138. multipers/multiparameter_module_approximation/ru_matrix.h +347 -347
  139. multipers/multiparameter_module_approximation/set_column.h +135 -135
  140. multipers/multiparameter_module_approximation/structure_higher_dim_barcode.h +36 -36
  141. multipers/multiparameter_module_approximation/unordered_set_column.h +166 -166
  142. multipers/multiparameter_module_approximation/utilities.h +403 -419
  143. multipers/multiparameter_module_approximation/vector_column.h +223 -223
  144. multipers/multiparameter_module_approximation/vector_matrix.h +331 -331
  145. multipers/multiparameter_module_approximation/vineyards.h +464 -464
  146. multipers/multiparameter_module_approximation/vineyards_trajectories.h +649 -649
  147. multipers/multiparameter_module_approximation.cp310-win_amd64.pyd +0 -0
  148. multipers/multiparameter_module_approximation.pyx +216 -217
  149. multipers/pickle.py +90 -53
  150. multipers/plots.py +342 -334
  151. multipers/point_measure.cp310-win_amd64.pyd +0 -0
  152. multipers/point_measure.pyx +322 -320
  153. multipers/simplex_tree_multi.cp310-win_amd64.pyd +0 -0
  154. multipers/simplex_tree_multi.pxd +133 -133
  155. multipers/simplex_tree_multi.pyx +18 -15
  156. multipers/simplex_tree_multi.pyx.tp +1939 -1935
  157. multipers/slicer.cp310-win_amd64.pyd +0 -0
  158. multipers/slicer.pxd +81 -20
  159. multipers/slicer.pxd.tp +215 -214
  160. multipers/slicer.pyx +1091 -308
  161. multipers/slicer.pyx.tp +924 -914
  162. multipers/tensor/tensor.h +672 -672
  163. multipers/tensor.pxd +13 -13
  164. multipers/test.pyx +44 -44
  165. multipers/tests/__init__.py +57 -57
  166. multipers/torch/diff_grids.py +217 -217
  167. multipers/torch/rips_density.py +310 -304
  168. {multipers-2.2.3.dist-info → multipers-2.3.0.dist-info}/LICENSE +21 -21
  169. {multipers-2.2.3.dist-info → multipers-2.3.0.dist-info}/METADATA +21 -11
  170. multipers-2.3.0.dist-info/RECORD +182 -0
  171. multipers/tests/test_diff_helper.py +0 -73
  172. multipers/tests/test_hilbert_function.py +0 -82
  173. multipers/tests/test_mma.py +0 -83
  174. multipers/tests/test_point_clouds.py +0 -49
  175. multipers/tests/test_python-cpp_conversion.py +0 -82
  176. multipers/tests/test_signed_betti.py +0 -181
  177. multipers/tests/test_signed_measure.py +0 -89
  178. multipers/tests/test_simplextreemulti.py +0 -221
  179. multipers/tests/test_slicer.py +0 -221
  180. multipers-2.2.3.dist-info/RECORD +0 -189
  181. {multipers-2.2.3.dist-info → multipers-2.3.0.dist-info}/WHEEL +0 -0
  182. {multipers-2.2.3.dist-info → multipers-2.3.0.dist-info}/top_level.txt +0 -0
@@ -1,769 +1,769 @@
1
- #pragma once
2
-
3
- #include "Simplex_tree_multi_interface.h"
4
- #include "multi_parameter_rank_invariant/persistence_slices.h"
5
- #include "tensor/tensor.h"
6
- #include <algorithm>
7
- #include <gudhi/One_critical_filtration.h>
8
- #include <gudhi/truc.h>
9
- #include <iostream>
10
- #include <limits>
11
- #include <oneapi/tbb/enumerable_thread_specific.h>
12
- #include <oneapi/tbb/parallel_for.h>
13
- #include <ostream>
14
- #include <utility> // std::pair
15
- #include <vector>
16
-
17
- namespace Gudhi {
18
- namespace multiparameter {
19
- namespace hilbert_function {
20
-
21
- // TODO : this function is ugly
22
- template <typename Filtration, typename indices_type>
23
- inline typename Filtration::value_type horizontal_line_filtration2(const Filtration &x,
24
- indices_type height,
25
- indices_type i,
26
- indices_type j,
27
- const std::vector<indices_type> &fixed_values) {
28
- const auto &inf = Filtration::Generator::T_inf;
29
- for (indices_type k = 0u; k < static_cast<indices_type>(x.size()); k++) {
30
- if (k == i || k == j) continue; // coordinate in the plane
31
- if (x[k] > fixed_values[k]) // simplex appears after the plane
32
- return inf;
33
- }
34
- if (x[j] <= height) // simplex apppears in the plane, but is it in the line
35
- // with height "height"
36
- return x[i];
37
- else
38
- return inf;
39
- }
40
-
41
- template <typename index_type>
42
- using hilbert_thread_data = tbb::enumerable_thread_specific<std::pair<Simplex_tree_std, std::vector<index_type>>>;
43
-
44
- template <typename Filtration, typename dtype, typename index_type>
45
- inline void compute_2d_hilbert_surface(
46
- python_interface::Simplex_tree_multi_interface<Filtration> &st_multi,
47
- // Simplex_tree_std &_st,
48
- hilbert_thread_data<index_type> &thread_simplex_tree,
49
- const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
50
- const std::vector<index_type> grid_shape,
51
- const std::vector<index_type> degrees,
52
- index_type i,
53
- index_type j,
54
- const std::vector<index_type> fixed_values,
55
- bool mobius_inverion,
56
- bool zero_pad,
57
- int expand_collapse_dim = 0) {
58
- using value_type = typename Filtration::value_type;
59
- // if (grid_shape.size() < 2 || st_multi.get_number_of_parameters() < 2)
60
- // throw std::invalid_argument("Grid shape has to have at least 2
61
- // element."); if (st_multi.get_number_of_parameters() - fixed_values.size()
62
- // != 2) throw std::invalid_argument("Fix more values for the
63
- // simplextree, which has a too big number of parameters");
64
- // assert(fixed_values.size() == st_multi.get_number_of_parameters());
65
-
66
- constexpr const bool verbose = false;
67
- index_type I = grid_shape[i + 1], J = grid_shape[j + 1];
68
- if constexpr (verbose) std::cout << "Grid shape : " << I << " " << J << std::endl;
69
-
70
- // grid2d out(I, std::vector<int>(J,0)); // zero of good size
71
- // std::vector<std::vector<index_type>> free_coordinates(grid_shape.size());
72
- // for (auto [r,k] = std::views::zip(free_coordinates,
73
- // std::ranges::iota(0,free_coordinates.size()))){ if (k==i) r =
74
- // std::ranges::iota(0,I); else if (k==j) r= std::ranges::iota(0,J);
75
- // else fixed_values[k];
76
- // }
77
- // tensor::static_tensor_view_view<value_type, index_type> dim2_view(out,
78
- // free_coordinates); auto coordinates_container =
79
-
80
- // Simplex_tree_std _st;
81
- // flatten<Simplex_tree_float,
82
- // Gudhi::multi_persistence::Simplex_tree_options_multidimensional_filtration>(_st, st_multi,-1); //
83
- // copies the st_multi to a standard 1-pers simplextree
84
- // tbb::enumerable_thread_specific<std::pair<Simplex_tree_std,
85
- // std::vector<index_type>>> thread_simplex_tree;
86
- tbb::parallel_for(0, J, [&](index_type height) {
87
- // SIMPLEXTREE INIT
88
- auto &[st_std, coordinates_container] = thread_simplex_tree.local();
89
- // if (st_std.num_simplices() == 0){ st_std = _st;}
90
- // COORDINATES INIT
91
- // if (coordinates_container.size() == 0) {
92
- // //This init is fine as only the j+1th coord is touched
93
- // coordinates_container.reserve(fixed_values.size()+1);
94
- // coordinates_container.push_back(0); // degree
95
- // for (auto c : fixed_values) coordinates_container.push_back(c);
96
- // }
97
- // coordinates_container.resize(fixed_values.size()+1); // Not necessary
98
-
99
- // if (coordinates_container.size() != fixed_values.size()+1 ||
100
- // st_std.num_simplices() == 0){ throw std::runtime_error("Bad tbb thread
101
- // local storage initialization.");
102
- // }
103
- // Coordinate initialization to fixed values
104
- // coordinates_container[0] = 0; // not necessary
105
- // for (auto [c, i] : std::views::zip(fixed_values, std::views::iota(0u,
106
- // fixed_values.size()))) // NIK APPLE CLANG coordinates_container[i+1] = c;
107
- for (auto i = 0u; i < fixed_values.size(); i++) coordinates_container[i + 1] = fixed_values[i];
108
-
109
- coordinates_container[j + 1] = height;
110
-
111
- Filtration multi_filtration(st_multi.get_number_of_parameters());
112
- auto sh_standard = st_std.complex_simplex_range().begin();
113
- auto _end = st_std.complex_simplex_range().end();
114
- auto sh_multi = st_multi.complex_simplex_range().begin();
115
- for (; sh_standard != _end; ++sh_multi, ++sh_standard) {
116
- // for (auto [sh_standard, sh_multi] :
117
- // std::ranges::views::zip(st_std.complex_simplex_range(),
118
- // st_multi.complex_simplex_range())){ // too bad apple clang exists
119
- multi_filtration = st_multi.filtration(*sh_multi);
120
- typename Simplex_tree_std::Filtration_value horizontal_filtration;
121
- if constexpr (Filtration::is_multi_critical) {
122
- horizontal_filtration = std::numeric_limits<typename Simplex_tree_std::Filtration_value>::infinity();
123
- for (const auto &stuff : multi_filtration)
124
- horizontal_filtration = std::min(horizontal_filtration,
125
- static_cast<Simplex_tree_std::Filtration_value>(
126
- horizontal_line_filtration2(stuff, height, i, j, fixed_values)));
127
- } else {
128
- horizontal_filtration = horizontal_line_filtration2(multi_filtration, height, i, j, fixed_values);
129
- }
130
- st_std.assign_filtration(*sh_standard, horizontal_filtration);
131
-
132
- if constexpr (verbose) {
133
- Gudhi::multi_filtration::One_critical_filtration<int> splx;
134
- for (auto vertex : st_multi.simplex_vertex_range(*sh_multi)) splx.push_back(vertex);
135
- std::cout << "Simplex " << splx << "/" << st_std.num_simplices() << " Filtration multi "
136
- << st_multi.filtration(*sh_multi) << " Filtration 1d " << st_std.filtration(*sh_standard) << "\n";
137
- }
138
- }
139
-
140
- if constexpr (verbose) {
141
- std::cout << "Coords : " << height << " [";
142
- for (auto stuff : fixed_values) std::cout << stuff << " ";
143
- std::cout << "]" << std::endl;
144
- }
145
- const std::vector<Barcode> barcodes = compute_dgms(st_std, degrees, expand_collapse_dim);
146
- index_type degree_index = 0;
147
- for (const auto &barcode : barcodes) { // TODO range view cartesian product
148
- coordinates_container[0] = degree_index;
149
- for (const auto &bar : barcode) {
150
- auto birth = bar.first; // float
151
- auto death = bar.second;
152
- // if constexpr (verbose) std::cout << "BEFORE " << birth << " " <<
153
- // death << " " << I << " \n"; death = death > I ? I : death; // TODO
154
- // FIXME if constexpr (verbose) std::cout <<"AFTER" << birth << " " <<
155
- // death << " " << I << " \n";
156
- if (birth > I) // some birth can be infinite
157
- continue;
158
-
159
- if (!mobius_inverion) {
160
- // throw std::logic_error("Not implemented");
161
- // death = death > I ? I : death;
162
- // for (int index = static_cast<int>(birth); index <
163
- // static_cast<int>(death); index ++){
164
- // out[degree_index][index][height]++;
165
- // }
166
-
167
- // Seems to bug on linux ????
168
-
169
- coordinates_container[i + 1] = static_cast<index_type>(birth);
170
- index_type shift_value = out.get_cum_resolution()[i + 1];
171
- index_type border = I;
172
- // index_type border = out.get_resolution()[i+1];
173
- dtype *ptr = &out[coordinates_container];
174
- auto stop_value = death > static_cast<value_type>(border) ? border : static_cast<index_type>(death);
175
- // Warning : for some reason linux static casts float inf to -min_int
176
- // so min doesnt work.
177
- if constexpr (verbose) {
178
- std::cout << "Adding : (";
179
- for (auto stuff : coordinates_container) std::cout << stuff << ", ";
180
- std::cout << ") With death " << death << " casted at " << static_cast<index_type>(death)
181
- << "with threshold at" << stop_value << " with " << border << std::endl;
182
- }
183
- for (index_type b = birth; b < stop_value; b++) {
184
- (*ptr)++; // adds one to the vector
185
- ptr += shift_value; // shift the pointer to the next element in the
186
- // segment [birth, death]
187
- }
188
- } else {
189
- // out[degree_index][static_cast<int>(birth)][height]++; // No need to
190
- // do mobius inversion on this axis, it can be done here if (death <
191
- // I) out[degree_index][static_cast<int>(death)][height]--;
192
- // else if (zero_pad)
193
- // {
194
- // out[degree_index].back()[height]--;
195
- // }
196
- // coordinates_container[0] = degree_index;
197
- coordinates_container[i + 1] = static_cast<index_type>(birth);
198
- out[coordinates_container]++;
199
-
200
- if constexpr (verbose) {
201
- std::cout << "Coordinate : ";
202
- for (auto c : coordinates_container) std::cout << c << ", ";
203
- std::cout << std::endl;
204
- std::cout << "axis, death, resolution : " << i + 1 << ", " << std::to_string(death) << ", "
205
- << out.get_resolution()[i + 1];
206
- std::cout << std::endl;
207
- }
208
-
209
- if (death < I) {
210
- coordinates_container[i + 1] = static_cast<index_type>(death);
211
- out[coordinates_container]--;
212
- } else if (zero_pad) {
213
- coordinates_container[i + 1] = I - 1;
214
- out[coordinates_container]--;
215
- }
216
- }
217
- // else
218
- // out[I-1][height]--;
219
- }
220
- degree_index++;
221
- }
222
- });
223
- return;
224
- }
225
-
226
- template <typename Filtration, typename dtype, typename index_type>
227
- void _rec_get_hilbert_surface(python_interface::Simplex_tree_multi_interface<Filtration> &st_multi,
228
- // Simplex_tree_std &_st,
229
- hilbert_thread_data<index_type> &thread_simplex_tree,
230
- const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
231
- const std::vector<index_type> grid_shape,
232
- const std::vector<index_type> degrees,
233
- std::vector<index_type> coordinates_to_compute,
234
- const std::vector<index_type> fixed_values,
235
- bool mobius_inverion = true,
236
- bool zero_pad = false,
237
- int expand_collapse_dim = 0) {
238
- constexpr const bool verbose = false;
239
-
240
- if constexpr (verbose) {
241
- std::cout << "Computing coordinates (";
242
- for (auto c : coordinates_to_compute) std::cout << c << ", ";
243
- std::cout << "). with fixed values (";
244
- for (auto c : fixed_values) {
245
- std::cout << c << ", ";
246
- }
247
- std::cout << ")." << std::endl;
248
- }
249
- if (coordinates_to_compute.size() == 2) {
250
- compute_2d_hilbert_surface(st_multi,
251
- // _st,
252
- thread_simplex_tree,
253
- out, // assumes its a zero tensor
254
- grid_shape,
255
- degrees,
256
- coordinates_to_compute[0],
257
- coordinates_to_compute[1],
258
- fixed_values,
259
- mobius_inverion,
260
- zero_pad,
261
- expand_collapse_dim);
262
- return;
263
- }
264
-
265
- // coordinate to iterate.size --
266
- auto coordinate_to_iterate = coordinates_to_compute.back();
267
- coordinates_to_compute.pop_back();
268
- tbb::parallel_for(0, grid_shape[coordinate_to_iterate + 1], [&](index_type z) {
269
- // Updates fixes values that defines the slice
270
- std::vector<index_type> _fixed_values = fixed_values; // TODO : do not copy this //thread local
271
- _fixed_values[coordinate_to_iterate] = z;
272
- _rec_get_hilbert_surface(st_multi,
273
- thread_simplex_tree,
274
- out,
275
- grid_shape,
276
- degrees,
277
- coordinates_to_compute,
278
- _fixed_values,
279
- mobius_inverion,
280
- zero_pad,
281
- expand_collapse_dim);
282
- });
283
- // rmq : with mobius_inversion + rec, the coordinates to compute size is 2 =>
284
- // first coord is always the initial 1st coord.
285
- // => inversion is only needed for coords > 2
286
- }
287
-
288
- template <typename Filtration, typename dtype, typename index_type>
289
- void get_hilbert_surface(python_interface::Simplex_tree_multi_interface<Filtration> &st_multi,
290
- const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
291
- const std::vector<index_type> &grid_shape,
292
- const std::vector<index_type> &degrees,
293
- std::vector<index_type> coordinates_to_compute,
294
- const std::vector<index_type> &fixed_values,
295
- bool mobius_inverion = true,
296
- bool zero_pad = false,
297
- bool expand_collapse = false) {
298
- if (degrees.size() == 0) return;
299
- // wrapper arount the rec version, that initialize the thread variables.
300
- if (coordinates_to_compute.size() < 2)
301
- throw std::logic_error("Not implemented for " + std::to_string(coordinates_to_compute.size()) + "<2 parameters.");
302
-
303
- Simplex_tree_std _st;
304
- Gudhi::multi_persistence::flatten(_st, st_multi,
305
- -1); // copies the st_multi to a standard 1-pers simplextree
306
- std::vector<index_type> coordinates_container(st_multi.get_number_of_parameters() + 1); // +1 for degree
307
- // coordinates_container.reserve(fixed_values.size()+1);
308
- // coordinates_container.push_back(0); // degree
309
- // for (auto c : fixed_values) coordinates_container.push_back(c);
310
- std::pair<Simplex_tree_std, std::vector<index_type>> thread_data_initialization = {_st, coordinates_container};
311
- const int max_dim = expand_collapse ? *std::max_element(degrees.begin(), degrees.end()) + 1 : 0;
312
- tbb::enumerable_thread_specific<std::pair<Simplex_tree_std, std::vector<index_type>>> thread_simplex_tree(
313
- thread_data_initialization); // this has a fixed size, so init should
314
- // be benefic
315
- _rec_get_hilbert_surface(st_multi,
316
- thread_simplex_tree,
317
- out,
318
- grid_shape,
319
- degrees,
320
- coordinates_to_compute,
321
- fixed_values,
322
- mobius_inverion,
323
- zero_pad,
324
- max_dim);
325
- }
326
-
327
- template <typename Filtration, typename dtype = int, typename indices_type>
328
- std::pair<std::vector<std::vector<indices_type>>, std::vector<dtype>> get_hilbert_signed_measure(
329
- python_interface::Simplex_tree_multi_interface<Filtration> &st_multi,
330
- dtype *data_ptr,
331
- std::vector<indices_type> grid_shape,
332
- const std::vector<indices_type> degrees,
333
- bool zero_pad = false,
334
- indices_type n_jobs = 0,
335
- const bool verbose = false,
336
- const bool expand_collapse = false) {
337
- if (degrees.size() == 0) return {{}, {}};
338
- // const bool verbose = false;
339
- // auto &st_multi =
340
- // get_simplextree_from_pointer<python_interface::interface_multi<Filtration>>(simplextree_ptr);
341
- tensor::static_tensor_view<dtype, indices_type> container(data_ptr, grid_shape); // assumes its a zero tensor
342
- std::vector<indices_type> coordinates_to_compute(st_multi.get_number_of_parameters());
343
- for (auto i = 0u; i < coordinates_to_compute.size(); i++) coordinates_to_compute[i] = i;
344
- // for (auto [c,i] : std::views::zip(coordinates_to_compute,
345
- // std::views::iota(0,st_multi.get_number_of_parameters()))) c=i; // NIK apple
346
- // clang
347
- std::vector<indices_type> fixed_values(st_multi.get_number_of_parameters());
348
-
349
- if (verbose) {
350
- std::cout << "Container shape : ";
351
- for (auto r : container.get_resolution()) std::cout << r << ", ";
352
- std::cout << "\nContainer size : " << container.size();
353
- std::cout << "\nComputing hilbert invariant ..." << std::flush;
354
- }
355
- if (zero_pad) {
356
- // +1 is bc degree is on first axis.
357
- for (auto i = 1; i < st_multi.get_number_of_parameters() + 1; i++)
358
- grid_shape[i]--; // get hilbert surface computes according to grid_shape.
359
- // for (auto i : std::views::iota(1,st_multi.get_number_of_parameters()+1))
360
- // grid_shape[i]--; // get hilbert surface computes according to grid_shape.
361
- }
362
-
363
- oneapi::tbb::task_arena arena(n_jobs); // limits the number of threads
364
- arena.execute([&] {
365
- get_hilbert_surface(st_multi,
366
- container,
367
- grid_shape,
368
- degrees,
369
- coordinates_to_compute,
370
- fixed_values,
371
- true,
372
- zero_pad,
373
- expand_collapse);
374
- });
375
-
376
- if (verbose) {
377
- std::cout << "Done.\n";
378
- std::cout << "Computing mobius inversion ..." << std::flush;
379
- }
380
-
381
- // for (indices_type axis :
382
- // std::views::iota(2,st_multi.get_number_of_parameters()+1)) // +1 for the
383
- // degree in axis 0
384
- for (indices_type axis = 2u; axis < st_multi.get_number_of_parameters() + 1; axis++) container.differentiate(axis);
385
- if (verbose) {
386
- std::cout << "Done.\n";
387
- std::cout << "Sparsifying the measure ..." << std::flush;
388
- }
389
- auto raw_signed_measure = container.sparsify();
390
- if (verbose) {
391
- std::cout << "Done.\n";
392
- }
393
- return raw_signed_measure;
394
- }
395
-
396
- template <typename Filtration, typename dtype, typename indices_type, typename... Args>
397
- void get_hilbert_surface_python(python_interface::Simplex_tree_multi_interface<Filtration> &st_multi,
398
- dtype *data_ptr,
399
- std::vector<indices_type> grid_shape,
400
- const std::vector<indices_type> degrees,
401
- const bool mobius_inversion,
402
- const bool zero_pad,
403
- indices_type n_jobs,
404
- bool expand_collapse) {
405
- constexpr const bool verbose = false;
406
- if (degrees.size() == 0) return;
407
- // const bool verbose = false;
408
- // auto &st_multi =
409
- // get_simplextree_from_pointer<python_interface::interface_multi<Filtration>>(simplextree_ptr);
410
- tensor::static_tensor_view<dtype, indices_type> container(data_ptr, grid_shape); // assumes its a zero tensor
411
- std::vector<indices_type> coordinates_to_compute(st_multi.get_number_of_parameters());
412
- for (auto i = 0u; i < coordinates_to_compute.size(); i++) coordinates_to_compute[i] = i;
413
- // for (auto [c,i] : std::views::zip(coordinates_to_compute,
414
- // std::views::iota(0,st_multi.get_number_of_parameters()))) c=i; // NIK apple
415
- // clang
416
- std::vector<indices_type> fixed_values(st_multi.get_number_of_parameters());
417
-
418
- if (verbose) {
419
- std::cout << "Container shape : ";
420
- for (auto r : container.get_resolution()) std::cout << r << ", ";
421
- std::cout << "\nContainer size : " << container.size();
422
- std::cout << "\nComputing hilbert invariant ...";
423
- }
424
- if (zero_pad) {
425
- // +1 is bc degree is on first axis.
426
- for (auto i = 1; i < st_multi.get_number_of_parameters() + 1; i++)
427
- grid_shape[i]--; // get hilbert surface computes according to grid_shape.
428
- // for (auto i : std::views::iota(1,st_multi.get_number_of_parameters()+1))
429
- // grid_shape[i]--; // get hilbert surface computes according to grid_shape.
430
- }
431
-
432
- oneapi::tbb::task_arena arena(n_jobs); // limits the number of threads
433
- arena.execute([&] {
434
- get_hilbert_surface(st_multi,
435
- container,
436
- grid_shape,
437
- degrees,
438
- coordinates_to_compute,
439
- fixed_values,
440
- mobius_inversion,
441
- zero_pad,
442
- expand_collapse);
443
- });
444
-
445
- if (mobius_inversion)
446
- for (indices_type axis = 2u; axis < st_multi.get_number_of_parameters() + 1; axis++) container.differentiate(axis);
447
- return;
448
- }
449
-
450
- /// FROM SLICER
451
- ///
452
- ///
453
-
454
- template <typename PersBackend, typename Structure, typename Filtration, typename dtype, typename index_type>
455
- inline void compute_2d_hilbert_surface(
456
- tbb::enumerable_thread_specific<std::pair<typename truc_interface::Truc<PersBackend, Structure, Filtration>::ThreadSafe,
457
- std::vector<index_type>>> &thread_stuff,
458
- const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
459
- const std::vector<index_type> grid_shape,
460
- const std::vector<index_type> degrees,
461
- index_type i,
462
- index_type j,
463
- const std::vector<index_type> fixed_values,
464
- const bool mobius_inverion,
465
- const bool zero_pad,
466
- const bool ignore_inf = true ) {
467
- using value_type = typename Filtration::value_type;
468
-
469
- constexpr const bool verbose = false;
470
- index_type I = grid_shape[i + 1], J = grid_shape[j + 1];
471
- if constexpr (verbose) std::cout << "Grid shape : " << I << " " << J << std::endl;
472
- tbb::parallel_for(0, J, [&](index_type height) {
473
- // SIMPLEXTREE INIT
474
- auto &[slicer, coordinates_container] = thread_stuff.local();
475
- for (auto i = 0u; i < fixed_values.size(); i++) coordinates_container[i + 1] = fixed_values[i];
476
-
477
- coordinates_container[j + 1] = height;
478
-
479
- auto &slice_filtration = slicer.get_one_filtration();
480
- const auto &multi_filtration = slicer.get_filtrations();
481
-
482
- for (std::size_t k = 0; k < multi_filtration.size(); k++) {
483
- value_type horizontal_filtration;
484
- if constexpr (Filtration::is_multi_critical) {
485
- horizontal_filtration = Filtration::Generator::T_inf;
486
- for (const auto &stuff : multi_filtration[k])
487
- horizontal_filtration =
488
- std::min(horizontal_filtration,
489
- static_cast<value_type>(horizontal_line_filtration2(stuff, height, i, j, fixed_values)));
490
- } else {
491
- horizontal_filtration = horizontal_line_filtration2(multi_filtration[k], height, i, j, fixed_values);
492
- }
493
- slice_filtration[k] = horizontal_filtration;
494
- }
495
-
496
- if constexpr (verbose) {
497
- std::cout << "Coords : " << height << " [";
498
- for (auto stuff : fixed_values) std::cout << stuff << " ";
499
- std::cout << "]" << std::endl;
500
- }
501
- using bc_type = typename truc_interface::Truc<PersBackend, Structure, Filtration>::split_barcode;
502
- if constexpr (PersBackend::is_vine) {
503
- if (!slicer.has_persistence()) [[unlikely]] {
504
- slicer.compute_persistence();
505
- } else {
506
- slicer.vineyard_update();
507
- }
508
- } else {
509
- slicer.compute_persistence(ignore_inf);
510
- }
511
- bc_type barcodes = slicer.get_barcode();
512
- index_type degree_index = 0;
513
- for (auto degree : degrees) { // TODO range view cartesian product
514
- const auto &barcode = barcodes[degree];
515
- coordinates_container[0] = degree_index;
516
- for (const auto &bar : barcode) {
517
- auto birth = bar.first; // float
518
- auto death = bar.second;
519
- if (birth > I) // some birth can be infinite
520
- continue;
521
-
522
- if (!mobius_inverion) {
523
- coordinates_container[i + 1] = static_cast<index_type>(birth);
524
- index_type shift_value = out.get_cum_resolution()[i + 1];
525
- index_type border = I;
526
- // index_type border = out.get_resolution()[i+1];
527
- dtype *ptr = &out[coordinates_container];
528
- auto stop_value = death > static_cast<value_type>(border) ? border : static_cast<index_type>(death);
529
- // Warning : for some reason linux static casts float inf to -min_int
530
- // so min doesnt work.
531
- if constexpr (verbose) {
532
- std::cout << "Adding : (";
533
- for (auto stuff : coordinates_container) std::cout << stuff << ", ";
534
- std::cout << ") With death " << death << " casted at " << static_cast<index_type>(death)
535
- << "with threshold at" << stop_value << " with " << border << std::endl;
536
- }
537
- for (index_type b = birth; b < stop_value; b++) {
538
- (*ptr)++; // adds one to the vector
539
- ptr += shift_value; // shift the pointer to the next element in the
540
- // segment [birth, death]
541
- }
542
- } else {
543
- coordinates_container[i + 1] = static_cast<index_type>(birth);
544
- out[coordinates_container]++;
545
-
546
- if constexpr (verbose) {
547
- std::cout << "Coordinate : ";
548
- for (auto c : coordinates_container) std::cout << c << ", ";
549
- std::cout << std::endl;
550
- std::cout << "axis, death, resolution : " << i + 1 << ", " << std::to_string(death) << ", "
551
- << out.get_resolution()[i + 1];
552
- std::cout << std::endl;
553
- }
554
-
555
- if (death < I) {
556
- coordinates_container[i + 1] = static_cast<index_type>(death);
557
- out[coordinates_container]--;
558
- } else if (zero_pad) {
559
- coordinates_container[i + 1] = I - 1;
560
- out[coordinates_container]--;
561
- }
562
- }
563
- }
564
- degree_index++;
565
- }
566
- });
567
- return;
568
- }
569
-
570
- template <typename PersBackend, typename Structure, typename Filtration, typename dtype, typename index_type>
571
- void _rec_get_hilbert_surface(
572
- tbb::enumerable_thread_specific<std::pair<typename truc_interface::Truc<PersBackend, Structure, Filtration>::ThreadSafe,
573
- std::vector<index_type>>> &thread_stuff,
574
- const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
575
- const std::vector<index_type> grid_shape,
576
- const std::vector<index_type> degrees,
577
- std::vector<index_type> coordinates_to_compute,
578
- const std::vector<index_type> fixed_values,
579
- const bool mobius_inverion = true,
580
- const bool zero_pad = false,
581
- const bool ignore_inf = true) {
582
- constexpr const bool verbose = false;
583
-
584
- if constexpr (verbose) {
585
- std::cout << "Computing coordinates (";
586
- for (auto c : coordinates_to_compute) std::cout << c << ", ";
587
- std::cout << "). with fixed values (";
588
- for (auto c : fixed_values) {
589
- std::cout << c << ", ";
590
- }
591
- std::cout << ")." << std::endl;
592
- }
593
- if (coordinates_to_compute.size() == 2) {
594
- compute_2d_hilbert_surface<PersBackend, Structure, Filtration, dtype, index_type>(thread_stuff,
595
- out, // assumes its a zero tensor
596
- grid_shape,
597
- degrees,
598
- coordinates_to_compute[0],
599
- coordinates_to_compute[1],
600
- fixed_values,
601
- mobius_inverion,
602
- zero_pad, ignore_inf);
603
- return;
604
- }
605
-
606
- // coordinate to iterate.size --
607
- auto coordinate_to_iterate = coordinates_to_compute.back();
608
- coordinates_to_compute.pop_back();
609
- tbb::parallel_for(0, grid_shape[coordinate_to_iterate + 1], [&](index_type z) {
610
- // Updates fixes values that defines the slice
611
- std::vector<index_type> _fixed_values = fixed_values; // TODO : do not copy this //thread local
612
- _fixed_values[coordinate_to_iterate] = z;
613
- _rec_get_hilbert_surface<PersBackend, Structure, Filtration, dtype, index_type>(
614
- thread_stuff, out, grid_shape, degrees, coordinates_to_compute, _fixed_values, mobius_inverion, zero_pad, ignore_inf);
615
- });
616
- // rmq : with mobius_inversion + rec, the coordinates to compute size is 2 =>
617
- // first coord is always the initial 1st coord.
618
- // => inversion is only needed for coords > 2
619
- }
620
-
621
- template <typename PersBackend, typename Structure, typename Filtration, typename dtype, typename index_type>
622
- void get_hilbert_surface(truc_interface::Truc<PersBackend, Structure, Filtration> &slicer,
623
- const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
624
- const std::vector<index_type> &grid_shape,
625
- const std::vector<index_type> &degrees,
626
- std::vector<index_type> coordinates_to_compute,
627
- const std::vector<index_type> &fixed_values,
628
- const bool mobius_inverion = true,
629
- const bool zero_pad = false,
630
- const bool ignore_inf = true) {
631
-
632
- if (degrees.size() == 0) return;
633
- // wrapper arount the rec version, that initialize the thread variables.
634
- if (coordinates_to_compute.size() < 2)
635
- throw std::logic_error("Not implemented for " + std::to_string(coordinates_to_compute.size()) + "<2 parameters.");
636
- using ThreadSafe = typename truc_interface::Truc<PersBackend, Structure, Filtration>::ThreadSafe;
637
- ThreadSafe slicer_thread(slicer);
638
- std::vector<index_type> coordinates_container(slicer_thread.num_parameters() + 1); // +1 for degree
639
- // coordinates_container.reserve(fixed_values.size()+1);
640
- // coordinates_container.push_back(0); // degree
641
- // for (auto c : fixed_values) coordinates_container.push_back(c);
642
- std::pair<ThreadSafe, std::vector<index_type>> thread_data_initialization = {slicer_thread, coordinates_container};
643
- tbb::enumerable_thread_specific<std::pair<ThreadSafe, std::vector<index_type>>> thread_stuff(
644
- thread_data_initialization); // this has a fixed size, so
645
- // init should be benefic
646
- _rec_get_hilbert_surface<PersBackend, Structure, Filtration, dtype, index_type>(
647
- thread_stuff, out, grid_shape, degrees, coordinates_to_compute, fixed_values, mobius_inverion, zero_pad, ignore_inf);
648
- }
649
-
650
- template <typename PersBackend,
651
- typename Structure,
652
- typename Filtration,
653
- typename dtype,
654
- typename indices_type,
655
- typename... Args>
656
- void get_hilbert_surface_python(truc_interface::Truc<PersBackend, Structure, Filtration> &slicer,
657
- dtype *data_ptr,
658
- std::vector<indices_type> grid_shape,
659
- const std::vector<indices_type> degrees,
660
- const bool mobius_inversion,
661
- const bool zero_pad,
662
- const bool ignore_inf,
663
- indices_type n_jobs) {
664
- const bool verbose = false;
665
- if (degrees.size() == 0) return;
666
- // const bool verbose = false;
667
- // auto &st_multi =
668
- // get_simplextree_from_pointer<python_interface::interface_multi<Filtration>>(simplextree_ptr);
669
- tensor::static_tensor_view<dtype, indices_type> container(data_ptr, grid_shape); // assumes its a zero tensor
670
- int num_parameters = slicer.num_parameters();
671
- std::vector<indices_type> coordinates_to_compute(num_parameters);
672
- for (auto i = 0u; i < coordinates_to_compute.size(); i++) coordinates_to_compute[i] = i;
673
- // for (auto [c,i] : std::views::zip(coordinates_to_compute,
674
- // std::views::iota(0,st_multi.get_number_of_parameters()))) c=i; // NIK apple
675
- // clang
676
- std::vector<indices_type> fixed_values(num_parameters);
677
-
678
- if (verbose) {
679
- std::cout << "Container shape : ";
680
- for (auto r : container.get_resolution()) std::cout << r << ", ";
681
- std::cout << "\nContainer size : " << container.size();
682
- std::cout << "\nComputing hilbert invariant ...";
683
- }
684
- if (zero_pad) {
685
- // +1 is bc degree is on first axis.
686
- for (auto i = 1; i < num_parameters + 1; i++)
687
- grid_shape[i]--; // get hilbert surface computes according to grid_shape.
688
- // for (auto i : std::views::iota(1,st_multi.get_number_of_parameters()+1))
689
- // grid_shape[i]--; // get hilbert surface computes according to grid_shape.
690
- }
691
-
692
- oneapi::tbb::task_arena arena(PersBackend::is_vine ? 1 : n_jobs); // limits the number of threads
693
- arena.execute([&] {
694
- get_hilbert_surface(
695
- slicer, container, grid_shape, degrees, coordinates_to_compute, fixed_values, mobius_inversion, zero_pad, ignore_inf);
696
- });
697
-
698
- if (mobius_inversion)
699
- for (indices_type axis = 2u; axis < num_parameters + 1; axis++) container.differentiate(axis);
700
- return;
701
- }
702
-
703
- template <typename PersBackend,
704
- typename Structure,
705
- typename Filtration,
706
- typename dtype,
707
- typename indices_type,
708
- typename... Args>
709
- std::pair<std::vector<std::vector<indices_type>>, std::vector<dtype>> get_hilbert_signed_measure(
710
- truc_interface::Truc<PersBackend, Structure, Filtration> &slicer,
711
- dtype *data_ptr,
712
- std::vector<indices_type> grid_shape,
713
- const std::vector<indices_type> degrees,
714
- bool zero_pad = false,
715
- indices_type n_jobs = 0,
716
- const bool verbose = false,
717
- const bool ignore_inf = true){
718
- if (degrees.size() == 0) return {{}, {}};
719
- // const bool verbose = false;
720
- // auto &st_multi =
721
- // get_simplextree_from_pointer<python_interface::interface_multi<Filtration>>(simplextree_ptr);
722
- tensor::static_tensor_view<dtype, indices_type> container(data_ptr, grid_shape); // assumes its a zero tensor
723
- std::vector<indices_type> coordinates_to_compute(slicer.num_parameters());
724
- for (auto i = 0u; i < coordinates_to_compute.size(); i++) coordinates_to_compute[i] = i;
725
- // for (auto [c,i] : std::views::zip(coordinates_to_compute,
726
- // std::views::iota(0,st_multi.get_number_of_parameters()))) c=i; // NIK apple
727
- // clang
728
- std::vector<indices_type> fixed_values(slicer.num_parameters());
729
-
730
- if (verbose) {
731
- std::cout << "Container shape : ";
732
- for (auto r : container.get_resolution()) std::cout << r << ", ";
733
- std::cout << "\nContainer size : " << container.size();
734
- std::cout << "\nComputing hilbert invariant ..." << std::flush;
735
- }
736
- if (zero_pad) {
737
- // +1 is bc degree is on first axis.
738
- for (auto i = 1u; i < slicer.num_parameters() + 1; i++)
739
- grid_shape[i]--; // get hilbert surface computes according to grid_shape.
740
- // for (auto i : std::views::iota(1,st_multi.get_number_of_parameters()+1))
741
- // grid_shape[i]--; // get hilbert surface computes according to grid_shape.
742
- }
743
-
744
- oneapi::tbb::task_arena arena(PersBackend::is_vine ? 1 : n_jobs); // limits the number of threads
745
- arena.execute([&] {
746
- get_hilbert_surface(slicer, container, grid_shape, degrees, coordinates_to_compute, fixed_values, true, zero_pad, ignore_inf);
747
- });
748
-
749
- if (verbose) {
750
- std::cout << "Done.\n";
751
- std::cout << "Computing mobius inversion ..." << std::flush;
752
- }
753
-
754
- // for (indices_type axis :
755
- // std::views::iota(2,st_multi.get_number_of_parameters()+1)) // +1 for the
756
- // degree in axis 0
757
- for (indices_type axis = 2; axis < static_cast<indices_type>(slicer.num_parameters() + 1); axis++)
758
- container.differentiate(axis);
759
- if (verbose) {
760
- std::cout << "Done.\n";
761
- std::cout << "Sparsifying the measure ..." << std::flush;
762
- }
763
- auto raw_signed_measure = container.sparsify();
764
- if (verbose) {
765
- std::cout << "Done." << std::endl;
766
- }
767
- return raw_signed_measure;
768
- }
769
- }}} // namespace Gudhi::multiparameter::hilbert_function
1
+ #pragma once
2
+
3
+ #include "Simplex_tree_multi_interface.h"
4
+ #include "multi_parameter_rank_invariant/persistence_slices.h"
5
+ #include "tensor/tensor.h"
6
+ #include <algorithm>
7
+ #include <gudhi/One_critical_filtration.h>
8
+ #include <gudhi/truc.h>
9
+ #include <iostream>
10
+ #include <limits>
11
+ #include <oneapi/tbb/enumerable_thread_specific.h>
12
+ #include <oneapi/tbb/parallel_for.h>
13
+ #include <ostream>
14
+ #include <utility> // std::pair
15
+ #include <vector>
16
+
17
+ namespace Gudhi {
18
+ namespace multiparameter {
19
+ namespace hilbert_function {
20
+
21
+ // TODO : this function is ugly
22
+ template <typename Filtration, typename indices_type>
23
+ inline typename Filtration::value_type horizontal_line_filtration2(const Filtration &x,
24
+ indices_type height,
25
+ indices_type i,
26
+ indices_type j,
27
+ const std::vector<indices_type> &fixed_values) {
28
+ const auto &inf = Filtration::Generator::T_inf;
29
+ for (indices_type k = 0u; k < static_cast<indices_type>(x.size()); k++) {
30
+ if (k == i || k == j) continue; // coordinate in the plane
31
+ if (x[k] > fixed_values[k]) // simplex appears after the plane
32
+ return inf;
33
+ }
34
+ if (x[j] <= height) // simplex apppears in the plane, but is it in the line
35
+ // with height "height"
36
+ return x[i];
37
+ else
38
+ return inf;
39
+ }
40
+
41
+ template <typename index_type>
42
+ using hilbert_thread_data = tbb::enumerable_thread_specific<std::pair<Simplex_tree_std, std::vector<index_type>>>;
43
+
44
+ template <typename Filtration, typename dtype, typename index_type>
45
+ inline void compute_2d_hilbert_surface(
46
+ python_interface::Simplex_tree_multi_interface<Filtration> &st_multi,
47
+ // Simplex_tree_std &_st,
48
+ hilbert_thread_data<index_type> &thread_simplex_tree,
49
+ const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
50
+ const std::vector<index_type> grid_shape,
51
+ const std::vector<index_type> degrees,
52
+ index_type i,
53
+ index_type j,
54
+ const std::vector<index_type> fixed_values,
55
+ bool mobius_inverion,
56
+ bool zero_pad,
57
+ int expand_collapse_dim = 0) {
58
+ using value_type = typename Filtration::value_type;
59
+ // if (grid_shape.size() < 2 || st_multi.get_number_of_parameters() < 2)
60
+ // throw std::invalid_argument("Grid shape has to have at least 2
61
+ // element."); if (st_multi.get_number_of_parameters() - fixed_values.size()
62
+ // != 2) throw std::invalid_argument("Fix more values for the
63
+ // simplextree, which has a too big number of parameters");
64
+ // assert(fixed_values.size() == st_multi.get_number_of_parameters());
65
+
66
+ constexpr const bool verbose = false;
67
+ index_type I = grid_shape[i + 1], J = grid_shape[j + 1];
68
+ if constexpr (verbose) std::cout << "Grid shape : " << I << " " << J << std::endl;
69
+
70
+ // grid2d out(I, std::vector<int>(J,0)); // zero of good size
71
+ // std::vector<std::vector<index_type>> free_coordinates(grid_shape.size());
72
+ // for (auto [r,k] = std::views::zip(free_coordinates,
73
+ // std::ranges::iota(0,free_coordinates.size()))){ if (k==i) r =
74
+ // std::ranges::iota(0,I); else if (k==j) r= std::ranges::iota(0,J);
75
+ // else fixed_values[k];
76
+ // }
77
+ // tensor::static_tensor_view_view<value_type, index_type> dim2_view(out,
78
+ // free_coordinates); auto coordinates_container =
79
+
80
+ // Simplex_tree_std _st;
81
+ // flatten<Simplex_tree_float,
82
+ // Gudhi::multi_persistence::Simplex_tree_options_multidimensional_filtration>(_st, st_multi,-1); //
83
+ // copies the st_multi to a standard 1-pers simplextree
84
+ // tbb::enumerable_thread_specific<std::pair<Simplex_tree_std,
85
+ // std::vector<index_type>>> thread_simplex_tree;
86
+ tbb::parallel_for(0, J, [&](index_type height) {
87
+ // SIMPLEXTREE INIT
88
+ auto &[st_std, coordinates_container] = thread_simplex_tree.local();
89
+ // if (st_std.num_simplices() == 0){ st_std = _st;}
90
+ // COORDINATES INIT
91
+ // if (coordinates_container.size() == 0) {
92
+ // //This init is fine as only the j+1th coord is touched
93
+ // coordinates_container.reserve(fixed_values.size()+1);
94
+ // coordinates_container.push_back(0); // degree
95
+ // for (auto c : fixed_values) coordinates_container.push_back(c);
96
+ // }
97
+ // coordinates_container.resize(fixed_values.size()+1); // Not necessary
98
+
99
+ // if (coordinates_container.size() != fixed_values.size()+1 ||
100
+ // st_std.num_simplices() == 0){ throw std::runtime_error("Bad tbb thread
101
+ // local storage initialization.");
102
+ // }
103
+ // Coordinate initialization to fixed values
104
+ // coordinates_container[0] = 0; // not necessary
105
+ // for (auto [c, i] : std::views::zip(fixed_values, std::views::iota(0u,
106
+ // fixed_values.size()))) // NIK APPLE CLANG coordinates_container[i+1] = c;
107
+ for (auto i = 0u; i < fixed_values.size(); i++) coordinates_container[i + 1] = fixed_values[i];
108
+
109
+ coordinates_container[j + 1] = height;
110
+
111
+ Filtration multi_filtration(st_multi.get_number_of_parameters());
112
+ auto sh_standard = st_std.complex_simplex_range().begin();
113
+ auto _end = st_std.complex_simplex_range().end();
114
+ auto sh_multi = st_multi.complex_simplex_range().begin();
115
+ for (; sh_standard != _end; ++sh_multi, ++sh_standard) {
116
+ // for (auto [sh_standard, sh_multi] :
117
+ // std::ranges::views::zip(st_std.complex_simplex_range(),
118
+ // st_multi.complex_simplex_range())){ // too bad apple clang exists
119
+ multi_filtration = st_multi.filtration(*sh_multi);
120
+ typename Simplex_tree_std::Filtration_value horizontal_filtration;
121
+ if constexpr (Filtration::is_multi_critical) {
122
+ horizontal_filtration = std::numeric_limits<typename Simplex_tree_std::Filtration_value>::infinity();
123
+ for (const auto &stuff : multi_filtration)
124
+ horizontal_filtration = std::min(horizontal_filtration,
125
+ static_cast<Simplex_tree_std::Filtration_value>(
126
+ horizontal_line_filtration2(stuff, height, i, j, fixed_values)));
127
+ } else {
128
+ horizontal_filtration = horizontal_line_filtration2(multi_filtration, height, i, j, fixed_values);
129
+ }
130
+ st_std.assign_filtration(*sh_standard, horizontal_filtration);
131
+
132
+ if constexpr (verbose) {
133
+ Gudhi::multi_filtration::One_critical_filtration<int> splx;
134
+ for (auto vertex : st_multi.simplex_vertex_range(*sh_multi)) splx.push_back(vertex);
135
+ std::cout << "Simplex " << splx << "/" << st_std.num_simplices() << " Filtration multi "
136
+ << st_multi.filtration(*sh_multi) << " Filtration 1d " << st_std.filtration(*sh_standard) << "\n";
137
+ }
138
+ }
139
+
140
+ if constexpr (verbose) {
141
+ std::cout << "Coords : " << height << " [";
142
+ for (auto stuff : fixed_values) std::cout << stuff << " ";
143
+ std::cout << "]" << std::endl;
144
+ }
145
+ const std::vector<Barcode> barcodes = compute_dgms(st_std, degrees, expand_collapse_dim);
146
+ index_type degree_index = 0;
147
+ for (const auto &barcode : barcodes) { // TODO range view cartesian product
148
+ coordinates_container[0] = degree_index;
149
+ for (const auto &bar : barcode) {
150
+ auto birth = bar.first; // float
151
+ auto death = bar.second;
152
+ // if constexpr (verbose) std::cout << "BEFORE " << birth << " " <<
153
+ // death << " " << I << " \n"; death = death > I ? I : death; // TODO
154
+ // FIXME if constexpr (verbose) std::cout <<"AFTER" << birth << " " <<
155
+ // death << " " << I << " \n";
156
+ if (birth > I) // some birth can be infinite
157
+ continue;
158
+
159
+ if (!mobius_inverion) {
160
+ // throw std::logic_error("Not implemented");
161
+ // death = death > I ? I : death;
162
+ // for (int index = static_cast<int>(birth); index <
163
+ // static_cast<int>(death); index ++){
164
+ // out[degree_index][index][height]++;
165
+ // }
166
+
167
+ // Seems to bug on linux ????
168
+
169
+ coordinates_container[i + 1] = static_cast<index_type>(birth);
170
+ index_type shift_value = out.get_cum_resolution()[i + 1];
171
+ index_type border = I;
172
+ // index_type border = out.get_resolution()[i+1];
173
+ dtype *ptr = &out[coordinates_container];
174
+ auto stop_value = death > static_cast<value_type>(border) ? border : static_cast<index_type>(death);
175
+ // Warning : for some reason linux static casts float inf to -min_int
176
+ // so min doesnt work.
177
+ if constexpr (verbose) {
178
+ std::cout << "Adding : (";
179
+ for (auto stuff : coordinates_container) std::cout << stuff << ", ";
180
+ std::cout << ") With death " << death << " casted at " << static_cast<index_type>(death)
181
+ << "with threshold at" << stop_value << " with " << border << std::endl;
182
+ }
183
+ for (index_type b = birth; b < stop_value; b++) {
184
+ (*ptr)++; // adds one to the vector
185
+ ptr += shift_value; // shift the pointer to the next element in the
186
+ // segment [birth, death]
187
+ }
188
+ } else {
189
+ // out[degree_index][static_cast<int>(birth)][height]++; // No need to
190
+ // do mobius inversion on this axis, it can be done here if (death <
191
+ // I) out[degree_index][static_cast<int>(death)][height]--;
192
+ // else if (zero_pad)
193
+ // {
194
+ // out[degree_index].back()[height]--;
195
+ // }
196
+ // coordinates_container[0] = degree_index;
197
+ coordinates_container[i + 1] = static_cast<index_type>(birth);
198
+ out[coordinates_container]++;
199
+
200
+ if constexpr (verbose) {
201
+ std::cout << "Coordinate : ";
202
+ for (auto c : coordinates_container) std::cout << c << ", ";
203
+ std::cout << std::endl;
204
+ std::cout << "axis, death, resolution : " << i + 1 << ", " << std::to_string(death) << ", "
205
+ << out.get_resolution()[i + 1];
206
+ std::cout << std::endl;
207
+ }
208
+
209
+ if (death < I) {
210
+ coordinates_container[i + 1] = static_cast<index_type>(death);
211
+ out[coordinates_container]--;
212
+ } else if (zero_pad) {
213
+ coordinates_container[i + 1] = I - 1;
214
+ out[coordinates_container]--;
215
+ }
216
+ }
217
+ // else
218
+ // out[I-1][height]--;
219
+ }
220
+ degree_index++;
221
+ }
222
+ });
223
+ return;
224
+ }
225
+
226
+ template <typename Filtration, typename dtype, typename index_type>
227
+ void _rec_get_hilbert_surface(python_interface::Simplex_tree_multi_interface<Filtration> &st_multi,
228
+ // Simplex_tree_std &_st,
229
+ hilbert_thread_data<index_type> &thread_simplex_tree,
230
+ const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
231
+ const std::vector<index_type> grid_shape,
232
+ const std::vector<index_type> degrees,
233
+ std::vector<index_type> coordinates_to_compute,
234
+ const std::vector<index_type> fixed_values,
235
+ bool mobius_inverion = true,
236
+ bool zero_pad = false,
237
+ int expand_collapse_dim = 0) {
238
+ constexpr const bool verbose = false;
239
+
240
+ if constexpr (verbose) {
241
+ std::cout << "Computing coordinates (";
242
+ for (auto c : coordinates_to_compute) std::cout << c << ", ";
243
+ std::cout << "). with fixed values (";
244
+ for (auto c : fixed_values) {
245
+ std::cout << c << ", ";
246
+ }
247
+ std::cout << ")." << std::endl;
248
+ }
249
+ if (coordinates_to_compute.size() == 2) {
250
+ compute_2d_hilbert_surface(st_multi,
251
+ // _st,
252
+ thread_simplex_tree,
253
+ out, // assumes its a zero tensor
254
+ grid_shape,
255
+ degrees,
256
+ coordinates_to_compute[0],
257
+ coordinates_to_compute[1],
258
+ fixed_values,
259
+ mobius_inverion,
260
+ zero_pad,
261
+ expand_collapse_dim);
262
+ return;
263
+ }
264
+
265
+ // coordinate to iterate.size --
266
+ auto coordinate_to_iterate = coordinates_to_compute.back();
267
+ coordinates_to_compute.pop_back();
268
+ tbb::parallel_for(0, grid_shape[coordinate_to_iterate + 1], [&](index_type z) {
269
+ // Updates fixes values that defines the slice
270
+ std::vector<index_type> _fixed_values = fixed_values; // TODO : do not copy this //thread local
271
+ _fixed_values[coordinate_to_iterate] = z;
272
+ _rec_get_hilbert_surface(st_multi,
273
+ thread_simplex_tree,
274
+ out,
275
+ grid_shape,
276
+ degrees,
277
+ coordinates_to_compute,
278
+ _fixed_values,
279
+ mobius_inverion,
280
+ zero_pad,
281
+ expand_collapse_dim);
282
+ });
283
+ // rmq : with mobius_inversion + rec, the coordinates to compute size is 2 =>
284
+ // first coord is always the initial 1st coord.
285
+ // => inversion is only needed for coords > 2
286
+ }
287
+
288
+ template <typename Filtration, typename dtype, typename index_type>
289
+ void get_hilbert_surface(python_interface::Simplex_tree_multi_interface<Filtration> &st_multi,
290
+ const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
291
+ const std::vector<index_type> &grid_shape,
292
+ const std::vector<index_type> &degrees,
293
+ std::vector<index_type> coordinates_to_compute,
294
+ const std::vector<index_type> &fixed_values,
295
+ bool mobius_inverion = true,
296
+ bool zero_pad = false,
297
+ bool expand_collapse = false) {
298
+ if (degrees.size() == 0) return;
299
+ // wrapper arount the rec version, that initialize the thread variables.
300
+ if (coordinates_to_compute.size() < 2)
301
+ throw std::logic_error("Not implemented for " + std::to_string(coordinates_to_compute.size()) + "<2 parameters.");
302
+
303
+ Simplex_tree_std _st;
304
+ Gudhi::multi_persistence::flatten(_st, st_multi,
305
+ -1); // copies the st_multi to a standard 1-pers simplextree
306
+ std::vector<index_type> coordinates_container(st_multi.get_number_of_parameters() + 1); // +1 for degree
307
+ // coordinates_container.reserve(fixed_values.size()+1);
308
+ // coordinates_container.push_back(0); // degree
309
+ // for (auto c : fixed_values) coordinates_container.push_back(c);
310
+ std::pair<Simplex_tree_std, std::vector<index_type>> thread_data_initialization = {_st, coordinates_container};
311
+ const int max_dim = expand_collapse ? *std::max_element(degrees.begin(), degrees.end()) + 1 : 0;
312
+ tbb::enumerable_thread_specific<std::pair<Simplex_tree_std, std::vector<index_type>>> thread_simplex_tree(
313
+ thread_data_initialization); // this has a fixed size, so init should
314
+ // be benefic
315
+ _rec_get_hilbert_surface(st_multi,
316
+ thread_simplex_tree,
317
+ out,
318
+ grid_shape,
319
+ degrees,
320
+ coordinates_to_compute,
321
+ fixed_values,
322
+ mobius_inverion,
323
+ zero_pad,
324
+ max_dim);
325
+ }
326
+
327
+ template <typename Filtration, typename dtype = int, typename indices_type>
328
+ std::pair<std::vector<std::vector<indices_type>>, std::vector<dtype>> get_hilbert_signed_measure(
329
+ python_interface::Simplex_tree_multi_interface<Filtration> &st_multi,
330
+ dtype *data_ptr,
331
+ std::vector<indices_type> grid_shape,
332
+ const std::vector<indices_type> degrees,
333
+ bool zero_pad = false,
334
+ indices_type n_jobs = 0,
335
+ const bool verbose = false,
336
+ const bool expand_collapse = false) {
337
+ if (degrees.size() == 0) return {{}, {}};
338
+ // const bool verbose = false;
339
+ // auto &st_multi =
340
+ // get_simplextree_from_pointer<python_interface::interface_multi<Filtration>>(simplextree_ptr);
341
+ tensor::static_tensor_view<dtype, indices_type> container(data_ptr, grid_shape); // assumes its a zero tensor
342
+ std::vector<indices_type> coordinates_to_compute(st_multi.get_number_of_parameters());
343
+ for (auto i = 0u; i < coordinates_to_compute.size(); i++) coordinates_to_compute[i] = i;
344
+ // for (auto [c,i] : std::views::zip(coordinates_to_compute,
345
+ // std::views::iota(0,st_multi.get_number_of_parameters()))) c=i; // NIK apple
346
+ // clang
347
+ std::vector<indices_type> fixed_values(st_multi.get_number_of_parameters());
348
+
349
+ if (verbose) {
350
+ std::cout << "Container shape : ";
351
+ for (auto r : container.get_resolution()) std::cout << r << ", ";
352
+ std::cout << "\nContainer size : " << container.size();
353
+ std::cout << "\nComputing hilbert invariant ..." << std::flush;
354
+ }
355
+ if (zero_pad) {
356
+ // +1 is bc degree is on first axis.
357
+ for (auto i = 1; i < st_multi.get_number_of_parameters() + 1; i++)
358
+ grid_shape[i]--; // get hilbert surface computes according to grid_shape.
359
+ // for (auto i : std::views::iota(1,st_multi.get_number_of_parameters()+1))
360
+ // grid_shape[i]--; // get hilbert surface computes according to grid_shape.
361
+ }
362
+
363
+ oneapi::tbb::task_arena arena(n_jobs); // limits the number of threads
364
+ arena.execute([&] {
365
+ get_hilbert_surface(st_multi,
366
+ container,
367
+ grid_shape,
368
+ degrees,
369
+ coordinates_to_compute,
370
+ fixed_values,
371
+ true,
372
+ zero_pad,
373
+ expand_collapse);
374
+ });
375
+
376
+ if (verbose) {
377
+ std::cout << "Done.\n";
378
+ std::cout << "Computing mobius inversion ..." << std::flush;
379
+ }
380
+
381
+ // for (indices_type axis :
382
+ // std::views::iota(2,st_multi.get_number_of_parameters()+1)) // +1 for the
383
+ // degree in axis 0
384
+ for (indices_type axis = 2u; axis < st_multi.get_number_of_parameters() + 1; axis++) container.differentiate(axis);
385
+ if (verbose) {
386
+ std::cout << "Done.\n";
387
+ std::cout << "Sparsifying the measure ..." << std::flush;
388
+ }
389
+ auto raw_signed_measure = container.sparsify();
390
+ if (verbose) {
391
+ std::cout << "Done.\n";
392
+ }
393
+ return raw_signed_measure;
394
+ }
395
+
396
+ template <typename Filtration, typename dtype, typename indices_type, typename... Args>
397
+ void get_hilbert_surface_python(python_interface::Simplex_tree_multi_interface<Filtration> &st_multi,
398
+ dtype *data_ptr,
399
+ std::vector<indices_type> grid_shape,
400
+ const std::vector<indices_type> degrees,
401
+ const bool mobius_inversion,
402
+ const bool zero_pad,
403
+ indices_type n_jobs,
404
+ bool expand_collapse) {
405
+ constexpr const bool verbose = false;
406
+ if (degrees.size() == 0) return;
407
+ // const bool verbose = false;
408
+ // auto &st_multi =
409
+ // get_simplextree_from_pointer<python_interface::interface_multi<Filtration>>(simplextree_ptr);
410
+ tensor::static_tensor_view<dtype, indices_type> container(data_ptr, grid_shape); // assumes its a zero tensor
411
+ std::vector<indices_type> coordinates_to_compute(st_multi.get_number_of_parameters());
412
+ for (auto i = 0u; i < coordinates_to_compute.size(); i++) coordinates_to_compute[i] = i;
413
+ // for (auto [c,i] : std::views::zip(coordinates_to_compute,
414
+ // std::views::iota(0,st_multi.get_number_of_parameters()))) c=i; // NIK apple
415
+ // clang
416
+ std::vector<indices_type> fixed_values(st_multi.get_number_of_parameters());
417
+
418
+ if (verbose) {
419
+ std::cout << "Container shape : ";
420
+ for (auto r : container.get_resolution()) std::cout << r << ", ";
421
+ std::cout << "\nContainer size : " << container.size();
422
+ std::cout << "\nComputing hilbert invariant ...";
423
+ }
424
+ if (zero_pad) {
425
+ // +1 is bc degree is on first axis.
426
+ for (auto i = 1; i < st_multi.get_number_of_parameters() + 1; i++)
427
+ grid_shape[i]--; // get hilbert surface computes according to grid_shape.
428
+ // for (auto i : std::views::iota(1,st_multi.get_number_of_parameters()+1))
429
+ // grid_shape[i]--; // get hilbert surface computes according to grid_shape.
430
+ }
431
+
432
+ oneapi::tbb::task_arena arena(n_jobs); // limits the number of threads
433
+ arena.execute([&] {
434
+ get_hilbert_surface(st_multi,
435
+ container,
436
+ grid_shape,
437
+ degrees,
438
+ coordinates_to_compute,
439
+ fixed_values,
440
+ mobius_inversion,
441
+ zero_pad,
442
+ expand_collapse);
443
+ });
444
+
445
+ if (mobius_inversion)
446
+ for (indices_type axis = 2u; axis < st_multi.get_number_of_parameters() + 1; axis++) container.differentiate(axis);
447
+ return;
448
+ }
449
+
450
+ /// FROM SLICER
451
+ ///
452
+ ///
453
+
454
+ template <typename PersBackend, typename Structure, typename Filtration, typename dtype, typename index_type>
455
+ inline void compute_2d_hilbert_surface(
456
+ tbb::enumerable_thread_specific<std::pair<typename truc_interface::Truc<PersBackend, Structure, Filtration>::ThreadSafe,
457
+ std::vector<index_type>>> &thread_stuff,
458
+ const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
459
+ const std::vector<index_type> grid_shape,
460
+ const std::vector<index_type> degrees,
461
+ index_type i,
462
+ index_type j,
463
+ const std::vector<index_type> fixed_values,
464
+ const bool mobius_inverion,
465
+ const bool zero_pad,
466
+ const bool ignore_inf = true ) {
467
+ using value_type = typename Filtration::value_type;
468
+
469
+ constexpr const bool verbose = false;
470
+ index_type I = grid_shape[i + 1], J = grid_shape[j + 1];
471
+ if constexpr (verbose) std::cout << "Grid shape : " << I << " " << J << std::endl;
472
+ tbb::parallel_for(0, J, [&](index_type height) {
473
+ // SIMPLEXTREE INIT
474
+ auto &[slicer, coordinates_container] = thread_stuff.local();
475
+ for (auto i = 0u; i < fixed_values.size(); i++) coordinates_container[i + 1] = fixed_values[i];
476
+
477
+ coordinates_container[j + 1] = height;
478
+
479
+ auto &slice_filtration = slicer.get_one_filtration();
480
+ const auto &multi_filtration = slicer.get_filtrations();
481
+
482
+ for (std::size_t k = 0; k < multi_filtration.size(); k++) {
483
+ value_type horizontal_filtration;
484
+ if constexpr (Filtration::is_multi_critical) {
485
+ horizontal_filtration = Filtration::Generator::T_inf;
486
+ for (const auto &stuff : multi_filtration[k])
487
+ horizontal_filtration =
488
+ std::min(horizontal_filtration,
489
+ static_cast<value_type>(horizontal_line_filtration2(stuff, height, i, j, fixed_values)));
490
+ } else {
491
+ horizontal_filtration = horizontal_line_filtration2(multi_filtration[k], height, i, j, fixed_values);
492
+ }
493
+ slice_filtration[k] = horizontal_filtration;
494
+ }
495
+
496
+ if constexpr (verbose) {
497
+ std::cout << "Coords : " << height << " [";
498
+ for (auto stuff : fixed_values) std::cout << stuff << " ";
499
+ std::cout << "]" << std::endl;
500
+ }
501
+ using bc_type = typename truc_interface::Truc<PersBackend, Structure, Filtration>::split_barcode;
502
+ if constexpr (PersBackend::is_vine) {
503
+ if (!slicer.has_persistence()) [[unlikely]] {
504
+ slicer.compute_persistence();
505
+ } else {
506
+ slicer.vineyard_update();
507
+ }
508
+ } else {
509
+ slicer.compute_persistence(ignore_inf);
510
+ }
511
+ bc_type barcodes = slicer.get_barcode();
512
+ index_type degree_index = 0;
513
+ for (auto degree : degrees) { // TODO range view cartesian product
514
+ const auto &barcode = barcodes[degree];
515
+ coordinates_container[0] = degree_index;
516
+ for (const auto &bar : barcode) {
517
+ auto birth = bar.first; // float
518
+ auto death = bar.second;
519
+ if (birth > I) // some birth can be infinite
520
+ continue;
521
+
522
+ if (!mobius_inverion) {
523
+ coordinates_container[i + 1] = static_cast<index_type>(birth);
524
+ index_type shift_value = out.get_cum_resolution()[i + 1];
525
+ index_type border = I;
526
+ // index_type border = out.get_resolution()[i+1];
527
+ dtype *ptr = &out[coordinates_container];
528
+ auto stop_value = death > static_cast<value_type>(border) ? border : static_cast<index_type>(death);
529
+ // Warning : for some reason linux static casts float inf to -min_int
530
+ // so min doesnt work.
531
+ if constexpr (verbose) {
532
+ std::cout << "Adding : (";
533
+ for (auto stuff : coordinates_container) std::cout << stuff << ", ";
534
+ std::cout << ") With death " << death << " casted at " << static_cast<index_type>(death)
535
+ << "with threshold at" << stop_value << " with " << border << std::endl;
536
+ }
537
+ for (index_type b = birth; b < stop_value; b++) {
538
+ (*ptr)++; // adds one to the vector
539
+ ptr += shift_value; // shift the pointer to the next element in the
540
+ // segment [birth, death]
541
+ }
542
+ } else {
543
+ coordinates_container[i + 1] = static_cast<index_type>(birth);
544
+ out[coordinates_container]++;
545
+
546
+ if constexpr (verbose) {
547
+ std::cout << "Coordinate : ";
548
+ for (auto c : coordinates_container) std::cout << c << ", ";
549
+ std::cout << std::endl;
550
+ std::cout << "axis, death, resolution : " << i + 1 << ", " << std::to_string(death) << ", "
551
+ << out.get_resolution()[i + 1];
552
+ std::cout << std::endl;
553
+ }
554
+
555
+ if (death < I) {
556
+ coordinates_container[i + 1] = static_cast<index_type>(death);
557
+ out[coordinates_container]--;
558
+ } else if (zero_pad) {
559
+ coordinates_container[i + 1] = I - 1;
560
+ out[coordinates_container]--;
561
+ }
562
+ }
563
+ }
564
+ degree_index++;
565
+ }
566
+ });
567
+ return;
568
+ }
569
+
570
+ template <typename PersBackend, typename Structure, typename Filtration, typename dtype, typename index_type>
571
+ void _rec_get_hilbert_surface(
572
+ tbb::enumerable_thread_specific<std::pair<typename truc_interface::Truc<PersBackend, Structure, Filtration>::ThreadSafe,
573
+ std::vector<index_type>>> &thread_stuff,
574
+ const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
575
+ const std::vector<index_type> grid_shape,
576
+ const std::vector<index_type> degrees,
577
+ std::vector<index_type> coordinates_to_compute,
578
+ const std::vector<index_type> fixed_values,
579
+ const bool mobius_inverion = true,
580
+ const bool zero_pad = false,
581
+ const bool ignore_inf = true) {
582
+ constexpr const bool verbose = false;
583
+
584
+ if constexpr (verbose) {
585
+ std::cout << "Computing coordinates (";
586
+ for (auto c : coordinates_to_compute) std::cout << c << ", ";
587
+ std::cout << "). with fixed values (";
588
+ for (auto c : fixed_values) {
589
+ std::cout << c << ", ";
590
+ }
591
+ std::cout << ")." << std::endl;
592
+ }
593
+ if (coordinates_to_compute.size() == 2) {
594
+ compute_2d_hilbert_surface<PersBackend, Structure, Filtration, dtype, index_type>(thread_stuff,
595
+ out, // assumes its a zero tensor
596
+ grid_shape,
597
+ degrees,
598
+ coordinates_to_compute[0],
599
+ coordinates_to_compute[1],
600
+ fixed_values,
601
+ mobius_inverion,
602
+ zero_pad, ignore_inf);
603
+ return;
604
+ }
605
+
606
+ // coordinate to iterate.size --
607
+ auto coordinate_to_iterate = coordinates_to_compute.back();
608
+ coordinates_to_compute.pop_back();
609
+ tbb::parallel_for(0, grid_shape[coordinate_to_iterate + 1], [&](index_type z) {
610
+ // Updates fixes values that defines the slice
611
+ std::vector<index_type> _fixed_values = fixed_values; // TODO : do not copy this //thread local
612
+ _fixed_values[coordinate_to_iterate] = z;
613
+ _rec_get_hilbert_surface<PersBackend, Structure, Filtration, dtype, index_type>(
614
+ thread_stuff, out, grid_shape, degrees, coordinates_to_compute, _fixed_values, mobius_inverion, zero_pad, ignore_inf);
615
+ });
616
+ // rmq : with mobius_inversion + rec, the coordinates to compute size is 2 =>
617
+ // first coord is always the initial 1st coord.
618
+ // => inversion is only needed for coords > 2
619
+ }
620
+
621
+ template <typename PersBackend, typename Structure, typename Filtration, typename dtype, typename index_type>
622
+ void get_hilbert_surface(truc_interface::Truc<PersBackend, Structure, Filtration> &slicer,
623
+ const tensor::static_tensor_view<dtype, index_type> &out, // assumes its a zero tensor
624
+ const std::vector<index_type> &grid_shape,
625
+ const std::vector<index_type> &degrees,
626
+ std::vector<index_type> coordinates_to_compute,
627
+ const std::vector<index_type> &fixed_values,
628
+ const bool mobius_inverion = true,
629
+ const bool zero_pad = false,
630
+ const bool ignore_inf = true) {
631
+
632
+ if (degrees.size() == 0) return;
633
+ // wrapper arount the rec version, that initialize the thread variables.
634
+ if (coordinates_to_compute.size() < 2)
635
+ throw std::logic_error("Not implemented for " + std::to_string(coordinates_to_compute.size()) + "<2 parameters.");
636
+ using ThreadSafe = typename truc_interface::Truc<PersBackend, Structure, Filtration>::ThreadSafe;
637
+ ThreadSafe slicer_thread(slicer);
638
+ std::vector<index_type> coordinates_container(slicer_thread.num_parameters() + 1); // +1 for degree
639
+ // coordinates_container.reserve(fixed_values.size()+1);
640
+ // coordinates_container.push_back(0); // degree
641
+ // for (auto c : fixed_values) coordinates_container.push_back(c);
642
+ std::pair<ThreadSafe, std::vector<index_type>> thread_data_initialization = {slicer_thread, coordinates_container};
643
+ tbb::enumerable_thread_specific<std::pair<ThreadSafe, std::vector<index_type>>> thread_stuff(
644
+ thread_data_initialization); // this has a fixed size, so
645
+ // init should be benefic
646
+ _rec_get_hilbert_surface<PersBackend, Structure, Filtration, dtype, index_type>(
647
+ thread_stuff, out, grid_shape, degrees, coordinates_to_compute, fixed_values, mobius_inverion, zero_pad, ignore_inf);
648
+ }
649
+
650
+ template <typename PersBackend,
651
+ typename Structure,
652
+ typename Filtration,
653
+ typename dtype,
654
+ typename indices_type,
655
+ typename... Args>
656
+ void get_hilbert_surface_python(truc_interface::Truc<PersBackend, Structure, Filtration> &slicer,
657
+ dtype *data_ptr,
658
+ std::vector<indices_type> grid_shape,
659
+ const std::vector<indices_type> degrees,
660
+ const bool mobius_inversion,
661
+ const bool zero_pad,
662
+ const bool ignore_inf,
663
+ indices_type n_jobs) {
664
+ const bool verbose = false;
665
+ if (degrees.size() == 0) return;
666
+ // const bool verbose = false;
667
+ // auto &st_multi =
668
+ // get_simplextree_from_pointer<python_interface::interface_multi<Filtration>>(simplextree_ptr);
669
+ tensor::static_tensor_view<dtype, indices_type> container(data_ptr, grid_shape); // assumes its a zero tensor
670
+ int num_parameters = slicer.num_parameters();
671
+ std::vector<indices_type> coordinates_to_compute(num_parameters);
672
+ for (auto i = 0u; i < coordinates_to_compute.size(); i++) coordinates_to_compute[i] = i;
673
+ // for (auto [c,i] : std::views::zip(coordinates_to_compute,
674
+ // std::views::iota(0,st_multi.get_number_of_parameters()))) c=i; // NIK apple
675
+ // clang
676
+ std::vector<indices_type> fixed_values(num_parameters);
677
+
678
+ if (verbose) {
679
+ std::cout << "Container shape : ";
680
+ for (auto r : container.get_resolution()) std::cout << r << ", ";
681
+ std::cout << "\nContainer size : " << container.size();
682
+ std::cout << "\nComputing hilbert invariant ...";
683
+ }
684
+ if (zero_pad) {
685
+ // +1 is bc degree is on first axis.
686
+ for (auto i = 1; i < num_parameters + 1; i++)
687
+ grid_shape[i]--; // get hilbert surface computes according to grid_shape.
688
+ // for (auto i : std::views::iota(1,st_multi.get_number_of_parameters()+1))
689
+ // grid_shape[i]--; // get hilbert surface computes according to grid_shape.
690
+ }
691
+
692
+ oneapi::tbb::task_arena arena(PersBackend::is_vine ? 1 : n_jobs); // limits the number of threads
693
+ arena.execute([&] {
694
+ get_hilbert_surface(
695
+ slicer, container, grid_shape, degrees, coordinates_to_compute, fixed_values, mobius_inversion, zero_pad, ignore_inf);
696
+ });
697
+
698
+ if (mobius_inversion)
699
+ for (indices_type axis = 2u; axis < num_parameters + 1; axis++) container.differentiate(axis);
700
+ return;
701
+ }
702
+
703
+ template <typename PersBackend,
704
+ typename Structure,
705
+ typename Filtration,
706
+ typename dtype,
707
+ typename indices_type,
708
+ typename... Args>
709
+ std::pair<std::vector<std::vector<indices_type>>, std::vector<dtype>> get_hilbert_signed_measure(
710
+ truc_interface::Truc<PersBackend, Structure, Filtration> &slicer,
711
+ dtype *data_ptr,
712
+ std::vector<indices_type> grid_shape,
713
+ const std::vector<indices_type> degrees,
714
+ bool zero_pad = false,
715
+ indices_type n_jobs = 0,
716
+ const bool verbose = false,
717
+ const bool ignore_inf = true){
718
+ if (degrees.size() == 0) return {{}, {}};
719
+ // const bool verbose = false;
720
+ // auto &st_multi =
721
+ // get_simplextree_from_pointer<python_interface::interface_multi<Filtration>>(simplextree_ptr);
722
+ tensor::static_tensor_view<dtype, indices_type> container(data_ptr, grid_shape); // assumes its a zero tensor
723
+ std::vector<indices_type> coordinates_to_compute(slicer.num_parameters());
724
+ for (auto i = 0u; i < coordinates_to_compute.size(); i++) coordinates_to_compute[i] = i;
725
+ // for (auto [c,i] : std::views::zip(coordinates_to_compute,
726
+ // std::views::iota(0,st_multi.get_number_of_parameters()))) c=i; // NIK apple
727
+ // clang
728
+ std::vector<indices_type> fixed_values(slicer.num_parameters());
729
+
730
+ if (verbose) {
731
+ std::cout << "Container shape : ";
732
+ for (auto r : container.get_resolution()) std::cout << r << ", ";
733
+ std::cout << "\nContainer size : " << container.size();
734
+ std::cout << "\nComputing hilbert invariant ..." << std::flush;
735
+ }
736
+ if (zero_pad) {
737
+ // +1 is bc degree is on first axis.
738
+ for (auto i = 1u; i < slicer.num_parameters() + 1; i++)
739
+ grid_shape[i]--; // get hilbert surface computes according to grid_shape.
740
+ // for (auto i : std::views::iota(1,st_multi.get_number_of_parameters()+1))
741
+ // grid_shape[i]--; // get hilbert surface computes according to grid_shape.
742
+ }
743
+
744
+ oneapi::tbb::task_arena arena(PersBackend::is_vine ? 1 : n_jobs); // limits the number of threads
745
+ arena.execute([&] {
746
+ get_hilbert_surface(slicer, container, grid_shape, degrees, coordinates_to_compute, fixed_values, true, zero_pad, ignore_inf);
747
+ });
748
+
749
+ if (verbose) {
750
+ std::cout << "Done.\n";
751
+ std::cout << "Computing mobius inversion ..." << std::flush;
752
+ }
753
+
754
+ // for (indices_type axis :
755
+ // std::views::iota(2,st_multi.get_number_of_parameters()+1)) // +1 for the
756
+ // degree in axis 0
757
+ for (indices_type axis = 2; axis < static_cast<indices_type>(slicer.num_parameters() + 1); axis++)
758
+ container.differentiate(axis);
759
+ if (verbose) {
760
+ std::cout << "Done.\n";
761
+ std::cout << "Sparsifying the measure ..." << std::flush;
762
+ }
763
+ auto raw_signed_measure = container.sparsify();
764
+ if (verbose) {
765
+ std::cout << "Done." << std::endl;
766
+ }
767
+ return raw_signed_measure;
768
+ }
769
+ }}} // namespace Gudhi::multiparameter::hilbert_function