17#ifndef MATRIX_FIELD_MULTI_SMALL_H_
18#define MATRIX_FIELD_MULTI_SMALL_H_
26namespace persistence_fields {
41template <
unsigned int minimum,
unsigned int maximum,
typename Unsigned_integer_type =
unsigned int,
42 class = std::enable_if_t<std::is_unsigned_v<Unsigned_integer_type> > >
48 using isInteger = std::enable_if_t<std::is_integral_v<T> >;
54 static_assert(maximum >= 2,
"Characteristics have to be positive.");
55 static_assert(minimum <= maximum,
"The given interval is not valid.");
56 static_assert(minimum != maximum || _is_prime(minimum),
"The given interval does not contain a prime number.");
57 static_assert(productOfAllCharacteristics_ != 1,
"The given interval does not contain a prime number.");
64 template <
typename Integer_type,
class = isInteger<Integer_type> >
66 : element_(_get_value(element)) {
67 static_assert(maximum >= 2,
"Characteristics has to be positive.");
68 static_assert(minimum <= maximum,
"The given interval is not valid.");
69 static_assert(minimum != maximum || _is_prime(minimum),
"The given interval does not contain a prime number.");
70 static_assert(productOfAllCharacteristics_ != 1,
"The given interval does not contain a prime number.");
78 : element_(toCopy.element_) {}
85 : element_(std::exchange(toMove.element_, 0)) {}
92 f1.element_ = _add(f1.element_, f2.element_);
107 template <
typename Integer_type,
class = isInteger<Integer_type> >
109 f.element_ = _add(f.element_, _get_value(v));
116 template <
typename Integer_type,
class = isInteger<Integer_type> >
118 const Integer_type& v) {
127 template <
typename Integer_type,
class = isInteger<Integer_type> >
138 f1.element_ = _subtract(f1.element_, f2.element_);
153 template <
typename Integer_type,
class = isInteger<Integer_type> >
155 f.element_ = _subtract(f.element_, _get_value(v));
162 template <
typename Integer_type,
class = isInteger<Integer_type> >
164 const Integer_type& v) {
173 template <
typename Integer_type,
class = isInteger<Integer_type> >
175 return _subtract(_get_value(v), f.element_);
183 f1.element_ = _multiply(f1.element_, f2.element_);
198 template <
typename Integer_type,
class = isInteger<Integer_type> >
200 f.element_ = _multiply(f.element_, _get_value(v));
207 template <
typename Integer_type,
class = isInteger<Integer_type> >
209 const Integer_type& v) {
218 template <
typename Integer_type,
class = isInteger<Integer_type> >
229 return f1.element_ == f2.element_;
236 template <
typename Integer_type,
class = isInteger<Integer_type> >
238 return _get_value(v) == f.element_;
245 template <
typename Integer_type,
class = isInteger<Integer_type> >
247 return _get_value(v) == f.element_;
261 template <
typename Integer_type,
class = isInteger<Integer_type> >
270 template <
typename Integer_type,
class = isInteger<Integer_type> >
279 std::swap(element_, other.element_);
287 template <
typename Integer_type,
class = isInteger<Integer_type> >
289 element_ = _get_value(value);
297 std::swap(f1.element_, f2.element_);
303 operator unsigned int()
const {
return element_; }
322 Characteristic gcd = std::gcd(element_, productOfAllCharacteristics_);
324 if (gcd == productOfCharacteristics)
329 const Element inv_qt = _get_inverse(element_, QT);
362 if (productOfCharacteristics == 0) {
367 if ((productOfCharacteristics % primes_[idx]) == 0) {
368 mult += partials_[idx];
390 static constexpr bool _is_prime(
const unsigned int p) {
391 if (p <= 1)
return false;
392 if (p <= 3)
return true;
393 if (p % 2 == 0 || p % 3 == 0)
return false;
395 for (
unsigned long i = 5; i * i <= p; i = i + 6)
396 if (p % i == 0 || p % (i + 2) == 0)
return false;
404 if (b < a) std::swap(a, b);
409 if (b >= productOfAllCharacteristics_ - res) res -= productOfAllCharacteristics_;
416 if (b >= productOfAllCharacteristics_ - b) temp_b -= productOfAllCharacteristics_;
422 if (UINT_MAX - element < v) {
425 element -= productOfAllCharacteristics_;
430 if (element >= productOfAllCharacteristics_) element -= productOfAllCharacteristics_;
436 element += productOfAllCharacteristics_;
442 static constexpr int _get_inverse(
Element element,
const Element mod) {
449 int quotient = A / M;
455 y = x - quotient * y;
464 template <
typename Integer_type,
class = isInteger<Integer_type> >
465 static constexpr Element _get_value(Integer_type e) {
466 if constexpr (std::is_signed_v<Integer_type>){
467 if (e < -
static_cast<Integer_type
>(productOfAllCharacteristics_)) e = e % productOfAllCharacteristics_;
468 if (e < 0)
return e += productOfAllCharacteristics_;
469 return e < static_cast<Integer_type>(productOfAllCharacteristics_) ? e : e % productOfAllCharacteristics_;
471 return e < productOfAllCharacteristics_ ? e : e % productOfAllCharacteristics_;
476 static inline const std::vector<Characteristic> primes_ = []() {
477 std::vector<Characteristic> res;
485 static inline constexpr Characteristic productOfAllCharacteristics_ = []() {
494 static inline const std::vector<Characteristic> partials_ = []() {
495 std::vector<Characteristic> res;
497 if (productOfAllCharacteristics_ == 1)
return res;
507 if (exp & 1) res.back() = _multiply(res.back(), base);
510 base = _multiply(base, base);
518 static inline constexpr Element multiplicativeID_ = 1;
Class representing an element of a multi-field, such that the product of all characteristics fits int...
Definition: Multi_field_small.h:43
friend Multi_field_element_with_small_characteristics operator-(Multi_field_element_with_small_characteristics f1, Multi_field_element_with_small_characteristics const &f2)
operator-
Definition: Multi_field_small.h:143
friend void operator+=(Multi_field_element_with_small_characteristics &f, const Integer_type &v)
operator+=
Definition: Multi_field_small.h:108
Multi_field_element_with_small_characteristics & operator=(Multi_field_element_with_small_characteristics other)
Assign operator.
Definition: Multi_field_small.h:278
friend void operator-=(Multi_field_element_with_small_characteristics &f, const Integer_type &v)
operator-=
Definition: Multi_field_small.h:154
friend void operator*=(Multi_field_element_with_small_characteristics &f1, Multi_field_element_with_small_characteristics const &f2)
operator*=
Definition: Multi_field_small.h:181
friend bool operator!=(const Integer_type v, const Multi_field_element_with_small_characteristics &f)
operator!=
Definition: Multi_field_small.h:262
friend void operator-=(Multi_field_element_with_small_characteristics &f1, Multi_field_element_with_small_characteristics const &f2)
operator-=
Definition: Multi_field_small.h:136
Element get_value() const
Returns the value of the element.
Definition: Multi_field_small.h:385
friend bool operator==(const Integer_type v, const Multi_field_element_with_small_characteristics &f)
operator==
Definition: Multi_field_small.h:237
Multi_field_element_with_small_characteristics()
Default constructor. Sets the element to 0.
Definition: Multi_field_small.h:53
friend void swap(Multi_field_element_with_small_characteristics &f1, Multi_field_element_with_small_characteristics &f2)
Swap operator.
Definition: Multi_field_small.h:295
friend Integer_type operator-(const Integer_type &v, const Multi_field_element_with_small_characteristics &f)
operator-
Definition: Multi_field_small.h:174
std::pair< 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.h:320
friend Multi_field_element_with_small_characteristics operator*(Multi_field_element_with_small_characteristics f, const Integer_type &v)
operator*
Definition: Multi_field_small.h:208
friend Integer_type operator+(const Integer_type &v, Multi_field_element_with_small_characteristics f)
operator+
Definition: Multi_field_small.h:128
Multi_field_element_with_small_characteristics get_inverse() const
Returns the inverse of the element in the multi-field, see .
Definition: Multi_field_small.h:310
friend Multi_field_element_with_small_characteristics operator+(Multi_field_element_with_small_characteristics f1, Multi_field_element_with_small_characteristics const &f2)
operator+
Definition: Multi_field_small.h:97
Element Characteristic
Definition: Multi_field_small.h:46
static 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.h:360
Multi_field_element_with_small_characteristics(Integer_type element)
Constructor setting the element to the given value.
Definition: Multi_field_small.h:65
Multi_field_element_with_small_characteristics(const Multi_field_element_with_small_characteristics &toCopy)
Copy constructor.
Definition: Multi_field_small.h:77
friend Multi_field_element_with_small_characteristics operator-(Multi_field_element_with_small_characteristics f, const Integer_type &v)
operator-
Definition: Multi_field_small.h:163
Unsigned_integer_type Element
Definition: Multi_field_small.h:45
friend Multi_field_element_with_small_characteristics operator*(Multi_field_element_with_small_characteristics f1, Multi_field_element_with_small_characteristics const &f2)
operator*
Definition: Multi_field_small.h:188
Multi_field_element_with_small_characteristics(Multi_field_element_with_small_characteristics &&toMove) noexcept
Move constructor.
Definition: Multi_field_small.h:84
friend bool operator==(const Multi_field_element_with_small_characteristics &f, const Integer_type v)
operator==
Definition: Multi_field_small.h:246
friend void operator+=(Multi_field_element_with_small_characteristics &f1, Multi_field_element_with_small_characteristics const &f2)
operator+=
Definition: Multi_field_small.h:90
static Multi_field_element_with_small_characteristics get_additive_identity()
Returns the additive identity of a field.
Definition: Multi_field_small.h:342
friend bool operator!=(const Multi_field_element_with_small_characteristics &f, const Integer_type v)
operator!=
Definition: Multi_field_small.h:271
friend bool operator!=(const Multi_field_element_with_small_characteristics &f1, const Multi_field_element_with_small_characteristics &f2)
operator!=
Definition: Multi_field_small.h:252
static Multi_field_element_with_small_characteristics get_multiplicative_identity()
Returns the multiplicative identity of a field.
Definition: Multi_field_small.h:350
static constexpr Characteristic get_characteristic()
Returns the product of all characteristics.
Definition: Multi_field_small.h:378
friend Integer_type operator*(const Integer_type &v, Multi_field_element_with_small_characteristics f)
operator*
Definition: Multi_field_small.h:219
friend void operator*=(Multi_field_element_with_small_characteristics &f, const Integer_type &v)
operator*=
Definition: Multi_field_small.h:199
friend Multi_field_element_with_small_characteristics operator+(Multi_field_element_with_small_characteristics f, const Integer_type &v)
operator+
Definition: Multi_field_small.h:117
friend bool operator==(const Multi_field_element_with_small_characteristics &f1, const Multi_field_element_with_small_characteristics &f2)
operator==
Definition: Multi_field_small.h:227
Multi_field_element_with_small_characteristics & operator=(const Integer_type &value)
Assign operator.
Definition: Multi_field_small.h:288
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14