17#ifndef MATRIX_FIELD_MULTI_SMALL_OPERATORS_H_
18#define MATRIX_FIELD_MULTI_SMALL_OPERATORS_H_
27namespace persistence_fields {
56 : productOfAllCharacteristics_(0)
66 : primes_(toCopy.primes_),
67 productOfAllCharacteristics_(toCopy.productOfAllCharacteristics_),
68 partials_(toCopy.partials_)
77 : primes_(std::move(toMove.primes_)),
78 productOfAllCharacteristics_(std::move(toMove.productOfAllCharacteristics_)),
79 partials_(std::move(toMove.partials_))
92 if (maximum < 2)
throw std::invalid_argument(
"Characteristic must be strictly positive");
93 if (minimum > maximum)
throw std::invalid_argument(
"The given interval is not valid.");
94 if (minimum == maximum && !_is_prime(minimum))
95 throw std::invalid_argument(
"The given interval does not contain a prime number.");
97 productOfAllCharacteristics_ = 1;
99 for (
unsigned int i = minimum; i <= static_cast<unsigned int>(maximum); ++i) {
101 primes_.push_back(i);
102 productOfAllCharacteristics_ *= i;
106 if (primes_.empty())
throw std::invalid_argument(
"The given interval does not contain a prime number.");
108 partials_.resize(primes_.size());
109 for (
unsigned int i = 0; i < primes_.size(); ++i) {
110 unsigned int p = primes_[i];
112 unsigned int exp = p - 1;
117 if (exp & 1) partials_[i] = _multiply(partials_[i], base, productOfAllCharacteristics_);
120 base = _multiply(base, base, productOfAllCharacteristics_);
145 return e < productOfAllCharacteristics_ ? e : e % productOfAllCharacteristics_;
346 res = _multiply(res, inv_qt, productOfAllCharacteristics_);
372 if (productOfCharacteristics == 0) {
376 for (
unsigned int idx = 0; idx < primes_.size(); ++idx) {
377 if ((productOfCharacteristics % primes_[idx]) == 0) {
378 multIdentity = _add(multIdentity, partials_[idx], productOfAllCharacteristics_);
390 primes_.swap(other.primes_);
391 productOfAllCharacteristics_ = other.productOfAllCharacteristics_;
392 partials_.swap(other.partials_);
401 f1.primes_.swap(f2.primes_);
402 std::swap(f1.productOfAllCharacteristics_, f2.productOfAllCharacteristics_);
403 f1.partials_.swap(f2.partials_);
407 std::vector<unsigned int> primes_;
409 std::vector<Characteristic> partials_;
416 static constexpr bool _is_prime(
const int p);
420Multi_field_operators_with_small_characteristics::_add(Element element, Element v,
421 Characteristic characteristic) {
422 if (UINT_MAX - element < v) {
425 element -= characteristic;
430 if (element >= characteristic) element -= characteristic;
436Multi_field_operators_with_small_characteristics::_subtract(Element element, Element v,
437 Characteristic characteristic) {
439 element += characteristic;
447Multi_field_operators_with_small_characteristics::_multiply(Element a, Element b,
448 Characteristic characteristic) {
452 if (b < a) std::swap(a, b);
457 if (b >= characteristic - res) res -= characteristic;
464 if (b >= characteristic - b) temp_b -= characteristic;
470inline constexpr long int Multi_field_operators_with_small_characteristics::_get_inverse(Element element,
471 Characteristic mod) {
475 long int y = 0, x = 1;
478 int quotient = A / M;
484 y = x - quotient * y;
493inline constexpr bool Multi_field_operators_with_small_characteristics::_is_prime(
const int p) {
494 if (p <= 1)
return false;
495 if (p <= 3)
return true;
496 if (p % 2 == 0 || p % 3 == 0)
return false;
498 for (
long i = 5; i * i <= p; i = i + 6)
499 if (p % i == 0 || p % (i + 2) == 0)
return false;
Class defining operators for a multi-field with "consecutive" characteristic range,...
Definition: Multi_field_small_operators.h:38
Element subtract(Element e1, Element e2) const
Returns the subtraction in the field of the first element by the second element.
Definition: Multi_field_small_operators.h:177
Element Characteristic
Definition: Multi_field_small_operators.h:41
Element multiply_and_add(Element e, Element m, Element a) const
Multiplies the first element with the second one and adds the third one. Returns the result in the fi...
Definition: Multi_field_small_operators.h:234
Element multiply(Element e1, Element e2) const
Returns the multiplication of two elements in the field.
Definition: Multi_field_small_operators.h:209
Element add_and_multiply(Element e, Element a, Element m) const
Adds the first element to the second one and multiplies the third one with it. Returns the result in ...
Definition: Multi_field_small_operators.h:276
Element get_partial_multiplicative_identity(const Characteristic &productOfCharacteristics) const
Returns the partial multiplicative identity of the multi-field from the given product....
Definition: Multi_field_small_operators.h:371
Element get_value(Element e) const
Returns the value of an element in the field. That is the positive value of the integer modulo the cu...
Definition: Multi_field_small_operators.h:144
static constexpr Element get_multiplicative_identity()
Returns the multiplicative identity of a field.
Definition: Multi_field_small_operators.h:362
Multi_field_operators_with_small_characteristics(const Multi_field_operators_with_small_characteristics &toCopy)
Copy constructor.
Definition: Multi_field_small_operators.h:65
bool are_equal(Element e1, Element e2) const
Returns true if the two given elements are equal in the field, false otherwise.
Definition: Multi_field_small_operators.h:315
Multi_field_operators_with_small_characteristics(int minCharacteristic, int maxCharacteristic)
Constructor setting the characteristics to all prime numbers between the two given integers....
Definition: Multi_field_small_operators.h:55
static constexpr Element get_additive_identity()
Returns the additive identity of a field.
Definition: Multi_field_small_operators.h:356
void add_and_multiply_inplace_front(Element &e, Element a, Element m) const
Adds the first element to the second one and multiplies the third one with it, that is ((e + a) * m) ...
Definition: Multi_field_small_operators.h:289
Element add(Element e1, Element e2) const
Returns the sum of two elements in the field.
Definition: Multi_field_small_operators.h:155
Multi_field_operators_with_small_characteristics(Multi_field_operators_with_small_characteristics &&toMove) noexcept
Move constructor.
Definition: Multi_field_small_operators.h:76
void set_characteristic(int minimum, int maximum)
Set the characteristics of the field, which are stored in a single value as a product of all of them....
Definition: Multi_field_small_operators.h:91
std::pair< Element, Characteristic > get_partial_inverse(const Element &e, const Characteristic &productOfCharacteristics) const
Returns the inverse of the given element in the multi-field corresponding to the given sub-product of...
Definition: Multi_field_small_operators.h:335
void add_and_multiply_inplace_back(Element e, Element a, Element &m) const
Adds the first element to the second one and multiplies the third one with it, that is ((e + a) * m) ...
Definition: Multi_field_small_operators.h:303
Multi_field_operators_with_small_characteristics & operator=(Multi_field_operators_with_small_characteristics other)
Assign operator.
Definition: Multi_field_small_operators.h:389
void subtract_inplace_back(Element e1, Element &e2) const
Stores in the second element the subtraction in the field of the first element by the second element,...
Definition: Multi_field_small_operators.h:198
unsigned int Element
Definition: Multi_field_small_operators.h:40
void multiply_and_add_inplace_back(Element e, Element m, Element &a) const
Multiplies the first element with the second one and adds the third one, that is (e * m + a) % produc...
Definition: Multi_field_small_operators.h:261
void multiply_inplace(Element &e1, Element e2) const
Stores in the first element the multiplication of two given elements in the field,...
Definition: Multi_field_small_operators.h:220
void subtract_inplace_front(Element &e1, Element e2) const
Stores in the first element the subtraction in the field of the first element by the second element,...
Definition: Multi_field_small_operators.h:188
void add_inplace(Element &e1, Element e2) const
Stores in the first element the sum of two given elements in the field, that is (e1 + e2) % productOf...
Definition: Multi_field_small_operators.h:166
Element get_inverse(const Element &e) const
Returns the inverse of the given element in the sense of with respect to the product of all characte...
Definition: Multi_field_small_operators.h:324
friend void swap(Multi_field_operators_with_small_characteristics &f1, Multi_field_operators_with_small_characteristics &f2)
Swap operator.
Definition: Multi_field_small_operators.h:399
void multiply_and_add_inplace_front(Element &e, Element m, Element a) const
Multiplies the first element with the second one and adds the third one, that is (e * m + a) % produc...
Definition: Multi_field_small_operators.h:247
Multi_field_operators_with_small_characteristics()
Default constructor, sets the product of all characteristics to 0.
Definition: Multi_field_small_operators.h:46
const Characteristic & get_characteristic() const
Returns the current characteristics as the product of all of them.
Definition: Multi_field_small_operators.h:135
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14