summaryrefslogtreecommitdiff
path: root/src/Persistent_cohomology/include/gudhi/Persistent_cohomology/Field_Zp.h
blob: 0673625ce22583f9df4695e6af2da6378cd8fdc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*    This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
 *    See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
 *    Author(s):       Clément Maria
 *
 *    Copyright (C) 2014 Inria
 *
 *    Modification(s):
 *      - YYYY/MM Author: Description of the modification
 */

#ifndef PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
#define PERSISTENT_COHOMOLOGY_FIELD_ZP_H_

#include <utility>
#include <vector>

namespace Gudhi {

namespace persistent_cohomology {

/** \brief Structure representing the coefficient field \f$\mathbb{Z}/p\mathbb{Z}\f$
 *
 * \implements CoefficientField
 * \ingroup persistent_cohomology
 */
class Field_Zp {
 public:
  typedef int Element;

  Field_Zp()
      : Prime(0),
        inverse_() {
  }

  void init(int charac) {
    assert(charac > 0);  // division by zero + non negative values
    Prime = charac;
    inverse_.clear();
    inverse_.reserve(charac);
    inverse_.push_back(0);
    for (int i = 1; i < Prime; ++i) {
      int inv = 1;
      while (((inv * i) % Prime) != 1)
        ++inv;
      inverse_.push_back(inv);
    }
  }

  /** Set x <- x + w * y*/
  Element plus_times_equal(const Element& x, const Element& y, const Element& w) {
    assert(Prime > 0);  // division by zero + non negative values
    Element result = (x + w * y) % Prime;
    if (result < 0)
      result += Prime;
    return result;
  }

// operator= defined on Element

  /** Returns y * w */
  Element times(const Element& y, const Element& w) {
    return plus_times_equal(0, y, (Element)w);
  }

  Element plus_equal(const Element& x, const Element& y) {
    return plus_times_equal(x, y, (Element)1);
  }

  /** \brief Returns the additive idendity \f$0_{\Bbbk}\f$ of the field.*/
  Element additive_identity() const {
    return 0;
  }
  /** \brief Returns the multiplicative identity \f$1_{\Bbbk}\f$ of the field.*/
  Element multiplicative_identity(Element = 0) const {
    return 1;
  }
  /** Returns the inverse in the field. Modifies P. ??? */
  std::pair<Element, Element> inverse(Element x, Element P) {
    return std::pair<Element, Element>(inverse_[x], P);
  }  // <------ return the product of field characteristic for which x is invertible

  /** Returns -x * y.*/
  Element times_minus(Element x, Element y) {
    assert(Prime > 0);  // division by zero + non negative values
    Element out = (-x * y) % Prime;
    return (out < 0) ? out + Prime : out;
  }

  /** \brief Returns the characteristic \f$p\f$ of the field.*/
  int characteristic() const {
    return Prime;
  }

 private:
  int Prime;
  /** Property map Element -> Element, which associate to an element its inverse in the field.*/
  std::vector<Element> inverse_;
};

}  // namespace persistent_cohomology

}  // namespace Gudhi

#endif  // PERSISTENT_COHOMOLOGY_FIELD_ZP_H_