Skip to content

Commit

Permalink
prepare export to pari vector
Browse files Browse the repository at this point in the history
  • Loading branch information
ckrause committed Mar 16, 2024
1 parent de3b8cd commit 89f8a64
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
16 changes: 13 additions & 3 deletions src/cmd/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ void Commands::minimize(const std::string& path) {
ProgramUtil::print(program, std::cout);
}

void throwConversionError(const std::string& format) {
throw std::runtime_error("program cannot be converted to " + format);
}

void Commands::export_(const std::string& path) {
initLog(true);
Program program = OeisProgram::getProgramAndSeqId(path).first;
Expand All @@ -210,13 +214,19 @@ void Commands::export_(const std::string& path) {
FormulaGenerator generator;
if (format.empty() || format == "formula") {
if (!generator.generate(program, -1, formula, settings.with_deps)) {
throw std::runtime_error("program cannot be converted to formula");
throwConversionError(format);
}
std::cout << formula.toString() << std::endl;
} else if (format == "pari") {
if (!generator.generate(program, -1, formula, settings.with_deps) ||
!Pari::convertToPari(formula)) {
throw std::runtime_error("program cannot be converted to pari");
!Pari::convertToPari(formula, false)) {
throwConversionError(format);
}
std::cout << Pari::toString(formula) << std::endl;
} else if (format == "pari-vector") {
if (!generator.generate(program, -1, formula, settings.with_deps) ||
!Pari::convertToPari(formula, true)) {
throwConversionError(format);
}
std::cout << Pari::toString(formula) << std::endl;
} else if (format == "loda") {
Expand Down
30 changes: 16 additions & 14 deletions src/form/pari.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,10 @@ void convertInitialTermsToIf(Formula& formula) {
}
}

void convertFracToPari(Expression& frac) {
std::string func = "floor";
if (ExpressionUtil::canBeNegative(frac.children.at(0)) ||
ExpressionUtil::canBeNegative(frac.children.at(1))) {
func = "truncate";
}
Expression wrapper(Expression::Type::FUNCTION, func, {frac});
frac = wrapper;
}

bool convertExprToPari(Expression& expr) {
bool convertExprToPari(Expression& expr, const Formula& f, bool as_vector) {
// convert bottom-up!
for (auto& c : expr.children) {
if (!convertExprToPari(c)) {
if (!convertExprToPari(c, f, as_vector)) {
return false;
}
}
Expand All @@ -49,6 +39,10 @@ bool convertExprToPari(Expression& expr) {
return false;
}
}
if (expr.type == Expression::Type::FUNCTION && as_vector &&
f.containsFunctionDef(expr.name)) {
expr.type = Expression::Type::VECTOR;
}
return true;
}

Expand Down Expand Up @@ -90,12 +84,20 @@ bool addLocalVars(Formula& f) {
return changed;
}

bool Pari::convertToPari(Formula& f) {
bool Pari::convertToPari(Formula& f, bool as_vector) {
Formula tmp;
for (auto& entry : f.entries) {
if (!convertExprToPari(entry.second)) {
auto left = entry.first;
auto right = entry.second;
if (as_vector && left.type == Expression::Type::FUNCTION) {
left.type = Expression::Type::VECTOR;
}
if (!convertExprToPari(right, f, as_vector)) {
return false;
}
tmp.entries[left] = right;
}
f = tmp;
addLocalVars(f);
convertInitialTermsToIf(f);
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/form/pari.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class Pari {
public:
static bool convertToPari(Formula& f);
static bool convertToPari(Formula& f, bool as_vector = false);

static std::string toString(const Formula& f);

Expand Down

0 comments on commit 89f8a64

Please sign in to comment.