Loading...
Searching...
No Matches
Z2_field_operators.h
Go to the documentation of this file.
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) 2024 Inria
6 *
7 * Modification(s):
8 * - YYYY/MM Author: Description of the modification
9 */
10
16
17#ifndef MATRIX_FIELD_Z2_OPERATORS_H_
18#define MATRIX_FIELD_Z2_OPERATORS_H_
19
20#include <utility>
21
22namespace Gudhi {
23namespace persistence_fields {
24
32{
33 public:
34 using Element = bool;
35 using Characteristic = unsigned int;
36 template <class T>
37 using isUnsignedInteger = std::enable_if_t<std::is_unsigned_v<T> >;
38 template <class T>
39 using isInteger = std::enable_if_t<std::is_integral_v<T> >;
40
41 constexpr static const Characteristic nullCharacteristic = 2;
42
48 static constexpr Characteristic get_characteristic() { return 2; }
49
58 template <typename Integer_type, class = isInteger<Integer_type> >
59 static Element get_value(Integer_type e)
60 {
61 if constexpr (std::is_same_v<Integer_type, bool>) {
62 return e;
63 } else {
64 return e < 2 && e >= 0 ? e : e % 2; // returns bool, so %2 won't be negative and is optimized to &
65 }
66 }
67
76 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
77 static Element add(Unsigned_integer_type e1, Unsigned_integer_type e2)
78 {
79 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
80 return e1 != e2;
81 } else {
82 return get_value(e1) != get_value(e2);
83 }
84 }
85
94 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
95 static void add_inplace(Unsigned_integer_type& e1, Unsigned_integer_type e2)
96 {
97 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
98 e1 = e1 != e2;
99 } else {
100 e1 = get_value(e1) != get_value(e2);
101 }
102 }
103
112 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
113 static Element subtract(Unsigned_integer_type e1, Unsigned_integer_type e2)
114 {
115 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
116 return e1 != e2;
117 } else {
118 return get_value(e1) != get_value(e2);
119 }
120 }
121
130 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
131 static void subtract_inplace_front(Unsigned_integer_type& e1, Unsigned_integer_type e2)
132 {
133 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
134 e1 = e1 != e2;
135 } else {
136 e1 = get_value(e1) != get_value(e2);
137 }
138 }
139
148 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
149 static void subtract_inplace_back(Unsigned_integer_type e1, Unsigned_integer_type& e2)
150 {
151 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
152 e2 = e1 != e2;
153 } else {
154 e2 = get_value(e1) != get_value(e2);
155 }
156 }
157
166 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
167 static Element multiply(Unsigned_integer_type e1, Unsigned_integer_type e2)
168 {
169 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
170 return e1 && e2;
171 } else {
172 return get_value(e1) ? get_value(e2) : false;
173 }
174 }
175
184 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
185 static void multiply_inplace(Unsigned_integer_type& e1, Unsigned_integer_type e2)
186 {
187 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
188 e1 = e1 && e2;
189 } else {
190 e1 = get_value(e1) ? get_value(e2) : false;
191 }
192 }
193
203 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
204 static Element multiply_and_add(Unsigned_integer_type e, Unsigned_integer_type m, Unsigned_integer_type a)
205 {
206 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
207 return (e && m) != a;
208 } else {
209 return multiply(e, m) != get_value(a);
210 }
211 }
212
222 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
223 static void multiply_and_add_inplace_front(Unsigned_integer_type& e, Unsigned_integer_type m, Unsigned_integer_type a)
224 {
225 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
226 e = (e && m) != a;
227 } else {
228 e = multiply(e, m) != get_value(a);
229 }
230 }
231
241 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
242 static void multiply_and_add_inplace_back(Unsigned_integer_type e, Unsigned_integer_type m, Unsigned_integer_type& a)
243 {
244 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
245 a = (e && m) != a;
246 } else {
247 a = multiply(e, m) != get_value(a);
248 }
249 }
250
261 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
262 static Element add_and_multiply(Unsigned_integer_type e, Unsigned_integer_type a, Unsigned_integer_type m)
263 {
264 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
265 return (e != a) && m;
266 } else {
267 return add(e, a) ? get_value(m) : false;
268 }
269 }
270
280 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
281 static void add_and_multiply_inplace_front(Unsigned_integer_type& e, Unsigned_integer_type a, Unsigned_integer_type m)
282 {
283 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
284 e = (e != a) && m;
285 } else {
286 e = add(e, a) ? get_value(m) : false;
287 }
288 }
289
299 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
300 static void add_and_multiply_inplace_back(Unsigned_integer_type& e, Unsigned_integer_type a, Unsigned_integer_type m)
301 {
302 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
303 m = (e != a) && m;
304 } else {
305 m = add(e, a) ? get_value(m) : false;
306 }
307 }
308
318 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
319 static bool are_equal(Unsigned_integer_type e1, Unsigned_integer_type e2)
320 {
321 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
322 return e1 == e2;
323 } else {
324 return get_value(e1) == get_value(e2);
325 }
326 }
327
335 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
336 static Element get_inverse(Unsigned_integer_type e)
337 {
338 if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
339 return e;
340 } else {
341 return get_value(e);
342 }
343 }
344
353 template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
354 static std::pair<Element, Characteristic> get_partial_inverse(Unsigned_integer_type e,
355 Characteristic productOfCharacteristics)
356 {
357 return {get_inverse(e), productOfCharacteristics};
358 }
359
365 static constexpr Element get_additive_identity() { return false; }
366
372 static constexpr Element get_multiplicative_identity() { return true; }
373
380 static constexpr Element get_partial_multiplicative_identity([[maybe_unused]] Characteristic productOfCharacteristics)
381 {
382 return true;
383 }
384};
385
386} // namespace persistence_fields
387} // namespace Gudhi
388
389#endif // MATRIX_FIELD_Z2_OPERATORS_H_
Class defining operators for the field.
Definition Z2_field_operators.h:32
static void add_inplace(Unsigned_integer_type &e1, Unsigned_integer_type e2)
Stores in the first element the sum of two given elements in the field, that is (e1 + e2) % 2,...
Definition Z2_field_operators.h:95
static Element multiply_and_add(Unsigned_integer_type e, Unsigned_integer_type m, Unsigned_integer_type a)
Multiplies the first element with the second one and adds the third one. Returns the result in the fi...
Definition Z2_field_operators.h:204
static void subtract_inplace_front(Unsigned_integer_type &e1, Unsigned_integer_type e2)
Stores in the first element the subtraction in the field of the first element by the second element,...
Definition Z2_field_operators.h:131
static Element get_value(Integer_type e)
Returns the value of an integer in the field. That is the positive value of the integer modulo the cu...
Definition Z2_field_operators.h:59
static void add_and_multiply_inplace_back(Unsigned_integer_type &e, Unsigned_integer_type a, Unsigned_integer_type m)
Adds the first element to the second one and multiplies the third one with it, that is ((e + a) * m) ...
Definition Z2_field_operators.h:300
static constexpr Element get_multiplicative_identity()
Returns the multiplicative identity of the field.
Definition Z2_field_operators.h:372
static constexpr Element get_additive_identity()
Returns the additive identity of the field.
Definition Z2_field_operators.h:365
static std::pair< Element, Characteristic > get_partial_inverse(Unsigned_integer_type e, Characteristic productOfCharacteristics)
For interface purposes with multi-fields. Returns the inverse together with the second argument.
Definition Z2_field_operators.h:354
static Element subtract(Unsigned_integer_type e1, Unsigned_integer_type e2)
Returns the subtraction in the field of the first element by the second element.
Definition Z2_field_operators.h:113
static constexpr Characteristic get_characteristic()
Returns the characteristic of the field, that is 2.
Definition Z2_field_operators.h:48
static void subtract_inplace_back(Unsigned_integer_type e1, Unsigned_integer_type &e2)
Stores in the second element the subtraction in the field of the first element by the second element,...
Definition Z2_field_operators.h:149
static void multiply_and_add_inplace_back(Unsigned_integer_type e, Unsigned_integer_type m, Unsigned_integer_type &a)
Multiplies the first element with the second one and adds the third one, that is (e * m + a) % 2,...
Definition Z2_field_operators.h:242
static Element add(Unsigned_integer_type e1, Unsigned_integer_type e2)
Returns the sum of two elements in the field.
Definition Z2_field_operators.h:77
static Element multiply(Unsigned_integer_type e1, Unsigned_integer_type e2)
Returns the multiplication of two elements in the field.
Definition Z2_field_operators.h:167
static constexpr Element get_partial_multiplicative_identity(Characteristic productOfCharacteristics)
For interface purposes with multi-fields. Returns the multiplicative identity of the field.
Definition Z2_field_operators.h:380
unsigned int Characteristic
Definition Z2_field_operators.h:35
static void multiply_and_add_inplace_front(Unsigned_integer_type &e, Unsigned_integer_type m, Unsigned_integer_type a)
Multiplies the first element with the second one and adds the third one, that is (e * m + a) % 2,...
Definition Z2_field_operators.h:223
static bool are_equal(Unsigned_integer_type e1, Unsigned_integer_type e2)
Returns true if the two given elements are equal in the field, false otherwise.
Definition Z2_field_operators.h:319
static Element add_and_multiply(Unsigned_integer_type e, Unsigned_integer_type a, Unsigned_integer_type m)
Adds the first element to the second one and multiplies the third one with it. Returns the result in ...
Definition Z2_field_operators.h:262
static void add_and_multiply_inplace_front(Unsigned_integer_type &e, Unsigned_integer_type a, Unsigned_integer_type m)
Adds the first element to the second one and multiplies the third one with it, that is ((e + a) * m) ...
Definition Z2_field_operators.h:281
bool Element
Definition Z2_field_operators.h:34
static void multiply_inplace(Unsigned_integer_type &e1, Unsigned_integer_type e2)
Stores in the first element the multiplication of two given elements in the field,...
Definition Z2_field_operators.h:185
static Element get_inverse(Unsigned_integer_type e)
Returns the inverse of the given element in the field.
Definition Z2_field_operators.h:336
Field namespace.
Definition Intro_field_elements_and_operators.h:16
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14