[mathicgb] 373/393: Moved the implementation of SigPolyBasis to use MonoMonoid.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:59:36 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 28cdd9d61ca7ae3c6dd0f2cd5f89ee4dd690ccd5
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Tue Sep 17 19:01:58 2013 +0200

    Moved the implementation of SigPolyBasis to use MonoMonoid.
---
 src/mathicgb/SigPolyBasis.cpp | 109 ++++++++++++++++++++----------------------
 src/mathicgb/SigPolyBasis.hpp |  52 ++++++++++----------
 2 files changed, 79 insertions(+), 82 deletions(-)

diff --git a/src/mathicgb/SigPolyBasis.cpp b/src/mathicgb/SigPolyBasis.cpp
index 06b456a..af1b538 100755
--- a/src/mathicgb/SigPolyBasis.cpp
+++ b/src/mathicgb/SigPolyBasis.cpp
@@ -4,6 +4,7 @@
 #include "SigPolyBasis.hpp"
 
 #include "Poly.hpp"
+#include "MathicIO.hpp"
 #include <mathic.h>
 #include <limits>
 #include <iostream>
@@ -36,9 +37,9 @@ SigPolyBasis::~SigPolyBasis()
 
   for (size_t i = 0; i < mBasis.size(); i++) {
     if (!mSignatures[i].isNull())
-      ring().freeMonomial(mSignatures[i]);
+      monoid().freeRaw(*mSignatures[i]);
     if (!mSigLeadRatio[i].isNull())
-      ring().freeMonomial(mSigLeadRatio[i]);
+      monoid().freeRaw(*mSigLeadRatio[i]);
   }
   for (size_t i = 0; i < mSignatureLookup.size(); ++i)
     delete mSignatureLookup[i];
