summaryrefslogtreecommitdiff
path: root/src/Persistent_cohomology/include/gudhi/Persistent_cohomology/Field_Zp.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/Persistent_cohomology/include/gudhi/Persistent_cohomology/Field_Zp.h')
-rw-r--r--src/Persistent_cohomology/include/gudhi/Persistent_cohomology/Field_Zp.h196
1 files changed, 106 insertions, 90 deletions
diff --git a/src/Persistent_cohomology/include/gudhi/Persistent_cohomology/Field_Zp.h b/src/Persistent_cohomology/include/gudhi/Persistent_cohomology/Field_Zp.h
index af0d6605..8b09a800 100644
--- a/src/Persistent_cohomology/include/gudhi/Persistent_cohomology/Field_Zp.h
+++ b/src/Persistent_cohomology/include/gudhi/Persistent_cohomology/Field_Zp.h
@@ -1,106 +1,122 @@
- /* This file is part of the Gudhi Library. The Gudhi library
- * (Geometric Understanding in Higher Dimensions) is a generic C++
- * library for computational topology.
- *
- * Author(s): Clément Maria
- *
- * Copyright (C) 2014 INRIA Sophia Antipolis-Méditerranée (France)
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef GUDHI_FIELD_ZP_H
-#define GUDHI_FIELD_ZP_H
-
-namespace Gudhi{
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Clément Maria
+ *
+ * Copyright (C) 2014 INRIA Sophia Antipolis-Méditerranée (France)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SRC_PERSISTENT_COHOMOLOGY_INCLUDE_GUDHI_PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
+#define SRC_PERSISTENT_COHOMOLOGY_INCLUDE_GUDHI_PERSISTENT_COHOMOLOGY_FIELD_ZP_H_
+
+#include <utility>
+#include <vector>
+
+namespace Gudhi {
/** \brief Structure representing the coefficient field \f$\mathbb{Z}/p\mathbb{Z}\f$
- *
- * \implements CoefficientField
- * \ingroup persistent_cohomology
- */
+ *
+ * \implements CoefficientField
+ * \ingroup persistent_cohomology
+ */
class Field_Zp {
-public:
-typedef int Element;
-
-Field_Zp()
-: Prime(-1)
-, inverse_() {}
-
-void init(int charac ) {
- assert(charac <= 32768);
- 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);
+ public:
+ typedef int Element;
+
+ Field_Zp()
+ : Prime(0),
+ inverse_(),
+ mult_id_all(1),
+ add_id_all(0) {
}
-}
-/** Set x <- x + w * y*/
-void plus_times_equal ( Element & x, Element y, Element w )
-{ x = (x + w * y) % Prime; }
-
-// operator= defined on Element
-
-/** Returns y * w */
-Element times ( Element y, int w ) {
- Element res = (y * w) % Prime;
- if(res < 0) return res+Prime;
- else return res;
-}
-
-void clear_coefficient(Element x) {}
+ void init(uint8_t charac) {
+ assert(charac != 0); // division by zero
+ 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);
+ }
+ }
-void plus_equal(Element & x, Element y) { x = ((x+y)%Prime); }
+ /** Set x <- x + w * y*/
+ void plus_times_equal(Element & x, Element y, Element w) {
+ assert(Prime != 0); // division by zero
+ x = (x + w * y) % Prime;
+ }
-/** \brief Returns the additive idendity \f$0_{\Bbbk}\f$ of the field.*/
-Element additive_identity () { return 0; }
-/** \brief Returns the multiplicative identity \f$1_{\Bbbk}\f$ of the field.*/
-Element multiplicative_identity ( Element P = 0) { 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
+// operator= defined on Element
-/** Returns -x * y.*/
-Element times_minus ( Element x, Element y )
-{
- Element out = (-x * y) % Prime;
- return (out < 0) ? out + Prime : out;
-}
+ /** Returns y * w */
+ Element times(Element y, int w) {
+ assert(Prime != 0); // division by zero
+ Element res = (y * w) % Prime;
+ if (res < 0)
+ return res + Prime;
+ else
+ return res;
+ }
+ void clear_coefficient(Element x) {
+ }
-bool is_one ( Element x ) { return x == 1; }
-bool is_zero ( Element x ) { return x == 0; }
+ void plus_equal(Element & x, Element y) {
+ assert(Prime != 0); // division by zero
+ x = ((x + y) % Prime);
+ }
-//bool is_null()
+ /** \brief Returns the additive idendity \f$0_{\Bbbk}\f$ of the field.*/
+ const Element& additive_identity() const {
+ return add_id_all;
+ }
+ /** \brief Returns the multiplicative identity \f$1_{\Bbbk}\f$ of the field.*/
+ const Element& multiplicative_identity(Element P = 0) const {
+ return mult_id_all;
+ }
+ /** 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
+ Element out = (-x * y) % Prime;
+ return (out < 0) ? out + Prime : out;
+ }
-/** \brief Returns the characteristic \f$p\f$ of the field.*/
-Element characteristic() { return Prime; }
+ /** \brief Returns the characteristic \f$p\f$ of the field.*/
+ const uint8_t& characteristic() const {
+ return Prime;
+ }
-private:
- Element Prime;
-/** Property map Element -> Element, which associate to an element its inverse in the field.*/
- std::vector< Element > inverse_;
+ private:
+ uint8_t Prime;
+ /** Property map Element -> Element, which associate to an element its inverse in the field.*/
+ std::vector<Element> inverse_;
+ const Element mult_id_all;
+ const Element add_id_all;
};
-} // namespace GUDHI
+} // namespace Gudhi
-#endif // GUDHI_FIELD_ZP_H
+#endif // SRC_PERSISTENT_COHOMOLOGY_INCLUDE_GUDHI_PERSISTENT_COHOMOLOGY_FIELD_ZP_H_