[poincare] Reducing an Expression which hasUnit in its children returns Undefined in general

This commit is contained in:
Ruben Dashyan
2020-02-05 16:36:38 +01:00
committed by Léa Saviot
parent f465637c86
commit cc280373d0
45 changed files with 56 additions and 0 deletions

View File

@@ -392,6 +392,7 @@ private:
}
void defaultDeepReduceChildren(ExpressionNode::ReductionContext reductionContext);
Expression defaultShallowReduce();
Expression defaultHandleUnitsInChildren(); // Children must be reduced
Expression shallowReduceUsingApproximation(ExpressionNode::ReductionContext reductionContext);
Expression defaultShallowBeautify() { return *this; }

View File

@@ -37,6 +37,7 @@ Expression AbsoluteValueNode::shallowReduce(ReductionContext reductionContext) {
Expression AbsoluteValue::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
Expression e = Expression::defaultShallowReduce();
e = e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -52,6 +52,7 @@ Complex<T> ArcCosineNode::computeOnComplex(const std::complex<T> c, Preferences:
Expression ArcCosine::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e = e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -52,6 +52,7 @@ Complex<T> ArcSineNode::computeOnComplex(const std::complex<T> c, Preferences::C
Expression ArcSine::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e = e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -51,6 +51,7 @@ Expression ArcTangentNode::shallowReduce(ReductionContext reductionContext) {
Expression ArcTangent::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e = e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -58,6 +58,7 @@ T BinomialCoefficientNode::compute(T k, T n) {
Expression BinomialCoefficient::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e = e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -14,6 +14,7 @@ Expression BinomialDistributionFunction::shallowReduce(Context * context, bool *
}
{
Expression e = Expression::defaultShallowReduce();
e = e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -39,6 +39,7 @@ Expression CeilingNode::shallowReduce(ReductionContext reductionContext) {
Expression Ceiling::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e = e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -37,6 +37,7 @@ Complex<T> ComplexArgumentNode::computeOnComplex(const std::complex<T> c, Prefer
Expression ComplexArgument::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e = e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -55,6 +55,7 @@ Complex<T> ComplexCartesianNode::templatedApproximate(Context * context, Prefere
Expression ComplexCartesian::shallowReduce() {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -56,6 +56,7 @@ int SimplePredictionIntervalNode::serialize(char * buffer, int bufferSize, Prefe
Expression ConfidenceInterval::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -35,6 +35,7 @@ Complex<T> ConjugateNode::computeOnComplex(const std::complex<T> c, Preferences:
Expression Conjugate::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -37,6 +37,7 @@ Expression CosineNode::shallowReduce(ReductionContext reductionContext) {
Expression Cosine::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -154,6 +154,7 @@ T DerivativeNode::riddersApproximation(Context * context, Preferences::ComplexFo
Expression Derivative::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -38,6 +38,7 @@ Expression DeterminantNode::shallowReduce(ReductionContext reductionContext) {
Expression Determinant::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -39,6 +39,7 @@ Evaluation<T> DivisionQuotientNode::templatedApproximate(Context * context, Pref
Expression DivisionQuotient::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -40,6 +40,7 @@ Evaluation<T> DivisionRemainderNode::templatedApproximate(Context * context, Pre
Expression DivisionRemainder::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -342,6 +342,17 @@ Expression Expression::defaultShallowReduce() {
return *this;
}
Expression Expression::defaultHandleUnitsInChildren() {
// Generically, an Expression does not accept any Unit in its children.
const int childrenCount = numberOfChildren();
for (int i = 0; i < childrenCount; i++) {
if (childAtIndex(i).hasUnit()) {
return replaceWithUndefinedInPlace();
}
}
return *this;
}
Expression Expression::shallowReduceUsingApproximation(ExpressionNode::ReductionContext reductionContext) {
double approx = node()->approximate(double(), reductionContext.context(), reductionContext.complexFormat(), reductionContext.angleUnit()).toScalar();
/* If approx is capped by the largest integer such as all smaller integers can

View File

@@ -74,6 +74,7 @@ Multiplication Factor::createMultiplicationOfIntegerPrimeDecomposition(Integer i
Expression Factor::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -87,6 +87,7 @@ int FactorialNode::serialize(char * buffer, int bufferSize, Preferences::PrintFl
Expression Factorial::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -38,6 +38,7 @@ Expression FloorNode::shallowReduce(ReductionContext reductionContext) {
Expression Floor::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -35,6 +35,7 @@ Complex<T> FracPartNode::computeOnComplex(const std::complex<T> c, Preferences::
Expression FracPart::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -52,6 +52,7 @@ Evaluation<T> GreatCommonDivisorNode::templatedApproximate(Context * context, Pr
Expression GreatCommonDivisor::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -11,6 +11,7 @@ Expression HyperbolicTrigonometricFunctionNode::shallowReduce(ReductionContext r
Expression HyperbolicTrigonometricFunction::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -27,6 +27,7 @@ Expression ImaginaryPartNode::shallowReduce(ReductionContext reductionContext) {
Expression ImaginaryPart::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -216,6 +216,7 @@ Expression Integral::UntypedBuilder(Expression children) {
Expression Integral::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -56,6 +56,7 @@ Evaluation<T> LeastCommonMultipleNode::templatedApproximate(Context * context, P
Expression LeastCommonMultiple::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -107,6 +107,7 @@ void Logarithm::deepReduceChildren(ExpressionNode::ReductionContext reductionCon
Expression CommonLogarithm::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}
@@ -123,6 +124,7 @@ Expression CommonLogarithm::shallowReduce(ExpressionNode::ReductionContext reduc
Expression Logarithm::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -43,6 +43,7 @@ Evaluation<T> MatrixDimensionNode::templatedApproximate(Context * context, Prefe
Expression MatrixDimension::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -44,6 +44,7 @@ Evaluation<T> MatrixIdentityNode::templatedApproximate(Context * context, Prefer
Expression MatrixIdentity::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -45,6 +45,7 @@ Evaluation<T> MatrixInverseNode::templatedApproximate(Context * context, Prefere
Expression MatrixInverse::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -37,6 +37,7 @@ Evaluation<T> MatrixTraceNode::templatedApproximate(Context * context, Preferenc
Expression MatrixTrace::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -41,6 +41,7 @@ Evaluation<T> MatrixTransposeNode::templatedApproximate(Context * context, Prefe
Expression MatrixTranspose::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -26,6 +26,7 @@ Expression NaperianLogarithmNode::shallowReduce(ReductionContext reductionContex
Expression NaperianLogarithm::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -15,6 +15,7 @@ Expression NormalDistributionFunction::shallowReduce(Context * context, bool * s
}
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -53,6 +53,7 @@ Evaluation<T> PermuteCoefficientNode::templatedApproximate(Context * context, Pr
Expression PermuteCoefficient::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -49,6 +49,7 @@ Evaluation<T> PredictionIntervalNode::templatedApproximate(Context * context, Pr
Expression PredictionInterval::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -62,6 +62,7 @@ Expression RandintNode::shallowReduce(ReductionContext reductionContext) {
Expression Randint::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -27,6 +27,7 @@ Expression RealPartNode::shallowReduce(ReductionContext reductionContext) {
Expression RealPart::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -42,6 +42,7 @@ Evaluation<T> RoundNode::templatedApproximate(Context * context, Preferences::Co
Expression Round::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -50,6 +50,7 @@ Evaluation<T> SequenceNode::templatedApproximate(Context * context, Preferences:
Expression Sequence::shallowReduce(Context * context) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -57,6 +57,7 @@ Complex<T> SignFunctionNode::computeOnComplex(const std::complex<T> c, Preferenc
Expression SignFunction::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e = e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -38,6 +38,7 @@ Expression SineNode::shallowReduce(ReductionContext reductionContext) {
Expression Sine::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -46,6 +46,7 @@ Expression SquareRootNode::shallowReduce(ReductionContext reductionContext) {
Expression SquareRoot::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}

View File

@@ -41,6 +41,7 @@ Expression TangentNode::shallowReduce(ReductionContext reductionContext) {
Expression Tangent::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
e.defaultHandleUnitsInChildren();
if (e.isUndefined()) {
return e;
}