17#ifndef MATRIX_FIELD_ZP_VAR_H_
18#define MATRIX_FIELD_ZP_VAR_H_
40template <
typename Unsigned_integer_type =
unsigned int,
41 class = std::enable_if_t<std::is_unsigned_v<Unsigned_integer_type> > >
48 using isInteger = std::enable_if_t<std::is_integral_v<T> >;
61 template <
typename Integer_type,
class = isInteger<Integer_type> >
89 if (characteristic <= 1)
90 throw std::invalid_argument(
"Characteristic must be strictly positive and a prime number.");
92 inverse_.resize(characteristic);
94 for (
Element i = 1; i < characteristic; ++i) {
97 while ((mult % characteristic) != 1) {
99 if (mult == characteristic)
throw std::invalid_argument(
"Characteristic must be a prime number.");
105 characteristic_ = characteristic;
120 f1.element_ = Shared_Zp_field_element::_add(f1.element_, f2.element_);
137 template <
typename Integer_type,
class = isInteger<Integer_type> >
140 f.element_ = Shared_Zp_field_element::_add(f.element_, _get_value(v));
148 template <
typename Integer_type,
class = isInteger<Integer_type> >
160 template <
typename Integer_type,
class = isInteger<Integer_type> >
172 f1.element_ = Shared_Zp_field_element::_subtract(f1.element_, f2.element_);
189 template <
typename Integer_type,
class = isInteger<Integer_type> >
192 f.element_ = Shared_Zp_field_element::_subtract(f.element_, _get_value(v));
200 template <
typename Integer_type,
class = isInteger<Integer_type> >
212 template <
typename Integer_type,
class = isInteger<Integer_type> >
215 return Shared_Zp_field_element::_subtract(_get_value(v), f.element_);
223 f1.element_ = Shared_Zp_field_element::_multiply(f1.element_, f2.element_);
240 template <
typename Integer_type,
class = isInteger<Integer_type> >
243 f.element_ = Shared_Zp_field_element::_multiply(f.element_, _get_value(v));
251 template <
typename Integer_type,
class = isInteger<Integer_type> >
263 template <
typename Integer_type,
class = isInteger<Integer_type> >
275 return f1.element_ == f2.element_;
283 template <
typename Integer_type,
class = isInteger<Integer_type> >
286 return Shared_Zp_field_element::_get_value(v) == f.element_;
294 template <
typename Integer_type,
class = isInteger<Integer_type> >
297 return Shared_Zp_field_element::_get_value(v) == f.element_;
310 template <
typename Integer_type,
class = isInteger<Integer_type> >
321 template <
typename Integer_type,
class = isInteger<Integer_type> >
332 std::swap(element_, other.element_);
341 if (
this == &other)
return *
this;
343 element_ = std::exchange(other.element_, 0);
352 template <
typename Integer_type,
class = isInteger<Integer_type> >
355 element_ = Shared_Zp_field_element::_get_value(value);
364 std::swap(f1.element_, f2.element_);
370 operator unsigned int()
const {
return element_; }
428 static inline std::vector<Element> inverse_;
432 if (UINT_MAX - element < v) {
435 element -= characteristic_;
440 if (element >= characteristic_) element -= characteristic_;
448 element += characteristic_;
463 if (v >= characteristic_ - element) element -= characteristic_;
469 if (v >= characteristic_ - v) temp_b -= characteristic_;
476 template <
typename Integer_type,
class = isInteger<Integer_type> >
477 static constexpr Element _get_value(Integer_type e)
479 if constexpr (std::is_signed_v<Integer_type>) {
480 if (e < -
static_cast<Integer_type
>(characteristic_)) e = e % characteristic_;
481 if (e < 0)
return e += characteristic_;
482 return e < static_cast<Integer_type>(characteristic_) ? e : e % characteristic_;
484 return e < characteristic_ ? e : e % characteristic_;
Class representing an element of the field for any prime number . If each instantiation of the class...
Definition Zp_field_shared.h:43
Shared_Zp_field_element & operator=(Shared_Zp_field_element &&other) noexcept
Move assign operator.
Definition Zp_field_shared.h:339
static void initialize(Characteristic characteristic)
Initialize the field to the given characteristic. Should be called first before constructing the fiel...
Definition Zp_field_shared.h:87
friend bool operator==(const Shared_Zp_field_element &f, const Integer_type &v)
operator==
Definition Zp_field_shared.h:295
Shared_Zp_field_element get_inverse() const
Returns the inverse of the element in the field.
Definition Zp_field_shared.h:377
friend bool operator==(const Shared_Zp_field_element &f1, const Shared_Zp_field_element &f2)
operator==
Definition Zp_field_shared.h:273
Shared_Zp_field_element()
Default constructor. Sets the element to 0.
Definition Zp_field_shared.h:53
Shared_Zp_field_element & operator=(const Integer_type &value)
Assign operator.
Definition Zp_field_shared.h:353
friend bool operator==(const Integer_type &v, const Shared_Zp_field_element &f)
operator==
Definition Zp_field_shared.h:284
Shared_Zp_field_element & operator=(Shared_Zp_field_element other)
Assign operator.
Definition Zp_field_shared.h:330
friend Integer_type operator+(const Integer_type &v, Shared_Zp_field_element f)
operator+
Definition Zp_field_shared.h:161
Element Characteristic
Definition Zp_field_shared.h:46
friend void operator-=(Shared_Zp_field_element &f1, const Shared_Zp_field_element &f2)
operator-=
Definition Zp_field_shared.h:170
friend Integer_type operator*(const Integer_type &v, Shared_Zp_field_element f)
operator*
Definition Zp_field_shared.h:264
friend Shared_Zp_field_element operator+(Shared_Zp_field_element f, const Integer_type &v)
operator+
Definition Zp_field_shared.h:149
friend bool operator!=(const Shared_Zp_field_element &f1, const Shared_Zp_field_element &f2)
operator!=
Definition Zp_field_shared.h:303
friend void operator*=(Shared_Zp_field_element &f1, const Shared_Zp_field_element &f2)
operator*=
Definition Zp_field_shared.h:221
static Shared_Zp_field_element get_partial_multiplicative_identity(Characteristic productOfCharacteristics)
For interface purposes with multi-fields. Returns the multiplicative identity of the field.
Definition Zp_field_shared.h:410
friend Shared_Zp_field_element operator*(Shared_Zp_field_element f, const Integer_type &v)
operator*
Definition Zp_field_shared.h:252
static Shared_Zp_field_element get_additive_identity()
Returns the additive identity of the field.
Definition Zp_field_shared.h:395
friend void operator-=(Shared_Zp_field_element &f, const Integer_type &v)
operator-=
Definition Zp_field_shared.h:190
Unsigned_integer_type Element
Definition Zp_field_shared.h:45
friend Shared_Zp_field_element operator-(Shared_Zp_field_element f1, const Shared_Zp_field_element &f2)
operator-
Definition Zp_field_shared.h:178
friend Shared_Zp_field_element operator+(Shared_Zp_field_element f1, const Shared_Zp_field_element &f2)
operator+
Definition Zp_field_shared.h:126
static Shared_Zp_field_element get_multiplicative_identity()
Returns the multiplicative identity of the field.
Definition Zp_field_shared.h:402
friend void operator*=(Shared_Zp_field_element &f, const Integer_type &v)
operator*=
Definition Zp_field_shared.h:241
friend void swap(Shared_Zp_field_element &f1, Shared_Zp_field_element &f2) noexcept
Swap operator.
Definition Zp_field_shared.h:362
static Characteristic get_characteristic()
Returns the current characteristic.
Definition Zp_field_shared.h:421
friend Shared_Zp_field_element operator-(Shared_Zp_field_element f, const Integer_type &v)
operator-
Definition Zp_field_shared.h:201
friend Shared_Zp_field_element operator*(Shared_Zp_field_element f1, const Shared_Zp_field_element &f2)
operator*
Definition Zp_field_shared.h:229
Element get_value() const
Returns the value of the element.
Definition Zp_field_shared.h:113
Shared_Zp_field_element(Integer_type element)
Constructor setting the element to the given value.
Definition Zp_field_shared.h:62
friend bool operator!=(const Integer_type &v, const Shared_Zp_field_element &f)
operator!=
Definition Zp_field_shared.h:311
std::pair< Shared_Zp_field_element, Characteristic > get_partial_inverse(Characteristic productOfCharacteristics) const
For interface purposes with multi-fields. Returns the inverse together with the argument.
Definition Zp_field_shared.h:385
friend Integer_type operator-(const Integer_type &v, const Shared_Zp_field_element &f)
operator-
Definition Zp_field_shared.h:213
Shared_Zp_field_element(Shared_Zp_field_element &&toMove) noexcept
Move constructor.
Definition Zp_field_shared.h:77
friend void operator+=(Shared_Zp_field_element &f, const Integer_type &v)
operator+=
Definition Zp_field_shared.h:138
friend bool operator!=(const Shared_Zp_field_element &f, const Integer_type &v)
operator!=
Definition Zp_field_shared.h:322
friend void operator+=(Shared_Zp_field_element &f1, const Shared_Zp_field_element &f2)
operator+=
Definition Zp_field_shared.h:118
Shared_Zp_field_element(const Shared_Zp_field_element &toCopy)=default
Copy constructor.
Field namespace.
Definition Intro_field_elements_and_operators.h:16
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14