Loading...
Searching...
No Matches
Z2_field.h
Go to the documentation of this file.
/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
* See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
* Author(s): Hannah Schreiber
*
* Copyright (C) 2022-24 Inria
*
* Modification(s):
* - YYYY/MM Author: Description of the modification
*/
#ifndef MATRIX_FIELD_Z2_H_
#define MATRIX_FIELD_Z2_H_
#include <utility>
namespace Gudhi {
namespace persistence_fields {
class Z2_field_element
{
public:
using Element = bool;
template <class T>
using isInteger = std::enable_if_t<std::is_integral_v<T> >;
Z2_field_element();
template <typename Integer_type, class = isInteger<Integer_type> >
Z2_field_element(Integer_type element);
Z2_field_element(const Z2_field_element& toCopy);
Z2_field_element(Z2_field_element&& toMove) noexcept;
friend void operator+=(Z2_field_element& f1, Z2_field_element const& f2) {
f1.element_ = (f1.element_ != f2.element_);
}
friend Z2_field_element operator+(Z2_field_element f1, Z2_field_element const& f2) {
f1 += f2;
return f1;
}
template <typename Integer_type, class = isInteger<Integer_type> >
friend void operator+=(Z2_field_element& f, const Integer_type& v) { f.element_ = (f.element_ != _get_value(v)); }
template <typename Integer_type, class = isInteger<Integer_type> >
friend Z2_field_element operator+(Z2_field_element f, const Integer_type& v) {
f += v;
return f;
}
template <typename Integer_type, class = isInteger<Integer_type> >
friend Integer_type operator+(const Integer_type& v, const Z2_field_element& f) {
return f.element_ != _get_value(v);
}
friend void operator-=(Z2_field_element& f1, Z2_field_element const& f2) {
f1.element_ = (f1.element_ != f2.element_);
}
friend Z2_field_element operator-(Z2_field_element f1, Z2_field_element const& f2) {
f1 -= f2;
return f1;
}
template <typename Integer_type, class = isInteger<Integer_type> >
friend void operator-=(Z2_field_element& f, const Integer_type& v) { f.element_ = (f.element_ != _get_value(v)); }
template <typename Integer_type, class = isInteger<Integer_type> >
friend Z2_field_element operator-(Z2_field_element f, const Integer_type& v) {
f -= v;
return f;
}
template <typename Integer_type, class = isInteger<Integer_type> >
friend Integer_type operator-(const Integer_type v, Z2_field_element const& f) {
return f.element_ != _get_value(v);
}
friend void operator*=(Z2_field_element& f1, Z2_field_element const& f2) {
f1.element_ = (f1.element_ && f2.element_);
}
friend Z2_field_element operator*(Z2_field_element f1, Z2_field_element const& f2) {
f1 *= f2;
return f1;
}
template <typename Integer_type, class = isInteger<Integer_type> >
friend void operator*=(Z2_field_element& f, const Integer_type& v) { f.element_ = (f.element_ && _get_value(v)); }
template <typename Integer_type, class = isInteger<Integer_type> >
friend Z2_field_element operator*(Z2_field_element f, const Integer_type& v) {
f *= v;
return f;
}
template <typename Integer_type, class = isInteger<Integer_type> >
friend Integer_type operator*(const Integer_type& v, Z2_field_element const& f) {
return f.element_ && _get_value(v);
}
friend bool operator==(const Z2_field_element& f1, const Z2_field_element& f2) { return f1.element_ == f2.element_; }
template <typename Integer_type, class = isInteger<Integer_type> >
friend bool operator==(const Integer_type& v, const Z2_field_element& f) {
return _get_value(v) == f.element_;
}
template <typename Integer_type, class = isInteger<Integer_type> >
friend bool operator==(const Z2_field_element& f, const Integer_type& v) {
return _get_value(v) == f.element_;
}
friend bool operator!=(const Z2_field_element& f1, const Z2_field_element& f2) { return !(f1 == f2); }
template <typename Integer_type, class = isInteger<Integer_type> >
friend bool operator!=(const Integer_type v, const Z2_field_element& f) {
return !(v == f);
}
template <typename Integer_type, class = isInteger<Integer_type> >
friend bool operator!=(const Z2_field_element& f, const Integer_type v) {
return !(v == f);
}
Z2_field_element& operator=(Z2_field_element other);
Z2_field_element& operator=(const unsigned int value);
friend void swap(Z2_field_element& f1, Z2_field_element& f2) { std::swap(f1.element_, f2.element_); }
operator unsigned int() const;
Z2_field_element get_inverse() const;
std::pair<Z2_field_element, unsigned int> get_partial_inverse(unsigned int productOfCharacteristics) const;
static Z2_field_element get_additive_identity();
static Z2_field_element get_multiplicative_identity();
static Z2_field_element get_partial_multiplicative_identity([[maybe_unused]] unsigned int productOfCharacteristics);
static constexpr unsigned int get_characteristic();
Element get_value() const;
// static constexpr bool handles_only_z2() { return true; }
private:
Element element_;
template <typename Integer_type, class = isInteger<Integer_type> >
static constexpr Element _get_value(Integer_type e) {
if constexpr (std::is_same_v<Integer_type, bool>) {
return e;
} else {
return e < 2 && e >= 0 ? e : e % 2; // returns bool, so %2 won't be negative and is optimized to &
}
}
};
inline Z2_field_element::Z2_field_element() : element_(false) {}
template <typename Integer_type, class>
inline Z2_field_element::Z2_field_element(Integer_type element) : element_(_get_value(element)) {}
inline Z2_field_element::Z2_field_element(const Z2_field_element& toCopy) : element_(toCopy.element_) {}
inline Z2_field_element::Z2_field_element(Z2_field_element&& toMove) noexcept
: element_(std::exchange(toMove.element_, 0)) {}
inline Z2_field_element& Z2_field_element::operator=(Z2_field_element other) {
std::swap(element_, other.element_);
return *this;
}
inline Z2_field_element& Z2_field_element::operator=(unsigned int const value) {
element_ = _get_value(value);
return *this;
}
inline Z2_field_element::operator unsigned int() const { return element_; }
inline Z2_field_element Z2_field_element::get_inverse() const {
return element_ ? Z2_field_element(1) : Z2_field_element();
}
inline std::pair<Z2_field_element, unsigned int> Z2_field_element::get_partial_inverse(
unsigned int productOfCharacteristics) const {
return {get_inverse(), productOfCharacteristics};
}
inline Z2_field_element Z2_field_element::get_additive_identity() { return Z2_field_element(); }
inline Z2_field_element Z2_field_element::get_multiplicative_identity() { return Z2_field_element(1); }
inline Z2_field_element Z2_field_element::get_partial_multiplicative_identity(
[[maybe_unused]] unsigned int productOfCharacteristics) {
return Z2_field_element(1);
}
inline constexpr unsigned int Z2_field_element::get_characteristic() { return 2; }
inline Z2_field_element::Element Z2_field_element::get_value() const { return element_; }
} // namespace persistence_fields
} // namespace Gudhi
#endif // MATRIX_FIELD_Z2_H_
Gudhi::persistence_fields::Z2_field_element
Class representing an element of the field.
Definition Z2_field.h:32
Gudhi::persistence_fields::Z2_field_element::operator-
friend Z2_field_element operator-(Z2_field_element f1, Z2_field_element const &f2)
operator-
Definition Z2_field.h:112
Gudhi::persistence_fields::Z2_field_element::operator+
friend Z2_field_element operator+(Z2_field_element f1, Z2_field_element const &f2)
operator+
Definition Z2_field.h:72
Gudhi::persistence_fields::Z2_field_element::operator*
friend Integer_type operator*(const Integer_type &v, Z2_field_element const &f)
operator*
Definition Z2_field.h:179
Gudhi::persistence_fields::Z2_field_element::operator+=
friend void operator+=(Z2_field_element &f1, Z2_field_element const &f2)
operator+=
Definition Z2_field.h:66
Gudhi::persistence_fields::Z2_field_element::get_partial_inverse
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
Gudhi::persistence_fields::Z2_field_element::operator-
friend Z2_field_element operator-(Z2_field_element f, const Integer_type &v)
operator-
Definition Z2_field.h:129
Gudhi::persistence_fields::Z2_field_element::get_characteristic
static constexpr unsigned int get_characteristic()
Returns the characteristic of the field, that is 2.
Definition Z2_field.h:348
Gudhi::persistence_fields::Z2_field_element::get_additive_identity
static Z2_field_element get_additive_identity()
Returns the additive identity of the field.
Definition Z2_field.h:339
Gudhi::persistence_fields::Z2_field_element::swap
friend void swap(Z2_field_element &f1, Z2_field_element &f2)
Swap operator.
Definition Z2_field.h:239
Gudhi::persistence_fields::Z2_field_element::get_multiplicative_identity
static Z2_field_element get_multiplicative_identity()
Returns the multiplicative identity of the field.
Definition Z2_field.h:341
Gudhi::persistence_fields::Z2_field_element::operator+=
friend void operator+=(Z2_field_element &f, const Integer_type &v)
operator+=
Definition Z2_field.h:82
Gudhi::persistence_fields::Z2_field_element::operator==
friend bool operator==(const Z2_field_element &f, const Integer_type &v)
operator==
Definition Z2_field.h:202
Gudhi::persistence_fields::Z2_field_element::operator+
friend Integer_type operator+(const Integer_type &v, const Z2_field_element &f)
operator+
Definition Z2_field.h:99
Gudhi::persistence_fields::Z2_field_element::operator-
friend Integer_type operator-(const Integer_type v, Z2_field_element const &f)
operator-
Definition Z2_field.h:139
Gudhi::persistence_fields::Z2_field_element::operator-=
friend void operator-=(Z2_field_element &f1, Z2_field_element const &f2)
operator-=
Definition Z2_field.h:106
Gudhi::persistence_fields::Z2_field_element::operator!=
friend bool operator!=(const Integer_type v, const Z2_field_element &f)
operator!=
Definition Z2_field.h:215
Gudhi::persistence_fields::Z2_field_element::get_inverse
Z2_field_element get_inverse() const
Returns the inverse of the element.
Definition Z2_field.h:330
Gudhi::persistence_fields::Z2_field_element::Z2_field_element
Z2_field_element()
Default constructor.
Definition Z2_field.h:308
Gudhi::persistence_fields::Z2_field_element::operator*
friend Z2_field_element operator*(Z2_field_element f1, Z2_field_element const &f2)
operator*
Definition Z2_field.h:152
Gudhi::persistence_fields::Z2_field_element::operator==
friend bool operator==(const Z2_field_element &f1, const Z2_field_element &f2)
operator==
Definition Z2_field.h:186
Gudhi::persistence_fields::Z2_field_element::operator==
friend bool operator==(const Integer_type &v, const Z2_field_element &f)
operator==
Definition Z2_field.h:193
Gudhi::persistence_fields::Z2_field_element::operator+
friend Z2_field_element operator+(Z2_field_element f, const Integer_type &v)
operator+
Definition Z2_field.h:89
Gudhi::persistence_fields::Z2_field_element::operator*=
friend void operator*=(Z2_field_element &f1, Z2_field_element const &f2)
operator*=
Definition Z2_field.h:146
Gudhi::persistence_fields::Z2_field_element::operator*
friend Z2_field_element operator*(Z2_field_element f, const Integer_type &v)
operator*
Definition Z2_field.h:169
Gudhi::persistence_fields::Z2_field_element::get_partial_multiplicative_identity
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
Gudhi::persistence_fields::Z2_field_element::operator=
Z2_field_element & operator=(Z2_field_element other)
Assign operator.
Definition Z2_field.h:318
Gudhi::persistence_fields::Z2_field_element::operator*=
friend void operator*=(Z2_field_element &f, const Integer_type &v)
operator*=
Definition Z2_field.h:162
Gudhi::persistence_fields::Z2_field_element::operator-=
friend void operator-=(Z2_field_element &f, const Integer_type &v)
operator-=
Definition Z2_field.h:122
Gudhi::persistence_fields::Z2_field_element::get_value
Element get_value() const
Returns the value of the element.
Definition Z2_field.h:350
Gudhi::persistence_fields::Z2_field_element::operator!=
friend bool operator!=(const Z2_field_element &f1, const Z2_field_element &f2)
operator!=
Definition Z2_field.h:208
Gudhi::persistence_fields::Z2_field_element::Element
bool Element
Definition Z2_field.h:34
Gudhi::persistence_fields::Z2_field_element::operator!=
friend bool operator!=(const Z2_field_element &f, const Integer_type v)
operator!=
Definition Z2_field.h:224
Gudhi::persistence_fields
Field namespace.
Definition Intro_field_elements_and_operators.h:16
Gudhi
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14