Gudhi  1.1.0
 All Classes Functions Typedefs Friends Groups Pages
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 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_MULTI_FIELD_H_
24 #define SRC_PERSISTENT_COHOMOLOGY_INCLUDE_GUDHI_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(uint16_t min_prime, uint16_t 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  uint16_t 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  // set m to primorial(bound_prime)
81  prod_characteristics_ = 1;
82  for (auto p : primes_) {
83  mpz_mul_ui(prod_characteristics_.get_mpz_t(),
84  prod_characteristics_.get_mpz_t(), p);
85  }
86 
87  // Uvect_
88  Element Ui;
89  Element tmp_elem;
90  for (auto p : primes_) {
91  assert(p != 0); // division by zero
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
101  mult_id_all = (mult_id_all + uvect) % prod_characteristics_;
102  }
103  }
104 
105  void clear_coefficient(Element & x) {
106  mpz_clear(x.get_mpz_t());
107  }
108 
110  const Element& additive_identity() const {
111  return add_id_all;
112  }
114  const Element& multiplicative_identity() const {
115  return mult_id_all;
116  } // 1 everywhere
117 
118  Element multiplicative_identity(Element Q) {
119  if (Q == prod_characteristics_) {
120  return multiplicative_identity();
121  }
122 
123  assert(prod_characteristics_ != 0); // division by zero
124  Element mult_id = 0;
125  for (unsigned int idx = 0; idx < primes_.size(); ++idx) {
126  assert(primes_[idx] != 0); // division by zero
127  if ((Q % primes_[idx]) == 0) {
128  mult_id = (mult_id + Uvect_[idx]) % prod_characteristics_;
129  }
130  }
131  return mult_id;
132  }
133 
135  Element times(const Element& y, const Element& w) {
136  return plus_times_equal(0, y, w);
137  }
138 
139  Element plus_equal(const Element& x, const Element& y) {
140  return plus_times_equal(x, y, (Element)1);
141  }
142 
144  const Element& characteristic() const {
145  return prod_characteristics_;
146  }
147 
149  std::pair<Element, Element> inverse(Element x, Element QS) {
150  Element QR;
151  mpz_gcd(QR.get_mpz_t(), x.get_mpz_t(), QS.get_mpz_t()); // QR <- gcd(x,QS)
152  if (QR == QS)
153  return std::pair<Element, Element>(additive_identity(), multiplicative_identity()); // partial inverse is 0
154  Element QT = QS / QR;
155  Element inv_qt;
156  mpz_invert(inv_qt.get_mpz_t(), x.get_mpz_t(), QT.get_mpz_t());
157 
158  assert(prod_characteristics_ != 0); // division by zero
159  return std::pair<Element, Element>(
160  (inv_qt * multiplicative_identity(QT)) % prod_characteristics_, QT);
161  }
163  Element times_minus(const Element& x, const Element& y) {
164  assert(prod_characteristics_ != 0); // division by zero
165  return prod_characteristics_ - ((x * y) % prod_characteristics_);
166  }
167 
169  Element plus_times_equal(const Element& x, const Element& y, const Element& w) {
170  assert(prod_characteristics_ != 0); // division by zero
171  Element result = (x + w * y) % prod_characteristics_;
172  if (result < 0)
173  result += prod_characteristics_;
174  return result;
175  }
176 
177  Element prod_characteristics_; // product of characteristics of the fields
178  // represented by the multi-field class
179  std::vector<uint16_t> primes_; // all the characteristics of the fields
180  std::vector<Element> Uvect_;
181  Element mult_id_all;
182  const Element add_id_all;
183 };
184 
185 } // namespace persistent_cohomology
186 
187 } // namespace Gudhi
188 
189 #endif // SRC_PERSISTENT_COHOMOLOGY_INCLUDE_GUDHI_PERSISTENT_COHOMOLOGY_MULTI_FIELD_H_
const Element & multiplicative_identity() const
Returns the multiplicative identity of the field.
Definition: Multi_field.h:114
const Element & characteristic() const
Returns the characteristic of the field.
Definition: Multi_field.h:144
std::pair< Element, Element > inverse(Element x, Element QS)
Definition: Multi_field.h:149
Element times(const Element &y, const Element &w)
Definition: Multi_field.h:135
Element times_minus(const Element &x, const Element &y)
Definition: Multi_field.h:163
const Element & additive_identity() const
Returns the additive idendity of the field.
Definition: Multi_field.h:110
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:169