[mathicgb] 37/393: F4Reducer now successfully computes the same result as the fallback reducer for all reductions of classic S-pairs that I've tried.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:58:29 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch upstream
in repository mathicgb.

commit 577509af79b418cde4e4c6798013125a7496b864
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Fri Sep 28 15:10:12 2012 +0200

    F4Reducer now successfully computes the same result as the fallback reducer for all reductions of classic S-pairs that I've tried.
---
 src/mathicgb/F4MatrixBuilder.cpp |  6 ++++--
 src/mathicgb/F4Reducer.cpp       | 10 ++++++++++
 src/mathicgb/Poly.cpp            | 10 ++++++++++
 src/mathicgb/Poly.hpp            | 34 ++++++++++++++++++++++++----------
 src/mathicgb/SparseMatrix.cpp    | 14 ++++++++++++++
 src/mathicgb/SparseMatrix.hpp    |  5 +++++
 src/test/SparseMatrix.cpp        | 39 +++++++++++++++++++++++++++++++++++++++
 7 files changed, 106 insertions(+), 12 deletions(-)

diff --git a/src/mathicgb/F4MatrixBuilder.cpp b/src/mathicgb/F4MatrixBuilder.cpp
index 2605e99..23bc588 100755
--- a/src/mathicgb/F4MatrixBuilder.cpp
+++ b/src/mathicgb/F4MatrixBuilder.cpp
@@ -14,10 +14,10 @@ void F4MatrixBuilder::addTwoRowsForSPairToMatrix(const Poly& polyA, const Poly&
 
   monomial multiple = ring().allocMonomial();
 
-  ring().monomialDivide(polyA.getLeadMonomial(), lcm, multiple);
+  ring().monomialDivide(lcm, polyA.getLeadMonomial(), multiple);
   addRowToMatrix(multiple, polyA);
 
-  ring().monomialDivide(polyB.getLeadMonomial(), lcm, multiple);
+  ring().monomialDivide(lcm, polyB.getLeadMonomial(), multiple);
   addRowToMatrix(multiple, polyB);
 
   ring().freeMonomial(lcm);
@@ -67,6 +67,8 @@ void F4MatrixBuilder::buildMatrixAndClear(QuadMatrix& matrix) {
       appendRowBottom(task.multiple, *task.poly);
   }
 
+  mBuilder.sortColumnsLeft(mBasis.order());
+  mBuilder.sortColumnsRight(mBasis.order());
   mBuilder.buildMatrixAndClear(matrix);
 }
 
diff --git a/src/mathicgb/F4Reducer.cpp b/src/mathicgb/F4Reducer.cpp
index 19e5188..3d79ff0 100755
--- a/src/mathicgb/F4Reducer.cpp
+++ b/src/mathicgb/F4Reducer.cpp
@@ -40,12 +40,22 @@ std::unique_ptr<Poly> F4Reducer::classicReduceSPoly
     F4MatrixBuilder builder(basis);
     builder.addTwoRowsForSPairToMatrix(a, b);
     builder.buildMatrixAndClear(qm);
+
+    // there has to be something to reduce
+    MATHICGB_ASSERT(qm.bottomLeft.rowCount() > 0);
   }
 
   SparseMatrix reduced;
   {
     F4MatrixReducer red;
     red.reduce(basis.ring(), qm, reduced);
+    if (reduced.rowCount() > 0) {
+      MATHICGB_ASSERT(reduced.rowCount() == 1);
+      Poly q(&basis.ring());
+      reduced.rowToPolynomial(0, qm.rightColumnMonomials, q);
+      MATHICGB_ASSERT(q == *p);
+    } else
+      MATHICGB_ASSERT(p->isZero());
   }
 
   return p;
diff --git a/src/mathicgb/Poly.cpp b/src/mathicgb/Poly.cpp
old mode 100644
new mode 100755
index 7a6c42e..79e0a8f
--- a/src/mathicgb/Poly.cpp
+++ b/src/mathicgb/Poly.cpp
@@ -272,12 +272,22 @@ size_t Poly::getMemoryUse() const
   return total;
 }
 