@@ -60,16 +61,16 @@ void SigPolyBasis::insert(Mono ownedSig, std::unique_ptr<Poly> f) {
   MATHICGB_ASSERT(!ownedSig.isNull());
   MATHICGB_ASSERT(monoid().fromPool(*ownedSig));
 
-  const size_t index = mSignatures.size();
-  mSignatures.push_back(Monoid::toOld(*ownedSig.release()));
-  auto sig = mSignatures.back();
+  const auto index = mSignatures.size();
+  mSignatures.push_back(ownedSig.release());
+  auto sig = *mSignatures.back();
   
-  const size_t component = ring().monomialGetComponent(sig);
+  const auto component = monoid().component(sig);
   MATHICGB_ASSERT(component < mSignatureLookup.size());
   mSignatureLookup[component]->insert(sig, index);
 
   auto ratio = ring().allocMonomial();
-  ring().monomialDivideToNegative(sig, f->getLeadMonomial(), ratio);
+  monoid().divideToNegative(f->getLeadMonomial(), sig, ratio);
 
   mSigLeadRatio.push_back(ratio);
 
@@ -84,33 +85,33 @@ void SigPolyBasis::insert(Mono ownedSig, std::unique_ptr<Poly> f) {
     mBasis.minimalLeadCount() == mMinimalMonoLookup->size());
   MATHICGB_ASSERT(mSignatures.size() == index + 1);
   MATHICGB_ASSERT(mBasis.size() == index + 1);
-  if (!mUseRatioRank || sig.isNull())
+  if (!mUseRatioRank)
     return;
 
   // compute rank of the ratio
-  RatioSortedType::iterator pos = mRatioSorted.insert(index);
+  auto pos = mRatioSorted.insert(index);
 again:
   Rank prevRank;
   if (pos == mRatioSorted.begin())
     prevRank = 0;
   else {
-    RatioSortedType::iterator prev = pos;
+    auto prev = pos;
     --prev;
     prevRank = mRatioRanks[*prev];
-    if (monoid().equal(ratio, mSigLeadRatio[*prev])) {
+    if (monoid().equal(ratio, *mSigLeadRatio[*prev])) {
       mRatioRanks.push_back(prevRank);
       return;
     }
   }
 
   Rank nextRank;
-  RatioSortedType::iterator next = pos;
+  auto next = pos;
   ++next;
   if (next == mRatioSorted.end())
     nextRank = std::numeric_limits<Rank>::max();
   else {
     nextRank = mRatioRanks[*next];
-    if (monoid().equal(ratio, mSigLeadRatio[*next])) {
+    if (monoid().equal(ratio, *mSigLeadRatio[*next])) {
       mRatioRanks.push_back(nextRank);
       return;
     }
@@ -132,8 +133,8 @@ again:
     MATHICGB_ASSERT(!mRatioSorted.empty());
     size_t rankSum = increment; // leave a gap at beginning
     Rank prevRank = *mRatioRanks.begin();
-    for (RatioSortedType::iterator it = mRatioSorted.begin();
-      it != mRatioSorted.end(); ++it) {
+    auto end = mRatioSorted.end();
+    for (auto it = mRatioSorted.begin(); it != end; ++it) {
       if (it == pos)
         continue;
       if (mRatioRanks[*it] != prevRank)
@@ -154,9 +155,9 @@ again:
     MATHICGB_ASSERT(mRatioRanks[*mRatioSorted.begin()] > 0);
     MATHICGB_ASSERT(mRatioRanks[*mRatioSorted.rbegin()] <
       std::numeric_limits<Rank>::max());
-    RatioSortedType::iterator it2 = mRatioSorted.begin();
+    auto it2 = mRatioSorted.begin();
     for (++it2; it2 != mRatioSorted.end(); ++it2) {
-      RatioSortedType::iterator prev = it2;
+      auto prev = it2;
       --prev;
       MATHICGB_ASSERT(mRatioRanks[*it2] == mRatioRanks[*prev] ||
         mRatioRanks[*it2] - 1 > mRatioRanks[*prev]);
@@ -373,16 +374,18 @@ bool SigPolyBasis::isSingularTopReducibleSlow(
   return false;
 }
 
-void SigPolyBasis::display(std::ostream& o) const {
+void SigPolyBasis::display(std::ostream& out) const {
   for (size_t i = 0; i < mBasis.size(); i++) {
-    o << i << " ";
-    if (!mSignatures[i].isNull())
-      ring().monomialDisplay(o, mSignatures[i]);
-    if (!mBasis.retired(i)) {
-      o << "  ";
-      mBasis.poly(i).display(o, false); // don't display component
-    }
-    o << std::endl;
+    out << i << " ";
+    // This is needed for compatibility with old tests.
+    // todo: change the tests so this isn't necessary.
+    if (monoid().isIdentity(*mSignatures[i]))
+      MathicIO<>().writeComponent(monoid(), *mSignatures[i], out);
+    else
+      MathicIO<>().writeMonomial(monoid(), true, *mSignatures[i], out);
+    out << "  ";
+    mBasis.poly(i).display(out, false);
+    out << '\n';
   }
 }
 
@@ -399,25 +402,24 @@ void SigPolyBasis::displayFancy
   for (size_t i = 0; i < mBasis.size(); i++) {
     indexOut << i << '\n';
 
-    if (!mSignatures[i].isNull()) {
-      monoid().copy(mSignatures[i], sig);
-      processor.postprocess(sig);
-      ring().monomialDisplay(sigOut, Monoid::toOld(*sig.ptr()));
-    }
-    sigOut << '\n';
+    monoid().copy(*mSignatures[i], *sig);
+    processor.postprocess(sig);
 
-    if (mBasis.retired(i))
-      polyOut << "retired";
+    if (monoid().isIdentity(*sig))
+      MathicIO<>().writeComponent(monoid(), *sig, out);
     else
-      mBasis.poly(i).display(polyOut, false);
+      MathicIO<>().writeMonomial(monoid(), true, *sig, out);
+    sigOut << '\n';
+
+    mBasis.poly(i).display(polyOut, false);
     polyOut << '\n';
   }
   out << pr;
 }
 
 void SigPolyBasis::postprocess(const MonoProcessor<PolyRing::Monoid>& processor) {
-  for (size_t i = 0; i < mSignatures.size(); ++i)
-    processor.postprocess(mSignatures[i]);
+  for (auto& mono : mSignatures)
+    processor.postprocess(*mono);
 }
 
 size_t SigPolyBasis::getMemoryUse() const
@@ -442,21 +444,21 @@ size_t SigPolyBasis::getMemoryUse() const
   return total;
 }
 
-size_t SigPolyBasis::ratioRank(const_monomial ratio) const {
+size_t SigPolyBasis::ratioRank(ConstMonoRef ratio) const {
   MATHICGB_ASSERT(mUseRatioRank);
   const size_t index = size();
   if (index == 0)
     return 0; // any value will do as there is nothing to compare to
   auto& sigLeadRatioNonConst =
-    const_cast<std::vector<monomial>&>(mSigLeadRatio);
+    const_cast<std::vector<MonoPtr>&>(mSigLeadRatio);
 
   sigLeadRatioNonConst.push_back(ratio.castAwayConst());
-  RatioSortedType::iterator pos = mRatioSorted.lower_bound(index);
+  auto pos = mRatioSorted.lower_bound(index);
   sigLeadRatioNonConst.pop_back();
 
   if (pos == mRatioSorted.end()) {
-    MATHICGB_ASSERT(ratioRank(*mRatioSorted.rbegin()) <
-      std::numeric_limits<Rank>::max());
+    MATHICGB_ASSERT
+      (ratioRank(*mRatioSorted.rbegin()) < std::numeric_limits<Rank>::max());
     return std::numeric_limits<Rank>::max();
   } else {
     if (monoid().equal(ratio, sigLeadRatio(*pos)))
@@ -464,7 +466,7 @@ size_t SigPolyBasis::ratioRank(const_monomial ratio) const {
     MATHICGB_ASSERT(ratioRank(*pos) > 0);
 #ifdef MATHICGB_DEBUG
     if (pos != mRatioSorted.begin()) {
-      RatioSortedType::iterator prev = pos;
+      auto prev = pos;
       --prev;
       MATHICGB_ASSERT(ratioRank(*pos) - 1 > ratioRank(*prev));
     }
@@ -478,24 +480,15 @@ SigPolyBasis::StoredRatioCmp::StoredRatioCmp(
   ConstMonoRef denominator,
   const SigPolyBasis& basis
 ):
-  mBasis(basis)
+  mBasis(basis),
+  mRatio(basis.monoid().alloc())
 {
   const auto& monoid = basis.ring().monoid();
-  mRatio = Monoid::toOld(*monoid.alloc().release());
-
   monoid.divideToNegative(denominator, numerator, mRatio);
-
-  if (SigPolyBasis::mUseRatioRank) {
-    mRatioRank = basis.ratioRank(mRatio);
-    mTmp = 0;
-  } else
-    mTmp = mBasis.ring().allocMonomial();
-}
-
-SigPolyBasis::StoredRatioCmp::~StoredRatioCmp() {
-  mBasis.ring().freeMonomial(mRatio);
-  if (!SigPolyBasis::mUseRatioRank)
-    mBasis.ring().freeMonomial(mTmp);
+  if (SigPolyBasis::mUseRatioRank)
+    mRatioRank = basis.ratioRank(*mRatio);
+  else
+    mTmp = mBasis.monoid().alloc();
 }
 
 MATHICGB_NAMESPACE_END
diff --git a/src/mathicgb/SigPolyBasis.hpp b/src/mathicgb/SigPolyBasis.hpp
index 86e9b6f..10dd7aa 100755
--- a/src/mathicgb/SigPolyBasis.hpp
+++ b/src/mathicgb/SigPolyBasis.hpp
@@ -13,8 +13,8 @@
 
 MATHICGB_NAMESPACE_BEGIN
 
-#ifndef USE_RATIO_RANK
-#define USE_RATIO_RANK true
+#ifndef MATHICGB_USE_RATIO_RANK
+#define MATHICGB_USE_RATIO_RANK true
 #endif
 
 class SigPolyBasis {
@@ -56,7 +56,7 @@ public:
 
   ConstMonoRef sigLeadRatio(size_t gen) const {
     MATHICGB_ASSERT(gen < size());
-    return mSigLeadRatio[gen];
+    return *mSigLeadRatio[gen];
   }
 
   // Signifies that the module has taken on another e_i.
@@ -70,7 +70,7 @@ public:
 
   ConstMonoRef signature(size_t gen) const {
     MATHICGB_ASSERT(gen < size());
-    return mSignatures[gen];
+    return *mSignatures[gen];
   }
 
   // Returns the index of a basis element that regular reduces term in
@@ -125,7 +125,6 @@ public:
       ConstMonoRef numerator,
       ConstMonoRef denominator,
       const SigPolyBasis& basis);
-    ~StoredRatioCmp();
 
     // compares the stored ratio to the basis element with index be.
     inline int compare(size_t be) const;
@@ -136,8 +135,8 @@ public:
 
     const SigPolyBasis& mBasis;
     size_t mRatioRank;
-    monomial mRatio;
-    mutable monomial mTmp;
+    Mono mRatio;
+    mutable Mono mTmp;
   };
 
 private:
@@ -148,7 +147,8 @@ private:
   void lowBaseDivisorsSlow(
     std::vector<size_t>& divisors,
     size_t maxDivisors,
-    size_t newGenerator) const;
+    size_t newGenerator
+  ) const;
 
   friend class StoredRatioCmp;
 
@@ -156,40 +156,44 @@ private:
 
   std::unique_ptr<MonoLookup::Factory const> const mMonoLookupFactory;
 
-  // may change at next insert!
+  /// The ratio rank can change at each insert!
   size_t ratioRank(size_t index) const {
     MATHICGB_ASSERT(index < size());
     return mRatioRanks[index];
   }
 
   // Only useful for comparing to basis elements. Two ratios might get the same
-  // rank without being equal. The proper rank may change when a new generator
+  // rank without being equal. All ranks can change when a new generator
   // is added.
-  size_t ratioRank(const_monomial ratio) const;
+  size_t ratioRank(ConstMonoRef ratio) const;
 
-  std::vector<monomial> mSignatures;
+  std::vector<MonoPtr> mSignatures;
 
   // the ratio signature/initial term including negative entries and module component
-  std::vector<monomial> mSigLeadRatio;
+  std::vector<MonoPtr> mSigLeadRatio;
 
   // true if giving each generator an integer id based on its
   // position in a sorted order of sig-lead ratios.
-  static const bool mUseRatioRank = USE_RATIO_RANK;
-  static const bool mUseStoredRatioRank = USE_RATIO_RANK;
+  static const bool mUseRatioRank = MATHICGB_USE_RATIO_RANK;
+  static const bool mUseStoredRatioRank = MATHICGB_USE_RATIO_RANK;
+
   class RatioOrder {
   public:
-    RatioOrder(std::vector<monomial>& ratio, const Monoid& monoid):
+    RatioOrder(std::vector<MonoPtr>& ratio, const Monoid& monoid):
       mRatio(ratio), mMonoid(monoid) {}
+
     bool operator()(size_t a, size_t b) const {
-      return mMonoid.lessThan(mRatio[a], mRatio[b]);
+      return mMonoid.lessThan(*mRatio[a], *mRatio[b]);
     }
+
   private:
-    std::vector<monomial>& mRatio;
+    std::vector<MonoPtr>& mRatio;
     const Monoid& mMonoid;
   };
-  typedef std::multiset<size_t, RatioOrder> RatioSortedType;
+
+  typedef std::multiset<size_t, RatioOrder> RatioSorted;
   typedef size_t Rank;
-  RatioSortedType mRatioSorted;
+  RatioSorted mRatioSorted;
   std::vector<Rank> mRatioRanks;
 
   std::vector<MonoLookup*> mSignatureLookup;
@@ -234,7 +238,7 @@ inline int SigPolyBasis::StoredRatioCmp::compare(size_t be) const {
   if (SigPolyBasis::mUseStoredRatioRank) {
 #ifdef MATHICGB_DEBUG
     const auto value =
-      mBasis.monoid().compare(mRatio, mBasis.sigLeadRatio(be));
+      mBasis.monoid().compare(*mRatio, mBasis.sigLeadRatio(be));
 #endif
     SigPolyBasis::Rank otherRank = mBasis.ratioRank(be);
     if (mRatioRank < otherRank) {
@@ -248,10 +252,10 @@ inline int SigPolyBasis::StoredRatioCmp::compare(size_t be) const {
       return EQ;
     }
   } else {
-    mBasis.monoid().multiply(mRatio, mBasis.leadMono(be), mTmp);
-    const auto value = mBasis.monoid().compare(mTmp, mBasis.signature(be));
+    mBasis.monoid().multiply(*mRatio, mBasis.leadMono(be), *mTmp);
+    const auto value = mBasis.monoid().compare(*mTmp, mBasis.signature(be));
     MATHICGB_ASSERT
-      (value == mBasis.monoid().compare(mRatio, mBasis.sigLeadRatio(be)));
+      (value == mBasis.monoid().compare(*mRatio, mBasis.sigLeadRatio(be)));
     return value;
   }
 }

-- 
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