[poincare/unit_convert] Fix UnitConvert::shallowReduce

This commit is contained in:
Léa Saviot
2020-02-05 11:23:25 +01:00
parent d98eddf960
commit ecdcb100fb
4 changed files with 25 additions and 15 deletions

View File

@@ -198,7 +198,6 @@ public:
int getPolynomialReducedCoefficients(const char * symbolName, Expression coefficients[], Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit, ExpressionNode::SymbolicComputation symbolicComputation) const;
Expression replaceSymbolWithExpression(const SymbolAbstract & symbol, const Expression & expression) { return node()->replaceSymbolWithExpression(symbol, expression); }
bool beautifiedExpressionHasUnits() const { return node()->beautifiedExpressionHasUnits(); } // This must be called on a beautified expression
bool isUnitsOnly(Context * context) const;
/* Complex */
static bool EncounteredComplex();

View File

@@ -128,7 +128,7 @@ public:
ReplaceAllSymbolsWithDefinitionsOrUndefined = 0,
ReplaceAllDefinedSymbolsWithDefinition = 1,
ReplaceDefinedFunctionsWithDefinitions = 2,
ReplaceAllSymbolsWithUndefinedAndDoNotReplaceUnits = 3 // Used in Expression::isUnitsOnly
ReplaceAllSymbolsWithUndefinedAndDoNotReplaceUnits = 3 // Used in UnitConvert::shallowReduce
};
enum class Sign {
Negative = -1,

View File

@@ -453,14 +453,6 @@ int Expression::getPolynomialReducedCoefficients(const char * symbolName, Expres
return degree;
}
bool Expression::isUnitsOnly(Context * context) const {
if (type() == ExpressionNode::Type::Unit) {
return true;
}
Expression thisBeautified = clone().reduce(ExpressionNode::ReductionContext(context, Preferences::ComplexFormat::Real, Preferences::AngleUnit::Degree, ExpressionNode::ReductionTarget::SystemForApproximation, ExpressionNode::SymbolicComputation::ReplaceAllSymbolsWithUndefinedAndDoNotReplaceUnits)); // The values do not really matter except for the symbolicComputation
return thisBeautified.reducedExpressionIsUnitsOnly();
}
/* Complex */
bool Expression::EncounteredComplex() {

View File

@@ -26,11 +26,30 @@ Evaluation<T> UnitConvertNode::templatedApproximate(Context * context, Preferenc
}
Expression UnitConvert::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
// UnitConvert the expression.
Expression finalUnit = childAtIndex(1).clone();
Expression division = Division::Builder(childAtIndex(0), childAtIndex(1));
division = division.simplify(reductionContext);
if (division.beautifiedExpressionHasUnits()) {
{
Expression e = Expression::defaultShallowReduce();
if (e.isUndefined()) {
return e;
}
}
// Find the unit
ReductionContext unitReductionContext = ReductionContext(
reductionContext.context(),
reductionContext.complexFormat(),
reductionContext.angleUnit(),
reductionContext.target(),
ExpressionNode::SymbolicComputation::ReplaceAllSymbolsWithUndefinedAndDoNotReplaceUnits);
Expression finalUnit = childAtIndex(1).reduce(unitReductionContext).getUnit();
if (finalUnit.isUndefined()) {
// There is no unit on the right
return replaceWithUndefinedInPlace();
}
// Divide the left member by the new unit
Expression division = Division::Builder(childAtIndex(0), finalUnit.clone());
division = division.reduce(reductionContext);
if (division.hasUnit()) {
// The left and right members are not homogeneous
return replaceWithUndefinedInPlace();
}
double floatValue = division.approximateToScalar<double>(reductionContext.context(), reductionContext.complexFormat(), reductionContext.angleUnit());