Skip to content

Commit

Permalink
improve detection of negative terms
Browse files Browse the repository at this point in the history
  • Loading branch information
ckrause committed Jan 7, 2024
1 parent e202e61 commit 700715f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ To install or update LODA, please follow the [installation instructions](https:/

## [Unreleased]

### Enhancements

* Simplify generated formulas

## v24.1.7

### Bugfixes
Expand Down
13 changes: 9 additions & 4 deletions src/form/expression_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,20 @@ bool ExpressionUtil::canBeNegative(const Expression& e) {
std::any_of(e.children.begin(), e.children.end(),
[](const Expression& c) { return !canBeNegative(c); })) {
return false;
} else if (e.name == "binomial") {
} else if (e.name == "binomial" || e.name == "floor" ||
e.name == "truncate") {
break; // infer from children
} else {
return true; // unknown function
}
case Expression::Type::POWER:
if (e.children.size() == 2 &&
e.children[1].type == Expression::Type::CONSTANT) {
return e.children[1].value.odd();
if (e.children.size() == 2) {
if (e.children[0].type == Expression::Type::CONSTANT) {
return e.children[0].value < Number::ZERO;
}
if (e.children[1].type == Expression::Type::CONSTANT) {
return e.children[1].value.odd();
}
}
case Expression::Type::SUM:
case Expression::Type::PRODUCT:
Expand Down
2 changes: 1 addition & 1 deletion tests/formula/program-formula.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A000073: a(n) = a(n-1)+a(n-2)+a(n-3), a(2) = 1, a(1) = 0, a(0) = 0
A000096: a(n) = binomial(n+2,2)-1
A000142: a(n) = n*a(n-1), a(0) = 1
A000165: a(n) = b(2*n), b(n) = n*b(n-2), b(1) = 1, b(0) = 1
A000168: a(n) = truncate((2*A151383(n))/(n+2)), A151383(n) = truncate((floor(binomial(2*n,n)/(n+1))*3^(n+1))/3)
A000168: a(n) = truncate((2*A151383(n))/(n+2)), A151383(n) = floor((floor(binomial(2*n,n)/(n+1))*3^(n+1))/3)
A000212: a(n) = floor((n^2)/3)
A000276: a(n) = truncate((2*c(n+1)*(n+3))/2), b(n) = b(n-1)*(n+1), b(2) = 6, b(1) = 2, b(0) = 1, c(n) = c(n-1)*(n+1)+b(n-1), c(2) = 5, c(1) = 1, c(0) = 0
A000278: a(n) = (-a(n-2))^2+a(n-1), a(1) = 1, a(0) = 0
Expand Down

0 comments on commit 700715f

Please sign in to comment.