#include #include #include #include #include #include #include #include extern "C" { #include } #include namespace Poincare { Expression::Type PredictionInterval::type() const { return Type::PredictionInterval; } Expression * PredictionInterval::clone() const { PredictionInterval * a = new PredictionInterval(m_operands, true); return a; } Expression * PredictionInterval::shallowReduce(Context& context, AngleUnit angleUnit) { Expression * e = Expression::shallowReduce(context, angleUnit); if (e != this) { return e; } Expression * op0 = editableOperand(0); Expression * op1 = editableOperand(1); #if MATRIX_EXACT_REDUCING if (op0->type() == Type::Matrix || op1->type() == Type::Matrix) { return replaceWith(new Undefined(), true); } #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 (op1->type() == Type::Rational) { Rational * r1 = static_cast(op1); if (!r1->denominator().isOne() || r1->numerator().isNegative()) { return replaceWith(new Undefined(), true); } } if (op0->type() != Type::Rational || op1->type() != 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); } 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()))); // 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 Expression * PredictionInterval::templatedApproximate(Context& context, AngleUnit angleUnit) const { Expression * pInput = operand(0)->approximate(context, angleUnit); Expression * nInput = operand(1)->approximate(context, angleUnit); if (pInput->type() != Type::Complex || nInput->type() != Type::Complex) { return new Complex(Complex::Float(NAN)); } 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::Float(NAN)); } Expression * operands[2]; operands[0] = new Complex(Complex::Float(p - 1.96*std::sqrt(p*(1.0-p))/std::sqrt(n))); operands[1] = new Complex(Complex::Float(p + 1.96*std::sqrt(p*(1.0-p))/std::sqrt(n))); return new Matrix(operands, 1, 2, false); } }