All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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-24 Inria
6 *
7 * Modification(s):
8 * - YYYY/MM Author: Description of the modification
9 */
10
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
48 template <typename Integer_type, class = isInteger<Integer_type> >
49 Z2_field_element(Integer_type element);
61 Z2_field_element(Z2_field_element&& toMove) noexcept;
62
66 friend void operator+=(Z2_field_element& f1, Z2_field_element const& f2) {
67 f1.element_ = (f1.element_ != f2.element_);
68 }
73 f1 += f2;
74 return f1;
75 }
81 template <typename Integer_type, class = isInteger<Integer_type> >
82 friend void operator+=(Z2_field_element& f, const Integer_type& v) { f.element_ = (f.element_ != _get_value(v)); }
88 template <typename Integer_type, class = isInteger<Integer_type> >
89 friend Z2_field_element operator+(Z2_field_element f, const Integer_type& v) {
90 f += v;
91 return f;
92 }
98 template <typename Integer_type, class = isInteger<Integer_type> >
99 friend Integer_type operator+(const Integer_type& v, const Z2_field_element& f) {
100 return f.element_ != _get_value(v);
101 }
102
106 friend void operator-=(Z2_field_element& f1, Z2_field_element const& f2) {
107 f1.element_ = (f1.element_ != f2.element_);
108 }
113 f1 -= f2;
114 return f1;
115 }
121 template <typename Integer_type, class = isInteger<Integer_type> >
122 friend void operator-=(Z2_field_element& f, const Integer_type& v) { f.element_ = (f.element_ != _get_value(v)); }
128 template <typename Integer_type, class = isInteger<Integer_type> >
129 friend Z2_field_element operator-(Z2_field_element f, const Integer_type& v) {
130 f -= v;
131 return f;
132 }
138 template <typename Integer_type, class = isInteger<Integer_type> >
139 friend Integer_type operator-(const Integer_type v, Z2_field_element const& f) {
140 return f.element_ != _get_value(v);
141 }
142
146 friend void operator*=(Z2_field_element& f1, Z2_field_element const& f2) {
147 f1.element_ = (f1.element_ && f2.element_);
148 }
153 f1 *= f2;
154 return f1;
155 }
161 template <typename Integer_type, class = isInteger<Integer_type> >
162 friend void operator*=(Z2_field_element& f, const Integer_type& v) { f.element_ = (f.element_ && _get_value(v)); }
168 template <typename Integer_type, class = isInteger<Integer_type> >
169 friend Z2_field_element operator*(Z2_field_element f, const Integer_type& v) {
170 f *= v;
171 return f;
172 }
178 template <typename Integer_type, class = isInteger<Integer_type> >
179 friend Integer_type operator*(const Integer_type& v, Z2_field_element const& f) {
180 return f.element_ && _get_value(v);
181 }
182
186 friend bool operator==(const Z2_field_element& f1, const Z2_field_element& f2) { return f1.element_ == f2.element_; }
192 template <typename Integer_type, class = isInteger<Integer_type> >
193 friend bool operator==(const Integer_type& v, const Z2_field_element& f) {
194 return _get_value(v) == f.element_;
195 }
201 template <typename Integer_type, class = isInteger<Integer_type> >
202 friend bool operator==(const Z2_field_element& f, const Integer_type& v) {
203 return _get_value(v) == f.element_;
204 }
208 friend bool operator!=(const Z2_field_element& f1, const Z2_field_element& f2) { return !(f1 == f2); }
214 template <typename Integer_type, class = isInteger<Integer_type> >
215 friend bool operator!=(const Integer_type v, const Z2_field_element& f) {
216 return !(v == f);
217 }
223 template <typename Integer_type, class = isInteger<Integer_type> >
224 friend bool operator!=(const Z2_field_element& f, const Integer_type v) {
225 return !(v == f);
226 }
227
235 Z2_field_element& operator=(const unsigned int value);
239 friend void swap(Z2_field_element& f1, Z2_field_element& f2) { std::swap(f1.element_, f2.element_); }
240
244 operator unsigned int() const;
245
258 std::pair<Z2_field_element, unsigned int> get_partial_inverse(unsigned int productOfCharacteristics) const;
259
278 static Z2_field_element get_partial_multiplicative_identity([[maybe_unused]] unsigned int productOfCharacteristics);
284 static constexpr unsigned int get_characteristic();
285
291 Element get_value() const;
292
293 // static constexpr bool handles_only_z2() { return true; }
294
295 private:
296 Element element_;
297
298 template <typename Integer_type, class = isInteger<Integer_type> >
299 static constexpr Element _get_value(Integer_type e) {
300 if constexpr (std::is_same_v<Integer_type, bool>) {
301 return e;
302 } else {
303 return e < 2 && e >= 0 ? e : e % 2; // returns bool, so %2 won't be negative and is optimized to &
304 }
305 }
306};
307
308inline Z2_field_element::Z2_field_element() : element_(false) {}
309
310template <typename Integer_type, class>
311inline Z2_field_element::Z2_field_element(Integer_type element) : element_(_get_value(element)) {}
312
313inline Z2_field_element::Z2_field_element(const Z2_field_element& toCopy) : element_(toCopy.element_) {}
314
316 : element_(std::exchange(toMove.element_, 0)) {}
317
319 std::swap(element_, other.element_);
320 return *this;
321}
322
323inline Z2_field_element& Z2_field_element::operator=(unsigned int const value) {
324 element_ = _get_value(value);
325 return *this;
326}
327
328inline Z2_field_element::operator unsigned int() const { return element_; }
329
331 return element_ ? Z2_field_element(1) : Z2_field_element();
332}
333
334inline std::pair<Z2_field_element, unsigned int> Z2_field_element::get_partial_inverse(
335 unsigned int productOfCharacteristics) const {
336 return {get_inverse(), productOfCharacteristics};
337}
338
340
342
344 [[maybe_unused]] unsigned int productOfCharacteristics) {
345 return Z2_field_element(1);
346}
347
348inline constexpr unsigned int Z2_field_element::get_characteristic() { return 2; }
349
351
352} // namespace persistence_fields
353} // namespace Gudhi
354
355#endif // MATRIX_FIELD_Z2_H_
Class representing an element of the field.
Definition: Z2_field.h:32
friend Z2_field_element operator-(Z2_field_element f1, Z2_field_element const &f2)
operator-
Definition: Z2_field.h:112
friend Z2_field_element operator+(Z2_field_element f1, Z2_field_element const &f2)
operator+
Definition: Z2_field.h:72
friend Integer_type operator*(const Integer_type &v, Z2_field_element const &f)
operator*
Definition: Z2_field.h:179
friend void operator+=(Z2_field_element &f1, Z2_field_element const &f2)
operator+=
Definition: Z2_field.h:66
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:334
friend Z2_field_element operator-(Z2_field_element f, const Integer_type &v)
operator-
Definition: Z2_field.h:129
static constexpr unsigned int get_characteristic()
Returns the characteristic of the field, that is 2.
Definition: Z2_field.h:348
static Z2_field_element get_additive_identity()
Returns the additive identity of the field.
Definition: Z2_field.h:339
friend void swap(Z2_field_element &f1, Z2_field_element &f2)
Swap operator.
Definition: Z2_field.h:239
static Z2_field_element get_multiplicative_identity()
Returns the multiplicative identity of the field.
Definition: Z2_field.h:341
friend void operator+=(Z2_field_element &f, const Integer_type &v)
operator+=
Definition: Z2_field.h:82
friend bool operator==(const Z2_field_element &f, const Integer_type &v)
operator==
Definition: Z2_field.h:202
friend Integer_type operator+(const Integer_type &v, const Z2_field_element &f)
operator+
Definition: Z2_field.h:99
friend Integer_type operator-(const Integer_type v, Z2_field_element const &f)
operator-
Definition: Z2_field.h:139
friend void operator-=(Z2_field_element &f1, Z2_field_element const &f2)
operator-=
Definition: Z2_field.h:106
friend bool operator!=(const Integer_type v, const Z2_field_element &f)
operator!=
Definition: Z2_field.h:215
Z2_field_element get_inverse() const
Returns the inverse of the element.
Definition: Z2_field.h:330
Z2_field_element()
Default constructor.
Definition: Z2_field.h:308
friend Z2_field_element operator*(Z2_field_element f1, Z2_field_element const &f2)
operator*
Definition: Z2_field.h:152
friend bool operator==(const Z2_field_element &f1, const Z2_field_element &f2)
operator==
Definition: Z2_field.h:186
friend bool operator==(const Integer_type &v, const Z2_field_element &f)
operator==
Definition: Z2_field.h:193
friend Z2_field_element operator+(Z2_field_element f, const Integer_type &v)
operator+
Definition: Z2_field.h:89
friend void operator*=(Z2_field_element &f1, Z2_field_element const &f2)
operator*=
Definition: Z2_field.h:146
friend Z2_field_element operator*(Z2_field_element f, const Integer_type &v)
operator*
Definition: Z2_field.h:169
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:343
Z2_field_element & operator=(Z2_field_element other)
Assign operator.
Definition: Z2_field.h:318
friend void operator*=(Z2_field_element &f, const Integer_type &v)
operator*=
Definition: Z2_field.h:162
friend void operator-=(Z2_field_element &f, const Integer_type &v)
operator-=
Definition: Z2_field.h:122
Element get_value() const
Returns the value of the element.
Definition: Z2_field.h:350
friend bool operator!=(const Z2_field_element &f1, const Z2_field_element &f2)
operator!=
Definition: Z2_field.h:208
bool Element
Definition: Z2_field.h:34
friend bool operator!=(const Z2_field_element &f, const Integer_type v)
operator!=
Definition: Z2_field.h:224
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14