[mathicgb] 309/393: Added writeTerm to MathicIO and now test both readTerm and writeTerm.
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 58292bf05342254709026636fc9bc44e28614d53
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date: Mon May 6 13:42:39 2013 +0200
Added writeTerm to MathicIO and now test both readTerm and writeTerm.
---
src/mathicgb/MathicIO.hpp | 76 ++++++++++++++++++++++++++++++++++++-----------
src/test/MathicIO.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 127 insertions(+), 19 deletions(-)
diff --git a/src/mathicgb/MathicIO.hpp b/src/mathicgb/MathicIO.hpp
index ba57efd..5362210 100755
--- a/src/mathicgb/MathicIO.hpp
+++ b/src/mathicgb/MathicIO.hpp
@@ -70,6 +70,14 @@ public:
Scanner& in
);
+ void writeTerm(
+ const PolyRing& ring,
+ const bool writeComponent,
+ const Coefficient coef,
+ ConstMonoRef mono,
+ std::ostream& out
+ );
+
/// Read a monomial with no coefficient. A 1 on its own is not
/// considered a coefficient here - it is the monomial with all
/// exponents zero and no coefficient.
@@ -82,6 +90,14 @@ public:
Scanner& in
);
+ /// Print a monomial with no coefficient.
+ void writeMonomial(
+ const Monoid& monoid,
+ const bool writeComponent,
+ ConstMonoRef mono,
+ std::ostream& out
+ );
+
/// Read the trailing indicator of the component of a module monomial.
void readComponent(
const Monoid& monoid,
@@ -89,10 +105,8 @@ public:
Scanner& in
);
- /// Print a monomial with no coefficient.
- void writeMonomial(
+ void writeComponent(
const Monoid& monoid,
- const bool writeComponent,
ConstMonoRef mono,
std::ostream& out
);
@@ -355,23 +369,42 @@ void MathicIO::readTerm(
) {
// ** Read coefficient, if any.
const auto& field = ring.field();
+ const auto& monoid = ring.monoid();
const bool negate = !in.match('+') && in.match('-');
- if (in.peekDigit())
+ if (in.peekDigit()) {
coef = field.toElement(in.readInteger<RawCoefficient>(negate));
- else if (negate)
+
+ if (!in.peekAlpha()) {
+ // Identify a number c on its own as the monomial 1 times c.
+ monoid.setIdentity(mono);
+ if (readComponent)
+ this->readComponent(monoid, mono, in);
+ return;
+ }
+ } else if (negate)
coef = field.minusOne();
else
coef = field.one();
- // ** Read monomial
- auto& monoid = ring.monoid();
- if (field.isOne(coef) && !in.peekAlpha()) {
- // Detect the monomial 1.
- monoid.setIdentity(mono);
- if (readComponent)
- this->readComponent(monoid, mono, in);
- } else
- readMonomial(monoid, readComponent, mono, in);
+ readMonomial(monoid, readComponent, mono, in);
+}
+
+void MathicIO::writeTerm(
+ const PolyRing& ring,
+ const bool writeComponent,
+ const Coefficient coef,
+ ConstMonoRef mono,
+ std::ostream& out
+) {
+ if (!ring.field().isOne(coef)) {
+ out << unchar(coef.value());
+ if (ring.monoid().isIdentity(mono)) {
+ if (writeComponent)
+ this->writeComponent(ring.monoid(), mono, out);
+ return;
+ }
+ }
+ writeMonomial(ring.monoid(), writeComponent, mono, out);
}
void MathicIO::readMonomial(
@@ -451,6 +484,15 @@ void MathicIO::readComponent(
in.expect('>');
}
+void MathicIO::writeComponent(
+ const Monoid& monoid,
+ ConstMonoRef mono,
+ std::ostream& out
+) {
+ MATHICGB_ASSERT(Monoid::HasComponent);
+ out << '<' << unchar(monoid.component(mono)) << '>';
+}
+
/// Print a monomial with no coefficient.
void MathicIO::writeMonomial(
const Monoid& monoid,
@@ -480,10 +522,8 @@ void MathicIO::writeMonomial(
}
if (!printedSome)
out << '1';
- if (writeComponent) {
- MATHICGB_ASSERT(Monoid::HasComponent);
- out << '<' << unchar(monoid.component(mono)) << '>';
- }
+ if (writeComponent)
+ this->writeComponent(monoid, mono, out);
}
#endif
diff --git a/src/test/MathicIO.cpp b/src/test/MathicIO.cpp
index 8d17fd5..e9f0081 100755
--- a/src/test/MathicIO.cpp
+++ b/src/test/MathicIO.cpp
@@ -104,7 +104,75 @@ TEST(MathicIO, ReadWriteMonomial) {
check("ab<2>", 2, 0,1, 1,1);
}
-// TODO: check readTerm
+TEST(MathicIO, ReadWriteTerm) {
+ typedef PolyRing::Monoid Monoid;
+ typedef Monoid::VarIndex VarIndex;
+ typedef Monoid::Exponent Exponent;
+ typedef PolyRing::Field Field;
+ typedef Field::Element Coefficient;
+ typedef Field::RawElement RawCoefficient;
+ static const auto NoComponent = static_cast<Exponent>(-1);
+
+ PolyRing ring(Field(101), Monoid(28));
+ auto&& m = ring.monoid();
+ auto&& f = ring.field();
+
+ auto check = [&](
+ const char* const inStr,
+ const char* const outStr,
+ const bool doComponent,
+ const RawCoefficient coef
+ ) {
+ for (int i = 0; i < 2; ++i) {
+ const char* str = i == 0 ? inStr : outStr;
+ if (str == 0)
+ continue;
+
+ // read term from string
+ auto monoRead = m.alloc();
+ Coefficient readCoef = f.zero();
+ Scanner in(str);
+ MathicIO().readTerm(ring, doComponent, readCoef, monoRead, in);
+
+ ASSERT_EQ(coef, readCoef.value());
+
+ // print monomial
+ std::ostringstream out;
+ MathicIO().writeTerm(ring, doComponent, readCoef, monoRead, out);
+ const auto correctStr = outStr == 0 ? inStr : outStr;
+ ASSERT_EQ(correctStr, out.str());
+ }
+ };
+
+ check("1", 0, false, 1);
+ check("+1", "1", false, 1);
+ check("2", 0, false, 2);
+ check("+102", "1", false, 1);
+
+ check("1<0>", 0, true, 1);
+ check("+1<2>", "1<2>", true, 1);
+ check("2<3>", 0, true, 2);
+ check("+3<4>", "3<4>", true, 3);
+
+ 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) {
Scanner in("101");
--
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