Loading...
Searching...
No Matches
Field_Zp.h
/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
* See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
* Author(s): Clément Maria
*
* Copyright (C) 2014 Inria
*
* Modification(s):
* - YYYY/MM Author: Description of the modification
*/
#ifndef PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
#define PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
#include <utility>
#include <vector>
#include <stdexcept>
namespace Gudhi {
namespace persistent_cohomology {
class Field_Zp {
public:
typedef int Element;
Field_Zp()
: Prime(0),
inverse_() {
}
void init(int charac) {
Prime = charac;
// Check that the provided prime is less than the maximum allowed as int, calculation below, and 'plus_times_equal' function : 46337 ; i.e (max_prime-1)*max_prime <= INT_MAX
if(Prime > 46337)
throw std::invalid_argument("Maximum homology_coeff_field allowed value is 46337");
// Check for primality
if (Prime <= 1)
throw std::invalid_argument("homology_coeff_field must be a prime number");
inverse_.clear();
inverse_.reserve(charac);
inverse_.push_back(0);
for (int i = 1; i < Prime; ++i) {
int inv = 1;
int mult = inv * i;
while ( (mult % Prime) != 1) {
++inv;
if(mult == Prime)
throw std::invalid_argument("homology_coeff_field must be a prime number");
mult = inv * i;
}
inverse_.push_back(inv);
}
}
Element plus_times_equal(const Element& x, const Element& y, const Element& w) {
assert(Prime > 0); // division by zero + non negative values
Element result = (x + w * y) % Prime;
if (result < 0)
result += Prime;
return result;
}
// operator= defined on Element
Element times(const Element& y, const Element& w) {
return plus_times_equal(0, y, (Element)w);
}
Element plus_equal(const Element& x, const Element& y) {
return plus_times_equal(x, y, (Element)1);
}
Element additive_identity() const {
return 0;
}
Element multiplicative_identity(Element = 0) const {
return 1;
}
std::pair<Element, Element> inverse(Element x, Element P) {
return std::pair<Element, Element>(inverse_[x], P);
} // <------ return the product of field characteristic for which x is invertible
Element times_minus(Element x, Element y) {
assert(Prime > 0); // division by zero + non negative values
Element out = (-x * y) % Prime;
return (out < 0) ? out + Prime : out;
}
int characteristic() const {
return Prime;
}
private:
int Prime;
std::vector<Element> inverse_;
};
} // namespace persistent_cohomology
} // namespace Gudhi
#endif // PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
Gudhi::persistent_cohomology::Field_Zp::times_minus
Element times_minus(Element x, Element y)
Definition Field_Zp.h:97
Gudhi::persistent_cohomology::Field_Zp::plus_times_equal
Element plus_times_equal(const Element &x, const Element &y, const Element &w)
Definition Field_Zp.h:64
Gudhi::persistent_cohomology::Field_Zp::additive_identity
Element additive_identity() const
Returns the additive idendity of the field.
Definition Field_Zp.h:84
Gudhi::persistent_cohomology::Field_Zp::inverse
std::pair< Element, Element > inverse(Element x, Element P)
Definition Field_Zp.h:92
Gudhi::persistent_cohomology::Field_Zp::multiplicative_identity
Element multiplicative_identity(Element=0) const
Returns the multiplicative identity of the field.
Definition Field_Zp.h:88
Gudhi::persistent_cohomology::Field_Zp::times
Element times(const Element &y, const Element &w)
Definition Field_Zp.h:75
Gudhi::persistent_cohomology::Field_Zp::characteristic
int characteristic() const
Returns the characteristic of the field.
Definition Field_Zp.h:104
Gudhi
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14
CoefficientField::Element
unspecified Element
Type of element of the field.
Definition CoefficientField.h:19