[poincare] Better simplification of sequences

This commit is contained in:
Laury
2022-02-24 18:48:10 +01:00
parent 23af100ed5
commit f95f9ebb2d
10 changed files with 31 additions and 21 deletions

View File

@@ -24,6 +24,7 @@ public:
// Properties
Type type() const override { return Type::Addition; }
virtual ExpressionNode::IntegerStatus integerStatus(Context * context) const override;
int polynomialDegree(Context * context, const char * symbolName) const override;
int getPolynomialCoefficients(Context * context, const char * symbolName, Expression coefficients[], ExpressionNode::SymbolicComputation symbolicComputation) const override;

View File

@@ -152,6 +152,7 @@ public:
bool isOfType(ExpressionNode::Type * types, int length) const { return node()->isOfType(types, length); }
ExpressionNode::Sign sign(Context * context) const { return node()->sign(context); }
ExpressionNode::NullStatus nullStatus(Context * context) const { return node()->nullStatus(context); }
ExpressionNode::IntegerStatus integerStatus(Context * context) const { return node()->integerStatus(context); }
bool isStrictly(ExpressionNode::Sign s, Context * context) const { return s == node()->sign(context) && node()->nullStatus(context) == ExpressionNode::NullStatus::NonNull; }
bool isUndefined() const { return node()->type() == ExpressionNode::Type::Undefined || node()->type() == ExpressionNode::Type::Unreal; }
bool isNumber() const { return node()->isNumber(); }

View File

@@ -162,7 +162,10 @@ public:
NonNull = 0,
Null = 1,
};
enum class IntegerStatus {
Unknown = -1,
Integer = 1,
};
class ComputationContext {
public:
ComputationContext(
@@ -233,6 +236,7 @@ public:
virtual Sign sign(Context * context) const { return Sign::Unknown; }
virtual NullStatus nullStatus(Context * context) const { return NullStatus::Unknown; }
virtual IntegerStatus integerStatus(Context * context) const { return IntegerStatus::Unknown; }
virtual bool isNumber() const { return false; }
virtual bool isRandom() const { return false; }
virtual bool isParameteredExpression() const { return false; }

View File

@@ -21,6 +21,7 @@ public:
// Properties
Type type() const override { return Type::Multiplication; }
virtual IntegerStatus integerStatus(Context * context) const override;
Sign sign(Context * context) const override;
int polynomialDegree(Context * context, const char * symbolName) const override;
int getPolynomialCoefficients(Context * context, const char * symbolName, Expression coefficients[], ExpressionNode::SymbolicComputation symbolicComputation) const override;

View File

@@ -18,6 +18,7 @@ public:
void setNegative(bool negative) { m_negative = negative; }
bool isInteger() const { return denominator().isOne(); }
NullStatus nullStatus(Context * context) const override { return isZero() ? NullStatus::Null : NullStatus::NonNull; }
virtual IntegerStatus integerStatus(Context * context) const { return isInteger() ? IntegerStatus::Integer : IntegerStatus::Unknown; }
// TreeNode
size_t size() const override;

View File

@@ -21,6 +21,7 @@ public:
// Expression Properties
Type type() const override { return Type::Symbol; }
virtual IntegerStatus integerStatus(Context * context) const { return context->expressionTypeForIdentifier(m_name, strlen(m_name)) == Context::SymbolAbstractType::Integer ? IntegerStatus::Integer : IntegerStatus::Unknown; }
Expression replaceSymbolWithExpression(const SymbolAbstract & symbol, const Expression & expression) override;
int polynomialDegree(Context * context, const char * symbolName) const override;
int getPolynomialCoefficients(Context * context, const char * symbolName, Expression coefficients[], ExpressionNode::SymbolicComputation symbolicComputation) const override;

View File

@@ -57,6 +57,16 @@ bool AdditionNode::derivate(ReductionContext reductionContext, Expression symbol
// Addition
ExpressionNode::IntegerStatus AdditionNode::integerStatus(Context * context) const {
int nbOfChildren = numberOfChildren();
for (int i = 0; i < nbOfChildren; i++) {
if (childAtIndex(i)->integerStatus(context) != IntegerStatus::Integer) {
return IntegerStatus::Unknown;
}
}
return IntegerStatus::Integer;
}
const Number Addition::NumeralFactor(const Expression & e) {
if (e.type() == ExpressionNode::Type::Multiplication && e.childAtIndex(0).isNumber()) {
Number result = e.childAtIndex(0).convert<Number>();

View File

@@ -42,6 +42,16 @@ ExpressionNode::Sign MultiplicationNode::sign(Context * context) const {
return (Sign)sign;
}
ExpressionNode::IntegerStatus MultiplicationNode::integerStatus(Context * context) const {
int nbOfChildren = numberOfChildren();
for (int i = 0; i < nbOfChildren; i++) {
if (childAtIndex(i)->integerStatus(context) != IntegerStatus::Integer) {
return IntegerStatus::Unknown;
}
}
return IntegerStatus::Integer;
}
int MultiplicationNode::polynomialDegree(Context * context, const char * symbolName) const {
int degree = 0;
for (ExpressionNode * c : children()) {

View File

@@ -166,21 +166,8 @@ Expression Sequence::deepReplaceReplaceableSymbols(Context * context, bool * did
Expression Sequence::replacedByDefinitionIfPossible(Context * context) {
// We try to replace the sequence by his definition ONLY if the index is an integer
bool canBeReplacedByExpression = false;
if (childAtIndex(0).type() == ExpressionNode::Type::Symbol) {
const char * symbolName = (childAtIndex(0).convert<SymbolAbstract>()).name();
if (context->expressionTypeForIdentifier(symbolName, strlen(symbolName)) == Context::SymbolAbstractType::Integer) {
canBeReplacedByExpression = true;
}
} else if (childAtIndex(0).type() == ExpressionNode::Type::Rational) {
Rational r = childAtIndex(0).convert<Rational>();
if (r.isInteger()) {
canBeReplacedByExpression = true;
}
}
if (!canBeReplacedByExpression) {
if (childAtIndex(0).integerStatus(context) != ExpressionNode::IntegerStatus::Integer) {
return Expression();
}

View File

@@ -45,12 +45,6 @@ Evaluation<T> SumAndProductNode::templatedApproximate(ApproximationContext appro
}
nContext.setApproximationForVariable<T>((T)i);
Expression child = Expression(childAtIndex(0)).clone();
if (child.type() == ExpressionNode::Type::Sequence) {
/* Since we cannot get the expression of a sequence term like we would for
* a function, we replace its potential abstract rank by the value it should
* have. We can then evaluate its value */
child.childAtIndex(0).replaceSymbolWithExpression(symbol, Float<T>::Builder(i));
}
approximationContext.setContext(&nContext);
result = evaluateWithNextTerm(T(), result, child.node()->approximate(T(), approximationContext), approximationContext.complexFormat());
if (result.isUndefined()) {