Files
Upsilon/poincare/src/parametered_expression_helper.cpp
Léa Saviot f6a5759a16 [poincare] Fix the replacement of unknowns in the storage
In the Graph app, define f(x) = cos(x) and g(x)=diff(f(x),x,x).
The graph and table are perfectly computed, but in the Calculation apps,
g(5) fails.
2018-12-19 09:40:37 +01:00

34 lines
1.1 KiB
C++

#include <poincare/parametered_expression_helper.h>
#include <poincare/symbol.h>
#include <string.h>
#include <assert.h>
namespace Poincare {
Expression ParameteredExpressionHelper::ReplaceUnknownInExpression(Expression e, const Symbol & symbolToReplace) {
assert(!e.isUninitialized());
assert(e.type() == ExpressionNode::Type::Integral
|| e.type() == ExpressionNode::Type::Derivative
|| e.type() == ExpressionNode::Type::Sum
|| e.type() == ExpressionNode::Type::Product);
assert(!symbolToReplace.isUninitialized());
assert(symbolToReplace.type() == ExpressionNode::Type::Symbol);
int numberOfChildren = e.numberOfChildren();
assert(numberOfChildren > 2);
Expression child1 = e.childAtIndex(1);
assert(child1.type() == ExpressionNode::Type::Symbol);
Symbol& parameterChild = static_cast<Symbol &>(child1);
if (strcmp(parameterChild.name(), symbolToReplace.name()) != 0) {
return e.defaultReplaceUnknown(symbolToReplace);
}
for (int i = 2; i < numberOfChildren; i++) {
e.childAtIndex(i).replaceUnknown(symbolToReplace);
}
return e;
}
}