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
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 PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
24 #define 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  }
46 
47  void init(int charac) {
48  assert(charac > 0); // division by zero + non negative values
49  Prime = charac;
50  inverse_.clear();
51  inverse_.reserve(charac);
52  inverse_.push_back(0);
53  for (int i = 1; i < Prime; ++i) {
54  int inv = 1;
55  while (((inv * i) % Prime) != 1)
56  ++inv;
57  inverse_.push_back(inv);
58  }
59  }
60 
62  Element plus_times_equal(const Element& x, const Element& y, const Element& w) {
63  assert(Prime > 0); // division by zero + non negative values
64  Element result = (x + w * y) % Prime;
65  if (result < 0)
66  result += Prime;
67  return result;
68  }
69 
70 // operator= defined on Element
71 
73  Element times(const Element& y, const Element& w) {
74  return plus_times_equal(0, y, (Element)w);
75  }
76 
77  Element plus_equal(const Element& x, const Element& y) {
78  return plus_times_equal(x, y, (Element)1);
79  }
80 
82  Element additive_identity() const {
83  return 0;
84  }
86  Element multiplicative_identity(Element = 0) const {
87  return 1;
88  }
90  std::pair<Element, Element> inverse(Element x, Element P) {
91  return std::pair<Element, Element>(inverse_[x], P);
92  } // <------ return the product of field characteristic for which x is invertible
93 
95  Element times_minus(Element x, Element y) {
96  assert(Prime > 0); // division by zero + non negative values
97  Element out = (-x * y) % Prime;
98  return (out < 0) ? out + Prime : out;
99  }
100 
102  int characteristic() const {
103  return Prime;
104  }
105 
106  private:
107  int Prime;
109  std::vector<Element> inverse_;
110 };
111 
112 } // namespace persistent_cohomology
113 
114 } // namespace Gudhi
115 
116 #endif // PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
int characteristic() const
Returns the characteristic of the field.
Definition: Field_Zp.h:102
Element additive_identity() const
Returns the additive idendity of the field.
Definition: Field_Zp.h:82
std::pair< Element, Element > inverse(Element x, Element P)
Definition: Field_Zp.h:90
Element times(const Element &y, const Element &w)
Definition: Field_Zp.h:73
Definition: SimplicialComplexForAlpha.h:26
Structure representing the coefficient field .
Definition: Field_Zp.h:38
Element times_minus(Element x, Element y)
Definition: Field_Zp.h:95
Element multiplicative_identity(Element=0) const
Returns the multiplicative identity of the field.
Definition: Field_Zp.h:86
Element plus_times_equal(const Element &x, const Element &y, const Element &w)
Definition: Field_Zp.h:62
GUDHI  Version 2.3.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Tue Sep 4 2018 14:32:59 for GUDHI by Doxygen 1.8.13