Field_Zp.h
1 /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2  * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3  * Author(s): ClĂ©ment Maria
4  *
5  * Copyright (C) 2014 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
12 #define PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
13 
14 #include <utility>
15 #include <vector>
16 #include <stdexcept>
17 
18 namespace Gudhi {
19 
20 namespace persistent_cohomology {
21 
27 class Field_Zp {
28  public:
29  typedef int Element;
30 
31  Field_Zp()
32  : Prime(0),
33  inverse_() {
34  }
35 
36  void init(int charac) {
37  Prime = charac;
38 
39  // 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
40  if(Prime > 46337)
41  throw std::invalid_argument("Maximum homology_coeff_field allowed value is 46337");
42 
43  // Check for primality
44  if (Prime <= 1)
45  throw std::invalid_argument("homology_coeff_field must be a prime number");
46 
47  inverse_.clear();
48  inverse_.reserve(charac);
49  inverse_.push_back(0);
50  for (int i = 1; i < Prime; ++i) {
51  int inv = 1;
52  int mult = inv * i;
53  while ( (mult % Prime) != 1) {
54  ++inv;
55  if(mult == Prime)
56  throw std::invalid_argument("homology_coeff_field must be a prime number");
57  mult = inv * i;
58  }
59  inverse_.push_back(inv);
60  }
61  }
62 
64  Element plus_times_equal(const Element& x, const Element& y, const Element& w) {
65  assert(Prime > 0); // division by zero + non negative values
66  Element result = (x + w * y) % Prime;
67  if (result < 0)
68  result += Prime;
69  return result;
70  }
71 
72 // operator= defined on Element
73 
75  Element times(const Element& y, const Element& w) {
76  return plus_times_equal(0, y, (Element)w);
77  }
78 
79  Element plus_equal(const Element& x, const Element& y) {
80  return plus_times_equal(x, y, (Element)1);
81  }
82 
84  Element additive_identity() const {
85  return 0;
86  }
88  Element multiplicative_identity(Element = 0) const {
89  return 1;
90  }
92  std::pair<Element, Element> inverse(Element x, Element P) {
93  return std::pair<Element, Element>(inverse_[x], P);
94  } // <------ return the product of field characteristic for which x is invertible
95 
97  Element times_minus(Element x, Element y) {
98  assert(Prime > 0); // division by zero + non negative values
99  Element out = (-x * y) % Prime;
100  return (out < 0) ? out + Prime : out;
101  }
102 
104  int characteristic() const {
105  return Prime;
106  }
107 
108  private:
109  int Prime;
111  std::vector<Element> inverse_;
112 };
113 
114 } // namespace persistent_cohomology
115 
116 } // namespace Gudhi
117 
118 #endif // PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
Structure representing the coefficient field .
Definition: Field_Zp.h:27
Element times_minus(Element x, Element y)
Definition: Field_Zp.h:97
Element plus_times_equal(const Element &x, const Element &y, const Element &w)
Definition: Field_Zp.h:64
std::pair< Element, Element > inverse(Element x, Element P)
Definition: Field_Zp.h:92
Element additive_identity() const
Returns the additive idendity of the field.
Definition: Field_Zp.h:84
Element multiplicative_identity(Element=0) const
Returns the multiplicative identity of the field.
Definition: Field_Zp.h:88
Element times(const Element &y, const Element &w)
Definition: Field_Zp.h:75
int characteristic() const
Returns the characteristic of the field.
Definition: Field_Zp.h:104
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14