Gudhi  1.1.0
 All Classes Functions Typedefs Friends Groups Pages
Field_Zp.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): Clément Maria
6  *
7  * Copyright (C) 2014 INRIA Sophia Antipolis-Méditerranée (France)
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef SRC_PERSISTENT_COHOMOLOGY_INCLUDE_GUDHI_PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
24 #define SRC_PERSISTENT_COHOMOLOGY_INCLUDE_GUDHI_PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
25 
26 #include <utility>
27 #include <vector>
28 
29 namespace Gudhi {
30 
31 namespace persistent_cohomology {
32 
38 class Field_Zp {
39  public:
40  typedef int Element;
41 
42  Field_Zp()
43  : Prime(0),
44  inverse_(),
45  mult_id_all(1),
46  add_id_all(0) {
47  }
48 
49  void init(uint16_t charac) {
50  assert(charac != 0); // division by zero
51  Prime = charac;
52  inverse_.clear();
53  inverse_.reserve(charac);
54  inverse_.push_back(0);
55  for (int i = 1; i < Prime; ++i) {
56  int inv = 1;
57  while (((inv * i) % Prime) != 1)
58  ++inv;
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
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  void clear_coefficient(Element x) {
80  }
81 
82  Element plus_equal(const Element& x, const Element& y) {
83  return plus_times_equal(x, y, (Element)1);
84  }
85 
87  const Element& additive_identity() const {
88  return add_id_all;
89  }
91  const Element& multiplicative_identity(Element P = 0) const {
92  return mult_id_all;
93  }
95  std::pair<Element, Element> inverse(Element x, Element P) {
96  return std::pair<Element, Element>(inverse_[x], P);
97  } // <------ return the product of field characteristic for which x is invertible
98 
100  Element times_minus(Element x, Element y) {
101  assert(Prime != 0); // division by zero
102  Element out = (-x * y) % Prime;
103  return (out < 0) ? out + Prime : out;
104  }
105 
107  const uint16_t& characteristic() const {
108  return Prime;
109  }
110 
111  private:
112  uint16_t Prime;
114  std::vector<Element> inverse_;
115  const Element mult_id_all;
116  const Element add_id_all;
117 };
118 
119 } // namespace persistent_cohomology
120 
121 } // namespace Gudhi
122 
123 #endif // SRC_PERSISTENT_COHOMOLOGY_INCLUDE_GUDHI_PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
std::pair< Element, Element > inverse(Element x, Element P)
Definition: Field_Zp.h:95
Element times(const Element &y, const Element &w)
Definition: Field_Zp.h:75
Structure representing the coefficient field .
Definition: Field_Zp.h:38
const Element & additive_identity() const
Returns the additive idendity of the field.
Definition: Field_Zp.h:87
Element times_minus(Element x, Element y)
Definition: Field_Zp.h:100
const Element & multiplicative_identity(Element P=0) const
Returns the multiplicative identity of the field.
Definition: Field_Zp.h:91
const uint16_t & characteristic() const
Returns the characteristic of the field.
Definition: Field_Zp.h:107
Element plus_times_equal(const Element &x, const Element &y, const Element &w)
Definition: Field_Zp.h:64