Numworks Epsilon  1.4.1
Graphing Calculator Operating System
prediction_interval.cpp
Go to the documentation of this file.
2 #include <poincare/matrix.h>
3 #include <poincare/addition.h>
5 #include <poincare/power.h>
6 #include <poincare/undefined.h>
7 #include <poincare/division.h>
8 #include <poincare/complex.h>
9 extern "C" {
10 #include <assert.h>
11 }
12 #include <cmath>
13 
14 namespace Poincare {
15 
18 }
19 
22  return a;
23 }
24 
25 int PredictionInterval::polynomialDegree(char symbolName) const {
26  return -1;
27 }
28 
29 Expression * PredictionInterval::shallowReduce(Context& context, AngleUnit angleUnit) {
30  Expression * e = Expression::shallowReduce(context, angleUnit);
31  if (e != this) {
32  return e;
33  }
34  Expression * op0 = editableOperand(0);
35  Expression * op1 = editableOperand(1);
36 #if MATRIX_EXACT_REDUCING
37  if (op0->type() == Type::Matrix || op1->type() == Type::Matrix) {
38  return replaceWith(new Undefined(), true);
39  }
40 #endif
41  if (op0->type() == Type::Rational) {
42  Rational * r0 = static_cast<Rational *>(op0);
43  if (r0->numerator().isNegative() || Integer::NaturalOrder(r0->numerator(), r0->denominator()) > 0) {
44  return replaceWith(new Undefined(), true);
45  }
46  }
47  if (op1->type() == Type::Rational) {
48  Rational * r1 = static_cast<Rational *>(op1);
49  if (!r1->denominator().isOne() || r1->numerator().isNegative()) {
50  return replaceWith(new Undefined(), true);
51  }
52  }
53  if (op0->type() != Type::Rational || op1->type() != Type::Rational) {
54  return this;
55  }
56  Rational * r0 = static_cast<Rational *>(op0);
57  Rational * r1 = static_cast<Rational *>(op1);
58  if (!r1->denominator().isOne() || r1->numerator().isNegative() || r0->numerator().isNegative() || Integer::NaturalOrder(r0->numerator(), r0->denominator()) > 0) {
59  return replaceWith(new Undefined(), true);
60  }
61  detachOperand(r0);
62  detachOperand(r1);
63  /* [r0-1.96*sqrt(r0*(1-r0)/r1), r0+1.96*sqrt(r0*(1-r0)/r1)]*/
64  // Compute numerator = r0*(1-r0)
65  Rational * numerator = new Rational(Rational::Multiplication(*r0, Rational(Integer::Subtraction(r0->denominator(), r0->numerator()), r0->denominator())));
66  // Compute sqr = sqrt(r0*(1-r0)/r1)
67  Expression * sqr = new Power(new Division(numerator, r1, false), new Rational(1, 2), false);
68  Expression * m = new Multiplication(new Rational(196, 100), sqr, false);
69  const Expression * newOperands[2] = {new Addition(r0, new Multiplication(new Rational(-1), m, false), false), new Addition(r0, m, true),};
70  Expression * matrix = replaceWith(new Matrix(newOperands, 1, 2, false), true);
71  return matrix->deepReduce(context, angleUnit);
72 }
73 
74 template<typename T>
75 Expression * PredictionInterval::templatedApproximate(Context& context, AngleUnit angleUnit) const {
76  Expression * pInput = operand(0)->approximate<T>(context, angleUnit);
77  Expression * nInput = operand(1)->approximate<T>(context, angleUnit);
78  if (pInput->type() != Type::Complex || nInput->type() != Type::Complex) {
79  return new Complex<T>(Complex<T>::Float(NAN));
80  }
81  T p = static_cast<Complex<T> *>(pInput)->toScalar();
82  T n = static_cast<Complex<T> *>(nInput)->toScalar();
83  delete pInput;
84  delete nInput;
85  if (std::isnan(p) || std::isnan(n) || n != (int)n || n < 0 || p < 0 || p > 1) {
86  return new Complex<T>(Complex<T>::Float(NAN));
87  }
88  Expression * operands[2];
89  operands[0] = new Complex<T>(Complex<T>::Float(p - 1.96*std::sqrt(p*(1.0-p))/std::sqrt(n)));
90  operands[1] = new Complex<T>(Complex<T>::Float(p + 1.96*std::sqrt(p*(1.0-p))/std::sqrt(n)));
91  return new Matrix(operands, 1, 2, false);
92 }
93 
94 }
95 
friend class Addition
Definition: expression.h:22
friend class Division
Definition: expression.h:25
#define NAN
Definition: math.h:30
friend class Multiplication
Definition: rational.h:12
Expression * replaceWith(Expression *newOperand, bool deleteAfterReplace=true)
Definition: expression.cpp:85
Expression * approximate(Context &context, AngleUnit angleUnit=AngleUnit::Default) const
Definition: expression.cpp:338
#define T(x)
Definition: events.cpp:26
const Expression *const * operands() const override
int polynomialDegree(char symbolName) const override
Expression * editableOperand(int i)
Definition: expression.h:176
friend class Rational
Definition: expression.h:18
friend class Power
Definition: expression.h:21
const Expression * m_operands[T]
friend class Multiplication
Definition: expression.h:20
static int NaturalOrder(const Integer &i, const Integer &j)
Definition: integer.cpp:212
#define isnan(x)
Definition: math.h:43
static Complex< T > Float(T x)
Definition: complex.cpp:23
static Integer Subtraction(const Integer &i, const Integer &j)
Definition: integer.cpp:236
friend class Matrix
Definition: expression.h:73
void detachOperand(const Expression *e)
Definition: expression.cpp:115
friend class PredictionInterval
Definition: expression.h:65
Expression * clone() const override
static Integer denominator(Integer *exponent)
Definition: integer.cpp:117
const Expression * operand(int i) const
Definition: expression.cpp:78
#define sqrt(x)
Definition: math.h:196
friend class Undefined
Definition: expression.h:17