[mathicgb] 310/393: Added readPoly and writePoly to MathicIO along with tests.
Doug Torrance
dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:59:27 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 84e370f82f43d4dc100d8356b25ab2ba8ced5c1b
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date: Mon May 6 14:36:22 2013 +0200
Added readPoly and writePoly to MathicIO along with tests.
---
src/mathicgb/MathicIO.hpp | 86 +++++++++++++++++++++++++++++----------------
src/mathicgb/MonoMonoid.hpp | 2 ++
src/mathicgb/Poly.hpp | 11 ++++++
src/test/MathicIO.cpp | 54 +++++++++++++++++++---------
4 files changed, 107 insertions(+), 46 deletions(-)
diff --git a/src/mathicgb/MathicIO.hpp b/src/mathicgb/MathicIO.hpp
index 5362210..59b782b 100755
--- a/src/mathicgb/MathicIO.hpp
+++ b/src/mathicgb/MathicIO.hpp
@@ -4,6 +4,7 @@
#include "Scanner.hpp"
#include "PolyRing.hpp"
#include "MonoProcessor.hpp"
+#include "Poly.hpp"
#include <ostream>
#include <string>
@@ -62,6 +63,18 @@ public:
std::ostream& out
);
+ Poly readPoly(
+ const PolyRing& ring,
+ const bool readComponent,
+ Scanner& in
+ );
+
+ void writePoly(
+ const Poly& poly,
+ const bool writeComponent,
+ std::ostream& out
+ );
+
void readTerm(
const PolyRing& ring,
const bool readComponent,
@@ -324,41 +337,54 @@ auto MathicIO::readBasis() -> BasisData {
);
}
-std::unique_ptr<Poly> MathicIO::readPolynomial(const PolyRing& ring) {
+*/
+
+Poly MathicIO::readPoly(
+ const PolyRing& ring,
+ const bool readComponent,
+ Scanner& in
+) {
Poly p(ring);
- if (mIn.match('0'))
- return;
+
+ // also skips whitespace
+ if (in.match('0') || in.match("+0") || in.match("-0"))
+ return std::move(p);
+ MATHICGB_ASSERT(!in.peekWhite());
auto mono = ring.monoid().alloc();
- while (true) {
- if (!isdigit(next) && !isalpha(next) && next != '<')
- break;
-
-
- // read coefficient
- const bool negate = !mIn.match('+') && mIn.match('-');
- coefficient coef = negate ? -1 : 1;
- mIn.matchReadInteger(coef, negate);
-
- // read monomial
- read
-
- ring.monoid().setToIdentity(mono);
-
-
- const size_t firstLocation = monoms.size();
- monoms.resize(firstLocation + R->maxMonomialSize());
- monomial m = &monoms[firstLocation];
- if (isalpha(next) || next == '<')
- R->monomialParse(i, m);
- else
- R->monomialSetIdentity(m); // have to do this to set hash value
- next = i.peek();
- if (next == '>')
- i.get();
+ auto coef = ring.field().zero();
+ do {
+ if (!p.isZero())
+ in.expect('+');
+ readTerm(ring, readComponent, coef, mono, in);
+ p.appendTerm(coef.value(), mono);
+ } while (!in.peekWhite() && !in.matchEOF());
+ return std::move(p);
+}
+
+void MathicIO::writePoly(
+ const Poly& poly,
+ const bool writeComponent,
+ std::ostream& out
+) {
+ if (poly.isZero()) {
+ out << '0';
+ return;
+ }
+
+ const auto end = poly.end();
+ for (auto it = poly.begin(); it != end; ++it) {
+ if (it != poly.begin())
+ out << '+';
+ writeTerm(
+ poly.ring(),
+ writeComponent,
+ poly.ring().field().toElement(it.getCoefficient()),
+ it.getMonomial(),
+ out
+ );
}
}
-*/
void MathicIO::readTerm(
const PolyRing& ring,
diff --git a/src/mathicgb/MonoMonoid.hpp b/src/mathicgb/MonoMonoid.hpp
index 5cb4c3d..23d64b6 100755
--- a/src/mathicgb/MonoMonoid.hpp
+++ b/src/mathicgb/MonoMonoid.hpp
@@ -309,6 +309,7 @@ public:
// *** Temporary compatibility code for migrating off PolyRing
friend class PolyRing;
+ friend class Poly;
static MonoRef toRef(Exponent* e) {return MonoRef(e);}
static ConstMonoRef toRef(const Exponent* e) {return ConstMonoRef(e);}
static Exponent* toOld(MonoRef e) {return rawPtr(e);}
@@ -1130,6 +1131,7 @@ public:
private:
friend class MonoMonoid;
friend class PolyRing; // todo: remove
+ friend class Poly; // todo: remove
Exponent* internalRawPtr() const {return mMono;}
MonoPtr(Exponent* mono): mMono(mono) {}
diff --git a/src/mathicgb/Poly.hpp b/src/mathicgb/Poly.hpp
index 823fa7d..059ccf0 100755
--- a/src/mathicgb/Poly.hpp
+++ b/src/mathicgb/Poly.hpp
@@ -93,6 +93,7 @@ public:
/// all iterators are invalid after this
void appendTerm(coefficient a, const_monomial m);
+ void appendTerm(coefficient a, PolyRing::Monoid::ConstMonoRef m);
/// Hint that space for termCount terms is going to be needed so the internal
/// storage should be expanded to fit that many terms.
@@ -182,6 +183,16 @@ inline void Poly::appendTerm(coefficient a, const_monomial m)
monoms.insert(monoms.end(), e, e + len);
}
+inline void Poly::appendTerm(coefficient a, PolyRing::Monoid::ConstMonoRef m) {
+ coeffs.push_back(a);
+ size_t len = R->maxMonomialSize();
+ auto& monoid = ring().monoid();
+ const auto offset = monoms.size();
+ monoms.resize(offset + monoid.entryCount());
+ monoid.copy(m, *PolyRing::Monoid::MonoPtr(monoms.data() + offset));
+}
+
+
#endif
// Local Variables:
diff --git a/src/test/MathicIO.cpp b/src/test/MathicIO.cpp
index e9f0081..f05ec3e 100755
--- a/src/test/MathicIO.cpp
+++ b/src/test/MathicIO.cpp
@@ -104,6 +104,44 @@ TEST(MathicIO, ReadWriteMonomial) {
check("ab<2>", 2, 0,1, 1,1);
}
+TEST(MathicIO, ReadWritePoly) {
+ typedef PolyRing::Monoid Monoid;
+ typedef PolyRing::Field Field;
+
+ PolyRing ring(Field(101), Monoid(28));
+
+ auto check = [&](
+ const char* const inStr,
+ const char* const outStr,
+ const bool doComponent
+ ) {
+ for (int i = 0; i < 2; ++i) {
+ const char* str = i == 0 ? inStr : outStr;
+ if (str == 0)
+ continue;
+
+ Scanner in(str);
+ const auto poly = MathicIO().readPoly(ring, doComponent, in);
+ std::ostringstream out;
+ MathicIO().writePoly(poly, doComponent, out);
+ const auto correctStr = outStr == 0 ? inStr : outStr;
+ ASSERT_EQ(correctStr, out.str());
+ }
+ };
+
+ check("+0", "0", false);
+ check("-0", "0", false);
+ check("+1", "1", false);
+ check("\t a\t", "a", false);
+ check("3a+1b5+2c6", "3a+b5+2c6", false);
+
+ check("+0", "0", true);
+ check("-0", "0", true);
+ check("+1<5>", "1<5>", true);
+ check("\t a<0>\t", "a<0>", true);
+ check("3a<1>+1b5<2>+2c6<3>", "3a<1>+b5<2>+2c6<3>", true);
+}
+
TEST(MathicIO, ReadWriteTerm) {
typedef PolyRing::Monoid Monoid;
typedef Monoid::VarIndex VarIndex;
@@ -156,22 +194,6 @@ TEST(MathicIO, ReadWriteTerm) {
check("+1a<0>", "a<0>", true, 1);
check("+2b", "2b", false, 2);
-
-
-/*
- check("1<0>", 0, 0);
- check("1<1>", 1);
- check("1<999>", 999);
-
- check("a1", NoComponent, 0,1, -1,-1, "a");
- check("b10<0>", 0, 1,10);
- check("A11", NoComponent, 26,11);
- check("B99<1>", 1, 27,99);
-
- check("ab", NoComponent, 0,1, 1,1);
- check("ba", NoComponent, 0,1, 1,1, "ab");
- check("a0c3b1", NoComponent, 1,1, 2,3, "bc3");
- check("ab<2>", 2, 0,1, 1,1);*/
}
TEST(MathicIO, ReadWriteBaseField) {
--
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