SingularIOHandler.cpp
Go to the documentation of this file.
/* Frobby: Software for monomial ideal computations.
Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
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 2 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/.
*/
#include "stdinc.h"
#include "SingularIOHandler.h"
#include "Scanner.h"
#include "VarNames.h"
#include "BigTermConsumer.h"
#include "FrobbyStringStream.h"
#include "DataType.h"
#include "CoefBigTermConsumer.h"
#include "IdealWriter.h"
#include "PolyWriter.h"
#include "error.h"
#include "InputConsumer.h"
#include <cstdio>
namespace IO {
namespace Singular {
void writeRing(const VarNames& names, FILE* out);
}
namespace S = Singular;
class SingularIdealWriter : public IdealWriter {
public:
SingularIdealWriter(FILE* out): IdealWriter(out) {
}
private:
virtual void doWriteHeader(bool first) {
S::writeRing(getNames(), getFile());
fputs("ideal I =", getFile());
}
virtual void doWriteTerm(const Term& term,
const TermTranslator& translator,
bool first) {
fputs(first ? "\n " : ",\n ", getFile());
IO::writeTermProduct(term, translator, getFile());
}
virtual void doWriteTerm(const vector<mpz_class>& term,
bool first) {
fputs(first ? "\n " : ",\n ", getFile());
writeTermProduct(term, getNames(), getFile());
}
virtual void doWriteFooter(bool wasZeroIdeal) {
if (wasZeroIdeal)
fputs("\n 0", getFile());
fputs(";\n", getFile());
}
virtual void doWriteEmptyList() {
S::writeRing(getNames(), getFile());
}
};
class SingularPolyWriter : public PolyWriter {
public:
SingularPolyWriter(FILE* out): PolyWriter(out) {
}
virtual void doWriteHeader() {
S::writeRing(getNames(), getFile());
fputs("poly p =", getFile());
}
virtual void doWriteTerm(const mpz_class& coef,
const Term& term,
const TermTranslator& translator,
bool firstGenerator) {
fputs("\n ", getFile());
writeCoefTermProduct(coef, term, translator, firstGenerator, getFile());
}
virtual void doWriteTerm(const mpz_class& coef,
const vector<mpz_class>& term,
bool firstGenerator) {
fputs("\n ", getFile());
writeCoefTermProduct(coef, term, getNames(), firstGenerator, getFile());
}
virtual void doWriteFooter(bool wasZero) {
if (wasZero)
fputs("\n 0", getFile());
fputs(";\n", getFile());
}
};
SingularIOHandler::SingularIOHandler():
IOHandlerCommon(staticGetName(),
"Format understandable by the program Singular.") {
registerInput(DataType::getMonomialIdealType());
registerInput(DataType::getMonomialIdealListType());
registerInput(DataType::getPolynomialType());
registerOutput(DataType::getMonomialIdealType());
registerOutput(DataType::getPolynomialType());
}
const char* SingularIOHandler::staticGetName() {
return "singular";
}
BigTermConsumer* SingularIOHandler::doCreateIdealWriter(FILE* out) {
return new SingularIdealWriter(out);
}
CoefBigTermConsumer* SingularIOHandler::
doCreatePolynomialWriter(FILE* out) {
return new SingularPolyWriter(out);
}
void SingularIOHandler::doWriteTerm(const vector<mpz_class>& term,
const VarNames& names,
FILE* out) {
writeTermProduct(term, names, out);
}
void SingularIOHandler::doReadTerm(Scanner& in,
const VarNames& names,
vector<mpz_class>& term) {
readTermProduct(in, names, term);
}
void SingularIOHandler::doReadTerm(Scanner& in, InputConsumer& consumer) {
consumer.consumeTermProductNotation(in);
}
void SingularIOHandler::doReadRing(Scanner& in, VarNames& names) {
names.clear();
in.expect("ring");
in.expect('R');
in.expect('=');
in.expect('0');
in.expect(',');
in.expect('(');
do {
names.addVarSyntaxCheckUnique(in, in.readIdentifier());
} while (in.match(','));
in.expect(')');
in.expect(',');
in.expect("lp");
in.expect(';');
in.expect("int");
in.expect("noVars");
in.expect("=");
if (in.match('1')) {
if (names.getVarCount() != 1 ||
names.getName(0) != string("dummy")) {
FrobbyStringStream errorMsg;
errorMsg <<
"A singular ring with no actual variables must have a single "
"place-holder variable named \"dummy\", and in this case ";
if (names.getVarCount() != 1)
errorMsg << "there are " << names.getVarCount()
<< " place-holder variables.";
else
errorMsg << "it has the name \"" << names.getName(0) << "\".";
reportSyntaxError(in, errorMsg);
}
names.clear();
} else if (!in.match('0')) {
// TODO: Replace following line with: in.expect('0', '1');
reportSyntaxError(in, "noVars must be either 0 or 1.");
}
in.expect(';');
}
bool SingularIOHandler::doPeekRing(Scanner& in) {
return in.peek('r') || in.peek('R');
}
void SingularIOHandler::doReadBareIdeal(Scanner& in, const VarNames& names,
BigTermConsumer& consumer) {
consumer.beginConsuming(names);
vector<mpz_class> term(names.getVarCount());
in.expect("ideal");
in.expect('I');
in.expect('=');
if (!in.match('0')) {
do {
readTerm(in, names, term);
consumer.consume(term);
} while (in.match(','));
}
in.expect(';');
consumer.doneConsuming();
}
void SingularIOHandler::doReadBareIdeal
(Scanner& in, InputConsumer& consumer) {
consumer.beginIdeal();
in.expect("ideal");
in.expect('I');
in.expect('=');
if (!in.match('0')) {
do {
consumer.consumeTermProductNotation(in);
} while (in.match(','));
}
in.expect(';');
consumer.endIdeal();
}
void SingularIOHandler::doReadBarePolynomial(Scanner& in,
const VarNames& names,
CoefBigTermConsumer& consumer) {
consumer.consumeRing(names);
vector<mpz_class> term(names.getVarCount());
mpz_class coef;
in.expect("poly");
in.expect('p');
in.expect('=');
consumer.beginConsuming();
bool first = true;
do {
readCoefTerm(coef, term, names, first, in);
consumer.consume(coef, term);
first = false;
} while (!in.match(';'));
consumer.doneConsuming();
}
void S::writeRing(const VarNames& names, FILE* out) {
if (names.getVarCount() == 0)
fputs("ring R = 0, (dummy), lp;\nint noVars = 1;\n", out);
else {
fputs("ring R = 0, (", out);
const char* pre = "";
for (unsigned int i = 0; i < names.getVarCount(); ++i) {
fputs(pre, out);
fputs(names.getName(i).c_str(), out);
pre = ", ";
}
fputs("), lp;\nint noVars = 0;\n", out);
}
}
}
BigTermConsumer.h
CoefBigTermConsumer.h
DataType.h
FrobbyStringStream.h
IdealWriter.h
InputConsumer.h
PolyWriter.h
Scanner.h
SingularIOHandler.h
VarNames.h
BigTermConsumer
Definition BigTermConsumer.h:29
BigTermConsumer::consume
virtual void consume(const vector< mpz_class > &term)=0
BigTermConsumer::doneConsuming
virtual void doneConsuming()=0
Must be called once after each time beginConsuming has been called.
BigTermConsumer::beginConsuming
virtual void beginConsuming()=0
Tell the consumer to begin consuming an ideal.
CoefBigTermConsumer
Definition CoefBigTermConsumer.h:29
CoefBigTermConsumer::beginConsuming
virtual void beginConsuming()=0
CoefBigTermConsumer::consume
virtual void consume(const mpz_class &coef, const Term &term)
Definition CoefBigTermConsumer.cpp:25
CoefBigTermConsumer::doneConsuming
virtual void doneConsuming()=0
CoefBigTermConsumer::consumeRing
virtual void consumeRing(const VarNames &names)=0
DataType::getMonomialIdealListType
static const DataType & getMonomialIdealListType()
Returns the one and only instance for monomial ideal lists.
Definition DataType.cpp:54
DataType::getMonomialIdealType
static const DataType & getMonomialIdealType()
Returns the one and only instance for monomial ideals.
Definition DataType.cpp:45
DataType::getPolynomialType
static const DataType & getPolynomialType()
Returns the one and only instance for polynomials.
Definition DataType.cpp:50
FrobbyStringStream
A replacement for stringstream.
Definition FrobbyStringStream.h:26
IOHandler::readTerm
void readTerm(Scanner &in, const VarNames &names, vector< mpz_class > &term)
Definition IOHandler.cpp:51
IO::IOHandlerCommon::IOHandlerCommon
IOHandlerCommon(const char *formatName, const char *formatDescription)
Definition IOHandlerCommon.cpp:27
IO::IOHandlerImpl::registerInput
void registerInput(const DataType &type)
Specify that input of the argument type is supported.
Definition IOHandlerImpl.cpp:60
IO::IOHandlerImpl::registerOutput
void registerOutput(const DataType &type)
Specify that output of the argument type is supported.
Definition IOHandlerImpl.cpp:67
IO::IdealWriter::getFile
FILE * getFile()
Definition IdealWriter.h:43
IO::IdealWriter::IdealWriter
IdealWriter(FILE *out)
Definition IdealWriter.cpp:25
IO::IdealWriter::getNames
const VarNames & getNames()
Definition IdealWriter.h:44
IO::PolyWriter::PolyWriter
PolyWriter(FILE *out)
Definition PolyWriter.cpp:25
IO::PolyWriter::getFile
FILE * getFile()
Definition PolyWriter.h:40
IO::PolyWriter::getNames
const VarNames & getNames() const
Definition PolyWriter.h:41
IO::SingularIOHandler::SingularIOHandler
SingularIOHandler()
Definition SingularIOHandler.cpp:106
IO::SingularIOHandler::doReadBarePolynomial
virtual void doReadBarePolynomial(Scanner &in, const VarNames &names, CoefBigTermConsumer &consumer)
Definition SingularIOHandler.cpp:233
IO::SingularIOHandler::doReadBareIdeal
virtual void doReadBareIdeal(Scanner &in, const VarNames &names, BigTermConsumer &consumer)
Definition SingularIOHandler.cpp:195
IO::SingularIOHandler::doPeekRing
virtual bool doPeekRing(Scanner &in)
Definition SingularIOHandler.cpp:191
IO::SingularIOHandler::doCreatePolynomialWriter
virtual CoefBigTermConsumer * doCreatePolynomialWriter(FILE *out)
Definition SingularIOHandler.cpp:125
IO::SingularIOHandler::doCreateIdealWriter
virtual BigTermConsumer * doCreateIdealWriter(FILE *out)
Definition SingularIOHandler.cpp:120
IO::SingularIOHandler::doReadRing
virtual void doReadRing(Scanner &in, VarNames &names)
Definition SingularIOHandler.cpp:145
IO::SingularIOHandler::doReadTerm
virtual void doReadTerm(Scanner &in, const VarNames &names, vector< mpz_class > &term)
Definition SingularIOHandler.cpp:135
IO::SingularIOHandler::doWriteTerm
virtual void doWriteTerm(const vector< mpz_class > &term, const VarNames &names, FILE *out)
Definition SingularIOHandler.cpp:129
IO::SingularIOHandler::staticGetName
static const char * staticGetName()
Definition SingularIOHandler.cpp:116
IO::SingularIdealWriter
Definition SingularIOHandler.cpp:39
IO::SingularIdealWriter::doWriteFooter
virtual void doWriteFooter(bool wasZeroIdeal)
Definition SingularIOHandler.cpp:63
IO::SingularIdealWriter::SingularIdealWriter
SingularIdealWriter(FILE *out)
Definition SingularIOHandler.cpp:41
IO::SingularIdealWriter::doWriteTerm
virtual void doWriteTerm(const vector< mpz_class > &term, bool first)
Definition SingularIOHandler.cpp:57
IO::SingularIdealWriter::doWriteTerm
virtual void doWriteTerm(const Term &term, const TermTranslator &translator, bool first)
Definition SingularIOHandler.cpp:50
IO::SingularIdealWriter::doWriteEmptyList
virtual void doWriteEmptyList()
Definition SingularIOHandler.cpp:69
IO::SingularIdealWriter::doWriteHeader
virtual void doWriteHeader(bool first)
Definition SingularIOHandler.cpp:45
IO::SingularPolyWriter
Definition SingularIOHandler.cpp:74
IO::SingularPolyWriter::SingularPolyWriter
SingularPolyWriter(FILE *out)
Definition SingularIOHandler.cpp:76
IO::SingularPolyWriter::doWriteHeader
virtual void doWriteHeader()
Definition SingularIOHandler.cpp:79
IO::SingularPolyWriter::doWriteTerm
virtual void doWriteTerm(const mpz_class &coef, const Term &term, const TermTranslator &translator, bool firstGenerator)
Definition SingularIOHandler.cpp:84
IO::SingularPolyWriter::doWriteTerm
virtual void doWriteTerm(const mpz_class &coef, const vector< mpz_class > &term, bool firstGenerator)
Definition SingularIOHandler.cpp:92
IO::SingularPolyWriter::doWriteFooter
virtual void doWriteFooter(bool wasZero)
Definition SingularIOHandler.cpp:99
InputConsumer
Definition InputConsumer.h:30
InputConsumer::beginIdeal
void beginIdeal()
Start consuming an ideal.
Definition InputConsumer.cpp:50
InputConsumer::consumeTermProductNotation
void consumeTermProductNotation(Scanner &in)
Reads a term in a format like "a^4*b*c^2".
Definition InputConsumer.cpp:148
InputConsumer::endIdeal
void endIdeal()
Done reading an ideal.
Definition InputConsumer.cpp:191
Scanner
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition Scanner.h:50
Scanner::peek
bool peek(char character)
Skips whitespace and returns true if the next character is equal to the parameter(s).
Definition Scanner.h:262
Scanner::expect
void expect(char expected)
Require the next character to be equal to expected.
Definition Scanner.h:231
Scanner::readIdentifier
const char * readIdentifier()
The returned string is only valid until the next method on this object gets called.
Definition Scanner.cpp:255
Scanner::match
bool match(char c)
Return true if the next character is c, and in that case skip past it.
Definition Scanner.h:215
TermTranslator
TermTranslator handles translation between terms whose exponents are infinite precision integers and ...
Definition TermTranslator.h:41
Term
Term represents a product of variables which does not include a coefficient.
Definition Term.h:49
VarNames
Defines the variables of a polynomial ring and facilities IO involving them.
Definition VarNames.h:40
VarNames::getVarCount
size_t getVarCount() const
Returns the current number of variables.
Definition VarNames.h:113
VarNames::getName
const string & getName(size_t index) const
The returned reference can become invalid next time addVar is called.
Definition VarNames.cpp:100
VarNames::clear
void clear()
Resets the number of variables to zero.
Definition VarNames.cpp:106
VarNames::addVarSyntaxCheckUnique
void addVarSyntaxCheckUnique(const Scanner &in, const string &name)
As addvar, except it reports a syntax error if name is already a variable.
Definition VarNames.cpp:68
reportSyntaxError
void reportSyntaxError(const Scanner &scanner, const string &errorMsg)
Definition error.cpp:44
error.h
IO::Singular
Definition SingularIOHandler.cpp:34
IO::Singular::writeRing
void writeRing(const VarNames &names, FILE *out)
Definition SingularIOHandler.cpp:254
IO
Definition CoCoA4IOHandler.cpp:33
IO::writeCoefTermProduct
void writeCoefTermProduct(const mpz_class &coef, const Term &term, const TermTranslator &translator, bool hidePlus, FILE *out)
Definition IOHandlerImpl.cpp:123
IO::readCoefTerm
void readCoefTerm(BigPolynomial &polynomial, bool firstTerm, Scanner &in)
Definition IOHandlerImpl.cpp:262
IO::readTermProduct
void readTermProduct(Scanner &in, const VarNames &names, vector< mpz_class > &term)
Definition IOHandlerImpl.cpp:108
IO::writeTermProduct
void writeTermProduct(const Term &term, const TermTranslator &translator, FILE *out)
Definition IOHandlerImpl.cpp:170
stdinc.h
This header file includes common definitions and is included as the first line of code in every imple...