+void Poly::setToZero() {
+  MATHICGB_ASSERT(R != 0);
+  coeffs.clear();
+  monoms.clear();
+}
+
 void Poly::see(bool print_comp) const
 {
   display(std::cout, print_comp);
   std::cout << std::endl;
 }
 
+std::ostream& operator<<(std::ostream& out, const Poly& p) {
+  p.see(false);
+}
+
 // Local Variables:
 // compile-command: "make -C .. "
 // indent-tabs-mode: nil
diff --git a/src/mathicgb/Poly.hpp b/src/mathicgb/Poly.hpp
old mode 100644
new mode 100755
index f8f4292..674f13d
--- a/src/mathicgb/Poly.hpp
+++ b/src/mathicgb/Poly.hpp
@@ -3,16 +3,14 @@
 #ifndef _poly_h_
 #define _poly_h_
 
-#include <vector>
 #include "PolyRing.hpp"
+#include <vector>
+#include <ostream>
+#include <utility>
 
 class Poly {
-  const PolyRing *R;
-  std::vector<coefficient> coeffs;
-  std::vector<int> monoms;
 public:
-  Poly(const PolyRing *R0) : R(R0) {};
-  ~Poly() {} // nothing needs to be done
+  Poly(const PolyRing *R0) : R(R0) {MATHICGB_ASSERT(R != 0);}
 
   void parse(std::istream &i); // reads into this.
   void display(std::ostream &o, bool print_comp=true) const;
@@ -30,11 +28,14 @@ public:
   public:
     iterator() {}
     iterator operator++() { ++ic; im += monsize; return *this; }
-    coefficient &getCoefficient() { return *ic; }
-    monomial getMonomial() { return &*im; }
+    coefficient &getCoefficient() const { return *ic; }
+    monomial getMonomial() const { return &*im; }
     size_t operator-(const iterator &b) const { return ic - b.ic; }
     friend bool operator==(const iterator &a, const iterator &b);
     friend bool operator!=(const iterator &a, const iterator &b);
+    std::pair<coefficient&, monomial> operator*() const {
+      return std::pair<coefficient&, monomial>(getCoefficient(), getMonomial());
+    }
   };
 
   class const_iterator {
@@ -49,11 +50,15 @@ public:
   public:
     const_iterator() {}
     const_iterator operator++() { ++ic; im += monsize; return *this; }
-    coefficient getCoefficient() { return *ic; }
-    const_monomial getMonomial() { return &*im; }
+    coefficient getCoefficient() const { return *ic; }
+    const_monomial getMonomial() const { return &*im; }
     size_t operator-(const const_iterator &b) const { return ic - b.ic; }
     friend bool operator==(const const_iterator &a, const const_iterator &b);
     friend bool operator!=(const const_iterator &a, const const_iterator &b);
+    std::pair<coefficient, const_monomial> operator*() const {
+      return std::pair<coefficient, const_monomial>
+        (getCoefficient(), getMonomial());
+    }
   };
 
   void append(iterator &first, iterator &last);
@@ -94,6 +99,8 @@ public:
 
   size_t getMemoryUse() const;
 
+  void setToZero();
+
   void copy(Poly &result) const;
   friend bool operator==(const Poly &a, const Poly &b);
 
@@ -104,8 +111,15 @@ public:
   const PolyRing& ring() const {return *R;}
 
   void dump() const; // used for debugging
+
+private:
+  const PolyRing *R;
+  std::vector<coefficient> coeffs;
+  std::vector<int> monoms;
 };
 
