Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ckrause committed Nov 17, 2024
1 parent 716cfb8 commit abfab3d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/cmd/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,8 @@ void Commands::testPari(const std::string& test_id) {
}

// evaluate PARI program
auto genSeq = pari_formula.eval(numTerms);
auto offset = ProgramUtil::getOffset(program);
auto genSeq = pari_formula.eval(offset, numTerms);

// compare results
if (genSeq != expSeq) {
Expand Down
12 changes: 6 additions & 6 deletions src/form/pari.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ std::string PariFormula::toString() const {
}
}

void PariFormula::printEvalCode(int64_t numTerms, std::ostream& out) const {
void PariFormula::printEvalCode(int64_t offset, int64_t numTerms,
std::ostream& out) const {
if (as_vector) {
// declare vectors
auto functions =
Expand All @@ -141,9 +142,8 @@ void PariFormula::printEvalCode(int64_t numTerms, std::ostream& out) const {
// main function
out << toString() << std::endl;
}
const int64_t start = 0;
const int64_t end = numTerms - 1;
out << "for(n=" << start << "," << end << ",";
const int64_t end = offset + numTerms - 1;
out << "for(n=" << offset << "," << end << ",";
if (as_vector) {
out << toString() << "; ";
out << "print(a[n+1])";
Expand All @@ -153,15 +153,15 @@ void PariFormula::printEvalCode(int64_t numTerms, std::ostream& out) const {
out << ")" << std::endl << "quit" << std::endl;
}

Sequence PariFormula::eval(int64_t numTerms) const {
Sequence PariFormula::eval(int64_t offset, int64_t numTerms) const {
const std::string gpPath("pari-loda.gp");
const std::string gpResult("pari-result.txt");
const int64_t maxparisize = 256; // in MB
std::ofstream gp(gpPath);
if (!gp) {
throw std::runtime_error("error generating gp file");
}
printEvalCode(numTerms, gp);
printEvalCode(offset, numTerms, gp);
gp.close();
std::string cmd = "gp -s " + std::to_string(maxparisize) + "M -q " + gpPath +
" > " + gpResult;
Expand Down
4 changes: 2 additions & 2 deletions src/form/pari.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class PariFormula {
static bool convert(const Formula& formula, bool as_vector,
PariFormula& pari_formula);

void printEvalCode(int64_t numTerms, std::ostream& out) const;
void printEvalCode(int64_t offset, int64_t numTerms, std::ostream& out) const;

std::string toString() const;

Sequence eval(int64_t numTerms) const;
Sequence eval(int64_t offset, int64_t numTerms) const;

private:
Formula main_formula;
Expand Down
2 changes: 2 additions & 0 deletions tests/formula/pari-function.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
A000023: a(n) = if(n==3,-2,if(n==2,2,if(n==1,-1,if(n==0,1,local(l1=a(n-1)); (n-1)*(2*a(n-2)+l1)-l1))))
A000027: a(n) = n
A000032: a(n) = if(n==2,3,if(n==1,1,if(n==0,2,a(n-1)+a(n-2))))
A000058: (a(n) = b(n)+1); (b(n) = if(n==0,1,local(l1=b(n-1)); l1*(l1+1)))
A001715: (a(n) = truncate(b(n)/6)); (b(n) = if(n==3,6,n*b(n-1)))

0 comments on commit abfab3d

Please sign in to comment.