From a2f435bed347b43a9efc2bf239a71143d4d07bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 23 Aug 2019 16:53:50 +0200 Subject: [PATCH] [poincare/inv_norm] Don't simplify if variance is not checked to be >0 --- poincare/src/inv_norm.cpp | 35 ++++++++++++++++++++++++++++---- poincare/test/simplification.cpp | 3 +++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/poincare/src/inv_norm.cpp b/poincare/src/inv_norm.cpp index 447645efd..95370a0c9 100644 --- a/poincare/src/inv_norm.cpp +++ b/poincare/src/inv_norm.cpp @@ -50,16 +50,43 @@ Expression InvNorm::shallowReduce(ExpressionNode::ReductionContext reductionCont } Expression c0 = childAtIndex(0); Expression c1 = childAtIndex(1); + Expression c2 = childAtIndex(2); + Context * context = reductionContext.context(); + + if (c0.deepIsMatrix(context) || c1.deepIsMatrix(context) || c2.deepIsMatrix(context)) { + return replaceWithUndefinedInPlace(); + } + + if (!c1.isReal(context) || !c2.isReal(context)) { + // If we cannot check that mu and variance are real, return + return *this; + } + { - Context * context = reductionContext.context(); - Expression c2 = childAtIndex(2); - if (c0.deepIsMatrix(context) || c1.deepIsMatrix(context) || c2.deepIsMatrix(context)) { + ExpressionNode::Sign s = c2.sign(context); + if (s == ExpressionNode::Sign::Negative) { return replaceWithUndefinedInPlace(); } - if (c0.type() != ExpressionNode::Type::Rational) { + // If we cannot check that the variance is positive, return + if (s != ExpressionNode::Sign::Positive) { return *this; } } + + // If we cannot check that the variance is not null, return + if (c2.type() != ExpressionNode::Type::Rational) { + return *this; + } + { + Rational r2 = static_cast(c2); + if (r2.isZero()) { + return replaceWithUndefinedInPlace(); + } + } + + if (c0.type() != ExpressionNode::Type::Rational) { + return *this; + } // Undef if x < 0 or x > 1 Rational r0 = static_cast(c0); if (r0.isNegative()) { diff --git a/poincare/test/simplification.cpp b/poincare/test/simplification.cpp index e7dc628a4..cec1614a6 100644 --- a/poincare/test/simplification.cpp +++ b/poincare/test/simplification.cpp @@ -1066,4 +1066,7 @@ QUIZ_CASE(poincare_probabolity) { assert_parsed_expression_simplify_to("invnorm(0.5,2,3)", "2"); assert_parsed_expression_simplify_to("invnorm(1,2,3)", "inf"); assert_parsed_expression_simplify_to("invnorm(1.3,2,3)", "undef"); + assert_parsed_expression_simplify_to("invnorm(3/4,2,random())", "invnorm(3/4,2,random())"); // random can be 0 + assert_parsed_expression_simplify_to("invnorm(0.5,2,0)", Undefined::Name()); + assert_parsed_expression_simplify_to("invnorm(0.5,2,-1)", Undefined::Name()); }