+std::ostream& operator<<(std::ostream& out, const Poly& p);
+
 inline bool operator==(const Poly::iterator &a, const Poly::iterator &b)
 {
   return a.ic == b.ic;
diff --git a/src/mathicgb/SparseMatrix.cpp b/src/mathicgb/SparseMatrix.cpp
index 451019a..e08c54f 100755
--- a/src/mathicgb/SparseMatrix.cpp
+++ b/src/mathicgb/SparseMatrix.cpp
@@ -1,7 +1,21 @@
 #include "stdinc.h"
 #include "SparseMatrix.hpp"
 
+#include "Poly.hpp"
+
 std::ostream& operator<<(std::ostream& out, const SparseMatrix& matrix) {
   matrix.print(out);
   return out;
 }
+
+void SparseMatrix::rowToPolynomial
+(RowIndex row, std::vector<monomial> colMonomials, Poly& poly) {
+  MATHICGB_ASSERT(colMonomials.size() == colCount());
+  poly.setToZero();
+  auto end = rowEnd(row);
+  for (auto it = rowBegin(row); it != end; ++it) {
+    MATHICGB_ASSERT(it.index() < colMonomials.size());
+    if (it.scalar() != 0)
+      poly.appendTerm(it.scalar(), colMonomials[it.index()]);
+  }
+}
diff --git a/src/mathicgb/SparseMatrix.hpp b/src/mathicgb/SparseMatrix.hpp
index 8ab3b83..b3712f4 100755
--- a/src/mathicgb/SparseMatrix.hpp
+++ b/src/mathicgb/SparseMatrix.hpp
@@ -7,6 +7,7 @@
 #include <limits>
 #include <sstream>
 #include <string>
+class Poly;
 
 /** A class that implements a sparse matrix.
 
@@ -349,6 +350,10 @@ class SparseMatrix {
   AllColIndicesIterator allColIndicesBegin() {return mColIndices.begin();}
   AllColIndicesIterator allColIndicesEnd() {return mColIndices.end();}
 
+  /// Let poly be the dot product of colMonomials and the given row.
+  void rowToPolynomial
+  (RowIndex row, std::vector<monomial> colMonomials, Poly& poly);
+
 
 private:
   friend class RowIterator;
diff --git a/src/test/SparseMatrix.cpp b/src/test/SparseMatrix.cpp
index dc68c62..b868137 100755
--- a/src/test/SparseMatrix.cpp
+++ b/src/test/SparseMatrix.cpp
@@ -1,7 +1,20 @@
 #include "mathicgb/stdinc.h"
 
 #include "mathicgb/SparseMatrix.hpp"
+#include "mathicgb/Poly.hpp"
+#include "mathicgb/PolyRing.hpp"
+#include "mathicgb/io-util.hpp"
 #include <gtest/gtest.h>
+#include <memory>
+
+namespace {
+  std::unique_ptr<Poly> parsePoly(const PolyRing& ring, std::string str) {
+    auto p = make_unique<Poly>(&ring);
+    std::istringstream in(str);
+    p->parse(in);
+    return p;
+  }
+}
 
 TEST(SparseMatrix, NoRows) {
   SparseMatrix mat; // test a matrix with no rows
@@ -44,3 +57,29 @@ TEST(SparseMatrix, Simple) {
   ASSERT_EQ("0: 5#101\n1:\n2: 5#102 2000#0\n", mat.toString()); 
   ASSERT_FALSE(mat.emptyRow(2));
 }
+
+TEST(SparseMatrix, toRow) {
+  auto ring = ringFromString("32003 6 1\n1 1 1 1 1 1");
+  auto polyForMonomials = parsePoly(*ring, "a5+a4+a3+a2+a1+a0");
+  std::vector<monomial> monomials;
+  for (auto it = polyForMonomials->begin(); it != polyForMonomials->end(); ++it)
+    monomials.push_back(it.getMonomial());
+
+  SparseMatrix mat;
+  mat.clear(6);
+  mat.rowDone();
+  mat.appendEntry(0,10);
+  mat.rowDone();
+  mat.appendEntry(2,20);
+  mat.appendEntry(3,0);
+  mat.appendEntry(4,40);
+  mat.rowDone();
+
+  Poly p(ring.get());
+  mat.rowToPolynomial(0, monomials, p);
+  ASSERT_EQ(*parsePoly(*ring, "0"), p);
+  mat.rowToPolynomial(1, monomials, p);
+  ASSERT_EQ(*parsePoly(*ring, "10a5"), p);
+  mat.rowToPolynomial(2, monomials, p);
+  ASSERT_EQ(*parsePoly(*ring, "20a3+40a1"), p);
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mathicgb.git



More information about the debian-science-commits mailing list