Loading...
Searching...
No Matches
Z2_field.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) 2022 Inria
6 *
7 * Modification(s):
8 * - YYYY/MM Author: Description of the modification
9 */
10
16
17#ifndef MATRIX_FIELD_Z2_H_
18#define MATRIX_FIELD_Z2_H_
19
20#include <utility>
21
22namespace Gudhi {
23namespace persistence_fields {
24
32{
33 public:
34 using Element = bool;
35 template <class T>
36 using isInteger = std::enable_if_t<std::is_integral_v<T> >;
37
41 Z2_field_element() : element_(false) {}
42
49 template <typename Integer_type, class = isInteger<Integer_type> >
50 Z2_field_element(Integer_type element) : element_(_get_value(element))
51 {}
52
58 Z2_field_element(const Z2_field_element& toCopy) = default;
59
65 Z2_field_element(Z2_field_element&& toMove) noexcept : element_(std::exchange(toMove.element_, false)) {}
66
67 ~Z2_field_element() = default;
68
72 friend void operator+=(Z2_field_element& f1, Z2_field_element const& f2)
73 {
74 f1.element_ = (f1.element_ != f2.element_);
75 }
76
81 {
82 f1 += f2;
83 return f1;
84 }
85
91 template <typename Integer_type, class = isInteger<Integer_type> >
92 friend void operator+=(Z2_field_element& f, const Integer_type& v)
93 {
94 f.element_ = (f.element_ != _get_value(v));
95 }
96
102 template <typename Integer_type, class = isInteger<Integer_type> >
103 friend Z2_field_element operator+(Z2_field_element f, const Integer_type& v)
104 {
105 f += v;
106 return f;
107 }
108
114 template <typename Integer_type, class = isInteger<Integer_type> >
115 friend Integer_type operator+(const Integer_type& v, const Z2_field_element& f)
116 {
117 return f.element_ != _get_value(v);
118 }
119
123 friend void operator-=(Z2_field_element& f1, Z2_field_element const& f2)
124 {
125 f1.element_ = (f1.element_ != f2.element_);
126 }
127
132 {
133 f1 -= f2;
134 return f1;
135 }
136
142 template <typename Integer_type, class = isInteger<Integer_type> >
143 friend void operator-=(Z2_field_element& f, const Integer_type& v)
144 {
145 f.element_ = (f.element_ != _get_value(v));
146 }
147
153 template <typename Integer_type, class = isInteger<Integer_type> >
154 friend Z2_field_element operator-(Z2_field_element f, const Integer_type& v)
155 {
156 f -= v;
157 return f;
158 }
159
165 template <typename Integer_type, class = isInteger<Integer_type> >
166 friend Integer_type operator-(const Integer_type v, Z2_field_element const& f)
167 {
168 return f.element_ != _get_value(v);
169 }
170
174 friend void operator*=(Z2_field_element& f1, Z2_field_element const& f2)
175 {
176 f1.element_ = (f1.element_ && f2.element_);
177 }
178
183 {
184 f1 *= f2;
185 return f1;
186 }
187
193 template <typename Integer_type, class = isInteger<Integer_type> >
194 friend void operator*=(Z2_field_element& f, const Integer_type& v)
195 {
196 f.element_ = (f.element_ && _get_value(v));
197 }
198
204 template <typename Integer_type, class = isInteger<Integer_type> >
205 friend Z2_field_element operator*(Z2_field_element f, const Integer_type& v)
206 {
207 f *= v;
208 return f;
209 }
210
216 template <typename Integer_type, class = isInteger<Integer_type> >
217 friend Integer_type operator*(const Integer_type& v, Z2_field_element const& f)
218 {
219 return f.element_ && _get_value(v);
220 }
221
225 friend bool operator==(const Z2_field_element& f1, const Z2_field_element& f2) { return f1.element_ == f2.element_; }
226
232 template <typename Integer_type, class = isInteger<Integer_type> >
233 friend bool operator==(const Integer_type& v, const Z2_field_element& f)
234 {
235 return _get_value(v) == f.element_;
236 }
237
243 template <typename Integer_type, class = isInteger<Integer_type> >
244 friend bool operator==(const Z2_field_element& f, const Integer_type& v)
245 {
246 return _get_value(v) == f.element_;
247 }
248
252 friend bool operator!=(const Z2_field_element& f1, const Z2_field_element& f2) { return !(f1 == f2); }
253
259 template <typename Integer_type, class = isInteger<Integer_type> >
260 friend bool operator!=(const Integer_type v, const Z2_field_element& f)
261 {
262 return !(v == f);
263 }
264
270 template <typename Integer_type, class = isInteger<Integer_type> >
271 friend bool operator!=(const Z2_field_element& f, const Integer_type v)
272 {
273 return !(v == f);
274 }
275
280 {
281 std::swap(element_, other.element_);
282 return *this;
283 }
284
289 {
290 if (this == &other) return *this;
291
292 element_ = std::exchange(other.element_, false);
293 return *this;
294 }
295
299 template <typename Integer_type, class = isInteger<Integer_type> >
300 Z2_field_element& operator=(const Integer_type value)
301 {
302 element_ = _get_value(value);
303 return *this;
304 }
305
309 friend void swap(Z2_field_element& f1, Z2_field_element& f2) noexcept { std::swap(f1.element_, f2.element_); }
310
314 operator unsigned int() const { return static_cast<unsigned int>(element_); }
315
321 [[nodiscard]] Z2_field_element get_inverse() const { return element_ ? Z2_field_element(1) : Z2_field_element(); }
322
329 [[nodiscard]] std::pair<Z2_field_element, unsigned int> get_partial_inverse(
330 unsigned int productOfCharacteristics) const
331 {
332 return {get_inverse(), productOfCharacteristics};
333 }
334
341
348
355 static Z2_field_element get_partial_multiplicative_identity([[maybe_unused]] unsigned int productOfCharacteristics)
356 {
357 return {1};
358 }
359
365 static constexpr unsigned int get_characteristic() { return 2; }
366
372 [[nodiscard]] Element get_value() const { return element_; }
373
374 private:
375 Element element_;
376
377 template <typename Integer_type, class = isInteger<Integer_type> >
378 static constexpr Element _get_value(Integer_type e)
379 {
380 if constexpr (std::is_same_v<Integer_type, bool>) {
381 return e;
382 } else {
383 return e < 2 && e >= 0 ? e : e % 2; // returns bool, so %2 won't be negative and is optimized to &
384 }
385 }
386};
387
388} // namespace persistence_fields
389} // namespace Gudhi
390
391#endif // MATRIX_FIELD_Z2_H_
Class representing an element of the field.
Definition Z2_field.h:32
static constexpr unsigned int get_characteristic()
Returns the characteristic of the field, that is 2.
Definition Z2_field.h:365
friend Z2_field_element operator-(Z2_field_element f1, Z2_field_element const &f2)
operator-
Definition Z2_field.h:131
Z2_field_element(Integer_type element)
Constructor setting the element to the given value.
Definition Z2_field.h:50
friend Z2_field_element operator+(Z2_field_element f1, Z2_field_element const &f2)
operator+
Definition Z2_field.h:80
friend Integer_type operator*(const Integer_type &v, Z2_field_element const &f)
operator*
Definition Z2_field.h:217
friend void operator+=(Z2_field_element &f1, Z2_field_element const &f2)
operator+=
Definition Z2_field.h:72
std::pair< Z2_field_element, unsigned int > get_partial_inverse(unsigned int productOfCharacteristics) const
For interface purposes with multi-fields. Returns the inverse together with the second argument.
Definition Z2_field.h:329
friend Z2_field_element operator-(Z2_field_element f, const Integer_type &v)
operator-
Definition Z2_field.h:154
friend void operator+=(Z2_field_element &f, const Integer_type &v)
operator+=
Definition Z2_field.h:92
Z2_field_element & operator=(Z2_field_element &&other) noexcept
Move assign operator.
Definition Z2_field.h:288
friend bool operator==(const Z2_field_element &f, const Integer_type &v)
operator==
Definition Z2_field.h:244
friend Integer_type operator+(const Integer_type &v, const Z2_field_element &f)
operator+
Definition Z2_field.h:115
friend Integer_type operator-(const Integer_type v, Z2_field_element const &f)
operator-
Definition Z2_field.h:166
friend void operator-=(Z2_field_element &f1, Z2_field_element const &f2)
operator-=
Definition Z2_field.h:123
friend bool operator!=(const Integer_type v, const Z2_field_element &f)
operator!=
Definition Z2_field.h:260
Element get_value() const
Returns the value of the element.
Definition Z2_field.h:372
Z2_field_element get_inverse() const
Returns the inverse of the element.
Definition Z2_field.h:321
Z2_field_element()
Default constructor.
Definition Z2_field.h:41
friend Z2_field_element operator*(Z2_field_element f1, Z2_field_element const &f2)
operator*
Definition Z2_field.h:182
friend bool operator==(const Z2_field_element &f1, const Z2_field_element &f2)
operator==
Definition Z2_field.h:225
static Z2_field_element get_multiplicative_identity()
Returns the multiplicative identity of the field.
Definition Z2_field.h:347
friend bool operator==(const Integer_type &v, const Z2_field_element &f)
operator==
Definition Z2_field.h:233
friend Z2_field_element operator+(Z2_field_element f, const Integer_type &v)
operator+
Definition Z2_field.h:103
friend void operator*=(Z2_field_element &f1, Z2_field_element const &f2)
operator*=
Definition Z2_field.h:174
static Z2_field_element get_additive_identity()
Returns the additive identity of the field.
Definition Z2_field.h:340
friend Z2_field_element operator*(Z2_field_element f, const Integer_type &v)
operator*
Definition Z2_field.h:205
Z2_field_element & operator=(Z2_field_element other)
Assign operator.
Definition Z2_field.h:279
friend void operator*=(Z2_field_element &f, const Integer_type &v)
operator*=
Definition Z2_field.h:194
static Z2_field_element get_partial_multiplicative_identity(unsigned int productOfCharacteristics)
For interface purposes with multi-fields. Returns the multiplicative identity of the field.
Definition Z2_field.h:355
friend void swap(Z2_field_element &f1, Z2_field_element &f2) noexcept
Swap operator.
Definition Z2_field.h:309
Z2_field_element & operator=(const Integer_type value)
Assign operator.
Definition Z2_field.h:300
friend void operator-=(Z2_field_element &f, const Integer_type &v)
operator-=
Definition Z2_field.h:143
Z2_field_element(const Z2_field_element &toCopy)=default
Copy constructor.
friend bool operator!=(const Z2_field_element &f1, const Z2_field_element &f2)
operator!=
Definition Z2_field.h:252
bool Element
Definition Z2_field.h:34
Z2_field_element(Z2_field_element &&toMove) noexcept
Move constructor.
Definition Z2_field.h:65
friend bool operator!=(const Z2_field_element &f, const Integer_type v)
operator!=
Definition Z2_field.h:271
Field namespace.
Definition Intro_field_elements_and_operators.h:16
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14