17#ifndef MATRIX_FIELD_ZP_VAR_H_
18#define MATRIX_FIELD_ZP_VAR_H_
26namespace persistence_fields {
40template <
typename Unsigned_integer_type =
unsigned int,
41 class = std::enable_if_t<std::is_unsigned_v<Unsigned_integer_type> > >
47 using isInteger = std::enable_if_t<std::is_integral_v<T> >;
59 template <
typename Integer_type,
class = isInteger<Integer_type> >
81 if (characteristic <= 1)
82 throw std::invalid_argument(
"Characteristic must be strictly positive and a prime number.");
84 inverse_.resize(characteristic);
86 for (
Element i = 1; i < characteristic; ++i) {
89 while ((mult % characteristic) != 1) {
91 if (mult == characteristic)
throw std::invalid_argument(
"Characteristic must be a prime number.");
97 characteristic_ = characteristic;
111 f1.element_ = Shared_Zp_field_element::_add(f1.element_, f2.element_);
125 template <
typename Integer_type,
class = isInteger<Integer_type> >
127 f.element_ = Shared_Zp_field_element::_add(f.element_, _get_value(v));
134 template <
typename Integer_type,
class = isInteger<Integer_type> >
144 template <
typename Integer_type,
class = isInteger<Integer_type> >
154 f1.element_ = Shared_Zp_field_element::_subtract(f1.element_, f2.element_);
168 template <
typename Integer_type,
class = isInteger<Integer_type> >
170 f.element_ = Shared_Zp_field_element::_subtract(f.element_, _get_value(v));
177 template <
typename Integer_type,
class = isInteger<Integer_type> >
187 template <
typename Integer_type,
class = isInteger<Integer_type> >
189 return Shared_Zp_field_element::_subtract(_get_value(v), f.element_);
196 f1.element_ = Shared_Zp_field_element::_multiply(f1.element_, f2.element_);
210 template <
typename Integer_type,
class = isInteger<Integer_type> >
212 f.element_ = Shared_Zp_field_element::_multiply(f.element_, _get_value(v));
219 template <
typename Integer_type,
class = isInteger<Integer_type> >
229 template <
typename Integer_type,
class = isInteger<Integer_type> >
239 return f1.element_ == f2.element_;
246 template <
typename Integer_type,
class = isInteger<Integer_type> >
248 return Shared_Zp_field_element::_get_value(v) == f.element_;
255 template <
typename Integer_type,
class = isInteger<Integer_type> >
257 return Shared_Zp_field_element::_get_value(v) == f.element_;
268 template <
typename Integer_type,
class = isInteger<Integer_type> >
277 template <
typename Integer_type,
class = isInteger<Integer_type> >
286 std::swap(element_, other.element_);
294 template <
typename Integer_type,
class = isInteger<Integer_type> >
296 element_ = Shared_Zp_field_element::_get_value(value);
307 operator unsigned int()
const {
return element_; }
360 static inline std::vector<Element> inverse_;
363 if (UINT_MAX - element < v) {
366 element -= characteristic_;
371 if (element >= characteristic_) element -= characteristic_;
377 element += characteristic_;
390 if (v >= characteristic_ - element) element -= characteristic_;
396 if (v >= characteristic_ - v) temp_b -= characteristic_;
403 template <
typename Integer_type,
class = isInteger<Integer_type> >
404 static constexpr Element _get_value(Integer_type e) {
405 if constexpr (std::is_signed_v<Integer_type>){
406 if (e < -
static_cast<Integer_type
>(characteristic_)) e = e % characteristic_;
407 if (e < 0)
return e += characteristic_;
408 return e < static_cast<Integer_type>(characteristic_) ? e : e % characteristic_;
410 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:42
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:80
friend bool operator==(const Shared_Zp_field_element &f, const Integer_type &v)
operator==
Definition: Zp_field_shared.h:256
Shared_Zp_field_element get_inverse() const
Returns the inverse of the element in the field.
Definition: Zp_field_shared.h:314
friend bool operator==(const Shared_Zp_field_element &f1, const Shared_Zp_field_element &f2)
operator==
Definition: Zp_field_shared.h:238
Shared_Zp_field_element()
Default constructor. Sets the element to 0.
Definition: Zp_field_shared.h:52
Shared_Zp_field_element & operator=(const Integer_type &value)
Assign operator.
Definition: Zp_field_shared.h:295
friend bool operator==(const Integer_type &v, const Shared_Zp_field_element &f)
operator==
Definition: Zp_field_shared.h:247
Shared_Zp_field_element & operator=(Shared_Zp_field_element other)
Assign operator.
Definition: Zp_field_shared.h:285
friend Integer_type operator+(const Integer_type &v, Shared_Zp_field_element f)
operator+
Definition: Zp_field_shared.h:145
Element Characteristic
Definition: Zp_field_shared.h:45
friend void operator-=(Shared_Zp_field_element &f1, const Shared_Zp_field_element &f2)
operator-=
Definition: Zp_field_shared.h:153
friend Integer_type operator*(const Integer_type &v, Shared_Zp_field_element f)
operator*
Definition: Zp_field_shared.h:230
friend Shared_Zp_field_element operator+(Shared_Zp_field_element f, const Integer_type &v)
operator+
Definition: Zp_field_shared.h:135
friend bool operator!=(const Shared_Zp_field_element &f1, const Shared_Zp_field_element &f2)
operator!=
Definition: Zp_field_shared.h:262
friend void operator*=(Shared_Zp_field_element &f1, const Shared_Zp_field_element &f2)
operator*=
Definition: Zp_field_shared.h:195
Shared_Zp_field_element(const Shared_Zp_field_element &toCopy)
Copy constructor.
Definition: Zp_field_shared.h:66
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:344
friend Shared_Zp_field_element operator*(Shared_Zp_field_element f, const Integer_type &v)
operator*
Definition: Zp_field_shared.h:220
static Shared_Zp_field_element get_additive_identity()
Returns the additive identity of the field.
Definition: Zp_field_shared.h:331
friend void operator-=(Shared_Zp_field_element &f, const Integer_type &v)
operator-=
Definition: Zp_field_shared.h:169
Unsigned_integer_type Element
Definition: Zp_field_shared.h:44
friend Shared_Zp_field_element operator-(Shared_Zp_field_element f1, const Shared_Zp_field_element &f2)
operator-
Definition: Zp_field_shared.h:159
friend Shared_Zp_field_element operator+(Shared_Zp_field_element f1, const Shared_Zp_field_element &f2)
operator+
Definition: Zp_field_shared.h:116
static Shared_Zp_field_element get_multiplicative_identity()
Returns the multiplicative identity of the field.
Definition: Zp_field_shared.h:337
friend void operator*=(Shared_Zp_field_element &f, const Integer_type &v)
operator*=
Definition: Zp_field_shared.h:211
friend void swap(Shared_Zp_field_element &f1, Shared_Zp_field_element &f2)
Swap operator.
Definition: Zp_field_shared.h:302
static Characteristic get_characteristic()
Returns the current characteristic.
Definition: Zp_field_shared.h:353
friend Shared_Zp_field_element operator-(Shared_Zp_field_element f, const Integer_type &v)
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:201
Element get_value() const
Returns the value of the element.
Definition: Zp_field_shared.h:105
Shared_Zp_field_element(Integer_type element)
Constructor setting the element to the given value.
Definition: Zp_field_shared.h:60
friend bool operator!=(const Integer_type &v, const Shared_Zp_field_element &f)
operator!=
Definition: Zp_field_shared.h:269
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:321
friend Integer_type operator-(const Integer_type &v, const Shared_Zp_field_element &f)
operator-
Definition: Zp_field_shared.h:188
Shared_Zp_field_element(Shared_Zp_field_element &&toMove) noexcept
Move constructor.
Definition: Zp_field_shared.h:72
friend void operator+=(Shared_Zp_field_element &f, const Integer_type &v)
operator+=
Definition: Zp_field_shared.h:126
friend bool operator!=(const Shared_Zp_field_element &f, const Integer_type &v)
operator!=
Definition: Zp_field_shared.h:278
friend void operator+=(Shared_Zp_field_element &f1, const Shared_Zp_field_element &f2)
operator+=
Definition: Zp_field_shared.h:110
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14