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 
17 namespace Gudhi {
18 
19 namespace persistent_cohomology {
20 
26 class Field_Zp {
27  public:
28  typedef int Element;
29 
30  Field_Zp()
31  : Prime(0),
32  inverse_() {
33  }
34 
35  void init(int charac) {
36  assert(charac > 0); // division by zero + non negative values
37  Prime = charac;
38  inverse_.clear();
39  inverse_.reserve(charac);
40  inverse_.push_back(0);
41  for (int i = 1; i < Prime; ++i) {
42  int inv = 1;
43  while (((inv * i) % Prime) != 1)
44  ++inv;
45  inverse_.push_back(inv);
46  }
47  }
48 
50  Element plus_times_equal(const Element& x, const Element& y, const Element& w) {
51  assert(Prime > 0); // division by zero + non negative values
52  Element result = (x + w * y) % Prime;
53  if (result < 0)
54  result += Prime;
55  return result;
56  }
57 
58 // operator= defined on Element
59 
61  Element times(const Element& y, const Element& w) {
62  return plus_times_equal(0, y, (Element)w);
63  }
64 
65  Element plus_equal(const Element& x, const Element& y) {
66  return plus_times_equal(x, y, (Element)1);
67  }
68 
70  Element additive_identity() const {
71  return 0;
72  }
74  Element multiplicative_identity(Element = 0) const {
75  return 1;
76  }
78  std::pair<Element, Element> inverse(Element x, Element P) {
79  return std::pair<Element, Element>(inverse_[x], P);
80  } // <------ return the product of field characteristic for which x is invertible
81 
83  Element times_minus(Element x, Element y) {
84  assert(Prime > 0); // division by zero + non negative values
85  Element out = (-x * y) % Prime;
86  return (out < 0) ? out + Prime : out;
87  }
88 
90  int characteristic() const {
91  return Prime;
92  }
93 
94  private:
95  int Prime;
97  std::vector<Element> inverse_;
98 };
99 
100 } // namespace persistent_cohomology
101 
102 } // namespace Gudhi
103 
104 #endif // PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
int characteristic() const
Returns the characteristic of the field.
Definition: Field_Zp.h:90
Element additive_identity() const
Returns the additive idendity of the field.
Definition: Field_Zp.h:70
std::pair< Element, Element > inverse(Element x, Element P)
Definition: Field_Zp.h:78
Element times(const Element &y, const Element &w)
Definition: Field_Zp.h:61
Definition: SimplicialComplexForAlpha.h:14
Structure representing the coefficient field .
Definition: Field_Zp.h:26
Element times_minus(Element x, Element y)
Definition: Field_Zp.h:83
Element multiplicative_identity(Element=0) const
Returns the multiplicative identity of the field.
Definition: Field_Zp.h:74
Element plus_times_equal(const Element &x, const Element &y, const Element &w)
Definition: Field_Zp.h:50
GUDHI  Version 3.1.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Mon Jan 20 2020 14:12:57 for GUDHI by Doxygen 1.8.13