Skip to content

Commit

Permalink
fix formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
ckrause committed Nov 3, 2024
1 parent a6872dd commit caf6f66
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/form/formula_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ bool FormulaGenerator::generateSingle(const Program& p) {
simplifyFormulaUsingVariants(formula, numTerms);

// evaluate program and add initial terms to formula
if (!addInitialTerms(numCells, numTerms)) {
const int64_t offset = ProgramUtil::getOffset(p);
if (!addInitialTerms(numCells, offset, numTerms)) {
return false;
}

Expand Down Expand Up @@ -365,7 +366,8 @@ void FormulaGenerator::prepareForPostLoop(
}

bool FormulaGenerator::addInitialTerms(
int64_t numCells, const std::map<std::string, int64_t>& numTerms) {
int64_t numCells, int64_t offset,
const std::map<std::string, int64_t>& numTerms) {
// calculate maximum offset
int64_t maxNumTerms = 0;
for (auto it : numTerms) {
Expand All @@ -374,7 +376,7 @@ bool FormulaGenerator::addInitialTerms(
maxNumTerms = std::max(maxNumTerms, it.second);
}
// evaluate program and add initial terms to formula
for (int64_t offset = 0; offset < maxNumTerms; offset++) {
for (int64_t n = 0; n < maxNumTerms; n++) {
try {
incEval.next(true, true); // skip final iteration and post loop code
} catch (const std::exception&) {
Expand All @@ -384,9 +386,9 @@ bool FormulaGenerator::addInitialTerms(
const auto state = incEval.getLoopStates().at(incEval.getPreviousSlice());
for (int64_t cell = 0; cell < numCells; cell++) {
auto name = getCellName(cell);
if (offset < numTerms.at(name)) {
if (n < numTerms.at(name)) {
Expression func(Expression::Type::FUNCTION, name,
{ExpressionUtil::newConstant(offset)});
{ExpressionUtil::newConstant(n + offset)});
Expression val(Expression::Type::CONSTANT, "", state.get(cell));
formula.entries[func] = val;
Log::get().debug("Added intial term: " + func.toString() + " = " +
Expand Down
2 changes: 1 addition & 1 deletion src/form/formula_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FormulaGenerator {
void prepareForPostLoop(int64_t numCells,
const std::map<int64_t, Expression>& preloopExprs);

bool addInitialTerms(int64_t numCells,
bool addInitialTerms(int64_t numCells, int64_t offset,
const std::map<std::string, int64_t>& numTerms);

std::string newName();
Expand Down

0 comments on commit caf6f66

Please sign in to comment.