diff --git a/poincare/Makefile b/poincare/Makefile index 1ab5cd34c..4e4d65d2e 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -98,6 +98,7 @@ objs += $(addprefix poincare/src/,\ parenthesis.o\ power.o\ print_float.o\ + prediction_interval.o\ preferences.o\ product.o\ randint.o\ diff --git a/poincare/include/poincare.h b/poincare/include/poincare.h index 0cb8bc8c5..b0f13d4a3 100644 --- a/poincare/include/poincare.h +++ b/poincare/include/poincare.h @@ -167,6 +167,7 @@ #include #include #include +#include #include #include #include diff --git a/poincare/include/poincare/prediction_interval.h b/poincare/include/poincare/prediction_interval.h index f8b44de9e..1cde5a3d1 100644 --- a/poincare/include/poincare/prediction_interval.h +++ b/poincare/include/poincare/prediction_interval.h @@ -2,32 +2,59 @@ #define POINCARE_PREDICTION_INTERVAL_H #include -#include +#include +#include namespace Poincare { -class PredictionInterval : public StaticHierarchy<2> { - using StaticHierarchy<2>::StaticHierarchy; +class PredictionIntervalNode : public ExpressionNode { public: - Type type() const override; - int polynomialDegree(char symbolName) const override; -private: - /* Layout */ - LayoutRef createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override { - return LayoutHelper::Prefix(this, floatDisplayMode, numberOfSignificantDigits, name()); + static PredictionIntervalNode * FailedAllocationStaticNode(); + PredictionIntervalNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); } + + // TreeNode + size_t size() const override { return sizeof(PredictionIntervalNode); } + int numberOfChildren() const override { return 2; } +#if POINCARE_TREE_LOG + virtual void logNodeName(std::ostream & stream) const override { + stream << "PredictionInterval"; } +#endif + + // ExpressionNode + + // Properties + Type type() const override { return Type::PredictionInterval; } + int polynomialDegree(char symbolName) const override { return -1; } +private: + // Layout + LayoutReference createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override; int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override { return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, name()); } const char * name() const { return "prediction95"; } - /* Simplification */ + // Simplification Expression shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const override; - /* Evaluation */ - Evaluation approximate(Expression::SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return templatedApproximate(context, angleUnit); } - Evaluation * approximate(Expression::DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return templatedApproximate(context, angleUnit); } + // Evaluation + Evaluation approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return templatedApproximate(context, angleUnit); } + Evaluation approximate(DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { return templatedApproximate(context, angleUnit); } template Evaluation templatedApproximate(Context& context, Preferences::AngleUnit angleUnit) const; }; + +class PredictionInterval : public Expression { +public: + PredictionInterval() : Expression(TreePool::sharedPool()->createTreeNode()) {} + PredictionInterval(const PredictionIntervalNode * n) : Expression(n) {} + PredictionInterval(Expression child1, Expression child2) : PredictionInterval() { + replaceChildAtIndexInPlace(0, child1); + replaceChildAtIndexInPlace(1, child2); + } + + // Expression + Expression shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const; +}; + } #endif diff --git a/poincare/src/confidence_interval.cpp b/poincare/src/confidence_interval.cpp index 61a8ed803..83ef42645 100644 --- a/poincare/src/confidence_interval.cpp +++ b/poincare/src/confidence_interval.cpp @@ -33,8 +33,8 @@ template Evaluation ConfidenceIntervalNode::templatedApproximate(Context& context, Preferences::AngleUnit angleUnit) const { Evaluation fInput = childAtIndex(0)->approximate(T(), context, angleUnit); Evaluation nInput = childAtIndex(1)->approximate(T(), context, angleUnit); - T f = static_cast >(fInput).toScalar(); - T n = static_cast >(nInput).toScalar(); + T f = static_cast &>(fInput).toScalar(); + T n = static_cast &>(nInput).toScalar(); if (std::isnan(f) || std::isnan(n) || n != (int)n || n < 0 || f < 0 || f > 1) { return Complex::Undefined(); } @@ -63,13 +63,13 @@ Expression ConfidenceInterval::shallowReduce(Context& context, Preferences::Angl } #endif if (c0.type() == ExpressionNode::Type::Rational) { - Rational r0 = static_cast(c0); + Rational r0 = static_cast(c0); if (r0.signedIntegerNumerator().isNegative() || Integer::NaturalOrder(r0.signedIntegerNumerator(), r0.integerDenominator()) > 0) { return Undefined(); } } if (c1.type() == ExpressionNode::Type::Rational) { - Rational r1 = static_cast(c1); + Rational r1 = static_cast(c1); if (!r1.integerDenominator().isOne() || r1.signedIntegerNumerator().isNegative()) { return Undefined(); } @@ -77,8 +77,8 @@ Expression ConfidenceInterval::shallowReduce(Context& context, Preferences::Angl if (c0.type() != ExpressionNode::Type::Rational || c1.type() != ExpressionNode::Type::Rational) { return *this; } - Rational r0 = static_cast(c0); - Rational r1 = static_cast(c1); + Rational r0 = static_cast(c0); + Rational r1 = static_cast(c1); // Compute [r0-1/sqr(r1), r0+1/sqr(r1)] Expression sqr = Power(r1, Rational(-1, 2)); Matrix matrix; diff --git a/poincare/src/expression_lexer.l b/poincare/src/expression_lexer.l index e5ab1686f..b13ff0d04 100644 --- a/poincare/src/expression_lexer.l +++ b/poincare/src/expression_lexer.l @@ -140,8 +140,8 @@ lcm { poincare_expression_yylval.expression = LeastCommonMultiple(); return FUNC ln { poincare_expression_yylval.expression = NaperianLogarithm(); return FUNCTION; } log { return LOGFUNCTION; } /*permute { poincare_expression_yylval.expression = new PermuteCoefficient(); return FUNCTION; } -prediction95 { poincare_expression_yylval.expression = new PredictionInterval(); return FUNCTION; } */ +prediction95 { poincare_expression_yylval.expression = PredictionInterval(); return FUNCTION; } prediction { poincare_expression_yylval.expression = SimplePredictionInterval(); return FUNCTION; } product { poincare_expression_yylval.expression = Product(); return FUNCTION; } quo { poincare_expression_yylval.expression = DivisionQuotient(); return FUNCTION; } diff --git a/poincare/src/prediction_interval.cpp b/poincare/src/prediction_interval.cpp index b279f098d..58e9c3de0 100644 --- a/poincare/src/prediction_interval.cpp +++ b/poincare/src/prediction_interval.cpp @@ -12,17 +12,33 @@ extern "C" { namespace Poincare { -ExpressionNode::Type PredictionInterval::type() const { - return Type::PredictionInterval; +PredictionIntervalNode * PredictionIntervalNode::FailedAllocationStaticNode() { + static AllocationFailureExpressionNode failure; + TreePool::sharedPool()->registerStaticNodeIfRequired(&failure); + return &failure; } -Expression * PredictionInterval::clone() const { - PredictionInterval * a = new PredictionInterval(m_operands, true); - return a; +LayoutReference PredictionIntervalNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { + return LayoutHelper::Prefix(PredictionInterval(this), floatDisplayMode, numberOfSignificantDigits, name()); } -int PredictionInterval::polynomialDegree(char symbolName) const { - return -1; +Expression PredictionIntervalNode::shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const { + return PredictionInterval(this).shallowReduce(context, angleUnit); +} + +template +Evaluation PredictionIntervalNode::templatedApproximate(Context& context, Preferences::AngleUnit angleUnit) const { + Evaluation pInput = childAtIndex(0)->approximate(T(), context, angleUnit); + Evaluation nInput = childAtIndex(1)->approximate(T(), context, angleUnit); + T p = static_cast &>(pInput).toScalar(); + T n = static_cast &>(nInput).toScalar(); + if (std::isnan(p) || std::isnan(n) || n != (int)n || n < 0 || p < 0 || p > 1) { + return Complex::Undefined(); + } + std::complex operands[2]; + operands[0] = std::complex(p - 1.96*std::sqrt(p*(1.0-p))/std::sqrt(n)); + operands[1] = std::complex(p + 1.96*std::sqrt(p*(1.0-p))/std::sqrt(n)); + return MatrixComplex(operands, 1, 2); } Expression PredictionInterval::shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const { @@ -30,62 +46,44 @@ Expression PredictionInterval::shallowReduce(Context& context, Preferences::Angl if (e.isUndefinedOrAllocationFailure()) { return e; } - Expression * op0 = childAtIndex(0); - Expression * op1 = childAtIndex(1); + Expression op0 = childAtIndex(0); + Expression op1 = childAtIndex(1); #if MATRIX_EXACT_REDUCING - if (op0->type() == Type::Matrix || op1->type() == Type::Matrix) { - return replaceWith(new Undefined(), true); + if (op0.type() == ExpressionNode::Type::Matrix || op1.type() == ExpressionNode::Type::Matrix) { + return Undefined(); } #endif - if (op0->type() == Type::Rational) { - Rational * r0 = static_cast(op0); - if (r0->numerator().isNegative() || Integer::NaturalOrder(r0->numerator(), r0->denominator()) > 0) { - return replaceWith(new Undefined(), true); + if (op0.type() == ExpressionNode::Type::Rational) { + Rational r0 = static_cast(op0); + if (r0.sign() == ExpressionNode::Sign::Negative || Integer::NaturalOrder(r0.unsignedIntegerNumerator(), r0.integerDenominator()) > 0) { + return Undefined(); } } - if (op1->type() == Type::Rational) { - Rational * r1 = static_cast(op1); - if (!r1->denominator().isOne() || r1->numerator().isNegative()) { - return replaceWith(new Undefined(), true); + if (op1.type() == ExpressionNode::Type::Rational) { + Rational r1 = static_cast(op1); + if (!r1.integerDenominator().isOne() || r1.sign() == ExpressionNode::Sign::Negative) { + return Undefined(); } } - if (op0->type() != Type::Rational || op1->type() != Type::Rational) { - return this; + if (op0.type() != ExpressionNode::Type::Rational || op1.type() != ExpressionNode::Type::Rational) { + return *this; } - Rational * r0 = static_cast(op0); - Rational * r1 = static_cast(op1); - if (!r1->denominator().isOne() || r1->numerator().isNegative() || r0->numerator().isNegative() || Integer::NaturalOrder(r0->numerator(), r0->denominator()) > 0) { - return replaceWith(new Undefined(), true); + Rational r0 = static_cast(op0); + Rational r1 = static_cast(op1); + if (!r1.integerDenominator().isOne() || r1.sign() == ExpressionNode::Sign::Negative || r0.sign() == ExpressionNode::Sign::Negative || Integer::NaturalOrder(r0.unsignedIntegerNumerator(), r0.integerDenominator()) > 0) { + return Undefined(); } - detachOperand(r0); - detachOperand(r1); /* [r0-1.96*sqrt(r0*(1-r0)/r1), r0+1.96*sqrt(r0*(1-r0)/r1)]*/ // Compute numerator = r0*(1-r0) - Rational * numerator = new Rational(Rational::Multiplication(*r0, Rational(Integer::Subtraction(r0->denominator(), r0->numerator()), r0->denominator()))); + Rational numerator = Rational::Multiplication(r0, Rational(Integer::Subtraction(r0.integerDenominator(), r0.unsignedIntegerNumerator()), r0.integerDenominator())); // Compute sqr = sqrt(r0*(1-r0)/r1) - Expression * sqr = new Power(new Division(numerator, r1, false), new Rational(1, 2), false); - Expression * m = new Multiplication(new Rational(196, 100), sqr, false); - const Expression * newOperands[2] = {new Addition(r0, new Multiplication(new Rational(-1), m, false), false), new Addition(r0, m, true),}; - Expression * matrix = replaceWith(new Matrix(newOperands, 1, 2, false), true); - return matrix->deepReduce(context, angleUnit); -} - -template -Evaluation PredictionInterval::templatedApproximate(Context& context, Preferences::AngleUnit angleUnit) const { - Evaluation * pInput = childAtIndex(0)->approximate(T(), context, angleUnit); - Evaluation * nInput = childAtIndex(1)->approximate(T(), context, angleUnit); - T p = static_cast *>(pInput)->toScalar(); - T n = static_cast *>(nInput)->toScalar(); - delete pInput; - delete nInput; - if (std::isnan(p) || std::isnan(n) || n != (int)n || n < 0 || p < 0 || p > 1) { - return new Complex(Complex::Undefined()); - } - std::complex operands[2]; - operands[0] = std::complex(p - 1.96*std::sqrt(p*(1.0-p))/std::sqrt(n)); - operands[1] = std::complex(p + 1.96*std::sqrt(p*(1.0-p))/std::sqrt(n)); - return new MatrixComplex(operands, 1, 2); + Expression sqr = Power(Division(numerator, r1), Rational(1, 2)); + Expression m = Multiplication(Rational(196, 100), sqr); + Matrix matrix; + matrix.addChildAtIndexInPlace(Addition(r0, Multiplication(Rational(-1), m)), 0, 0); + matrix.addChildAtIndexInPlace(Addition(r0, m), 1, 1); + matrix.setDimensions(1, 2); + return matrix.deepReduce(context, angleUnit); } } - diff --git a/poincare/test/function.cpp b/poincare/test/function.cpp index 3407c6014..b07bebb38 100644 --- a/poincare/test/function.cpp +++ b/poincare/test/function.cpp @@ -43,9 +43,7 @@ QUIZ_CASE(poincare_parse_function) { assert_parsed_expression_type("permute(10, 4)", ExpressionNode::Type::PermuteCoefficient); #endif assert_parsed_expression_type("prediction(0.1, 100)", ExpressionNode::Type::ConfidenceInterval); -#if 0 assert_parsed_expression_type("prediction95(0.1, 100)", ExpressionNode::Type::PredictionInterval); -#endif assert_parsed_expression_type("product(n, 4, 10)", ExpressionNode::Type::Product); assert_parsed_expression_type("quo(29, 10)", ExpressionNode::Type::DivisionQuotient); @@ -174,11 +172,10 @@ QUIZ_CASE(poincare_function_evaluate) { assert_parsed_expression_evaluates_to("prediction(0.1, 100)", "[[0,0.2]]"); assert_parsed_expression_evaluates_to("prediction(0.1, 100)", "[[0,0.2]]"); -#if 0 + assert_parsed_expression_evaluates_to("prediction95(0.1, 100)", "[[0.0412,0.1588]]"); assert_parsed_expression_evaluates_to("prediction95(0.1, 100)", "[[0.0412,0.1588]]"); -#endif assert_parsed_expression_evaluates_to("product(2+n*I, 1, 5)", "(-100)-540*I"); assert_parsed_expression_evaluates_to("product(2+n*I, 1, 5)", "(-100)-540*I");