multipers 2.2.3__cp311-cp311-win_amd64.whl → 2.3.0__cp311-cp311-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.cp311-win_amd64.pyd +0 -0
  17. multipers/function_rips.pyx +105 -105
  18. multipers/grids.cp311-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.cp311-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.cp311-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.cp311-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.cp311-win_amd64.pyd +0 -0
  152. multipers/point_measure.pyx +322 -320
  153. multipers/simplex_tree_multi.cp311-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.cp311-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,531 +1,531 @@
1
- /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2
- * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3
- * Author(s): Hannah Schreiber, Clément Maria
4
- *
5
- * Copyright (C) 2022-24 Inria
6
- *
7
- * Modification(s):
8
- * - YYYY/MM Author: Description of the modification
9
- */
10
-
11
- /**
12
- * @file Multi_field_small.h
13
- * @author Hannah Schreiber, Clément Maria
14
- * @brief Contains the @ref Gudhi::persistence_fields::Multi_field_element_with_small_characteristics class.
15
- */
16
-
17
- #ifndef MATRIX_FIELD_MULTI_SMALL_H_
18
- #define MATRIX_FIELD_MULTI_SMALL_H_
19
-
20
- #include <utility>
21
- #include <vector>
22
- #include <limits.h>
23
- #include <numeric>
24
-
25
- namespace Gudhi {
26
- namespace persistence_fields {
27
-
28
- /**
29
- * @class Multi_field_element_with_small_characteristics Multi_field_small.h gudhi/Fields/Multi_field_small.h
30
- * @ingroup persistence_fields
31
- *
32
- * @brief Class representing an element of a multi-field, such that the product of all characteristics fits into
33
- * the given @p Unsigned_integer_type template argument. The characteristics will corresponds to all prime numbers
34
- * in the interval given as other template arguments.
35
- *
36
- * @tparam minimum Interval closed lower bound.
37
- * @tparam maximum Interval closed upper bound.
38
- * @tparam Unsigned_integer_type A native unsigned integer type: unsigned int, long unsigned int, etc.
39
- * Will be used as the field element type.
40
- */
41
- template <unsigned int minimum, unsigned int maximum, typename Unsigned_integer_type = unsigned int,
42
- class = std::enable_if_t<std::is_unsigned_v<Unsigned_integer_type> > >
43
- class Multi_field_element_with_small_characteristics {
44
- public:
45
- using Element = Unsigned_integer_type; /**< Type for the elements in the field. */
46
- using Characteristic = Element; /**< Type for the field characteristic. */
47
- template <class T>
48
- using isInteger = std::enable_if_t<std::is_integral_v<T> >;
49
-
50
- /**
51
- * @brief Default constructor. Sets the element to 0.
52
- */
53
- Multi_field_element_with_small_characteristics() : element_(0) {
54
- static_assert(maximum >= 2, "Characteristics have to be positive.");
55
- static_assert(minimum <= maximum, "The given interval is not valid.");
56
- static_assert(minimum != maximum || _is_prime(minimum), "The given interval does not contain a prime number.");
57
- static_assert(productOfAllCharacteristics_ != 1, "The given interval does not contain a prime number.");
58
- }
59
- /**
60
- * @brief Constructor setting the element to the given value.
61
- *
62
- * @param element Value of the element.
63
- */
64
- template <typename Integer_type, class = isInteger<Integer_type> >
65
- Multi_field_element_with_small_characteristics(Integer_type element)
66
- : element_(_get_value(element)) {
67
- static_assert(maximum >= 2, "Characteristics has to be positive.");
68
- static_assert(minimum <= maximum, "The given interval is not valid.");
69
- static_assert(minimum != maximum || _is_prime(minimum), "The given interval does not contain a prime number.");
70
- static_assert(productOfAllCharacteristics_ != 1, "The given interval does not contain a prime number.");
71
- }
72
- /**
73
- * @brief Copy constructor.
74
- *
75
- * @param toCopy Element to copy.
76
- */
77
- Multi_field_element_with_small_characteristics(const Multi_field_element_with_small_characteristics& toCopy)
78
- : element_(toCopy.element_) {}
79
- /**
80
- * @brief Move constructor.
81
- *
82
- * @param toMove Element to move.
83
- */
84
- Multi_field_element_with_small_characteristics(Multi_field_element_with_small_characteristics&& toMove) noexcept
85
- : element_(std::exchange(toMove.element_, 0)) {}
86
-
87
- /**
88
- * @brief operator+=
89
- */
90
- friend void operator+=(Multi_field_element_with_small_characteristics& f1,
91
- Multi_field_element_with_small_characteristics const& f2) {
92
- f1.element_ = _add(f1.element_, f2.element_);
93
- }
94
- /**
95
- * @brief operator+
96
- */
97
- friend Multi_field_element_with_small_characteristics operator+(
98
- Multi_field_element_with_small_characteristics f1, Multi_field_element_with_small_characteristics const& f2) {
99
- f1 += f2;
100
- return f1;
101
- }
102
- /**
103
- * @brief operator+=
104
- *
105
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
106
- */
107
- template <typename Integer_type, class = isInteger<Integer_type> >
108
- friend void operator+=(Multi_field_element_with_small_characteristics& f, const Integer_type& v) {
109
- f.element_ = _add(f.element_, _get_value(v));
110
- }
111
- /**
112
- * @brief operator+
113
- *
114
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
115
- */
116
- template <typename Integer_type, class = isInteger<Integer_type> >
117
- friend Multi_field_element_with_small_characteristics operator+(Multi_field_element_with_small_characteristics f,
118
- const Integer_type& v) {
119
- f += v;
120
- return f;
121
- }
122
- /**
123
- * @brief operator+
124
- *
125
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
126
- */
127
- template <typename Integer_type, class = isInteger<Integer_type> >
128
- friend Integer_type operator+(const Integer_type& v, Multi_field_element_with_small_characteristics f) {
129
- f += v;
130
- return f.element_;
131
- }
132
-
133
- /**
134
- * @brief operator-=
135
- */
136
- friend void operator-=(Multi_field_element_with_small_characteristics& f1,
137
- Multi_field_element_with_small_characteristics const& f2) {
138
- f1.element_ = _subtract(f1.element_, f2.element_);
139
- }
140
- /**
141
- * @brief operator-
142
- */
143
- friend Multi_field_element_with_small_characteristics operator-(
144
- Multi_field_element_with_small_characteristics f1, Multi_field_element_with_small_characteristics const& f2) {
145
- f1 -= f2;
146
- return f1;
147
- }
148
- /**
149
- * @brief operator-=
150
- *
151
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
152
- */
153
- template <typename Integer_type, class = isInteger<Integer_type> >
154
- friend void operator-=(Multi_field_element_with_small_characteristics& f, const Integer_type& v) {
155
- f.element_ = _subtract(f.element_, _get_value(v));
156
- }
157
- /**
158
- * @brief operator-
159
- *
160
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
161
- */
162
- template <typename Integer_type, class = isInteger<Integer_type> >
163
- friend Multi_field_element_with_small_characteristics operator-(Multi_field_element_with_small_characteristics f,
164
- const Integer_type& v) {
165
- f -= v;
166
- return f;
167
- }
168
- /**
169
- * @brief operator-
170
- *
171
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
172
- */
173
- template <typename Integer_type, class = isInteger<Integer_type> >
174
- friend Integer_type operator-(const Integer_type& v, const Multi_field_element_with_small_characteristics& f) {
175
- return _subtract(_get_value(v), f.element_);
176
- }
177
-
178
- /**
179
- * @brief operator*=
180
- */
181
- friend void operator*=(Multi_field_element_with_small_characteristics& f1,
182
- Multi_field_element_with_small_characteristics const& f2) {
183
- f1.element_ = _multiply(f1.element_, f2.element_);
184
- }
185
- /**
186
- * @brief operator*
187
- */
188
- friend Multi_field_element_with_small_characteristics operator*(
189
- Multi_field_element_with_small_characteristics f1, Multi_field_element_with_small_characteristics const& f2) {
190
- f1 *= f2;
191
- return f1;
192
- }
193
- /**
194
- * @brief operator*=
195
- *
196
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
197
- */
198
- template <typename Integer_type, class = isInteger<Integer_type> >
199
- friend void operator*=(Multi_field_element_with_small_characteristics& f, const Integer_type& v) {
200
- f.element_ = _multiply(f.element_, _get_value(v));
201
- }
202
- /**
203
- * @brief operator*
204
- *
205
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
206
- */
207
- template <typename Integer_type, class = isInteger<Integer_type> >
208
- friend Multi_field_element_with_small_characteristics operator*(Multi_field_element_with_small_characteristics f,
209
- const Integer_type& v) {
210
- f *= v;
211
- return f;
212
- }
213
- /**
214
- * @brief operator*
215
- *
216
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
217
- */
218
- template <typename Integer_type, class = isInteger<Integer_type> >
219
- friend Integer_type operator*(const Integer_type& v, Multi_field_element_with_small_characteristics f) {
220
- f *= v;
221
- return f.element_;
222
- }
223
-
224
- /**
225
- * @brief operator==
226
- */
227
- friend bool operator==(const Multi_field_element_with_small_characteristics& f1,
228
- const Multi_field_element_with_small_characteristics& f2) {
229
- return f1.element_ == f2.element_;
230
- }
231
- /**
232
- * @brief operator==
233
- *
234
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
235
- */
236
- template <typename Integer_type, class = isInteger<Integer_type> >
237
- friend bool operator==(const Integer_type v, const Multi_field_element_with_small_characteristics& f) {
238
- return _get_value(v) == f.element_;
239
- }
240
- /**
241
- * @brief operator==
242
- *
243
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
244
- */
245
- template <typename Integer_type, class = isInteger<Integer_type> >
246
- friend bool operator==(const Multi_field_element_with_small_characteristics& f, const Integer_type v) {
247
- return _get_value(v) == f.element_;
248
- }
249
- /**
250
- * @brief operator!=
251
- */
252
- friend bool operator!=(const Multi_field_element_with_small_characteristics& f1,
253
- const Multi_field_element_with_small_characteristics& f2) {
254
- return !(f1 == f2);
255
- }
256
- /**
257
- * @brief operator!=
258
- *
259
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
260
- */
261
- template <typename Integer_type, class = isInteger<Integer_type> >
262
- friend bool operator!=(const Integer_type v, const Multi_field_element_with_small_characteristics& f) {
263
- return !(v == f);
264
- }
265
- /**
266
- * @brief operator!=
267
- *
268
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
269
- */
270
- template <typename Integer_type, class = isInteger<Integer_type> >
271
- friend bool operator!=(const Multi_field_element_with_small_characteristics& f, const Integer_type v) {
272
- return !(v == f);
273
- }
274
-
275
- /**
276
- * @brief Assign operator.
277
- */
278
- Multi_field_element_with_small_characteristics& operator=(Multi_field_element_with_small_characteristics other) {
279
- std::swap(element_, other.element_);
280
- return *this;
281
- }
282
- /**
283
- * @brief Assign operator.
284
- *
285
- * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
286
- */
287
- template <typename Integer_type, class = isInteger<Integer_type> >
288
- Multi_field_element_with_small_characteristics& operator=(const Integer_type& value) {
289
- element_ = _get_value(value);
290
- return *this;
291
- }
292
- /**
293
- * @brief Swap operator.
294
- */
295
- friend void swap(Multi_field_element_with_small_characteristics& f1,
296
- Multi_field_element_with_small_characteristics& f2) {
297
- std::swap(f1.element_, f2.element_);
298
- }
299
-
300
- /**
301
- * @brief Casts the element into an unsigned int.
302
- */
303
- operator unsigned int() const { return element_; }
304
-
305
- /**
306
- * @brief Returns the inverse of the element in the multi-field, see @cite boissonnat:hal-00922572.
307
- *
308
- * @return The inverse.
309
- */
310
- Multi_field_element_with_small_characteristics get_inverse() const {
311
- return get_partial_inverse(productOfAllCharacteristics_).first;
312
- }
313
- /**
314
- * @brief Returns the inverse of the element with respect to a sub-product of the characteristics in the multi-field,
315
- * see @cite boissonnat:hal-00922572.
316
- *
317
- * @param productOfCharacteristics Sub-product of the characteristics.
318
- * @return Pair of the inverse and the characteristic the inverse corresponds to.
319
- */
320
- std::pair<Multi_field_element_with_small_characteristics,Characteristic> get_partial_inverse(
321
- Characteristic productOfCharacteristics) const {
322
- Characteristic gcd = std::gcd(element_, productOfAllCharacteristics_);
323
-
324
- if (gcd == productOfCharacteristics)
325
- return {Multi_field_element_with_small_characteristics(), multiplicativeID_}; // partial inverse is 0
326
-
327
- Characteristic QT = productOfCharacteristics / gcd;
328
-
329
- const Element inv_qt = _get_inverse(element_, QT);
330
-
331
- auto res = get_partial_multiplicative_identity(QT);
332
- res *= inv_qt;
333
-
334
- return {res, QT};
335
- }
336
-
337
- /**
338
- * @brief Returns the additive identity of a field.
339
- *
340
- * @return The additive identity of a field.
341
- */
342
- static Multi_field_element_with_small_characteristics get_additive_identity() {
343
- return Multi_field_element_with_small_characteristics<minimum, maximum>();
344
- }
345
- /**
346
- * @brief Returns the multiplicative identity of a field.
347
- *
348
- * @return The multiplicative identity of a field.
349
- */
350
- static Multi_field_element_with_small_characteristics get_multiplicative_identity() {
351
- return Multi_field_element_with_small_characteristics<minimum, maximum>(multiplicativeID_);
352
- }
353
- /**
354
- * @brief Returns the partial multiplicative identity of the multi-field from the given product.
355
- * See @cite boissonnat:hal-00922572 for more details.
356
- *
357
- * @param productOfCharacteristics Product of the different characteristics to take into account in the multi-field.
358
- * @return The partial multiplicative identity of the multi-field.
359
- */
360
- static Multi_field_element_with_small_characteristics get_partial_multiplicative_identity(
361
- const Characteristic& productOfCharacteristics) {
362
- if (productOfCharacteristics == 0) {
363
- return Multi_field_element_with_small_characteristics<minimum, maximum>(multiplicativeID_);
364
- }
365
- Multi_field_element_with_small_characteristics<minimum, maximum> mult;
366
- for (Characteristic idx = 0; idx < primes_.size(); ++idx) {
367
- if ((productOfCharacteristics % primes_[idx]) == 0) {
368
- mult += partials_[idx];
369
- }
370
- }
371
- return mult;
372
- }
373
- /**
374
- * @brief Returns the product of all characteristics.
375
- *
376
- * @return The product of all characteristics.
377
- */
378
- static constexpr Characteristic get_characteristic() { return productOfAllCharacteristics_; }
379
-
380
- /**
381
- * @brief Returns the value of the element.
382
- *
383
- * @return Value of the element.
384
- */
385
- Element get_value() const { return element_; }
386
-
387
- // static constexpr bool handles_only_z2() { return false; }
388
-
389
- private:
390
- static constexpr bool _is_prime(const unsigned int p) {
391
- if (p <= 1) return false;
392
- if (p <= 3) return true;
393
- if (p % 2 == 0 || p % 3 == 0) return false;
394
-
395
- for (unsigned long i = 5; i * i <= p; i = i + 6)
396
- if (p % i == 0 || p % (i + 2) == 0) return false;
397
-
398
- return true;
399
- }
400
- static constexpr Element _multiply(Element a, Element b) {
401
- Element res = 0;
402
- Element temp_b = 0;
403
-
404
- if (b < a) std::swap(a, b);
405
-
406
- while (a != 0) {
407
- if (a & 1) {
408
- /* Add b to res, modulo m, without overflow */
409
- if (b >= productOfAllCharacteristics_ - res) res -= productOfAllCharacteristics_;
410
- res += b;
411
- }
412
- a >>= 1;
413
-
414
- /* Double b, modulo m */
415
- temp_b = b;
416
- if (b >= productOfAllCharacteristics_ - b) temp_b -= productOfAllCharacteristics_;
417
- b += temp_b;
418
- }
419
- return res;
420
- }
421
- static constexpr Element _add(Element element, Element v) {
422
- if (UINT_MAX - element < v) {
423
- // automatic unsigned integer overflow behaviour will make it work
424
- element += v;
425
- element -= productOfAllCharacteristics_;
426
- return element;
427
- }
428
-
429
- element += v;
430
- if (element >= productOfAllCharacteristics_) element -= productOfAllCharacteristics_;
431
-
432
- return element;
433
- }
434
- static constexpr Element _subtract(Element element, Element v) {
435
- if (element < v) {
436
- element += productOfAllCharacteristics_;
437
- }
438
- element -= v;
439
-
440
- return element;
441
- }
442
- static constexpr int _get_inverse(Element element, const Element mod) {
443
- // to solve: Ax + My = 1
444
- int M = mod;
445
- int A = element;
446
- int y = 0, x = 1;
447
- // extended euclidean division
448
- while (A > 1) {
449
- int quotient = A / M;
450
- int temp = M;
451
-
452
- M = A % M, A = temp;
453
- temp = y;
454
-
455
- y = x - quotient * y;
456
- x = temp;
457
- }
458
-
459
- if (x < 0) x += mod;
460
-
461
- return x;
462
- }
463
-
464
- template <typename Integer_type, class = isInteger<Integer_type> >
465
- static constexpr Element _get_value(Integer_type e) {
466
- if constexpr (std::is_signed_v<Integer_type>){
467
- if (e < -static_cast<Integer_type>(productOfAllCharacteristics_)) e = e % productOfAllCharacteristics_;
468
- if (e < 0) return e += productOfAllCharacteristics_;
469
- return e < static_cast<Integer_type>(productOfAllCharacteristics_) ? e : e % productOfAllCharacteristics_;
470
- } else {
471
- return e < productOfAllCharacteristics_ ? e : e % productOfAllCharacteristics_;
472
- }
473
- }
474
-
475
- Element element_;
476
- static inline const std::vector<Characteristic> primes_ = []() {
477
- std::vector<Characteristic> res;
478
- for (Characteristic i = minimum; i <= maximum; ++i) {
479
- if (_is_prime(i)) {
480
- res.push_back(i);
481
- }
482
- }
483
- return res;
484
- }();
485
- static inline constexpr Characteristic productOfAllCharacteristics_ = []() {
486
- Characteristic res = 1;
487
- for (Characteristic i = minimum; i <= maximum; ++i) {
488
- if (_is_prime(i)) {
489
- res *= i;
490
- }
491
- }
492
- return res;
493
- }();
494
- static inline const std::vector<Characteristic> partials_ = []() {
495
- std::vector<Characteristic> res;
496
-
497
- if (productOfAllCharacteristics_ == 1) return res;
498
-
499
- for (Characteristic i = 0; i < primes_.size(); ++i) {
500
- Characteristic p = primes_[i];
501
- Characteristic base = productOfAllCharacteristics_ / p;
502
- Characteristic exp = p - 1;
503
- res.push_back(1);
504
-
505
- while (exp > 0) {
506
- // If exp is odd, multiply with result
507
- if (exp & 1) res.back() = _multiply(res.back(), base);
508
- // y must be even now
509
- exp = exp >> 1;
510
- base = _multiply(base, base);
511
- }
512
- }
513
-
514
- return res;
515
- }();
516
- // If I understood the paper well, multiplicativeID_ always equals to 1. But in Clement's code,
517
- // multiplicativeID_ is computed (see commented lambda function below). TODO: verify with Clement.
518
- static inline constexpr Element multiplicativeID_ = 1; /*= [](){
519
- unsigned int res = 0;
520
- for (unsigned int i = 0; i < partials_.size(); ++i){
521
- res = (res + partials_[i]) % productOfAllCharacteristics_;
522
- }
523
-
524
- return res;
525
- }();*/
526
- };
527
-
528
- } // namespace persistence_fields
529
- } // namespace Gudhi
530
-
531
- #endif // MATRIX_FIELD_MULTI_SMALL_H_
1
+ /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2
+ * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3
+ * Author(s): Hannah Schreiber, Clément Maria
4
+ *
5
+ * Copyright (C) 2022-24 Inria
6
+ *
7
+ * Modification(s):
8
+ * - YYYY/MM Author: Description of the modification
9
+ */
10
+
11
+ /**
12
+ * @file Multi_field_small.h
13
+ * @author Hannah Schreiber, Clément Maria
14
+ * @brief Contains the @ref Gudhi::persistence_fields::Multi_field_element_with_small_characteristics class.
15
+ */
16
+
17
+ #ifndef MATRIX_FIELD_MULTI_SMALL_H_
18
+ #define MATRIX_FIELD_MULTI_SMALL_H_
19
+
20
+ #include <utility>
21
+ #include <vector>
22
+ #include <limits.h>
23
+ #include <numeric>
24
+
25
+ namespace Gudhi {
26
+ namespace persistence_fields {
27
+
28
+ /**
29
+ * @class Multi_field_element_with_small_characteristics Multi_field_small.h gudhi/Fields/Multi_field_small.h
30
+ * @ingroup persistence_fields
31
+ *
32
+ * @brief Class representing an element of a multi-field, such that the product of all characteristics fits into
33
+ * the given @p Unsigned_integer_type template argument. The characteristics will corresponds to all prime numbers
34
+ * in the interval given as other template arguments.
35
+ *
36
+ * @tparam minimum Interval closed lower bound.
37
+ * @tparam maximum Interval closed upper bound.
38
+ * @tparam Unsigned_integer_type A native unsigned integer type: unsigned int, long unsigned int, etc.
39
+ * Will be used as the field element type.
40
+ */
41
+ template <unsigned int minimum, unsigned int maximum, typename Unsigned_integer_type = unsigned int,
42
+ class = std::enable_if_t<std::is_unsigned_v<Unsigned_integer_type> > >
43
+ class Multi_field_element_with_small_characteristics {
44
+ public:
45
+ using Element = Unsigned_integer_type; /**< Type for the elements in the field. */
46
+ using Characteristic = Element; /**< Type for the field characteristic. */
47
+ template <class T>
48
+ using isInteger = std::enable_if_t<std::is_integral_v<T> >;
49
+
50
+ /**
51
+ * @brief Default constructor. Sets the element to 0.
52
+ */
53
+ Multi_field_element_with_small_characteristics() : element_(0) {
54
+ static_assert(maximum >= 2, "Characteristics have to be positive.");
55
+ static_assert(minimum <= maximum, "The given interval is not valid.");
56
+ static_assert(minimum != maximum || _is_prime(minimum), "The given interval does not contain a prime number.");
57
+ static_assert(productOfAllCharacteristics_ != 1, "The given interval does not contain a prime number.");
58
+ }
59
+ /**
60
+ * @brief Constructor setting the element to the given value.
61
+ *
62
+ * @param element Value of the element.
63
+ */
64
+ template <typename Integer_type, class = isInteger<Integer_type> >
65
+ Multi_field_element_with_small_characteristics(Integer_type element)
66
+ : element_(_get_value(element)) {
67
+ static_assert(maximum >= 2, "Characteristics has to be positive.");
68
+ static_assert(minimum <= maximum, "The given interval is not valid.");
69
+ static_assert(minimum != maximum || _is_prime(minimum), "The given interval does not contain a prime number.");
70
+ static_assert(productOfAllCharacteristics_ != 1, "The given interval does not contain a prime number.");
71
+ }
72
+ /**
73
+ * @brief Copy constructor.
74
+ *
75
+ * @param toCopy Element to copy.
76
+ */
77
+ Multi_field_element_with_small_characteristics(const Multi_field_element_with_small_characteristics& toCopy)
78
+ : element_(toCopy.element_) {}
79
+ /**
80
+ * @brief Move constructor.
81
+ *
82
+ * @param toMove Element to move.
83
+ */
84
+ Multi_field_element_with_small_characteristics(Multi_field_element_with_small_characteristics&& toMove) noexcept
85
+ : element_(std::exchange(toMove.element_, 0)) {}
86
+
87
+ /**
88
+ * @brief operator+=
89
+ */
90
+ friend void operator+=(Multi_field_element_with_small_characteristics& f1,
91
+ Multi_field_element_with_small_characteristics const& f2) {
92
+ f1.element_ = _add(f1.element_, f2.element_);
93
+ }
94
+ /**
95
+ * @brief operator+
96
+ */
97
+ friend Multi_field_element_with_small_characteristics operator+(
98
+ Multi_field_element_with_small_characteristics f1, Multi_field_element_with_small_characteristics const& f2) {
99
+ f1 += f2;
100
+ return f1;
101
+ }
102
+ /**
103
+ * @brief operator+=
104
+ *
105
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
106
+ */
107
+ template <typename Integer_type, class = isInteger<Integer_type> >
108
+ friend void operator+=(Multi_field_element_with_small_characteristics& f, const Integer_type& v) {
109
+ f.element_ = _add(f.element_, _get_value(v));
110
+ }
111
+ /**
112
+ * @brief operator+
113
+ *
114
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
115
+ */
116
+ template <typename Integer_type, class = isInteger<Integer_type> >
117
+ friend Multi_field_element_with_small_characteristics operator+(Multi_field_element_with_small_characteristics f,
118
+ const Integer_type& v) {
119
+ f += v;
120
+ return f;
121
+ }
122
+ /**
123
+ * @brief operator+
124
+ *
125
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
126
+ */
127
+ template <typename Integer_type, class = isInteger<Integer_type> >
128
+ friend Integer_type operator+(const Integer_type& v, Multi_field_element_with_small_characteristics f) {
129
+ f += v;
130
+ return f.element_;
131
+ }
132
+
133
+ /**
134
+ * @brief operator-=
135
+ */
136
+ friend void operator-=(Multi_field_element_with_small_characteristics& f1,
137
+ Multi_field_element_with_small_characteristics const& f2) {
138
+ f1.element_ = _subtract(f1.element_, f2.element_);
139
+ }
140
+ /**
141
+ * @brief operator-
142
+ */
143
+ friend Multi_field_element_with_small_characteristics operator-(
144
+ Multi_field_element_with_small_characteristics f1, Multi_field_element_with_small_characteristics const& f2) {
145
+ f1 -= f2;
146
+ return f1;
147
+ }
148
+ /**
149
+ * @brief operator-=
150
+ *
151
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
152
+ */
153
+ template <typename Integer_type, class = isInteger<Integer_type> >
154
+ friend void operator-=(Multi_field_element_with_small_characteristics& f, const Integer_type& v) {
155
+ f.element_ = _subtract(f.element_, _get_value(v));
156
+ }
157
+ /**
158
+ * @brief operator-
159
+ *
160
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
161
+ */
162
+ template <typename Integer_type, class = isInteger<Integer_type> >
163
+ friend Multi_field_element_with_small_characteristics operator-(Multi_field_element_with_small_characteristics f,
164
+ const Integer_type& v) {
165
+ f -= v;
166
+ return f;
167
+ }
168
+ /**
169
+ * @brief operator-
170
+ *
171
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
172
+ */
173
+ template <typename Integer_type, class = isInteger<Integer_type> >
174
+ friend Integer_type operator-(const Integer_type& v, const Multi_field_element_with_small_characteristics& f) {
175
+ return _subtract(_get_value(v), f.element_);
176
+ }
177
+
178
+ /**
179
+ * @brief operator*=
180
+ */
181
+ friend void operator*=(Multi_field_element_with_small_characteristics& f1,
182
+ Multi_field_element_with_small_characteristics const& f2) {
183
+ f1.element_ = _multiply(f1.element_, f2.element_);
184
+ }
185
+ /**
186
+ * @brief operator*
187
+ */
188
+ friend Multi_field_element_with_small_characteristics operator*(
189
+ Multi_field_element_with_small_characteristics f1, Multi_field_element_with_small_characteristics const& f2) {
190
+ f1 *= f2;
191
+ return f1;
192
+ }
193
+ /**
194
+ * @brief operator*=
195
+ *
196
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
197
+ */
198
+ template <typename Integer_type, class = isInteger<Integer_type> >
199
+ friend void operator*=(Multi_field_element_with_small_characteristics& f, const Integer_type& v) {
200
+ f.element_ = _multiply(f.element_, _get_value(v));
201
+ }
202
+ /**
203
+ * @brief operator*
204
+ *
205
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
206
+ */
207
+ template <typename Integer_type, class = isInteger<Integer_type> >
208
+ friend Multi_field_element_with_small_characteristics operator*(Multi_field_element_with_small_characteristics f,
209
+ const Integer_type& v) {
210
+ f *= v;
211
+ return f;
212
+ }
213
+ /**
214
+ * @brief operator*
215
+ *
216
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
217
+ */
218
+ template <typename Integer_type, class = isInteger<Integer_type> >
219
+ friend Integer_type operator*(const Integer_type& v, Multi_field_element_with_small_characteristics f) {
220
+ f *= v;
221
+ return f.element_;
222
+ }
223
+
224
+ /**
225
+ * @brief operator==
226
+ */
227
+ friend bool operator==(const Multi_field_element_with_small_characteristics& f1,
228
+ const Multi_field_element_with_small_characteristics& f2) {
229
+ return f1.element_ == f2.element_;
230
+ }
231
+ /**
232
+ * @brief operator==
233
+ *
234
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
235
+ */
236
+ template <typename Integer_type, class = isInteger<Integer_type> >
237
+ friend bool operator==(const Integer_type v, const Multi_field_element_with_small_characteristics& f) {
238
+ return _get_value(v) == f.element_;
239
+ }
240
+ /**
241
+ * @brief operator==
242
+ *
243
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
244
+ */
245
+ template <typename Integer_type, class = isInteger<Integer_type> >
246
+ friend bool operator==(const Multi_field_element_with_small_characteristics& f, const Integer_type v) {
247
+ return _get_value(v) == f.element_;
248
+ }
249
+ /**
250
+ * @brief operator!=
251
+ */
252
+ friend bool operator!=(const Multi_field_element_with_small_characteristics& f1,
253
+ const Multi_field_element_with_small_characteristics& f2) {
254
+ return !(f1 == f2);
255
+ }
256
+ /**
257
+ * @brief operator!=
258
+ *
259
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
260
+ */
261
+ template <typename Integer_type, class = isInteger<Integer_type> >
262
+ friend bool operator!=(const Integer_type v, const Multi_field_element_with_small_characteristics& f) {
263
+ return !(v == f);
264
+ }
265
+ /**
266
+ * @brief operator!=
267
+ *
268
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
269
+ */
270
+ template <typename Integer_type, class = isInteger<Integer_type> >
271
+ friend bool operator!=(const Multi_field_element_with_small_characteristics& f, const Integer_type v) {
272
+ return !(v == f);
273
+ }
274
+
275
+ /**
276
+ * @brief Assign operator.
277
+ */
278
+ Multi_field_element_with_small_characteristics& operator=(Multi_field_element_with_small_characteristics other) {
279
+ std::swap(element_, other.element_);
280
+ return *this;
281
+ }
282
+ /**
283
+ * @brief Assign operator.
284
+ *
285
+ * @tparam Integer_type A native integer type. Should be able to contain the characteristic if signed.
286
+ */
287
+ template <typename Integer_type, class = isInteger<Integer_type> >
288
+ Multi_field_element_with_small_characteristics& operator=(const Integer_type& value) {
289
+ element_ = _get_value(value);
290
+ return *this;
291
+ }
292
+ /**
293
+ * @brief Swap operator.
294
+ */
295
+ friend void swap(Multi_field_element_with_small_characteristics& f1,
296
+ Multi_field_element_with_small_characteristics& f2) {
297
+ std::swap(f1.element_, f2.element_);
298
+ }
299
+
300
+ /**
301
+ * @brief Casts the element into an unsigned int.
302
+ */
303
+ operator unsigned int() const { return element_; }
304
+
305
+ /**
306
+ * @brief Returns the inverse of the element in the multi-field, see @cite boissonnat:hal-00922572.
307
+ *
308
+ * @return The inverse.
309
+ */
310
+ Multi_field_element_with_small_characteristics get_inverse() const {
311
+ return get_partial_inverse(productOfAllCharacteristics_).first;
312
+ }
313
+ /**
314
+ * @brief Returns the inverse of the element with respect to a sub-product of the characteristics in the multi-field,
315
+ * see @cite boissonnat:hal-00922572.
316
+ *
317
+ * @param productOfCharacteristics Sub-product of the characteristics.
318
+ * @return Pair of the inverse and the characteristic the inverse corresponds to.
319
+ */
320
+ std::pair<Multi_field_element_with_small_characteristics,Characteristic> get_partial_inverse(
321
+ Characteristic productOfCharacteristics) const {
322
+ Characteristic gcd = std::gcd(element_, productOfAllCharacteristics_);
323
+
324
+ if (gcd == productOfCharacteristics)
325
+ return {Multi_field_element_with_small_characteristics(), multiplicativeID_}; // partial inverse is 0
326
+
327
+ Characteristic QT = productOfCharacteristics / gcd;
328
+
329
+ const Element inv_qt = _get_inverse(element_, QT);
330
+
331
+ auto res = get_partial_multiplicative_identity(QT);
332
+ res *= inv_qt;
333
+
334
+ return {res, QT};
335
+ }
336
+
337
+ /**
338
+ * @brief Returns the additive identity of a field.
339
+ *
340
+ * @return The additive identity of a field.
341
+ */
342
+ static Multi_field_element_with_small_characteristics get_additive_identity() {
343
+ return Multi_field_element_with_small_characteristics<minimum, maximum>();
344
+ }
345
+ /**
346
+ * @brief Returns the multiplicative identity of a field.
347
+ *
348
+ * @return The multiplicative identity of a field.
349
+ */
350
+ static Multi_field_element_with_small_characteristics get_multiplicative_identity() {
351
+ return Multi_field_element_with_small_characteristics<minimum, maximum>(multiplicativeID_);
352
+ }
353
+ /**
354
+ * @brief Returns the partial multiplicative identity of the multi-field from the given product.
355
+ * See @cite boissonnat:hal-00922572 for more details.
356
+ *
357
+ * @param productOfCharacteristics Product of the different characteristics to take into account in the multi-field.
358
+ * @return The partial multiplicative identity of the multi-field.
359
+ */
360
+ static Multi_field_element_with_small_characteristics get_partial_multiplicative_identity(
361
+ const Characteristic& productOfCharacteristics) {
362
+ if (productOfCharacteristics == 0) {
363
+ return Multi_field_element_with_small_characteristics<minimum, maximum>(multiplicativeID_);
364
+ }
365
+ Multi_field_element_with_small_characteristics<minimum, maximum> mult;
366
+ for (Characteristic idx = 0; idx < primes_.size(); ++idx) {
367
+ if ((productOfCharacteristics % primes_[idx]) == 0) {
368
+ mult += partials_[idx];
369
+ }
370
+ }
371
+ return mult;
372
+ }
373
+ /**
374
+ * @brief Returns the product of all characteristics.
375
+ *
376
+ * @return The product of all characteristics.
377
+ */
378
+ static constexpr Characteristic get_characteristic() { return productOfAllCharacteristics_; }
379
+
380
+ /**
381
+ * @brief Returns the value of the element.
382
+ *
383
+ * @return Value of the element.
384
+ */
385
+ Element get_value() const { return element_; }
386
+
387
+ // static constexpr bool handles_only_z2() { return false; }
388
+
389
+ private:
390
+ static constexpr bool _is_prime(const unsigned int p) {
391
+ if (p <= 1) return false;
392
+ if (p <= 3) return true;
393
+ if (p % 2 == 0 || p % 3 == 0) return false;
394
+
395
+ for (unsigned long i = 5; i * i <= p; i = i + 6)
396
+ if (p % i == 0 || p % (i + 2) == 0) return false;
397
+
398
+ return true;
399
+ }
400
+ static constexpr Element _multiply(Element a, Element b) {
401
+ Element res = 0;
402
+ Element temp_b = 0;
403
+
404
+ if (b < a) std::swap(a, b);
405
+
406
+ while (a != 0) {
407
+ if (a & 1) {
408
+ /* Add b to res, modulo m, without overflow */
409
+ if (b >= productOfAllCharacteristics_ - res) res -= productOfAllCharacteristics_;
410
+ res += b;
411
+ }
412
+ a >>= 1;
413
+
414
+ /* Double b, modulo m */
415
+ temp_b = b;
416
+ if (b >= productOfAllCharacteristics_ - b) temp_b -= productOfAllCharacteristics_;
417
+ b += temp_b;
418
+ }
419
+ return res;
420
+ }
421
+ static constexpr Element _add(Element element, Element v) {
422
+ if (UINT_MAX - element < v) {
423
+ // automatic unsigned integer overflow behaviour will make it work
424
+ element += v;
425
+ element -= productOfAllCharacteristics_;
426
+ return element;
427
+ }
428
+
429
+ element += v;
430
+ if (element >= productOfAllCharacteristics_) element -= productOfAllCharacteristics_;
431
+
432
+ return element;
433
+ }
434
+ static constexpr Element _subtract(Element element, Element v) {
435
+ if (element < v) {
436
+ element += productOfAllCharacteristics_;
437
+ }
438
+ element -= v;
439
+
440
+ return element;
441
+ }
442
+ static constexpr int _get_inverse(Element element, const Element mod) {
443
+ // to solve: Ax + My = 1
444
+ int M = mod;
445
+ int A = element;
446
+ int y = 0, x = 1;
447
+ // extended euclidean division
448
+ while (A > 1) {
449
+ int quotient = A / M;
450
+ int temp = M;
451
+
452
+ M = A % M, A = temp;
453
+ temp = y;
454
+
455
+ y = x - quotient * y;
456
+ x = temp;
457
+ }
458
+
459
+ if (x < 0) x += mod;
460
+
461
+ return x;
462
+ }
463
+
464
+ template <typename Integer_type, class = isInteger<Integer_type> >
465
+ static constexpr Element _get_value(Integer_type e) {
466
+ if constexpr (std::is_signed_v<Integer_type>){
467
+ if (e < -static_cast<Integer_type>(productOfAllCharacteristics_)) e = e % productOfAllCharacteristics_;
468
+ if (e < 0) return e += productOfAllCharacteristics_;
469
+ return e < static_cast<Integer_type>(productOfAllCharacteristics_) ? e : e % productOfAllCharacteristics_;
470
+ } else {
471
+ return e < productOfAllCharacteristics_ ? e : e % productOfAllCharacteristics_;
472
+ }
473
+ }
474
+
475
+ Element element_;
476
+ static inline const std::vector<Characteristic> primes_ = []() {
477
+ std::vector<Characteristic> res;
478
+ for (Characteristic i = minimum; i <= maximum; ++i) {
479
+ if (_is_prime(i)) {
480
+ res.push_back(i);
481
+ }
482
+ }
483
+ return res;
484
+ }();
485
+ static inline constexpr Characteristic productOfAllCharacteristics_ = []() {
486
+ Characteristic res = 1;
487
+ for (Characteristic i = minimum; i <= maximum; ++i) {
488
+ if (_is_prime(i)) {
489
+ res *= i;
490
+ }
491
+ }
492
+ return res;
493
+ }();
494
+ static inline const std::vector<Characteristic> partials_ = []() {
495
+ std::vector<Characteristic> res;
496
+
497
+ if (productOfAllCharacteristics_ == 1) return res;
498
+
499
+ for (Characteristic i = 0; i < primes_.size(); ++i) {
500
+ Characteristic p = primes_[i];
501
+ Characteristic base = productOfAllCharacteristics_ / p;
502
+ Characteristic exp = p - 1;
503
+ res.push_back(1);
504
+
505
+ while (exp > 0) {
506
+ // If exp is odd, multiply with result
507
+ if (exp & 1) res.back() = _multiply(res.back(), base);
508
+ // y must be even now
509
+ exp = exp >> 1;
510
+ base = _multiply(base, base);
511
+ }
512
+ }
513
+
514
+ return res;
515
+ }();
516
+ // If I understood the paper well, multiplicativeID_ always equals to 1. But in Clement's code,
517
+ // multiplicativeID_ is computed (see commented lambda function below). TODO: verify with Clement.
518
+ static inline constexpr Element multiplicativeID_ = 1; /*= [](){
519
+ unsigned int res = 0;
520
+ for (unsigned int i = 0; i < partials_.size(); ++i){
521
+ res = (res + partials_[i]) % productOfAllCharacteristics_;
522
+ }
523
+
524
+ return res;
525
+ }();*/
526
+ };
527
+
528
+ } // namespace persistence_fields
529
+ } // namespace Gudhi
530
+
531
+ #endif // MATRIX_FIELD_MULTI_SMALL_H_