Multi_field.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_MULTI_FIELD_H_
24 #define PERSISTENT_COHOMOLOGY_MULTI_FIELD_H_
25 
26 #include <gmpxx.h>
27 
28 #include <vector>
29 #include <utility>
30 
31 namespace Gudhi {
32 
33 namespace persistent_cohomology {
34 
43 class Multi_field {
44  public:
45  typedef mpz_class Element;
46 
47  Multi_field()
48  : prod_characteristics_(0),
49  mult_id_all(0),
50  add_id_all(0) {
51  }
52 
53  /* Initialize the multi-field. The generation of prime numbers might fail with
54  * a very small probability.*/
55  void init(int min_prime, int max_prime) {
56  if (max_prime < 2) {
57  std::cerr << "There is no prime less than " << max_prime << std::endl;
58  }
59  if (min_prime > max_prime) {
60  std::cerr << "No prime in [" << min_prime << ":" << max_prime << "]"
61  << std::endl;
62  }
63  // fill the list of prime numbers
64  int curr_prime = min_prime;
65  mpz_t tmp_prime;
66  mpz_init_set_ui(tmp_prime, min_prime);
67  // test if min_prime is prime
68  int is_prime = mpz_probab_prime_p(tmp_prime, 25); // probabilistic primality test
69 
70  if (is_prime == 0) { // min_prime is composite
71  mpz_nextprime(tmp_prime, tmp_prime);
72  curr_prime = mpz_get_ui(tmp_prime);
73  }
74 
75  while (curr_prime <= max_prime) {
76  primes_.push_back(curr_prime);
77  mpz_nextprime(tmp_prime, tmp_prime);
78  curr_prime = mpz_get_ui(tmp_prime);
79  }
80  mpz_clear(tmp_prime);
81  // set m to primorial(bound_prime)
82  prod_characteristics_ = 1;
83  for (auto p : primes_) {
84  prod_characteristics_ *= p;
85  }
86 
87  // Uvect_
88  Element Ui;
89  Element tmp_elem;
90  for (auto p : primes_) {
91  assert(p > 0); // division by zero + non negative values
92  tmp_elem = prod_characteristics_ / p;
93  // Element tmp_elem_bis = 10;
94  mpz_powm_ui(tmp_elem.get_mpz_t(), tmp_elem.get_mpz_t(), p - 1,
95  prod_characteristics_.get_mpz_t());
96  Uvect_.push_back(tmp_elem);
97  }
98  mult_id_all = 0;
99  for (auto uvect : Uvect_) {
100  assert(prod_characteristics_ > 0); // division by zero + non negative values
101  mult_id_all = (mult_id_all + uvect) % prod_characteristics_;
102  }
103  }
104 
106  const Element& additive_identity() const {
107  return add_id_all;
108  }
110  const Element& multiplicative_identity() const {
111  return mult_id_all;
112  } // 1 everywhere
113 
114  Element multiplicative_identity(Element Q) {
115  if (Q == prod_characteristics_) {
116  return multiplicative_identity();
117  }
118 
119  assert(prod_characteristics_ > 0); // division by zero + non negative values
120  Element mult_id = 0;
121  for (unsigned int idx = 0; idx < primes_.size(); ++idx) {
122  assert(primes_[idx] > 0); // division by zero + non negative values
123  if ((Q % primes_[idx]) == 0) {
124  mult_id = (mult_id + Uvect_[idx]) % prod_characteristics_;
125  }
126  }
127  return mult_id;
128  }
129 
131  Element times(const Element& y, const Element& w) {
132  return plus_times_equal(0, y, w);
133  }
134 
135  Element plus_equal(const Element& x, const Element& y) {
136  return plus_times_equal(x, y, (Element)1);
137  }
138 
140  const Element& characteristic() const {
141  return prod_characteristics_;
142  }
143 
145  std::pair<Element, Element> inverse(Element x, Element QS) {
146  Element QR;
147  mpz_gcd(QR.get_mpz_t(), x.get_mpz_t(), QS.get_mpz_t()); // QR <- gcd(x,QS)
148  if (QR == QS)
149  return std::pair<Element, Element>(additive_identity(), multiplicative_identity()); // partial inverse is 0
150  Element QT = QS / QR;
151  Element inv_qt;
152  mpz_invert(inv_qt.get_mpz_t(), x.get_mpz_t(), QT.get_mpz_t());
153 
154  assert(prod_characteristics_ > 0); // division by zero + non negative values
155  return { (inv_qt * multiplicative_identity(QT)) % prod_characteristics_, QT };
156  }
158  Element times_minus(const Element& x, const Element& y) {
159  assert(prod_characteristics_ > 0); // division by zero + non negative values
160  /* This assumes that (x*y)%pc cannot be zero, but Field_Zp has specific code for the 0 case ??? */
161  return prod_characteristics_ - ((x * y) % prod_characteristics_);
162  }
163 
165  Element plus_times_equal(const Element& x, const Element& y, const Element& w) {
166  assert(prod_characteristics_ > 0); // division by zero + non negative values
167  Element result = (x + w * y) % prod_characteristics_;
168  if (result < 0)
169  result += prod_characteristics_;
170  return result;
171  }
172 
173  Element prod_characteristics_; // product of characteristics of the fields
174  // represented by the multi-field class
175  std::vector<int> primes_; // all the characteristics of the fields
176  std::vector<Element> Uvect_;
177  Element mult_id_all;
178  const Element add_id_all;
179 };
180 
181 } // namespace persistent_cohomology
182 
183 } // namespace Gudhi
184 
185 #endif // PERSISTENT_COHOMOLOGY_MULTI_FIELD_H_
std::pair< Element, Element > inverse(Element x, Element QS)
Definition: Multi_field.h:145
const Element & multiplicative_identity() const
Returns the multiplicative identity of the field.
Definition: Multi_field.h:110
Element times(const Element &y, const Element &w)
Definition: Multi_field.h:131
Definition: SimplicialComplexForAlpha.h:26
Element times_minus(const Element &x, const Element &y)
Definition: Multi_field.h:158
Structure representing coefficients in a set of finite fields simultaneously using the chinese remain...
Definition: Multi_field.h:43
Element plus_times_equal(const Element &x, const Element &y, const Element &w)
Definition: Multi_field.h:165
const Element & additive_identity() const
Returns the additive idendity of the field.
Definition: Multi_field.h:106
const Element & characteristic() const
Returns the characteristic of the field.
Definition: Multi_field.h:140
GUDHI  Version 2.2.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Thu Jun 14 2018 15:00:54 for GUDHI by Doxygen 1.8.13