Gudhi  1.2.0
 All Classes Functions Variables 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 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  mpz_mul_ui(prod_characteristics_.get_mpz_t(),
85  prod_characteristics_.get_mpz_t(), p);
86  }
87 
88  // Uvect_
89  Element Ui;
90  Element tmp_elem;
91  for (auto p : primes_) {
92  assert(p > 0); // division by zero + non negative values
93  tmp_elem = prod_characteristics_ / p;
94  // Element tmp_elem_bis = 10;
95  mpz_powm_ui(tmp_elem.get_mpz_t(), tmp_elem.get_mpz_t(), p - 1,
96  prod_characteristics_.get_mpz_t());
97  Uvect_.push_back(tmp_elem);
98  }
99  mult_id_all = 0;
100  for (auto uvect : Uvect_) {
101  assert(prod_characteristics_ > 0); // division by zero + non negative values
102  mult_id_all = (mult_id_all + uvect) % prod_characteristics_;
103  }
104  }
105 
107  const Element& additive_identity() const {
108  return add_id_all;
109  }
111  const Element& multiplicative_identity() const {
112  return mult_id_all;
113  } // 1 everywhere
114 
115  Element multiplicative_identity(Element Q) {
116  if (Q == prod_characteristics_) {
117  return multiplicative_identity();
118  }
119 
120  assert(prod_characteristics_ > 0); // division by zero + non negative values
121  Element mult_id = 0;
122  for (unsigned int idx = 0; idx < primes_.size(); ++idx) {
123  assert(primes_[idx] > 0); // division by zero + non negative values
124  if ((Q % primes_[idx]) == 0) {
125  mult_id = (mult_id + Uvect_[idx]) % prod_characteristics_;
126  }
127  }
128  return mult_id;
129  }
130 
132  Element times(const Element& y, const Element& w) {
133  return plus_times_equal(0, y, w);
134  }
135 
136  Element plus_equal(const Element& x, const Element& y) {
137  return plus_times_equal(x, y, (Element)1);
138  }
139 
141  const Element& characteristic() const {
142  return prod_characteristics_;
143  }
144 
146  std::pair<Element, Element> inverse(Element x, Element QS) {
147  Element QR;
148  mpz_gcd(QR.get_mpz_t(), x.get_mpz_t(), QS.get_mpz_t()); // QR <- gcd(x,QS)
149  if (QR == QS)
150  return std::pair<Element, Element>(additive_identity(), multiplicative_identity()); // partial inverse is 0
151  Element QT = QS / QR;
152  Element inv_qt;
153  mpz_invert(inv_qt.get_mpz_t(), x.get_mpz_t(), QT.get_mpz_t());
154 
155  assert(prod_characteristics_ > 0); // division by zero + non negative values
156  return { (inv_qt * multiplicative_identity(QT)) % prod_characteristics_, QT };
157  }
159  Element times_minus(const Element& x, const Element& y) {
160  assert(prod_characteristics_ > 0); // division by zero + non negative values
161  /* This assumes that (x*y)%pc cannot be zero, but Field_Zp has specific code for the 0 case ??? */
162  return prod_characteristics_ - ((x * y) % prod_characteristics_);
163  }
164 
166  Element plus_times_equal(const Element& x, const Element& y, const Element& w) {
167  assert(prod_characteristics_ > 0); // division by zero + non negative values
168  Element result = (x + w * y) % prod_characteristics_;
169  if (result < 0)
170  result += prod_characteristics_;
171  return result;
172  }
173 
174  Element prod_characteristics_; // product of characteristics of the fields
175  // represented by the multi-field class
176  std::vector<int> primes_; // all the characteristics of the fields
177  std::vector<Element> Uvect_;
178  Element mult_id_all;
179  const Element add_id_all;
180 };
181 
182 } // namespace persistent_cohomology
183 
184 } // namespace Gudhi
185 
186 #endif // PERSISTENT_COHOMOLOGY_MULTI_FIELD_H_
const Element & multiplicative_identity() const
Returns the multiplicative identity of the field.
Definition: Multi_field.h:111
const Element & characteristic() const
Returns the characteristic of the field.
Definition: Multi_field.h:141
std::pair< Element, Element > inverse(Element x, Element QS)
Definition: Multi_field.h:146
Element times(const Element &y, const Element &w)
Definition: Multi_field.h:132
Element times_minus(const Element &x, const Element &y)
Definition: Multi_field.h:159
const Element & additive_identity() const
Returns the additive idendity of the field.
Definition: Multi_field.h:107
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:166