Loading...
Searching...
No Matches
Z2_field_operators.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) 2024 Inria
*
* Modification(s):
* - YYYY/MM Author: Description of the modification
*/
#ifndef MATRIX_FIELD_Z2_OPERATORS_H_
#define MATRIX_FIELD_Z2_OPERATORS_H_
#include <utility>
namespace Gudhi {
namespace persistence_fields {
class Z2_field_operators
{
public:
using Element = bool;
using Characteristic = unsigned int;
template <class T>
using isUnsignedInteger = std::enable_if_t<std::is_unsigned_v<T> >;
template <class T>
using isInteger = std::enable_if_t<std::is_integral_v<T> >;
Z2_field_operators(){};
static constexpr Characteristic get_characteristic() { return 2; }
template <typename Integer_type, class = isInteger<Integer_type> >
static 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 &
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static Element add(Unsigned_integer_type e1, Unsigned_integer_type e2) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
return e1 != e2;
} else {
return get_value(e1) != get_value(e2);
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static void add_inplace(Unsigned_integer_type& e1, Unsigned_integer_type e2) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
e1 = e1 != e2;
} else {
e1 = get_value(e1) != get_value(e2);
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static Element subtract(Unsigned_integer_type e1, Unsigned_integer_type e2) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
return e1 != e2;
} else {
return get_value(e1) != get_value(e2);
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static void subtract_inplace_front(Unsigned_integer_type& e1, Unsigned_integer_type e2) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
e1 = e1 != e2;
} else {
e1 = get_value(e1) != get_value(e2);
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static void subtract_inplace_back(Unsigned_integer_type e1, Unsigned_integer_type& e2) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
e2 = e1 != e2;
} else {
e2 = get_value(e1) != get_value(e2);
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static Element multiply(Unsigned_integer_type e1, Unsigned_integer_type e2) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
return e1 && e2;
} else {
return get_value(e1) ? get_value(e2) : false;
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static void multiply_inplace(Unsigned_integer_type& e1, Unsigned_integer_type e2) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
e1 = e1 && e2;
} else {
e1 = get_value(e1) ? get_value(e2) : false;
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static Element multiply_and_add(Unsigned_integer_type e, Unsigned_integer_type m, Unsigned_integer_type a) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
return (e && m) != a;
} else {
return multiply(e, m) != get_value(a);
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static void multiply_and_add_inplace_front(Unsigned_integer_type& e,
Unsigned_integer_type m,
Unsigned_integer_type a) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
e = (e && m) != a;
} else {
e = multiply(e, m) != get_value(a);
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static void multiply_and_add_inplace_back(Unsigned_integer_type e,
Unsigned_integer_type m,
Unsigned_integer_type& a) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
a = (e && m) != a;
} else {
a = multiply(e, m) != get_value(a);
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static Element add_and_multiply(Unsigned_integer_type e, Unsigned_integer_type a, Unsigned_integer_type m) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
return (e != a) && m;
} else {
return add(e, a) ? get_value(m) : false;
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static void add_and_multiply_inplace_front(Unsigned_integer_type& e, Unsigned_integer_type a, Unsigned_integer_type m) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
e = (e != a) && m;
} else {
e = add(e, a) ? get_value(m) : false;
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static void add_and_multiply_inplace_back(Unsigned_integer_type& e, Unsigned_integer_type a, Unsigned_integer_type m) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
m = (e != a) && m;
} else {
m = add(e, a) ? get_value(m) : false;
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static bool are_equal(Unsigned_integer_type e1, Unsigned_integer_type e2) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
return e1 == e2;
} else {
return get_value(e1) == get_value(e2);
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static Element get_inverse(Unsigned_integer_type e) {
if constexpr (std::is_same_v<Unsigned_integer_type, bool>) {
return e;
} else {
return get_value(e);
}
}
template <typename Unsigned_integer_type, class = isUnsignedInteger<Unsigned_integer_type> >
static std::pair<Element, Characteristic> get_partial_inverse(
Unsigned_integer_type e, Characteristic productOfCharacteristics) {
return {get_inverse(e), productOfCharacteristics};
}
static constexpr Element get_additive_identity() { return false; }
static constexpr Element get_multiplicative_identity() { return true; }
static constexpr Element get_partial_multiplicative_identity(
[[maybe_unused]] Characteristic productOfCharacteristics) {
return true;
}
// static constexpr bool handles_only_z2() { return true; }
};
} // namespace persistence_fields
} // namespace Gudhi
#endif // MATRIX_FIELD_Z2_OPERATORS_H_
Gudhi::persistence_fields::Z2_field_operators::add_inplace
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:96
Gudhi::persistence_fields::Z2_field_operators::multiply_and_add
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:198
Gudhi::persistence_fields::Z2_field_operators::subtract_inplace_front
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:130
Gudhi::persistence_fields::Z2_field_operators::get_value
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:62
Gudhi::persistence_fields::Z2_field_operators::add_and_multiply_inplace_back
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:292
Gudhi::persistence_fields::Z2_field_operators::get_multiplicative_identity
static constexpr Element get_multiplicative_identity()
Returns the multiplicative identity of the field.
Definition Z2_field_operators.h:358
Gudhi::persistence_fields::Z2_field_operators::get_additive_identity
static constexpr Element get_additive_identity()
Returns the additive identity of the field.
Definition Z2_field_operators.h:352
Gudhi::persistence_fields::Z2_field_operators::get_partial_inverse
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:342
Gudhi::persistence_fields::Z2_field_operators::subtract
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
Gudhi::persistence_fields::Z2_field_operators::get_characteristic
static constexpr Characteristic get_characteristic()
Returns the characteristic of the field, that is 2.
Definition Z2_field_operators.h:51
Gudhi::persistence_fields::Z2_field_operators::subtract_inplace_back
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:146
Gudhi::persistence_fields::Z2_field_operators::multiply_and_add_inplace_back
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:236
Gudhi::persistence_fields::Z2_field_operators::add
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:79
Gudhi::persistence_fields::Z2_field_operators::multiply
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:163
Gudhi::persistence_fields::Z2_field_operators::get_partial_multiplicative_identity
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:365
Gudhi::persistence_fields::Z2_field_operators::Characteristic
unsigned int Characteristic
Definition Z2_field_operators.h:35
Gudhi::persistence_fields::Z2_field_operators::multiply_and_add_inplace_front
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:216
Gudhi::persistence_fields::Z2_field_operators::are_equal
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:310
Gudhi::persistence_fields::Z2_field_operators::add_and_multiply
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:257
Gudhi::persistence_fields::Z2_field_operators::add_and_multiply_inplace_front
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:275
Gudhi::persistence_fields::Z2_field_operators::Element
bool Element
Definition Z2_field_operators.h:34
Gudhi::persistence_fields::Z2_field_operators::Z2_field_operators
Z2_field_operators()
Default constructor.
Definition Z2_field_operators.h:44
Gudhi::persistence_fields::Z2_field_operators::multiply_inplace
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:180
Gudhi::persistence_fields::Z2_field_operators::get_inverse
static Element get_inverse(Unsigned_integer_type e)
Returns the inverse of the given element in the field.
Definition Z2_field_operators.h:326
Gudhi::persistence_fields
Field namespace.
Definition Intro_field_elements_and_operators.h:16
Gudhi
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14