17#ifndef MATRIX_FIELD_MULTI_SMALL_SHARED_H_
18#define MATRIX_FIELD_MULTI_SMALL_SHARED_H_
27namespace persistence_fields {
43template <
typename Unsigned_integer_type =
unsigned int,
44 class = std::enable_if_t<std::is_unsigned_v<Unsigned_integer_type> > >
50 using isInteger = std::enable_if_t<std::is_integral_v<T> >;
62 template <
typename Integer_type,
class = isInteger<Integer_type> >
71 : element_(toCopy.element_) {}
79 : element_(std::exchange(toMove.element_, 0)) {}
89 static void initialize(
unsigned int minimum,
unsigned int maximum) {
90 if (maximum < 2)
throw std::invalid_argument(
"Characteristic must be strictly positive");
91 if (minimum > maximum)
throw std::invalid_argument(
"The given interval is not valid.");
92 if (minimum == maximum && !_is_prime(minimum))
93 throw std::invalid_argument(
"The given interval does not contain a prime number.");
95 productOfAllCharacteristics_ = 1;
97 for (
unsigned int i = minimum; i <= maximum; ++i) {
100 productOfAllCharacteristics_ *= i;
104 if (primes_.empty())
throw std::invalid_argument(
"The given interval does not contain a prime number.");
106 partials_.resize(primes_.size());
115 if (exp & 1) partials_[i] = _multiply(partials_[i], base);
118 base = _multiply(base, base);
134 f1.element_ = _add(f1.element_, f2.element_);
150 template <
typename Integer_type,
class = isInteger<Integer_type> >
152 f.element_ = _add(f.element_, _get_value(v));
159 template <
typename Integer_type,
class = isInteger<Integer_type> >
170 template <
typename Integer_type,
class = isInteger<Integer_type> >
181 f1.element_ = _subtract(f1.element_, f2.element_);
197 template <
typename Integer_type,
class = isInteger<Integer_type> >
199 f.element_ = _subtract(f.element_, _get_value(v));
206 template <
typename Integer_type,
class = isInteger<Integer_type> >
217 template <
typename Integer_type,
class = isInteger<Integer_type> >
219 return _subtract(_get_value(v), f.element_);
227 f1.element_ = _multiply(f1.element_, f2.element_);
243 template <
typename Integer_type,
class = isInteger<Integer_type> >
245 f.element_ = _multiply(f.element_, _get_value(v));
252 template <
typename Integer_type,
class = isInteger<Integer_type> >
263 template <
typename Integer_type,
class = isInteger<Integer_type> >
274 return f1.element_ == f2.element_;
281 template <
typename Integer_type,
class = isInteger<Integer_type> >
283 return _get_value(v) == f.element_;
290 template <
typename Integer_type,
class = isInteger<Integer_type> >
292 return _get_value(v) == f.element_;
306 template <
typename Integer_type,
class = isInteger<Integer_type> >
315 template <
typename Integer_type,
class = isInteger<Integer_type> >
325 std::swap(element_, other.element_);
333 template <
typename Integer_type,
class = isInteger<Integer_type> >
335 element_ = _get_value(value);
343 std::swap(f1.element_, f2.element_);
349 operator unsigned int()
const {
return element_; }
368 Characteristic gcd = std::gcd(element_, productOfAllCharacteristics_);
370 if (gcd == productOfCharacteristics)
375 const Element inv_qt = _get_inverse(element_, QT);
408 if (productOfCharacteristics == 0) {
413 if ((productOfCharacteristics % primes_[idx]) == 0) {
414 mult += partials_[idx];
436 static constexpr bool _is_prime(
const unsigned int p) {
437 if (p <= 1)
return false;
438 if (p <= 3)
return true;
439 if (p % 2 == 0 || p % 3 == 0)
return false;
441 for (
unsigned long i = 5; i * i <= p; i = i + 6)
442 if (p % i == 0 || p % (i + 2) == 0)
return false;
450 if (b < a) std::swap(a, b);
455 if (b >= productOfAllCharacteristics_ - res) res -= productOfAllCharacteristics_;
462 if (b >= productOfAllCharacteristics_ - b) temp_b -= productOfAllCharacteristics_;
468 if (UINT_MAX - element < v) {
471 element -= productOfAllCharacteristics_;
476 if (element >= productOfAllCharacteristics_) element -= productOfAllCharacteristics_;
482 element += productOfAllCharacteristics_;
495 int quotient = A / M;
501 y = x - quotient * y;
510 template <
typename Integer_type,
class = isInteger<Integer_type> >
511 static constexpr Element _get_value(Integer_type e) {
512 if constexpr (std::is_signed_v<Integer_type>){
513 if (e < -
static_cast<Integer_type
>(productOfAllCharacteristics_)) e = e % productOfAllCharacteristics_;
514 if (e < 0)
return e += productOfAllCharacteristics_;
515 return e < static_cast<Integer_type>(productOfAllCharacteristics_) ? e : e % productOfAllCharacteristics_;
517 return e < productOfAllCharacteristics_ ? e : e % productOfAllCharacteristics_;
522 static inline std::vector<Characteristic> primes_;
524 static inline std::vector<Characteristic> partials_;
525 static inline constexpr Element multiplicativeID_ = 1;
Class representing an element of a multi-field, such that productOfAllCharacteristics ^ 2 fits into t...
Definition: Multi_field_small_shared.h:45
friend Integer_type operator*(const Integer_type &v, Shared_multi_field_element_with_small_characteristics f)
operator*
Definition: Multi_field_small_shared.h:264
static void initialize(unsigned int minimum, unsigned int maximum)
Initialize the multi-field to the characteristics (primes) contained in the given interval....
Definition: Multi_field_small_shared.h:89
Shared_multi_field_element_with_small_characteristics get_inverse() const
Returns the inverse of the element in the multi-field, see .
Definition: Multi_field_small_shared.h:356
friend Shared_multi_field_element_with_small_characteristics operator+(Shared_multi_field_element_with_small_characteristics f, const Integer_type &v)
operator+
Definition: Multi_field_small_shared.h:160
friend Integer_type operator+(const Integer_type &v, Shared_multi_field_element_with_small_characteristics f)
operator+
Definition: Multi_field_small_shared.h:171
friend Shared_multi_field_element_with_small_characteristics operator*(Shared_multi_field_element_with_small_characteristics f, const Integer_type &v)
operator*
Definition: Multi_field_small_shared.h:253
friend void operator+=(Shared_multi_field_element_with_small_characteristics &f1, Shared_multi_field_element_with_small_characteristics const &f2)
operator+=
Definition: Multi_field_small_shared.h:132
Shared_multi_field_element_with_small_characteristics(Integer_type element)
Constructor setting the element to the given value.
Definition: Multi_field_small_shared.h:63
friend void operator-=(Shared_multi_field_element_with_small_characteristics &f, const Integer_type &v)
operator-=
Definition: Multi_field_small_shared.h:198
friend bool operator!=(const Shared_multi_field_element_with_small_characteristics &f1, const Shared_multi_field_element_with_small_characteristics &f2)
operator!=
Definition: Multi_field_small_shared.h:297
friend bool operator==(const Integer_type &v, const Shared_multi_field_element_with_small_characteristics &f)
operator==
Definition: Multi_field_small_shared.h:282
static Shared_multi_field_element_with_small_characteristics get_multiplicative_identity()
Returns the multiplicative identity of a field.
Definition: Multi_field_small_shared.h:396
Shared_multi_field_element_with_small_characteristics & operator=(const Integer_type &value)
Assign operator.
Definition: Multi_field_small_shared.h:334
friend Shared_multi_field_element_with_small_characteristics operator-(Shared_multi_field_element_with_small_characteristics f, const Integer_type &v)
operator-
Definition: Multi_field_small_shared.h:207
Unsigned_integer_type Element
Definition: Multi_field_small_shared.h:47
Shared_multi_field_element_with_small_characteristics(const Shared_multi_field_element_with_small_characteristics &toCopy)
Copy constructor.
Definition: Multi_field_small_shared.h:69
Element Characteristic
Definition: Multi_field_small_shared.h:48
friend Shared_multi_field_element_with_small_characteristics operator-(Shared_multi_field_element_with_small_characteristics f1, Shared_multi_field_element_with_small_characteristics const &f2)
operator-
Definition: Multi_field_small_shared.h:186
friend bool operator!=(const Integer_type v, const Shared_multi_field_element_with_small_characteristics &f)
operator!=
Definition: Multi_field_small_shared.h:307
static Shared_multi_field_element_with_small_characteristics get_partial_multiplicative_identity(const Characteristic &productOfCharacteristics)
Returns the partial multiplicative identity of the multi-field from the given product....
Definition: Multi_field_small_shared.h:406
friend void operator+=(Shared_multi_field_element_with_small_characteristics &f, const Integer_type &v)
operator+=
Definition: Multi_field_small_shared.h:151
static Characteristic get_characteristic()
Returns the product of all characteristics.
Definition: Multi_field_small_shared.h:424
Shared_multi_field_element_with_small_characteristics & operator=(Shared_multi_field_element_with_small_characteristics other)
Assign operator.
Definition: Multi_field_small_shared.h:323
static Shared_multi_field_element_with_small_characteristics get_additive_identity()
Returns the additive identity of a field.
Definition: Multi_field_small_shared.h:388
friend Shared_multi_field_element_with_small_characteristics operator+(Shared_multi_field_element_with_small_characteristics f1, Shared_multi_field_element_with_small_characteristics const &f2)
operator+
Definition: Multi_field_small_shared.h:139
std::pair< Shared_multi_field_element_with_small_characteristics, Characteristic > get_partial_inverse(Characteristic productOfCharacteristics) const
Returns the inverse of the element with respect to a sub-product of the characteristics in the multi-...
Definition: Multi_field_small_shared.h:366
Shared_multi_field_element_with_small_characteristics()
Default constructor. Sets the element to 0.
Definition: Multi_field_small_shared.h:55
friend Integer_type operator-(const Integer_type &v, const Shared_multi_field_element_with_small_characteristics &f)
operator-
Definition: Multi_field_small_shared.h:218
friend Shared_multi_field_element_with_small_characteristics operator*(Shared_multi_field_element_with_small_characteristics f1, Shared_multi_field_element_with_small_characteristics const &f2)
operator*
Definition: Multi_field_small_shared.h:232
Shared_multi_field_element_with_small_characteristics(Shared_multi_field_element_with_small_characteristics &&toMove) noexcept
Move constructor.
Definition: Multi_field_small_shared.h:77
friend bool operator==(const Shared_multi_field_element_with_small_characteristics &f, const Integer_type &v)
operator==
Definition: Multi_field_small_shared.h:291
friend void operator*=(Shared_multi_field_element_with_small_characteristics &f, const Integer_type &v)
operator*=
Definition: Multi_field_small_shared.h:244
friend void operator-=(Shared_multi_field_element_with_small_characteristics &f1, Shared_multi_field_element_with_small_characteristics const &f2)
operator-=
Definition: Multi_field_small_shared.h:179
friend void operator*=(Shared_multi_field_element_with_small_characteristics &f1, Shared_multi_field_element_with_small_characteristics const &f2)
operator*=
Definition: Multi_field_small_shared.h:225
Element get_value() const
Returns the value of the element.
Definition: Multi_field_small_shared.h:431
friend bool operator==(const Shared_multi_field_element_with_small_characteristics &f1, const Shared_multi_field_element_with_small_characteristics &f2)
operator==
Definition: Multi_field_small_shared.h:272
friend void swap(Shared_multi_field_element_with_small_characteristics &f1, Shared_multi_field_element_with_small_characteristics &f2)
Swap operator.
Definition: Multi_field_small_shared.h:341
friend bool operator!=(const Shared_multi_field_element_with_small_characteristics &f, const Integer_type v)
operator!=
Definition: Multi_field_small_shared.h:316
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14