17 #ifndef MATRIX_FIELD_MULTI_SHARED_H_
18 #define MATRIX_FIELD_MULTI_SHARED_H_
26 namespace persistence_fields {
73 static void initialize(
unsigned int minimum,
unsigned int maximum);
79 f1.element_ += f2.element_;
80 mpz_mod(f1.element_.get_mpz_t(), f1.element_.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
94 mpz_mod(f.element_.get_mpz_t(), f.element_.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
108 mpz_mod(v.get_mpz_t(), v.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
116 f1.element_ -= f2.element_;
117 mpz_mod(f1.element_.get_mpz_t(), f1.element_.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
131 mpz_mod(f.element_.get_mpz_t(), f.element_.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
144 if (v >= productOfAllCharacteristics_)
145 mpz_mod(v.get_mpz_t(), v.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
146 if (f.element_ > v) v += productOfAllCharacteristics_;
155 f1.element_ *= f2.element_;
156 mpz_mod(f1.element_.get_mpz_t(), f1.element_.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
170 mpz_mod(f.element_.get_mpz_t(), f.element_.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
184 mpz_mod(v.get_mpz_t(), v.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
192 return f1.element_ == f2.element_;
198 if (v < productOfAllCharacteristics_)
return v == f.element_;
200 mpz_mod(e.get_mpz_t(), e.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
201 return e == f.element_;
207 if (v < productOfAllCharacteristics_)
return v == f.element_;
209 mpz_mod(e.get_mpz_t(), e.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
210 return e == f.element_;
239 std::swap(f1.element_, f2.element_);
245 operator unsigned int()
const;
249 operator mpz_class()
const;
306 static inline std::vector<unsigned int> primes_;
308 static inline std::vector<element_type> partials_;
311 static constexpr
bool _is_prime(
const int p);
317 mpz_mod(element_.get_mpz_t(), element_.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
321 : element_(toCopy.element_) {}
324 : element_(std::move(toMove.element_)) {}
327 if (maximum < 2)
throw std::invalid_argument(
"Characteristic must be strictly positive");
328 if (minimum > maximum)
throw std::invalid_argument(
"The given interval is not valid.");
329 if (minimum == maximum && !_is_prime(minimum))
330 throw std::invalid_argument(
"The given interval does not contain a prime number.");
332 unsigned int curr_prime = minimum;
334 mpz_init_set_ui(tmp_prime, minimum);
336 int is_prime = mpz_probab_prime_p(tmp_prime, 25);
339 mpz_nextprime(tmp_prime, tmp_prime);
340 curr_prime = mpz_get_ui(tmp_prime);
344 while (curr_prime <= maximum) {
345 primes_.push_back(curr_prime);
346 mpz_nextprime(tmp_prime, tmp_prime);
347 curr_prime = mpz_get_ui(tmp_prime);
349 mpz_clear(tmp_prime);
351 if (primes_.empty())
throw std::invalid_argument(
"The given interval does not contain a prime number.");
353 productOfAllCharacteristics_ = 1;
354 for (
const unsigned int p : primes_) {
355 productOfAllCharacteristics_ *= p;
358 partials_.resize(primes_.size());
359 for (
unsigned int i = 0; i < primes_.size(); ++i) {
360 unsigned int p = primes_[i];
361 partials_[i] = productOfAllCharacteristics_ / p;
362 mpz_powm_ui(partials_[i].get_mpz_t(), partials_[i].get_mpz_t(), p - 1, productOfAllCharacteristics_.get_mpz_t());
373 std::swap(element_, other.element_);
378 mpz_mod(element_.get_mpz_t(), value.get_mpz_t(), productOfAllCharacteristics_.get_mpz_t());
382 inline Shared_multi_field_element::operator
unsigned int()
const {
return element_.get_ui(); }
384 inline Shared_multi_field_element::operator mpz_class()
const {
return element_; }
390 inline std::pair<Shared_multi_field_element, Shared_multi_field_element::characteristic_type>
393 mpz_gcd(QR.get_mpz_t(), element_.get_mpz_t(), productOfCharacteristics.get_mpz_t());
400 mpz_invert(inv_qt.get_mpz_t(), element_.get_mpz_t(), QT.get_mpz_t());
418 if (productOfCharacteristics == 0) {
422 for (
unsigned int idx = 0; idx < primes_.size(); ++idx) {
423 if ((productOfCharacteristics % primes_[idx]) == 0) {
424 mult += partials_[idx];
431 return productOfAllCharacteristics_;
436 inline constexpr
bool Shared_multi_field_element::_is_prime(
const 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 (
long i = 5; i * i <= p; i = i + 6)
442 if (p % i == 0 || p % (i + 2) == 0)
return false;
Class representing an element of a multi-field. If each instanciation of the class can represent anot...
Definition: Multi_field_shared.h:38
static Shared_multi_field_element get_partial_multiplicative_identity(const characteristic_type &productOfCharacteristics)
Returns the partial multiplicative identity of the multi-field from the given product....
Definition: Multi_field_shared.h:416
friend void operator*=(Shared_multi_field_element &f1, Shared_multi_field_element const &f2)
operator*=
Definition: Multi_field_shared.h:154
static Shared_multi_field_element get_multiplicative_identity()
Returns the multiplicative identity of a field.
Definition: Multi_field_shared.h:412
friend void operator-=(Shared_multi_field_element &f1, Shared_multi_field_element const &f2)
operator-=
Definition: Multi_field_shared.h:115
Shared_multi_field_element & operator=(Shared_multi_field_element other)
Assign operator.
Definition: Multi_field_shared.h:372
friend bool operator!=(const element_type &v, const Shared_multi_field_element &f)
operator!=
Definition: Multi_field_shared.h:221
friend bool operator==(const Shared_multi_field_element &f1, const Shared_multi_field_element &f2)
operator==
Definition: Multi_field_shared.h:191
friend Shared_multi_field_element operator*(Shared_multi_field_element f1, Shared_multi_field_element const &f2)
operator*
Definition: Multi_field_shared.h:161
friend Shared_multi_field_element operator+(Shared_multi_field_element f, element_type const v)
operator+
Definition: Multi_field_shared.h:99
std::pair< Shared_multi_field_element, characteristic_type > get_partial_inverse(const characteristic_type &productOfCharacteristics) const
Returns the inverse of the element with respect to a sub-product of the characteristics in the multi-...
Definition: Multi_field_shared.h:391
mpz_class element_type
Definition: Multi_field_shared.h:40
friend Shared_multi_field_element operator+(Shared_multi_field_element f1, Shared_multi_field_element const &f2)
operator+
Definition: Multi_field_shared.h:85
friend bool operator!=(const Shared_multi_field_element &f, const element_type &v)
operator!=
Definition: Multi_field_shared.h:225
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_shared.h:326
Shared_multi_field_element get_inverse() const
Returns the inverse of the element in the multi-field, see .
Definition: Multi_field_shared.h:386
friend Shared_multi_field_element operator-(Shared_multi_field_element f, element_type const v)
operator-
Definition: Multi_field_shared.h:136
friend void operator-=(Shared_multi_field_element &f, element_type const v)
operator-=
Definition: Multi_field_shared.h:129
friend element_type operator*(element_type v, Shared_multi_field_element const &f)
operator*
Definition: Multi_field_shared.h:182
element_type characteristic_type
Definition: Multi_field_shared.h:41
Shared_multi_field_element()
Default constructor. Sets the element to 0.
Definition: Multi_field_shared.h:314
static Shared_multi_field_element get_additive_identity()
Returns the additive identity of a field.
Definition: Multi_field_shared.h:408
friend void operator+=(Shared_multi_field_element &f, element_type const v)
operator+=
Definition: Multi_field_shared.h:92
friend bool operator!=(const Shared_multi_field_element &f1, const Shared_multi_field_element &f2)
operator!=
Definition: Multi_field_shared.h:215
friend void operator*=(Shared_multi_field_element &f, element_type const v)
operator*=
Definition: Multi_field_shared.h:168
friend Shared_multi_field_element operator-(Shared_multi_field_element f1, Shared_multi_field_element const &f2)
operator-
Definition: Multi_field_shared.h:122
friend Shared_multi_field_element operator*(Shared_multi_field_element f, element_type const v)
operator*
Definition: Multi_field_shared.h:175
friend bool operator==(const Shared_multi_field_element &f, const element_type &v)
operator==
Definition: Multi_field_shared.h:206
friend void swap(Shared_multi_field_element &f1, Shared_multi_field_element &f2)
Swap operator.
Definition: Multi_field_shared.h:238
element_type get_value() const
Returns the value of the element.
Definition: Multi_field_shared.h:434
friend element_type operator+(element_type v, Shared_multi_field_element const &f)
operator+
Definition: Multi_field_shared.h:106
friend bool operator==(const element_type &v, const Shared_multi_field_element &f)
operator==
Definition: Multi_field_shared.h:197
static characteristic_type get_characteristic()
Returns the product of all characteristics.
Definition: Multi_field_shared.h:430
friend element_type operator-(element_type v, Shared_multi_field_element const &f)
operator-
Definition: Multi_field_shared.h:143
friend void operator+=(Shared_multi_field_element &f1, Shared_multi_field_element const &f2)
operator+=
Definition: Multi_field_shared.h:78
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14