multipers 2.4.0b1__cp312-cp312-macosx_11_0_arm64.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.
Files changed (184) hide show
  1. multipers/.dylibs/libboost_timer.dylib +0 -0
  2. multipers/.dylibs/libc++.1.0.dylib +0 -0
  3. multipers/.dylibs/libtbb.12.17.dylib +0 -0
  4. multipers/__init__.py +33 -0
  5. multipers/_signed_measure_meta.py +426 -0
  6. multipers/_slicer_meta.py +231 -0
  7. multipers/array_api/__init__.py +62 -0
  8. multipers/array_api/numpy.py +124 -0
  9. multipers/array_api/torch.py +133 -0
  10. multipers/data/MOL2.py +458 -0
  11. multipers/data/UCR.py +18 -0
  12. multipers/data/__init__.py +1 -0
  13. multipers/data/graphs.py +466 -0
  14. multipers/data/immuno_regions.py +27 -0
  15. multipers/data/minimal_presentation_to_st_bf.py +0 -0
  16. multipers/data/pytorch2simplextree.py +91 -0
  17. multipers/data/shape3d.py +101 -0
  18. multipers/data/synthetic.py +113 -0
  19. multipers/distances.py +202 -0
  20. multipers/filtration_conversions.pxd +736 -0
  21. multipers/filtration_conversions.pxd.tp +226 -0
  22. multipers/filtrations/__init__.py +21 -0
  23. multipers/filtrations/density.py +529 -0
  24. multipers/filtrations/filtrations.py +480 -0
  25. multipers/filtrations.pxd +534 -0
  26. multipers/filtrations.pxd.tp +332 -0
  27. multipers/function_rips.cpython-312-darwin.so +0 -0
  28. multipers/function_rips.pyx +104 -0
  29. multipers/grids.cpython-312-darwin.so +0 -0
  30. multipers/grids.pyx +538 -0
  31. multipers/gudhi/Persistence_slices_interface.h +213 -0
  32. multipers/gudhi/Simplex_tree_interface.h +274 -0
  33. multipers/gudhi/Simplex_tree_multi_interface.h +648 -0
  34. multipers/gudhi/gudhi/Bitmap_cubical_complex.h +450 -0
  35. multipers/gudhi/gudhi/Bitmap_cubical_complex_base.h +1070 -0
  36. multipers/gudhi/gudhi/Bitmap_cubical_complex_periodic_boundary_conditions_base.h +579 -0
  37. multipers/gudhi/gudhi/Debug_utils.h +52 -0
  38. multipers/gudhi/gudhi/Degree_rips_bifiltration.h +2307 -0
  39. multipers/gudhi/gudhi/Dynamic_multi_parameter_filtration.h +2524 -0
  40. multipers/gudhi/gudhi/Fields/Multi_field.h +453 -0
  41. multipers/gudhi/gudhi/Fields/Multi_field_operators.h +460 -0
  42. multipers/gudhi/gudhi/Fields/Multi_field_shared.h +444 -0
  43. multipers/gudhi/gudhi/Fields/Multi_field_small.h +584 -0
  44. multipers/gudhi/gudhi/Fields/Multi_field_small_operators.h +490 -0
  45. multipers/gudhi/gudhi/Fields/Multi_field_small_shared.h +580 -0
  46. multipers/gudhi/gudhi/Fields/Z2_field.h +391 -0
  47. multipers/gudhi/gudhi/Fields/Z2_field_operators.h +389 -0
  48. multipers/gudhi/gudhi/Fields/Zp_field.h +493 -0
  49. multipers/gudhi/gudhi/Fields/Zp_field_operators.h +384 -0
  50. multipers/gudhi/gudhi/Fields/Zp_field_shared.h +492 -0
  51. multipers/gudhi/gudhi/Flag_complex_edge_collapser.h +337 -0
  52. multipers/gudhi/gudhi/Matrix.h +2200 -0
  53. multipers/gudhi/gudhi/Multi_filtration/Multi_parameter_generator.h +1712 -0
  54. multipers/gudhi/gudhi/Multi_filtration/multi_filtration_conversions.h +237 -0
  55. multipers/gudhi/gudhi/Multi_filtration/multi_filtration_utils.h +225 -0
  56. multipers/gudhi/gudhi/Multi_parameter_filtered_complex.h +485 -0
  57. multipers/gudhi/gudhi/Multi_parameter_filtration.h +2643 -0
  58. multipers/gudhi/gudhi/Multi_persistence/Box.h +233 -0
  59. multipers/gudhi/gudhi/Multi_persistence/Line.h +309 -0
  60. multipers/gudhi/gudhi/Multi_persistence/Multi_parameter_filtered_complex_pcoh_interface.h +268 -0
  61. multipers/gudhi/gudhi/Multi_persistence/Persistence_interface_cohomology.h +159 -0
  62. multipers/gudhi/gudhi/Multi_persistence/Persistence_interface_matrix.h +463 -0
  63. multipers/gudhi/gudhi/Multi_persistence/Point.h +853 -0
  64. multipers/gudhi/gudhi/Off_reader.h +173 -0
  65. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix.h +834 -0
  66. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix_with_column_compression.h +838 -0
  67. multipers/gudhi/gudhi/Persistence_matrix/Boundary_matrix.h +833 -0
  68. multipers/gudhi/gudhi/Persistence_matrix/Chain_matrix.h +1367 -0
  69. multipers/gudhi/gudhi/Persistence_matrix/Id_to_index_overlay.h +1157 -0
  70. multipers/gudhi/gudhi/Persistence_matrix/Position_to_index_overlay.h +869 -0
  71. multipers/gudhi/gudhi/Persistence_matrix/RU_matrix.h +905 -0
  72. multipers/gudhi/gudhi/Persistence_matrix/allocators/entry_constructors.h +122 -0
  73. multipers/gudhi/gudhi/Persistence_matrix/base_pairing.h +260 -0
  74. multipers/gudhi/gudhi/Persistence_matrix/base_swap.h +288 -0
  75. multipers/gudhi/gudhi/Persistence_matrix/chain_pairing.h +170 -0
  76. multipers/gudhi/gudhi/Persistence_matrix/chain_rep_cycles.h +247 -0
  77. multipers/gudhi/gudhi/Persistence_matrix/chain_vine_swap.h +571 -0
  78. multipers/gudhi/gudhi/Persistence_matrix/columns/chain_column_extra_properties.h +182 -0
  79. multipers/gudhi/gudhi/Persistence_matrix/columns/column_dimension_holder.h +130 -0
  80. multipers/gudhi/gudhi/Persistence_matrix/columns/column_utilities.h +235 -0
  81. multipers/gudhi/gudhi/Persistence_matrix/columns/entry_types.h +312 -0
  82. multipers/gudhi/gudhi/Persistence_matrix/columns/heap_column.h +1092 -0
  83. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_list_column.h +923 -0
  84. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_set_column.h +914 -0
  85. multipers/gudhi/gudhi/Persistence_matrix/columns/list_column.h +930 -0
  86. multipers/gudhi/gudhi/Persistence_matrix/columns/naive_vector_column.h +1071 -0
  87. multipers/gudhi/gudhi/Persistence_matrix/columns/row_access.h +203 -0
  88. multipers/gudhi/gudhi/Persistence_matrix/columns/set_column.h +886 -0
  89. multipers/gudhi/gudhi/Persistence_matrix/columns/unordered_set_column.h +984 -0
  90. multipers/gudhi/gudhi/Persistence_matrix/columns/vector_column.h +1213 -0
  91. multipers/gudhi/gudhi/Persistence_matrix/index_mapper.h +58 -0
  92. multipers/gudhi/gudhi/Persistence_matrix/matrix_dimension_holders.h +227 -0
  93. multipers/gudhi/gudhi/Persistence_matrix/matrix_row_access.h +200 -0
  94. multipers/gudhi/gudhi/Persistence_matrix/ru_pairing.h +166 -0
  95. multipers/gudhi/gudhi/Persistence_matrix/ru_rep_cycles.h +319 -0
  96. multipers/gudhi/gudhi/Persistence_matrix/ru_vine_swap.h +562 -0
  97. multipers/gudhi/gudhi/Persistence_on_a_line.h +152 -0
  98. multipers/gudhi/gudhi/Persistence_on_rectangle.h +617 -0
  99. multipers/gudhi/gudhi/Persistent_cohomology/Field_Zp.h +118 -0
  100. multipers/gudhi/gudhi/Persistent_cohomology/Multi_field.h +173 -0
  101. multipers/gudhi/gudhi/Persistent_cohomology/Persistent_cohomology_column.h +128 -0
  102. multipers/gudhi/gudhi/Persistent_cohomology.h +769 -0
  103. multipers/gudhi/gudhi/Points_off_io.h +171 -0
  104. multipers/gudhi/gudhi/Projective_cover_kernel.h +379 -0
  105. multipers/gudhi/gudhi/Simple_object_pool.h +69 -0
  106. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_iterators.h +559 -0
  107. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_node_explicit_storage.h +83 -0
  108. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_siblings.h +121 -0
  109. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_star_simplex_iterators.h +277 -0
  110. multipers/gudhi/gudhi/Simplex_tree/filtration_value_utils.h +155 -0
  111. multipers/gudhi/gudhi/Simplex_tree/hooks_simplex_base.h +62 -0
  112. multipers/gudhi/gudhi/Simplex_tree/indexing_tag.h +27 -0
  113. multipers/gudhi/gudhi/Simplex_tree/serialization_utils.h +60 -0
  114. multipers/gudhi/gudhi/Simplex_tree/simplex_tree_options.h +105 -0
  115. multipers/gudhi/gudhi/Simplex_tree.h +3170 -0
  116. multipers/gudhi/gudhi/Slicer.h +848 -0
  117. multipers/gudhi/gudhi/Thread_safe_slicer.h +393 -0
  118. multipers/gudhi/gudhi/distance_functions.h +62 -0
  119. multipers/gudhi/gudhi/graph_simplicial_complex.h +104 -0
  120. multipers/gudhi/gudhi/multi_simplex_tree_helpers.h +147 -0
  121. multipers/gudhi/gudhi/persistence_interval.h +263 -0
  122. multipers/gudhi/gudhi/persistence_matrix_options.h +188 -0
  123. multipers/gudhi/gudhi/reader_utils.h +367 -0
  124. multipers/gudhi/gudhi/simple_mdspan.h +484 -0
  125. multipers/gudhi/gudhi/slicer_helpers.h +779 -0
  126. multipers/gudhi/tmp_h0_pers/mma_interface_h0.h +223 -0
  127. multipers/gudhi/tmp_h0_pers/naive_merge_tree.h +536 -0
  128. multipers/io.cpython-312-darwin.so +0 -0
  129. multipers/io.pyx +472 -0
  130. multipers/ml/__init__.py +0 -0
  131. multipers/ml/accuracies.py +90 -0
  132. multipers/ml/invariants_with_persistable.py +79 -0
  133. multipers/ml/kernels.py +176 -0
  134. multipers/ml/mma.py +713 -0
  135. multipers/ml/one.py +472 -0
  136. multipers/ml/point_clouds.py +352 -0
  137. multipers/ml/signed_measures.py +1667 -0
  138. multipers/ml/sliced_wasserstein.py +461 -0
  139. multipers/ml/tools.py +113 -0
  140. multipers/mma_structures.cpython-312-darwin.so +0 -0
  141. multipers/mma_structures.pxd +134 -0
  142. multipers/mma_structures.pyx +1483 -0
  143. multipers/mma_structures.pyx.tp +1126 -0
  144. multipers/multi_parameter_rank_invariant/diff_helpers.h +85 -0
  145. multipers/multi_parameter_rank_invariant/euler_characteristic.h +95 -0
  146. multipers/multi_parameter_rank_invariant/function_rips.h +317 -0
  147. multipers/multi_parameter_rank_invariant/hilbert_function.h +761 -0
  148. multipers/multi_parameter_rank_invariant/persistence_slices.h +149 -0
  149. multipers/multi_parameter_rank_invariant/rank_invariant.h +350 -0
  150. multipers/multiparameter_edge_collapse.py +41 -0
  151. multipers/multiparameter_module_approximation/approximation.h +2541 -0
  152. multipers/multiparameter_module_approximation/debug.h +107 -0
  153. multipers/multiparameter_module_approximation/format_python-cpp.h +292 -0
  154. multipers/multiparameter_module_approximation/utilities.h +428 -0
  155. multipers/multiparameter_module_approximation.cpython-312-darwin.so +0 -0
  156. multipers/multiparameter_module_approximation.pyx +286 -0
  157. multipers/ops.cpython-312-darwin.so +0 -0
  158. multipers/ops.pyx +231 -0
  159. multipers/pickle.py +89 -0
  160. multipers/plots.py +550 -0
  161. multipers/point_measure.cpython-312-darwin.so +0 -0
  162. multipers/point_measure.pyx +409 -0
  163. multipers/simplex_tree_multi.cpython-312-darwin.so +0 -0
  164. multipers/simplex_tree_multi.pxd +136 -0
  165. multipers/simplex_tree_multi.pyx +11719 -0
  166. multipers/simplex_tree_multi.pyx.tp +2102 -0
  167. multipers/slicer.cpython-312-darwin.so +0 -0
  168. multipers/slicer.pxd +2097 -0
  169. multipers/slicer.pxd.tp +263 -0
  170. multipers/slicer.pyx +13042 -0
  171. multipers/slicer.pyx.tp +1259 -0
  172. multipers/tensor/tensor.h +672 -0
  173. multipers/tensor.pxd +13 -0
  174. multipers/test.pyx +44 -0
  175. multipers/tests/__init__.py +70 -0
  176. multipers/torch/__init__.py +1 -0
  177. multipers/torch/diff_grids.py +240 -0
  178. multipers/torch/rips_density.py +310 -0
  179. multipers/vector_interface.pxd +46 -0
  180. multipers-2.4.0b1.dist-info/METADATA +131 -0
  181. multipers-2.4.0b1.dist-info/RECORD +184 -0
  182. multipers-2.4.0b1.dist-info/WHEEL +6 -0
  183. multipers-2.4.0b1.dist-info/licenses/LICENSE +21 -0
  184. multipers-2.4.0b1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1367 @@
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
4
+ *
5
+ * Copyright (C) 2022-24 Inria
6
+ *
7
+ * Modification(s):
8
+ * - YYYY/MM Author: Description of the modification
9
+ */
10
+
11
+ /**
12
+ * @file Chain_matrix.h
13
+ * @author Hannah Schreiber
14
+ * @brief Contains the @ref Gudhi::persistence_matrix::Chain_matrix class.
15
+ */
16
+
17
+ #ifndef PM_CHAIN_MATRIX_H
18
+ #define PM_CHAIN_MATRIX_H
19
+
20
+ #include <iostream> //print() only
21
+ #include <set>
22
+ #include <map>
23
+ #include <stdexcept>
24
+ #include <vector>
25
+ #include <utility> //std::swap, std::move & std::exchange
26
+ #include <algorithm> //std::sort
27
+
28
+ #include <gudhi/Persistence_matrix/Id_to_index_overlay.h> //friend
29
+ #include <gudhi/Persistence_matrix/index_mapper.h>
30
+
31
+ namespace Gudhi {
32
+ namespace persistence_matrix {
33
+
34
+ /**
35
+ * @class Chain_matrix Chain_matrix.h gudhi/Persistence_matrix/Chain_matrix.h
36
+ * @ingroup persistence_matrix
37
+ *
38
+ * @brief %Matrix structure storing a compatible base of a filtered chain complex. See @cite zigzag.
39
+ * The base is constructed from the boundaries of the cells in the complex. Allows the persistent homology to be
40
+ * computed, as well as representative cycles. Supports vineyards (see @cite vineyards) and the removal
41
+ * of maximal cells while maintaining a valid barcode. Provides an access to its columns and rows.
42
+ *
43
+ * @tparam Master_matrix An instantiation of @ref Matrix from which all types and options are deduced.
44
+ */
45
+ template <class Master_matrix>
46
+ class Chain_matrix : public Master_matrix::Matrix_dimension_option,
47
+ public Master_matrix::Chain_pairing_option,
48
+ public Master_matrix::Chain_vine_swap_option,
49
+ public Master_matrix::Chain_representative_cycles_option,
50
+ public Master_matrix::Matrix_row_access_option,
51
+ protected std::conditional_t<
52
+ Master_matrix::Option_list::has_vine_update &&
53
+ (Master_matrix::Option_list::has_column_pairings ||
54
+ Master_matrix::Option_list::can_retrieve_representative_cycles),
55
+ Index_mapper<typename Master_matrix::template Dictionary<typename Master_matrix::Pos_index>>,
56
+ Dummy_index_mapper>
57
+ {
58
+ private:
59
+ using Dim_opt = typename Master_matrix::Matrix_dimension_option;
60
+ using Pair_opt = typename Master_matrix::Chain_pairing_option;
61
+ using Swap_opt = typename Master_matrix::Chain_vine_swap_option;
62
+ using Rep_opt = typename Master_matrix::Chain_representative_cycles_option;
63
+ using RA_opt = typename Master_matrix::Matrix_row_access_option;
64
+
65
+ static constexpr bool hasPivotToPosMap_ =
66
+ Master_matrix::Option_list::has_vine_update && (Master_matrix::Option_list::has_column_pairings ||
67
+ Master_matrix::Option_list::can_retrieve_representative_cycles);
68
+
69
+ using Pivot_to_pos_mapper_opt =
70
+ std::conditional_t<hasPivotToPosMap_,
71
+ Index_mapper<typename Master_matrix::template Dictionary<typename Master_matrix::Pos_index>>,
72
+ Dummy_index_mapper>;
73
+
74
+ public:
75
+ /**
76
+ * @brief Field operators class. Necessary only if @ref PersistenceMatrixOptions::is_z2 is false.
77
+ */
78
+ using Field_operators = typename Master_matrix::Field_operators;
79
+ using Field_element = typename Master_matrix::Element; /**< Type of an field element. */
80
+ using Column = typename Master_matrix::Column; /**< Column type. */
81
+ using Row = typename Master_matrix::Row; /**< Row type, only necessary with row
82
+ access option. */
83
+ using Entry = typename Master_matrix::Matrix_entry; /**< @ref Entry "Matrix entry" type. */
84
+ using Entry_constructor = typename Master_matrix::Entry_constructor; /**< Factory of @ref Entry classes. */
85
+ using Column_settings = typename Master_matrix::Column_settings; /**< Structure giving access to the columns
86
+ to necessary external classes. */
87
+ using Boundary = typename Master_matrix::Boundary; /**< Type of an input column. */
88
+ using Entry_representative = typename Master_matrix::Entry_representative; /**< %Entry content representative. */
89
+ using Index = typename Master_matrix::Index; /**< @ref MatIdx index type. */
90
+ using ID_index = typename Master_matrix::ID_index; /**< @ref IDIdx index type. */
91
+ using Pos_index = typename Master_matrix::Pos_index; /**< @ref PosIdx index type. */
92
+ using Dimension = typename Master_matrix::Dimension; /**< Dimension value type. */
93
+
94
+ /**
95
+ * @brief Constructs an empty matrix. Only available if @ref PersistenceMatrixOptions::has_column_pairings is
96
+ * true or @ref PersistenceMatrixOptions::has_vine_update is false. Otherwise, birth and death comparators have
97
+ * to be provided.
98
+ *
99
+ * @param colSettings Pointer to an existing setting structure for the columns. The structure should contain all
100
+ * the necessary external classes specifically necessary for the chosen column type, such as custom allocators.
101
+ */
102
+ Chain_matrix(Column_settings* colSettings);
103
+ /**
104
+ * @brief Constructs a new matrix from the given ranges of @ref Matrix::Entry_representative. Each range corresponds
105
+ * to a column (the order of the ranges are preserved). The content of the ranges is assumed to be sorted by
106
+ * increasing IDs. The IDs of the simplices are also assumed to be consecutive, ordered by filtration value, starting
107
+ * with 0. Only available if @ref PersistenceMatrixOptions::has_column_pairings is true or
108
+ * @ref PersistenceMatrixOptions::has_vine_update is false. Otherwise, birth and death
109
+ * comparators have to be provided.
110
+ *
111
+ * @tparam Boundary_range Range type for @ref Matrix::Entry_representative ranges.
112
+ * Assumed to have a begin(), end() and size() method.
113
+ * @param orderedBoundaries Range of boundaries: @p orderedBoundaries is interpreted as a boundary matrix of a
114
+ * filtered **simplicial** complex, whose boundaries are ordered by filtration order.
115
+ * Therefore, `orderedBoundaries[i]` should store the boundary of the \f$ i^{th} \f$ simplex in the filtration,
116
+ * as an ordered list of indices of its facets (again those indices correspond to their respective position
117
+ * in the matrix). That is why the indices of the simplices are assumed to be consecutive and starting with 0
118
+ * (an empty boundary is interpreted as a vertex boundary and not as a non existing simplex).
119
+ * All dimensions up to the maximal dimension of interest have to be present. If only a higher dimension is of
120
+ * interest and not everything should be stored, then use the @ref insert_boundary method instead
121
+ * (after creating the matrix with the
122
+ * @ref Chain_matrix(unsigned int numberOfColumns, Column_settings* colSettings)
123
+ * constructor preferably).
124
+ * @param colSettings Pointer to an existing setting structure for the columns. The structure should contain all
125
+ * the necessary external classes specifically necessary for the chosen column type, such as custom allocators.
126
+ */
127
+ template <class Boundary_range = Boundary>
128
+ Chain_matrix(const std::vector<Boundary_range>& orderedBoundaries, Column_settings* colSettings);
129
+ /**
130
+ * @brief Constructs a new empty matrix and reserves space for the given number of columns. Only available
131
+ * if @ref PersistenceMatrixOptions::has_column_pairings is true or @ref PersistenceMatrixOptions::has_vine_update
132
+ * is false. Otherwise, birth and death comparators have to be provided.
133
+ *
134
+ * @param numberOfColumns Number of columns to reserve space for.
135
+ * @param colSettings Pointer to an existing setting structure for the columns. The structure should contain all
136
+ * the necessary external classes specifically necessary for the chosen column type, such as custom allocators.
137
+ */
138
+ Chain_matrix(unsigned int numberOfColumns, Column_settings* colSettings);
139
+ /**
140
+ * @brief Constructs an empty matrix and stores the given comparators.
141
+ *
142
+ * @warning If @ref PersistenceMatrixOptions::has_vine_update is false, the comparators are not used.
143
+ * And if @ref PersistenceMatrixOptions::has_vine_update is true, but
144
+ * @ref PersistenceMatrixOptions::has_column_pairings is also true, the comparators are ignored and
145
+ * the current barcode is used to compare birth and deaths. Therefore it is useless to provide them in those cases.
146
+ *
147
+ * @tparam BirthComparatorFunction Type of the birth comparator: (@ref Pos_index, @ref Pos_index) -> bool
148
+ * @tparam DeathComparatorFunction Type of the death comparator: (@ref Pos_index, @ref Pos_index) -> bool
149
+ * @param colSettings Pointer to an existing setting structure for the columns. The structure should contain all
150
+ * the necessary external classes specifically necessary for the chosen column type, such as custom allocators.
151
+ * @param birthComparator Method taking two @ref PosIdx indices as input and returning true if and only if
152
+ * the birth associated to the first position is strictly less than birth associated to
153
+ * the second one with respect to some self defined order. It is used while swapping two unpaired or
154
+ * two negative columns.
155
+ * @param deathComparator Method taking two @ref PosIdx indices as input and returning true if and only if
156
+ * the death associated to the first position is strictly less than death associated to
157
+ * the second one with respect to some self defined order. It is used while swapping two positive but paired
158
+ * columns.
159
+ */
160
+ template <typename BirthComparatorFunction, typename DeathComparatorFunction>
161
+ Chain_matrix(Column_settings* colSettings,
162
+ const BirthComparatorFunction& birthComparator,
163
+ const DeathComparatorFunction& deathComparator);
164
+ /**
165
+ * @brief Constructs a new matrix from the given ranges of @ref Matrix::Entry_representative. Each range corresponds
166
+ * to a column (the order of the ranges are preserved). The content of the ranges is assumed to be sorted by
167
+ * increasing IDs. The IDs of the simplices are also assumed to be consecutive, ordered by filtration value, starting
168
+ * with 0.
169
+ *
170
+ * @warning If @ref PersistenceMatrixOptions::has_vine_update is false, the comparators are not used.
171
+ * And if @ref PersistenceMatrixOptions::has_vine_update is true, but
172
+ * @ref PersistenceMatrixOptions::has_column_pairings is also true, the comparators are ignored and
173
+ * the current barcode is used to compare birth and deaths. Therefore it is useless to provide them in those cases.
174
+ *
175
+ * @tparam BirthComparatorFunction Type of the birth comparator: (@ref Pos_index, @ref Pos_index) -> bool
176
+ * @tparam DeathComparatorFunction Type of the death comparator: (@ref Pos_index, @ref Pos_index) -> bool
177
+ * @tparam Boundary_range Range type for @ref Matrix::Entry_representative ranges.
178
+ * Assumed to have a begin(), end() and size() method.
179
+ * @param orderedBoundaries Range of boundaries: @p orderedBoundaries is interpreted as a boundary matrix of a
180
+ * filtered **simplicial** complex, whose boundaries are ordered by filtration order.
181
+ * Therefore, `orderedBoundaries[i]` should store the boundary of the \f$ i^{th} \f$ simplex in the filtration,
182
+ * as an ordered list of indices of its facets (again those indices correspond to their respective position
183
+ * in the matrix). That is why the indices of the simplices are assumed to be consecutive and starting with 0
184
+ * (an empty boundary is interpreted as a vertex boundary and not as a non existing simplex).
185
+ * All dimensions up to the maximal dimension of interest have to be present. If only a higher dimension is of
186
+ * interest and not everything should be stored, then use the @ref insert_boundary method instead
187
+ * (after creating the matrix with the @ref Chain_matrix(unsigned int, Column_settings*,
188
+ * const BirthComparatorFunction&, const DeathComparatorFunction&) constructor preferably).
189
+ * @param colSettings Pointer to an existing setting structure for the columns. The structure should contain all
190
+ * the necessary external classes specifically necessary for the chosen column type, such as custom allocators.
191
+ * @param birthComparator Method taking two @ref PosIdx indices as input and returning true if and only if
192
+ * the birth associated to the first position is strictly less than birth associated to
193
+ * the second one with respect to some self defined order. It is used while swapping two unpaired or
194
+ * two negative columns.
195
+ * @param deathComparator Method taking two @ref PosIdx indices as input and returning true if and only if
196
+ * the death associated to the first position is strictly less than death associated to
197
+ * the second one with respect to some self defined order. It is used while swapping two positive but paired
198
+ * columns.
199
+ */
200
+ template <typename BirthComparatorFunction, typename DeathComparatorFunction, class Boundary_range = Boundary>
201
+ Chain_matrix(const std::vector<Boundary_range>& orderedBoundaries,
202
+ Column_settings* colSettings,
203
+ const BirthComparatorFunction& birthComparator,
204
+ const DeathComparatorFunction& deathComparator);
205
+ /**
206
+ * @brief Constructs a new empty matrix and reserves space for the given number of columns.
207
+ *
208
+ * @warning If @ref PersistenceMatrixOptions::has_vine_update is false, the comparators are not used.
209
+ * And if @ref PersistenceMatrixOptions::has_vine_update is true, but
210
+ * @ref PersistenceMatrixOptions::has_column_pairings is also true, the comparators are ignored and
211
+ * the current barcode is used to compare birth and deaths. Therefore it is useless to provide them in those cases.
212
+ *
213
+ * @tparam BirthComparatorFunction Type of the birth comparator: (@ref Pos_index, @ref Pos_index) -> bool
214
+ * @tparam DeathComparatorFunction Type of the death comparator: (@ref Pos_index, @ref Pos_index) -> bool
215
+ * @param numberOfColumns Number of columns to reserve space for.
216
+ * @param colSettings Pointer to an existing setting structure for the columns. The structure should contain all
217
+ * the necessary external classes specifically necessary for the chosen column type, such as custom allocators.
218
+ * @param birthComparator Method taking two @ref PosIdx indices as input and returning true if and only if
219
+ * the birth associated to the first position is strictly less than birth associated to
220
+ * the second one with respect to some self defined order. It is used while swapping two unpaired or
221
+ * two negative columns.
222
+ * @param deathComparator Method taking two @ref PosIdx indices as input and returning true if and only if
223
+ * the death associated to the first position is strictly less than death associated to
224
+ * the second one with respect to some self defined order. It is used while swapping two positive but paired
225
+ * columns.
226
+ */
227
+ template <typename BirthComparatorFunction, typename DeathComparatorFunction>
228
+ Chain_matrix(unsigned int numberOfColumns,
229
+ Column_settings* colSettings,
230
+ const BirthComparatorFunction& birthComparator,
231
+ const DeathComparatorFunction& deathComparator);
232
+ /**
233
+ * @brief Copy constructor. If @p colSettings is not a null pointer, its value is kept
234
+ * instead of the one in the copied matrix.
235
+ *
236
+ * @param matrixToCopy Matrix to copy.
237
+ * @param colSettings Either a pointer to an existing setting structure for the columns or a null pointer.
238
+ * The structure should contain all the necessary external classes specifically necessary for the chosen column type,
239
+ * such as custom allocators. If null pointer, the pointer stored in @p matrixToCopy is used instead.
240
+ */
241
+ Chain_matrix(const Chain_matrix& matrixToCopy, Column_settings* colSettings = nullptr);
242
+ /**
243
+ * @brief Move constructor.
244
+ *
245
+ * @param other Matrix to move.
246
+ */
247
+ Chain_matrix(Chain_matrix&& other) noexcept;
248
+
249
+ ~Chain_matrix() = default;
250
+
251
+ /**
252
+ * @brief Inserts at the end of the matrix a new ordered column corresponding to the given boundary.
253
+ * This means that it is assumed that this method is called on boundaries in the order of the filtration.
254
+ * It also assumes that the cells in the given boundary are identified by their relative position in the filtration,
255
+ * starting at 0. If it is not the case, use the other
256
+ * @ref insert_boundary(ID_index cellIndex, const Boundary_range& boundary, Dimension dim) "insert_boundary"
257
+ * instead by indicating the cell ID used in the boundaries when the cell is inserted.
258
+ *
259
+ * Different to the constructor, the boundaries do not have to come from a simplicial complex, but also from
260
+ * a more general entry complex. This includes cubical complexes or Morse complexes for example.
261
+ *
262
+ * When inserted, the given boundary is reduced and from the reduction process, the column is deduced in the form of:
263
+ * `IDIdx + linear combination of older column IDIdxs`. If the barcode is stored, it will be updated.
264
+ *
265
+ * @tparam Boundary_range Range of @ref Matrix::Entry_representative. Assumed to have a begin(), end() and size()
266
+ * method.
267
+ * @param boundary Boundary generating the new column. The content should be ordered by ID.
268
+ * @param dim Dimension of the cell whose boundary is given. If the complex is simplicial,
269
+ * this parameter can be omitted as it can be deduced from the size of the boundary.
270
+ * @return The @ref MatIdx indices of the unpaired chains used to reduce the boundary.
271
+ */
272
+ template <class Boundary_range = Boundary>
273
+ std::vector<Entry_representative> insert_boundary(
274
+ const Boundary_range& boundary,
275
+ Dimension dim = Master_matrix::template get_null_value<Dimension>());
276
+ /**
277
+ * @brief It does the same as the other version, but allows the boundary cells to be identified without restrictions
278
+ * except that all IDs have to be strictly increasing in the order of filtration. Note that you should avoid then
279
+ * to use the other insertion method to avoid overwriting IDs.
280
+ *
281
+ * As a cell has to be inserted before one of its cofaces in a valid filtration (recall that it is assumed that
282
+ * the cells are inserted by order of filtration), it is sufficient to indicate the ID of the cell being inserted.
283
+ *
284
+ * @tparam Boundary_range Range of @ref Matrix::Entry_representative. Assumed to have a begin(), end() and size()
285
+ * method.
286
+ * @param cellID @ref IDIdx index to use to identify the new cell.
287
+ * @param boundary Boundary generating the new column. The indices of the boundary have to correspond to the
288
+ * @p cellID values of precedent calls of the method for the corresponding cells and should be ordered in
289
+ * increasing order.
290
+ * @param dim Dimension of the cell whose boundary is given. If the complex is simplicial,
291
+ * this parameter can be omitted as it can be deduced from the size of the boundary.
292
+ * @return The @ref MatIdx index of the inserted boundary.
293
+ */
294
+ template <class Boundary_range = Boundary>
295
+ std::vector<Entry_representative> insert_boundary(
296
+ ID_index cellID,
297
+ const Boundary_range& boundary,
298
+ Dimension dim = Master_matrix::template get_null_value<Dimension>());
299
+ /**
300
+ * @brief Returns the column at the given @ref MatIdx index.
301
+ * The type of the column depends on the chosen options, see @ref PersistenceMatrixOptions::column_type.
302
+ *
303
+ * @param columnIndex @ref MatIdx index of the column to return.
304
+ * @return Reference to the column.
305
+ */
306
+ Column& get_column(Index columnIndex);
307
+ /**
308
+ * @brief Returns the column at the given @ref MatIdx index.
309
+ * The type of the column depends on the chosen options, see @ref PersistenceMatrixOptions::column_type.
310
+ *
311
+ * @param columnIndex @ref MatIdx index of the column to return.
312
+ * @return Const reference to the column.
313
+ */
314
+ const Column& get_column(Index columnIndex) const;
315
+ /**
316
+ * @brief Only available if @ref PersistenceMatrixOptions::has_removable_columns and
317
+ * @ref PersistenceMatrixOptions::has_vine_update are true, as well as,
318
+ * @ref PersistenceMatrixOptions::has_map_column_container and @ref PersistenceMatrixOptions::has_column_pairings.
319
+ * Assumes that the cell is maximal in the current complex and removes it such that the matrix remains consistent
320
+ * (i.e., the matrix is still a compatible bases of the chain complex in the sense of @cite zigzag).
321
+ * The maximality of the cell is not verified.
322
+ * Also updates the barcode if it is stored.
323
+ *
324
+ * Note that using the other version of the method could perform better depending on how the data is
325
+ * maintained on the side of the user, that is, if providing the second parameter is easy.
326
+ *
327
+ * See also @ref remove_last.
328
+ *
329
+ * @param cellID @ref IDIdx index of the cell to remove
330
+ */
331
+ void remove_maximal_cell(ID_index cellID);
332
+ /**
333
+ * @brief Only available if @ref PersistenceMatrixOptions::has_removable_columns,
334
+ * @ref PersistenceMatrixOptions::has_vine_update and @ref PersistenceMatrixOptions::has_map_column_container
335
+ * are true.
336
+ * Assumes that the cell is maximal in the current complex and removes it such that the matrix remains consistent
337
+ * (i.e., it is still a compatible bases of the chain complex in the sense of @cite zigzag).
338
+ * The maximality of the cell is not verified.
339
+ * Also updates the barcode if it is stored.
340
+ *
341
+ * To maintain the compatibility, vine swaps are done to move the cell up to the end of the filtration. Once at
342
+ * the end, the removal is trivial. But for @ref chainmatrix "chain matrices", swaps do not actually swap the position
343
+ * of the column every time, so the cells appearing after @p cellID in the filtration have to be searched first within
344
+ * the matrix. If the user has an easy access to the @ref IDIdx of the cells in the order of filtration, passing them
345
+ * by argument with @p columnsToSwap allows to skip a linear search process. Typically, if the user knows that the
346
+ * cell he wants to remove is already the last cell of the filtration, calling
347
+ * @ref remove_maximal_cell(ID_index cellIndex, const std::vector<ID_index>& columnsToSwap)
348
+ * "remove_maximal_cell(cellID, {})" will be faster than @ref remove_last().
349
+ *
350
+ * See also @ref remove_last.
351
+ *
352
+ * @param cellID @ref IDIdx index of the cell to remove
353
+ * @param columnsToSwap Vector of @ref IDIdx indices of the cells coming after @p cellID in the filtration.
354
+ */
355
+ void remove_maximal_cell(ID_index cellID, const std::vector<ID_index>& columnsToSwap);
356
+ /**
357
+ * @brief Only available if @ref PersistenceMatrixOptions::has_removable_columns is true and,
358
+ * if @ref PersistenceMatrixOptions::has_map_column_container is true or
359
+ * @ref PersistenceMatrixOptions::has_vine_update is false.
360
+ * Removes the last cell in the filtration from the matrix and updates the barcode if it is stored.
361
+ *
362
+ * See also @ref remove_maximal_cell.
363
+ *
364
+ * @warning If @ref PersistenceMatrixOptions::has_vine_update is true, the last cell does not have to
365
+ * be at the end of the matrix container and therefore has to be searched first. In this case, if the user
366
+ * already knows the @ref IDIdx of the last cell, calling
367
+ * @ref remove_maximal_cell(ID_index cellIndex, const std::vector<ID_index>& columnsToSwap)
368
+ * "remove_maximal_cell(cellID, {})" instead allows to skip the search.
369
+ */
370
+ void remove_last();
371
+
372
+ /**
373
+ * @brief Returns the current number of columns in the matrix.
374
+ *
375
+ * @return The number of columns.
376
+ */
377
+ Index get_number_of_columns() const;
378
+
379
+ /**
380
+ * @brief Returns the dimension of the given column.
381
+ *
382
+ * @param columnIndex @ref MatIdx index of the column representing the cell.
383
+ * @return Dimension of the cell.
384
+ */
385
+ Dimension get_column_dimension(Index columnIndex) const;
386
+
387
+ /**
388
+ * @brief Adds column at @p sourceColumnIndex onto the column at @p targetColumnIndex in the matrix.
389
+ *
390
+ * @warning They will be no verification to ensure that the addition makes sense for the validity of a
391
+ * @ref chainmatrix "chain matrix". For example, a right-to-left addition could corrupt the computation
392
+ * of the barcode if done blindly. So should be used with care.
393
+ *
394
+ * @param sourceColumnIndex @ref MatIdx index of the source column.
395
+ * @param targetColumnIndex @ref MatIdx index of the target column.
396
+ */
397
+ void add_to(Index sourceColumnIndex, Index targetColumnIndex);
398
+ /**
399
+ * @brief Multiplies the target column with the coefficient and then adds the source column to it.
400
+ * That is: `targetColumn = (targetColumn * coefficient) + sourceColumn`.
401
+ *
402
+ * @warning They will be no verification to ensure that the addition makes sense for the validity of a
403
+ * @ref chainmatrix "chain matrix". For example, a right-to-left addition could corrupt the computation
404
+ * of the barcode if done blindly. So should be used with care.
405
+ *
406
+ * @param sourceColumnIndex @ref MatIdx index of the source column.
407
+ * @param coefficient Value to multiply.
408
+ * @param targetColumnIndex @ref MatIdx index of the target column.
409
+ */
410
+ void multiply_target_and_add_to(Index sourceColumnIndex, const Field_element& coefficient, Index targetColumnIndex);
411
+ /**
412
+ * @brief Multiplies the source column with the coefficient before adding it to the target column.
413
+ * That is: `targetColumn += (coefficient * sourceColumn)`. The source column will **not** be modified.
414
+ *
415
+ * @warning They will be no verification to ensure that the addition makes sense for the validity of a
416
+ * @ref chainmatrix "chain matrix". For example, a right-to-left addition could corrupt the computation
417
+ * of the barcode if done blindly. So should be used with care.
418
+ *
419
+ * @param coefficient Value to multiply.
420
+ * @param sourceColumnIndex @ref MatIdx index of the source column.
421
+ * @param targetColumnIndex @ref MatIdx index of the target column.
422
+ */
423
+ void multiply_source_and_add_to(const Field_element& coefficient, Index sourceColumnIndex, Index targetColumnIndex);
424
+
425
+ /**
426
+ * @brief Indicates if the entry at given coordinates has value zero.
427
+ *
428
+ * @param columnIndex @ref MatIdx index of the column of the entry.
429
+ * @param rowIndex @ref rowindex "Row index" of the row of the entry.
430
+ * @return true If the entry has value zero.
431
+ * @return false Otherwise.
432
+ */
433
+ bool is_zero_entry(Index columnIndex, ID_index rowIndex) const;
434
+ /**
435
+ * @brief Indicates if the column at given index has value zero. Note that if the matrix is valid, this method
436
+ * should always return false.
437
+ *
438
+ * @param columnIndex @ref MatIdx index of the column.
439
+ * @return true If the column has value zero.
440
+ * @return false Otherwise.
441
+ */
442
+ bool is_zero_column(Index columnIndex);
443
+
444
+ /**
445
+ * @brief Returns the column with given @ref rowindex "row index" as pivot. Assumes that the pivot exists.
446
+ *
447
+ * @param cellID @ref rowindex "Row index" of the pivot.
448
+ * @return @ref MatIdx index of the column with the given pivot.
449
+ */
450
+ Index get_column_with_pivot(ID_index cellID) const;
451
+ /**
452
+ * @brief Returns the @ref rowindex "row index" of the pivot of the given column.
453
+ *
454
+ * @param columnIndex @ref MatIdx index of the column
455
+ * @return The @ref rowindex "row index" of the pivot.
456
+ */
457
+ ID_index get_pivot(Index columnIndex);
458
+
459
+ /**
460
+ * @brief Resets the matrix to an empty matrix.
461
+ *
462
+ * @param colSettings Pointer to an existing setting structure for the columns. The structure should contain all
463
+ * the necessary external classes specifically necessary for the chosen column type, such as custom allocators.
464
+ */
465
+ void reset(Column_settings* colSettings)
466
+ {
467
+ if constexpr (Master_matrix::Option_list::has_matrix_maximal_dimension_access) Dim_opt::_reset();
468
+ if constexpr (Master_matrix::Option_list::has_column_pairings) Pair_opt::_reset();
469
+ if constexpr (Master_matrix::Option_list::can_retrieve_representative_cycles) Rep_opt::_reset();
470
+ if constexpr (hasPivotToPosMap_) Pivot_to_pos_mapper_opt::map_.clear();
471
+ matrix_.clear();
472
+ pivotToColumnIndex_.clear();
473
+ nextIndex_ = 0;
474
+ nextPosition_ = 0;
475
+ colSettings_ = colSettings;
476
+ }
477
+
478
+ /**
479
+ * @brief Assign operator.
480
+ */
481
+ Chain_matrix& operator=(const Chain_matrix& other);
482
+ /**
483
+ * @brief Move assign operator.
484
+ */
485
+ Chain_matrix& operator=(Chain_matrix&& other) noexcept;
486
+
487
+ /**
488
+ * @brief Swap operator.
489
+ */
490
+ friend void swap(Chain_matrix& matrix1, Chain_matrix& matrix2) noexcept
491
+ {
492
+ swap(static_cast<Dim_opt&>(matrix1), static_cast<Dim_opt&>(matrix2));
493
+ swap(static_cast<Pair_opt&>(matrix1), static_cast<Pair_opt&>(matrix2));
494
+ swap(static_cast<Swap_opt&>(matrix1), static_cast<Swap_opt&>(matrix2));
495
+ swap(static_cast<Rep_opt&>(matrix1), static_cast<Rep_opt&>(matrix2));
496
+ swap(static_cast<Pivot_to_pos_mapper_opt&>(matrix1), static_cast<Pivot_to_pos_mapper_opt&>(matrix2));
497
+ matrix1.matrix_.swap(matrix2.matrix_);
498
+ matrix1.pivotToColumnIndex_.swap(matrix2.pivotToColumnIndex_);
499
+ std::swap(matrix1.nextIndex_, matrix2.nextIndex_);
500
+ std::swap(matrix1.nextPosition_, matrix2.nextPosition_);
501
+ std::swap(matrix1.colSettings_, matrix2.colSettings_);
502
+
503
+ if constexpr (Master_matrix::Option_list::has_row_access) {
504
+ swap(static_cast<RA_opt&>(matrix1), static_cast<RA_opt&>(matrix2));
505
+ }
506
+ }
507
+
508
+ void print() const; // for debug
509
+
510
+ friend class Id_to_index_overlay<Chain_matrix<Master_matrix>, Master_matrix>;
511
+
512
+ private:
513
+ using Column_container = typename Master_matrix::Column_container;
514
+ using Dictionary = typename Master_matrix::template Dictionary<Index>;
515
+ using Barcode = typename Master_matrix::Barcode;
516
+ using Bar_dictionary = typename Master_matrix::Bar_dictionary;
517
+ using Tmp_column = typename std::
518
+ conditional<Master_matrix::Option_list::is_z2, std::set<ID_index>, std::map<ID_index, Field_element>>::type;
519
+
520
+ friend Swap_opt; // direct access to index mapper
521
+ friend Pair_opt; // direct access to index mapper
522
+ friend Rep_opt; // direct access to index mapper
523
+
524
+ Column_container matrix_; /**< Column container. */
525
+ Dictionary pivotToColumnIndex_; /**< Map from @ref IDIdx to @ref MatIdx index. */
526
+ Index nextIndex_; /**< Next unused column index. */
527
+ Pos_index nextPosition_; /**< Next relative position in the filtration. */
528
+ Column_settings* colSettings_; /**< Entry factory. */
529
+
530
+ template <class Boundary_range>
531
+ std::vector<Entry_representative> _reduce_boundary(ID_index cellID, const Boundary_range& boundary, Dimension dim);
532
+ void _reduce_by_G(Tmp_column& column, std::vector<Entry_representative>& chainsInH, Index currentIndex);
533
+ void _reduce_by_F(Tmp_column& column, std::vector<Entry_representative>& chainsInF, Index currentIndex);
534
+ void _build_from_H(ID_index cellID, Tmp_column& column, std::vector<Entry_representative>& chainsInH);
535
+ void _update_largest_death_in_F(const std::vector<Entry_representative>& chainsInF);
536
+ void _insert_chain(const Tmp_column& column, Dimension dimension);
537
+ void _insert_chain(const Tmp_column& column, Dimension dimension, Index pair);
538
+ void _add_to(const Column& column, Tmp_column& set, unsigned int coef);
539
+ template <typename F>
540
+ void _add_to(Column& target, F&& addition);
541
+ void _remove_last(Index lastIndex);
542
+ void _update_barcode(Pos_index birth);
543
+ void _add_bar(Dimension dim);
544
+ template <class Container>
545
+ void _container_insert(const Container& column, Index pos, Dimension dim);
546
+ template <class ColumnIterator>
547
+ void _container_insert(const ColumnIterator& rep);
548
+
549
+ static void _insert_in(ID_index cellID, Tmp_column& column);
550
+ };
551
+
552
+ template <class Master_matrix>
553
+ inline Chain_matrix<Master_matrix>::Chain_matrix(Column_settings* colSettings)
554
+ : Dim_opt(Master_matrix::template get_null_value<Dimension>()),
555
+ Pair_opt(),
556
+ Swap_opt(),
557
+ Rep_opt(),
558
+ RA_opt(),
559
+ Pivot_to_pos_mapper_opt(),
560
+ nextIndex_(0),
561
+ nextPosition_(0),
562
+ colSettings_(colSettings)
563
+ {}
564
+
565
+ template <class Master_matrix>
566
+ template <class Boundary_range>
567
+ inline Chain_matrix<Master_matrix>::Chain_matrix(const std::vector<Boundary_range>& orderedBoundaries,
568
+ Column_settings* colSettings)
569
+ : Dim_opt(Master_matrix::template get_null_value<Dimension>()),
570
+ Pair_opt(),
571
+ Swap_opt(),
572
+ Rep_opt(),
573
+ RA_opt(orderedBoundaries.size()),
574
+ Pivot_to_pos_mapper_opt(),
575
+ nextIndex_(0),
576
+ nextPosition_(0),
577
+ colSettings_(colSettings)
578
+ {
579
+ matrix_.reserve(orderedBoundaries.size());
580
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
581
+ pivotToColumnIndex_.reserve(orderedBoundaries.size());
582
+ } else {
583
+ pivotToColumnIndex_.resize(orderedBoundaries.size(), Master_matrix::template get_null_value<Index>());
584
+ }
585
+
586
+ for (const Boundary_range& b : orderedBoundaries) {
587
+ insert_boundary(b);
588
+ }
589
+ }
590
+
591
+ template <class Master_matrix>
592
+ inline Chain_matrix<Master_matrix>::Chain_matrix(unsigned int numberOfColumns, Column_settings* colSettings)
593
+ : Dim_opt(Master_matrix::template get_null_value<Dimension>()),
594
+ Pair_opt(),
595
+ Swap_opt(),
596
+ Rep_opt(),
597
+ RA_opt(numberOfColumns),
598
+ Pivot_to_pos_mapper_opt(),
599
+ nextIndex_(0),
600
+ nextPosition_(0),
601
+ colSettings_(colSettings)
602
+ {
603
+ matrix_.reserve(numberOfColumns);
604
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
605
+ pivotToColumnIndex_.reserve(numberOfColumns);
606
+ } else {
607
+ pivotToColumnIndex_.resize(numberOfColumns, Master_matrix::template get_null_value<Index>());
608
+ }
609
+ }
610
+
611
+ template <class Master_matrix>
612
+ template <typename BirthComparatorFunction, typename DeathComparatorFunction>
613
+ inline Chain_matrix<Master_matrix>::Chain_matrix(Column_settings* colSettings,
614
+ const BirthComparatorFunction& birthComparator,
615
+ const DeathComparatorFunction& deathComparator)
616
+ : Dim_opt(Master_matrix::template get_null_value<Dimension>()),
617
+ Pair_opt(),
618
+ Swap_opt(birthComparator, deathComparator),
619
+ Rep_opt(),
620
+ RA_opt(),
621
+ Pivot_to_pos_mapper_opt(),
622
+ nextIndex_(0),
623
+ nextPosition_(0),
624
+ colSettings_(colSettings)
625
+ {}
626
+
627
+ template <class Master_matrix>
628
+ template <typename BirthComparatorFunction, typename DeathComparatorFunction, class Boundary_range>
629
+ inline Chain_matrix<Master_matrix>::Chain_matrix(const std::vector<Boundary_range>& orderedBoundaries,
630
+ Column_settings* colSettings,
631
+ const BirthComparatorFunction& birthComparator,
632
+ const DeathComparatorFunction& deathComparator)
633
+ : Dim_opt(Master_matrix::template get_null_value<Dimension>()),
634
+ Pair_opt(),
635
+ Swap_opt(birthComparator, deathComparator),
636
+ Rep_opt(),
637
+ RA_opt(orderedBoundaries.size()),
638
+ Pivot_to_pos_mapper_opt(),
639
+ nextIndex_(0),
640
+ nextPosition_(0),
641
+ colSettings_(colSettings)
642
+ {
643
+ matrix_.reserve(orderedBoundaries.size());
644
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
645
+ pivotToColumnIndex_.reserve(orderedBoundaries.size());
646
+ } else {
647
+ pivotToColumnIndex_.resize(orderedBoundaries.size(), Master_matrix::template get_null_value<Index>());
648
+ }
649
+ for (const Boundary_range& b : orderedBoundaries) {
650
+ insert_boundary(b);
651
+ }
652
+ }
653
+
654
+ template <class Master_matrix>
655
+ template <typename BirthComparatorFunction, typename DeathComparatorFunction>
656
+ inline Chain_matrix<Master_matrix>::Chain_matrix(unsigned int numberOfColumns,
657
+ Column_settings* colSettings,
658
+ const BirthComparatorFunction& birthComparator,
659
+ const DeathComparatorFunction& deathComparator)
660
+ : Dim_opt(Master_matrix::template get_null_value<Dimension>()),
661
+ Pair_opt(),
662
+ Swap_opt(birthComparator, deathComparator),
663
+ Rep_opt(),
664
+ RA_opt(numberOfColumns),
665
+ Pivot_to_pos_mapper_opt(),
666
+ nextIndex_(0),
667
+ nextPosition_(0),
668
+ colSettings_(colSettings)
669
+ {
670
+ matrix_.reserve(numberOfColumns);
671
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
672
+ pivotToColumnIndex_.reserve(numberOfColumns);
673
+ } else {
674
+ pivotToColumnIndex_.resize(numberOfColumns, Master_matrix::template get_null_value<Index>());
675
+ }
676
+ }
677
+
678
+ template <class Master_matrix>
679
+ inline Chain_matrix<Master_matrix>::Chain_matrix(const Chain_matrix& matrixToCopy, Column_settings* colSettings)
680
+ : Dim_opt(static_cast<const Dim_opt&>(matrixToCopy)),
681
+ Pair_opt(static_cast<const Pair_opt&>(matrixToCopy)),
682
+ Swap_opt(static_cast<const Swap_opt&>(matrixToCopy)),
683
+ Rep_opt(static_cast<const Rep_opt&>(matrixToCopy)),
684
+ RA_opt(static_cast<const RA_opt&>(matrixToCopy)),
685
+ Pivot_to_pos_mapper_opt(static_cast<const Pivot_to_pos_mapper_opt&>(matrixToCopy)),
686
+ pivotToColumnIndex_(matrixToCopy.pivotToColumnIndex_),
687
+ nextIndex_(matrixToCopy.nextIndex_),
688
+ nextPosition_(matrixToCopy.nextPosition_),
689
+ colSettings_(colSettings == nullptr ? matrixToCopy.colSettings_ : colSettings)
690
+ {
691
+ matrix_.reserve(matrixToCopy.matrix_.size());
692
+ for (const auto& cont : matrixToCopy.matrix_) {
693
+ _container_insert(cont);
694
+ }
695
+ }
696
+
697
+ template <class Master_matrix>
698
+ inline Chain_matrix<Master_matrix>::Chain_matrix(Chain_matrix&& other) noexcept
699
+ : Dim_opt(std::move(static_cast<Dim_opt&>(other))),
700
+ Pair_opt(std::move(static_cast<Pair_opt&>(other))),
701
+ Swap_opt(std::move(static_cast<Swap_opt&>(other))),
702
+ Rep_opt(std::move(static_cast<Rep_opt&>(other))),
703
+ RA_opt(std::move(static_cast<RA_opt&>(other))),
704
+ Pivot_to_pos_mapper_opt(std::move(static_cast<Pivot_to_pos_mapper_opt&>(other))),
705
+ matrix_(std::move(other.matrix_)),
706
+ pivotToColumnIndex_(std::move(other.pivotToColumnIndex_)),
707
+ nextIndex_(std::exchange(other.nextIndex_, 0)),
708
+ nextPosition_(std::exchange(other.nextPosition_, 0)),
709
+ colSettings_(std::exchange(other.colSettings_, nullptr))
710
+ {}
711
+
712
+ template <class Master_matrix>
713
+ template <class Boundary_range>
714
+ inline std::vector<typename Master_matrix::Entry_representative> Chain_matrix<Master_matrix>::insert_boundary(
715
+ const Boundary_range& boundary,
716
+ Dimension dim)
717
+ {
718
+ return insert_boundary(nextIndex_, boundary, dim);
719
+ }
720
+
721
+ template <class Master_matrix>
722
+ template <class Boundary_range>
723
+ inline std::vector<typename Master_matrix::Entry_representative>
724
+ Chain_matrix<Master_matrix>::insert_boundary(ID_index cellID, const Boundary_range& boundary, Dimension dim)
725
+ {
726
+ if constexpr (!Master_matrix::Option_list::has_map_column_container) {
727
+ if (pivotToColumnIndex_.size() <= cellID) {
728
+ pivotToColumnIndex_.resize((cellID * 2) + 1, Master_matrix::template get_null_value<Index>());
729
+ }
730
+ }
731
+
732
+ if constexpr (hasPivotToPosMap_) {
733
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
734
+ Pivot_to_pos_mapper_opt::map_.try_emplace(cellID, nextPosition_);
735
+ } else {
736
+ if (Pivot_to_pos_mapper_opt::map_.size() <= cellID)
737
+ Pivot_to_pos_mapper_opt::map_.resize(pivotToColumnIndex_.size(),
738
+ Master_matrix::template get_null_value<Pos_index>());
739
+ Pivot_to_pos_mapper_opt::map_[cellID] = nextPosition_;
740
+ }
741
+ }
742
+
743
+ if constexpr (Master_matrix::Option_list::has_matrix_maximal_dimension_access) {
744
+ Dim_opt::_update_up(dim == Master_matrix::template get_null_value<Dimension>()
745
+ ? (boundary.size() == 0 ? 0 : boundary.size() - 1)
746
+ : dim);
747
+ }
748
+
749
+ return _reduce_boundary(cellID, boundary, dim);
750
+ }
751
+
752
+ template <class Master_matrix>
753
+ inline typename Chain_matrix<Master_matrix>::Column& Chain_matrix<Master_matrix>::get_column(Index columnIndex)
754
+ {
755
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
756
+ return matrix_.at(columnIndex);
757
+ } else {
758
+ return matrix_[columnIndex];
759
+ }
760
+ }
761
+
762
+ template <class Master_matrix>
763
+ inline const typename Chain_matrix<Master_matrix>::Column& Chain_matrix<Master_matrix>::get_column(
764
+ Index columnIndex) const
765
+ {
766
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
767
+ return matrix_.at(columnIndex);
768
+ } else {
769
+ return matrix_[columnIndex];
770
+ }
771
+ }
772
+
773
+ template <class Master_matrix>
774
+ inline void Chain_matrix<Master_matrix>::remove_maximal_cell(ID_index cellID)
775
+ {
776
+ static_assert(Master_matrix::Option_list::has_removable_columns,
777
+ "'remove_maximal_cell' is not implemented for the chosen options.");
778
+ static_assert(Master_matrix::Option_list::has_map_column_container && Master_matrix::Option_list::has_vine_update &&
779
+ Master_matrix::Option_list::has_column_pairings,
780
+ "'remove_maximal_cell' is not implemented for the chosen options.");
781
+
782
+ // TODO: find simple test to verify that col at columnIndex is maximal even without row access.
783
+
784
+ const auto& pivotToPosition = Pivot_to_pos_mapper_opt::map_;
785
+ auto it = pivotToPosition.find(cellID);
786
+ if (it == pivotToPosition.end()) return; // cell does not exists. TODO: put an assert instead?
787
+ Pos_index startPos = it->second;
788
+ Index startIndex = pivotToColumnIndex_.at(cellID);
789
+
790
+ if (startPos != nextPosition_ - 1) {
791
+ std::vector<Index> colToSwap;
792
+ colToSwap.reserve(matrix_.size());
793
+
794
+ for (auto& p : pivotToPosition) {
795
+ if (p.second > startPos) colToSwap.push_back(pivotToColumnIndex_.at(p.first));
796
+ }
797
+ std::sort(colToSwap.begin(), colToSwap.end(), [&](Index c1, Index c2) {
798
+ return pivotToPosition.at(get_pivot(c1)) < pivotToPosition.at(get_pivot(c2));
799
+ });
800
+
801
+ for (Index i : colToSwap) {
802
+ startIndex = Swap_opt::vine_swap(startIndex, i);
803
+ }
804
+ }
805
+
806
+ _remove_last(startIndex);
807
+ }
808
+
809
+ template <class Master_matrix>
810
+ inline void Chain_matrix<Master_matrix>::remove_maximal_cell(ID_index cellID,
811
+ const std::vector<ID_index>& columnsToSwap)
812
+ {
813
+ static_assert(Master_matrix::Option_list::has_removable_columns,
814
+ "'remove_maximal_cell' is not implemented for the chosen options.");
815
+ static_assert(Master_matrix::Option_list::has_map_column_container && Master_matrix::Option_list::has_vine_update,
816
+ "'remove_maximal_cell' is not implemented for the chosen options.");
817
+
818
+ // TODO: find simple test to verify that col at columnIndex is maximal even without row access.
819
+
820
+ Index startIndex = pivotToColumnIndex_.at(cellID);
821
+
822
+ for (ID_index i : columnsToSwap) {
823
+ startIndex = Swap_opt::vine_swap(startIndex, pivotToColumnIndex_.at(i));
824
+ }
825
+
826
+ _remove_last(startIndex);
827
+ }
828
+
829
+ template <class Master_matrix>
830
+ inline void Chain_matrix<Master_matrix>::remove_last()
831
+ {
832
+ static_assert(Master_matrix::Option_list::has_removable_columns,
833
+ "'remove_last' is not implemented for the chosen options.");
834
+ static_assert(Master_matrix::Option_list::has_map_column_container || !Master_matrix::Option_list::has_vine_update,
835
+ "'remove_last' is not implemented for the chosen options.");
836
+
837
+ if (nextIndex_ == 0 || matrix_.empty()) return; // empty matrix
838
+
839
+ if constexpr (Master_matrix::Option_list::has_vine_update) {
840
+ // careful: linear because of the search of the last index. It is better to keep track of the @ref IDIdx index
841
+ // of the last column while performing swaps (or the @ref MatIdx with the return values of `vine_swap` + get_pivot)
842
+ // and then call `remove_maximal_cell` with it and an empty `columnsToSwap`.
843
+
844
+ ID_index pivot = 0;
845
+ Index colIndex = 0;
846
+ for (auto& p : pivotToColumnIndex_) {
847
+ if (p.first > pivot) { // pivots have to be strictly increasing in order of filtration
848
+ pivot = p.first;
849
+ colIndex = p.second;
850
+ }
851
+ }
852
+ _remove_last(colIndex);
853
+ } else {
854
+ _remove_last(nextIndex_ - 1);
855
+ }
856
+ }
857
+
858
+ template <class Master_matrix>
859
+ inline typename Chain_matrix<Master_matrix>::Index Chain_matrix<Master_matrix>::get_number_of_columns() const
860
+ {
861
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
862
+ return matrix_.size();
863
+ } else {
864
+ return nextIndex_; // matrix could have been resized much bigger while insert
865
+ }
866
+ }
867
+
868
+ template <class Master_matrix>
869
+ inline typename Chain_matrix<Master_matrix>::Dimension Chain_matrix<Master_matrix>::get_column_dimension(
870
+ Index columnIndex) const
871
+ {
872
+ return get_column(columnIndex).get_dimension();
873
+ }
874
+
875
+ template <class Master_matrix>
876
+ inline void Chain_matrix<Master_matrix>::add_to(Index sourceColumnIndex, Index targetColumnIndex)
877
+ {
878
+ auto& col = get_column(targetColumnIndex);
879
+ _add_to(col, [&]() { col += get_column(sourceColumnIndex); });
880
+ }
881
+
882
+ template <class Master_matrix>
883
+ inline void Chain_matrix<Master_matrix>::multiply_target_and_add_to(Index sourceColumnIndex,
884
+ const Field_element& coefficient,
885
+ Index targetColumnIndex)
886
+ {
887
+ auto& col = get_column(targetColumnIndex);
888
+ _add_to(col, [&]() { col.multiply_target_and_add(coefficient, get_column(sourceColumnIndex)); });
889
+ }
890
+
891
+ template <class Master_matrix>
892
+ inline void Chain_matrix<Master_matrix>::multiply_source_and_add_to(const Field_element& coefficient,
893
+ Index sourceColumnIndex,
894
+ Index targetColumnIndex)
895
+ {
896
+ auto& col = get_column(targetColumnIndex);
897
+ _add_to(col, [&]() { col.multiply_source_and_add(get_column(sourceColumnIndex), coefficient); });
898
+ }
899
+
900
+ template <class Master_matrix>
901
+ inline bool Chain_matrix<Master_matrix>::is_zero_entry(Index columnIndex, ID_index rowIndex) const
902
+ {
903
+ return !get_column(columnIndex).is_non_zero(rowIndex);
904
+ }
905
+
906
+ template <class Master_matrix>
907
+ inline bool Chain_matrix<Master_matrix>::is_zero_column(Index columnIndex)
908
+ {
909
+ return get_column(columnIndex).is_empty();
910
+ }
911
+
912
+ template <class Master_matrix>
913
+ inline typename Chain_matrix<Master_matrix>::Index Chain_matrix<Master_matrix>::get_column_with_pivot(
914
+ ID_index cellID) const
915
+ {
916
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
917
+ return pivotToColumnIndex_.at(cellID);
918
+ } else {
919
+ return pivotToColumnIndex_[cellID];
920
+ }
921
+ }
922
+
923
+ template <class Master_matrix>
924
+ inline typename Chain_matrix<Master_matrix>::ID_index Chain_matrix<Master_matrix>::get_pivot(Index columnIndex)
925
+ {
926
+ return get_column(columnIndex).get_pivot();
927
+ }
928
+
929
+ template <class Master_matrix>
930
+ inline Chain_matrix<Master_matrix>& Chain_matrix<Master_matrix>::operator=(const Chain_matrix& other)
931
+ {
932
+ if (this == &other) return *this;
933
+
934
+ Dim_opt::operator=(other);
935
+ Swap_opt::operator=(other);
936
+ Pair_opt::operator=(other);
937
+ Rep_opt::operator=(other);
938
+ Pivot_to_pos_mapper_opt::operator=(other);
939
+ matrix_.clear();
940
+ pivotToColumnIndex_ = other.pivotToColumnIndex_;
941
+ nextIndex_ = other.nextIndex_;
942
+ nextPosition_ = other.nextPosition_;
943
+ colSettings_ = other.colSettings_;
944
+
945
+ matrix_.reserve(other.matrix_.size());
946
+ for (const auto& cont : other.matrix_) {
947
+ _container_insert(cont);
948
+ }
949
+
950
+ return *this;
951
+ }
952
+
953
+ template <class Master_matrix>
954
+ inline Chain_matrix<Master_matrix>& Chain_matrix<Master_matrix>::operator=(Chain_matrix&& other) noexcept
955
+ {
956
+ if (this == &other) return *this;
957
+
958
+ Dim_opt::operator=(std::move(other));
959
+ Pair_opt::operator=(std::move(other));
960
+ Swap_opt::operator=(std::move(other));
961
+ Rep_opt::operator=(std::move(other));
962
+ RA_opt::operator=(std::move(other));
963
+ Pivot_to_pos_mapper_opt::operator=(std::move(other));
964
+ matrix_ = std::move(other.matrix_);
965
+ pivotToColumnIndex_ = std::move(other.pivotToColumnIndex_);
966
+ nextIndex_ = std::exchange(other.nextIndex_, 0);
967
+ nextPosition_ = std::exchange(other.nextPosition_, 0);
968
+ colSettings_ = std::exchange(other.colSettings_, nullptr);
969
+
970
+ return *this;
971
+ }
972
+
973
+ template <class Master_matrix>
974
+ inline void Chain_matrix<Master_matrix>::print() const
975
+ {
976
+ std::cout << "Column Matrix:\n";
977
+ if constexpr (!Master_matrix::Option_list::has_map_column_container) {
978
+ for (ID_index i = 0; i < pivotToColumnIndex_.size(); ++i) {
979
+ Index pos = pivotToColumnIndex_[i];
980
+ if (pos != Master_matrix::template get_null_value<Index>()) {
981
+ const Column& col = matrix_[pos];
982
+ for (const auto& entry : col) {
983
+ std::cout << entry.get_row_index() << " ";
984
+ }
985
+ std::cout << "(" << i << ", " << pos << ")\n";
986
+ }
987
+ }
988
+ if constexpr (Master_matrix::Option_list::has_row_access) {
989
+ std::cout << "\n";
990
+ std::cout << "Row Matrix:\n";
991
+ for (ID_index i = 0; i < pivotToColumnIndex_.size(); ++i) {
992
+ Index pos = pivotToColumnIndex_[i];
993
+ if (pos != Master_matrix::template get_null_value<Index>()) {
994
+ const Row& row = RA_opt::get_row(pos);
995
+ for (const auto& entry : row) {
996
+ std::cout << entry.get_column_index() << " ";
997
+ }
998
+ std::cout << "(" << i << ", " << pos << ")\n";
999
+ }
1000
+ }
1001
+ }
1002
+ } else {
1003
+ for (const auto& p : pivotToColumnIndex_) {
1004
+ const Column& col = matrix_.at(p.second);
1005
+ for (const auto& entry : col) {
1006
+ std::cout << entry.get_row_index() << " ";
1007
+ }
1008
+ std::cout << "(" << p.first << ", " << p.second << ")\n";
1009
+ }
1010
+ if constexpr (Master_matrix::Option_list::has_row_access) {
1011
+ std::cout << "\n";
1012
+ std::cout << "Row Matrix:\n";
1013
+ for (const auto& p : pivotToColumnIndex_) {
1014
+ const Row& row = RA_opt::get_row(p.first);
1015
+ for (const auto& entry : row) {
1016
+ std::cout << entry.get_column_index() << " ";
1017
+ }
1018
+ std::cout << "(" << p.first << ", " << p.second << ")\n";
1019
+ }
1020
+ }
1021
+ }
1022
+ std::cout << "\n";
1023
+ }
1024
+
1025
+ template <class Master_matrix>
1026
+ template <class Boundary_range>
1027
+ inline std::vector<typename Master_matrix::Entry_representative>
1028
+ Chain_matrix<Master_matrix>::_reduce_boundary(ID_index cellID, const Boundary_range& boundary, Dimension dim)
1029
+ {
1030
+ Tmp_column column(boundary.begin(), boundary.end());
1031
+ if (dim == Master_matrix::template get_null_value<Dimension>())
1032
+ dim = boundary.begin() == boundary.end() ? 0 : boundary.size() - 1;
1033
+ std::vector<Entry_representative> chainsInH; // for corresponding indices in H (paired columns)
1034
+ std::vector<Entry_representative> chainsInF; // for corresponding indices in F (unpaired, essential columns)
1035
+
1036
+ auto get_last = [&column]() {
1037
+ if constexpr (Master_matrix::Option_list::is_z2)
1038
+ return *(column.rbegin());
1039
+ else
1040
+ return column.rbegin()->first;
1041
+ };
1042
+
1043
+ if (boundary.begin() == boundary.end()) {
1044
+ _insert_in(cellID, column);
1045
+ _insert_chain(column, dim);
1046
+ return chainsInF;
1047
+ }
1048
+
1049
+ Index currentIndex = get_column_with_pivot(get_last());
1050
+
1051
+ while (get_column(currentIndex).is_paired()) {
1052
+ _reduce_by_G(column, chainsInH, currentIndex);
1053
+
1054
+ if (column.empty()) {
1055
+ // produce the sum of all col_h in chains_in_H
1056
+ _build_from_H(cellID, column, chainsInH);
1057
+ // create a new cycle (in F) sigma - \sum col_h
1058
+ _insert_chain(column, dim);
1059
+ return chainsInF;
1060
+ }
1061
+
1062
+ currentIndex = get_column_with_pivot(get_last());
1063
+ }
1064
+
1065
+ while (!column.empty()) {
1066
+ currentIndex = get_column_with_pivot(get_last());
1067
+
1068
+ if (!get_column(currentIndex).is_paired()) {
1069
+ // only fills currentEssentialCycleIndices if Z2 coefficients, so chainsInF remains empty
1070
+ _reduce_by_F(column, chainsInF, currentIndex);
1071
+ } else {
1072
+ _reduce_by_G(column, chainsInH, currentIndex);
1073
+ }
1074
+ }
1075
+
1076
+ _update_largest_death_in_F(chainsInF);
1077
+
1078
+ // Compute the new column cellID + \sum col_h, for col_h in chains_in_H
1079
+ _build_from_H(cellID, column, chainsInH);
1080
+
1081
+ // Create and insert (\sum col_h) + sigma (in H, paired with chain_fp) in matrix_
1082
+ _insert_chain(column, dim, Master_matrix::get_row_index(chainsInF[0]));
1083
+
1084
+ return chainsInF;
1085
+ }
1086
+
1087
+ template <class Master_matrix>
1088
+ inline void Chain_matrix<Master_matrix>::_reduce_by_G(Tmp_column& column,
1089
+ std::vector<Entry_representative>& chainsInH,
1090
+ Index currentIndex)
1091
+ {
1092
+ Column& col = get_column(currentIndex);
1093
+ if constexpr (Master_matrix::Option_list::is_z2) {
1094
+ _add_to(col, column, 1U); // Reduce with the column col_g
1095
+ chainsInH.push_back(col.get_paired_chain_index()); // keep the col_h with which col_g is paired
1096
+ } else {
1097
+ Field_element coef = col.get_pivot_value();
1098
+ auto& operators = colSettings_->operators;
1099
+ coef = operators.get_inverse(coef);
1100
+ operators.multiply_inplace(coef, operators.get_characteristic() - column.rbegin()->second);
1101
+
1102
+ _add_to(col, column, coef); // Reduce with the column col_g
1103
+ chainsInH.emplace_back(col.get_paired_chain_index(), coef); // keep the col_h with which col_g is paired
1104
+ }
1105
+ }
1106
+
1107
+ template <class Master_matrix>
1108
+ inline void Chain_matrix<Master_matrix>::_reduce_by_F(Tmp_column& column,
1109
+ std::vector<Entry_representative>& chainsInF,
1110
+ Index currentIndex)
1111
+ {
1112
+ Column& col = get_column(currentIndex);
1113
+ if constexpr (Master_matrix::Option_list::is_z2) {
1114
+ _add_to(col, column, 1U); // Reduce with the column col_g
1115
+ chainsInF.push_back(currentIndex);
1116
+ } else {
1117
+ Field_element coef = col.get_pivot_value();
1118
+ auto& operators = colSettings_->operators;
1119
+ coef = operators.get_inverse(coef);
1120
+ operators.multiply_inplace(coef, operators.get_characteristic() - column.rbegin()->second);
1121
+
1122
+ _add_to(col, column, coef); // Reduce with the column col_g
1123
+ chainsInF.emplace_back(currentIndex, operators.get_characteristic() - coef);
1124
+ }
1125
+ }
1126
+
1127
+ template <class Master_matrix>
1128
+ inline void Chain_matrix<Master_matrix>::_build_from_H(ID_index cellID,
1129
+ Tmp_column& column,
1130
+ std::vector<Entry_representative>& chainsInH)
1131
+ {
1132
+ _insert_in(cellID, column);
1133
+
1134
+ for (const auto& idx_h : chainsInH) {
1135
+ _add_to(get_column(Master_matrix::get_row_index(idx_h)), column, Master_matrix::get_element(idx_h));
1136
+ }
1137
+ }
1138
+
1139
+ template <class Master_matrix>
1140
+ inline void Chain_matrix<Master_matrix>::_update_largest_death_in_F(const std::vector<Entry_representative>& chainsInF)
1141
+ {
1142
+ Index toUpdate = Master_matrix::get_row_index(chainsInF[0]);
1143
+ if constexpr (Master_matrix::Option_list::is_z2) {
1144
+ for (auto other_col_it = chainsInF.begin() + 1; other_col_it != chainsInF.end(); ++other_col_it) {
1145
+ add_to(*other_col_it, toUpdate);
1146
+ }
1147
+ } else {
1148
+ get_column(toUpdate) *= Master_matrix::get_element(chainsInF[0]);
1149
+ for (auto other_col_it = chainsInF.begin() + 1; other_col_it != chainsInF.end(); ++other_col_it) {
1150
+ multiply_source_and_add_to(
1151
+ Master_matrix::get_element(*other_col_it), Master_matrix::get_row_index(*other_col_it), toUpdate);
1152
+ }
1153
+ }
1154
+ }
1155
+
1156
+ template <class Master_matrix>
1157
+ inline void Chain_matrix<Master_matrix>::_insert_chain(const Tmp_column& column, Dimension dimension)
1158
+ {
1159
+ _container_insert(column, nextIndex_, dimension);
1160
+ _add_bar(dimension);
1161
+
1162
+ ++nextIndex_;
1163
+ }
1164
+
1165
+ template <class Master_matrix>
1166
+ inline void Chain_matrix<Master_matrix>::_insert_chain(const Tmp_column& column, Dimension dimension, Index pair)
1167
+ {
1168
+ // true when no vine updates and if nextIndex_ is updated in remove_last for special case of no vines
1169
+ // because then @ref PosIdx == @ref MatIdx
1170
+ Pos_index pairPos = pair;
1171
+
1172
+ _container_insert(column, nextIndex_, dimension);
1173
+
1174
+ get_column(nextIndex_).assign_paired_chain(pair);
1175
+ auto& pairCol = get_column(pair);
1176
+ pairCol.assign_paired_chain(nextIndex_);
1177
+
1178
+ if constexpr (Master_matrix::Option_list::has_column_pairings && Master_matrix::Option_list::has_vine_update) {
1179
+ pairPos = Pivot_to_pos_mapper_opt::map_[pairCol.get_pivot()];
1180
+ }
1181
+
1182
+ _update_barcode(pairPos);
1183
+
1184
+ ++nextIndex_;
1185
+ }
1186
+
1187
+ template <class Master_matrix>
1188
+ inline void Chain_matrix<Master_matrix>::_add_to(const Column& column,
1189
+ Tmp_column& set,
1190
+ [[maybe_unused]] unsigned int coef)
1191
+ {
1192
+ if constexpr (Master_matrix::Option_list::is_z2) {
1193
+ std::pair<typename std::set<Index>::iterator, bool> res_insert;
1194
+ for (const Entry& entry : column) {
1195
+ res_insert = set.insert(entry.get_row_index());
1196
+ if (!res_insert.second) {
1197
+ set.erase(res_insert.first);
1198
+ }
1199
+ }
1200
+ } else {
1201
+ auto& operators = colSettings_->operators;
1202
+ for (const Entry& entry : column) {
1203
+ auto res = set.emplace(entry.get_row_index(), entry.get_element());
1204
+ if (res.second) {
1205
+ operators.multiply_inplace(res.first->second, coef);
1206
+ } else {
1207
+ operators.multiply_and_add_inplace_back(entry.get_element(), coef, res.first->second);
1208
+ if (res.first->second == Field_operators::get_additive_identity()) {
1209
+ set.erase(res.first);
1210
+ }
1211
+ }
1212
+ }
1213
+ }
1214
+ }
1215
+
1216
+ template <class Master_matrix>
1217
+ template <typename F>
1218
+ inline void Chain_matrix<Master_matrix>::_add_to(Column& target, F&& addition)
1219
+ {
1220
+ auto pivot = target.get_pivot();
1221
+ std::forward<F>(addition)();
1222
+
1223
+ if (pivot != target.get_pivot()) {
1224
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
1225
+ std::swap(pivotToColumnIndex_.at(pivot), pivotToColumnIndex_.at(target.get_pivot()));
1226
+ } else {
1227
+ std::swap(pivotToColumnIndex_[pivot], pivotToColumnIndex_[target.get_pivot()]);
1228
+ }
1229
+ }
1230
+ }
1231
+
1232
+ template <class Master_matrix>
1233
+ inline void Chain_matrix<Master_matrix>::_remove_last(Index lastIndex)
1234
+ {
1235
+ static_assert(Master_matrix::Option_list::has_removable_columns,
1236
+ "'_remove_last' is not implemented for the chosen options.");
1237
+ static_assert(Master_matrix::Option_list::has_map_column_container || !Master_matrix::Option_list::has_vine_update,
1238
+ "'_remove_last' is not implemented for the chosen options.");
1239
+
1240
+ ID_index pivot;
1241
+
1242
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
1243
+ auto itToErase = matrix_.find(lastIndex);
1244
+ Column& colToErase = itToErase->second;
1245
+ pivot = colToErase.get_pivot();
1246
+
1247
+ if constexpr (Master_matrix::Option_list::has_matrix_maximal_dimension_access) {
1248
+ Dim_opt::_update_down(colToErase.get_dimension());
1249
+ }
1250
+
1251
+ if (colToErase.is_paired()) matrix_.at(colToErase.get_paired_chain_index()).unassign_paired_chain();
1252
+ pivotToColumnIndex_.erase(pivot);
1253
+ matrix_.erase(itToErase);
1254
+ } else {
1255
+ GUDHI_CHECK(lastIndex == nextIndex_ - 1 && nextIndex_ == matrix_.size(),
1256
+ std::logic_error("Chain_matrix::_remove_last - Indexation problem."));
1257
+
1258
+ Column& colToErase = matrix_[lastIndex];
1259
+ pivot = colToErase.get_pivot();
1260
+
1261
+ if constexpr (Master_matrix::Option_list::has_matrix_maximal_dimension_access) {
1262
+ Dim_opt::_update_down(colToErase.get_dimension());
1263
+ }
1264
+
1265
+ if (colToErase.is_paired()) matrix_.at(colToErase.get_paired_chain_index()).unassign_paired_chain();
1266
+ pivotToColumnIndex_[pivot] = Master_matrix::template get_null_value<Index>();
1267
+ matrix_.pop_back();
1268
+ // TODO: resize matrix_ when a lot is removed? Could be not the best strategy if user inserts a lot back afterwards.
1269
+ }
1270
+
1271
+ if constexpr (!Master_matrix::Option_list::has_vine_update) {
1272
+ --nextIndex_; // should not be updated when there are vine updates, as possibly lastIndex != nextIndex - 1
1273
+ }
1274
+
1275
+ --nextPosition_;
1276
+ if constexpr (Master_matrix::Option_list::has_column_pairings) {
1277
+ Pair_opt::_erase_bar(nextPosition_);
1278
+ }
1279
+ if constexpr (hasPivotToPosMap_) {
1280
+ Pivot_to_pos_mapper_opt::map_.erase(pivot);
1281
+ }
1282
+
1283
+ if constexpr (Master_matrix::Option_list::has_row_access) {
1284
+ GUDHI_CHECK(
1285
+ RA_opt::get_row(pivot).size() == 0,
1286
+ std::invalid_argument(
1287
+ "Chain_matrix::_remove_last - Column asked to be removed does not corresponds to a maximal simplex."));
1288
+ if constexpr (Master_matrix::Option_list::has_removable_rows) {
1289
+ RA_opt::erase_empty_row(pivot);
1290
+ }
1291
+ }
1292
+ }
1293
+
1294
+ template <class Master_matrix>
1295
+ inline void Chain_matrix<Master_matrix>::_update_barcode(Pos_index birth)
1296
+ {
1297
+ if constexpr (Master_matrix::Option_list::has_column_pairings) {
1298
+ Pair_opt::_update_barcode(birth, nextPosition_);
1299
+ }
1300
+ ++nextPosition_;
1301
+ }
1302
+
1303
+ template <class Master_matrix>
1304
+ inline void Chain_matrix<Master_matrix>::_add_bar(Dimension dim)
1305
+ {
1306
+ if constexpr (Master_matrix::Option_list::has_column_pairings) {
1307
+ Pair_opt::_add_bar(dim, nextPosition_);
1308
+ }
1309
+ ++nextPosition_;
1310
+ }
1311
+
1312
+ template <class Master_matrix>
1313
+ template <class Container>
1314
+ inline void Chain_matrix<Master_matrix>::_container_insert(const Container& column, Index pos, Dimension dim)
1315
+ {
1316
+ ID_index pivot = Master_matrix::get_row_index(*(column.rbegin()));
1317
+
1318
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
1319
+ pivotToColumnIndex_.try_emplace(pivot, pos);
1320
+ if constexpr (Master_matrix::Option_list::has_row_access) {
1321
+ matrix_.try_emplace(pos, Column(pos, column, dim, RA_opt::_get_rows_ptr(), colSettings_));
1322
+ } else {
1323
+ matrix_.try_emplace(pos, Column(column, dim, colSettings_));
1324
+ }
1325
+ } else {
1326
+ if constexpr (Master_matrix::Option_list::has_row_access) {
1327
+ matrix_.emplace_back(pos, column, dim, RA_opt::_get_rows_ptr(), colSettings_);
1328
+ } else {
1329
+ matrix_.emplace_back(column, dim, colSettings_);
1330
+ }
1331
+ pivotToColumnIndex_[pivot] = pos;
1332
+ }
1333
+ }
1334
+
1335
+ template <class Master_matrix>
1336
+ template <class ColumnIterator> // Pair (pos,Column) if has_map_column_container, Column otherwise
1337
+ inline void Chain_matrix<Master_matrix>::_container_insert(const ColumnIterator& rep)
1338
+ {
1339
+ if constexpr (Master_matrix::Option_list::has_map_column_container) {
1340
+ const auto& col = rep.second;
1341
+ if constexpr (Master_matrix::Option_list::has_row_access) {
1342
+ matrix_.try_emplace(rep.first, Column(col, col.get_column_index(), RA_opt::_get_rows_ptr(), colSettings_));
1343
+ } else {
1344
+ matrix_.try_emplace(rep.first, Column(col, colSettings_));
1345
+ }
1346
+ } else {
1347
+ if constexpr (Master_matrix::Option_list::has_row_access) {
1348
+ matrix_.emplace_back(rep, rep.get_column_index(), RA_opt::_get_rows_ptr(), colSettings_);
1349
+ } else {
1350
+ matrix_.emplace_back(rep, colSettings_);
1351
+ }
1352
+ }
1353
+ }
1354
+
1355
+ template <class Master_matrix>
1356
+ inline void Chain_matrix<Master_matrix>::_insert_in(ID_index cellID, Tmp_column& column)
1357
+ {
1358
+ if constexpr (Master_matrix::Option_list::is_z2)
1359
+ column.insert(cellID);
1360
+ else
1361
+ column.emplace(cellID, 1);
1362
+ }
1363
+
1364
+ } // namespace persistence_matrix
1365
+ } // namespace Gudhi
1366
+
1367
+ #endif // PM_CHAIN_MATRIX_H