Numworks Epsilon  1.4.1
Graphing Calculator Operating System
confidence_interval.cpp
Go to the documentation of this file.
2 #include <poincare/matrix.h>
3 #include <poincare/complex.h>
4 #include <poincare/addition.h>
6 #include <poincare/power.h>
7 #include <poincare/undefined.h>
8 extern "C" {
9 #include <assert.h>
10 }
11 #include <cmath>
12 
13 namespace Poincare {
14 
17 }
18 
21  return a;
22 }
23 
24 int ConfidenceInterval::polynomialDegree(char symbolName) const {
25  return -1;
26 }
27 
28 Expression * ConfidenceInterval::shallowReduce(Context& context, AngleUnit angleUnit) {
29  Expression * e = Expression::shallowReduce(context, angleUnit);
30  if (e != this) {
31  return e;
32  }
33  Expression * op0 = editableOperand(0);
34  Expression * op1 = editableOperand(1);
35 #if MATRIX_EXACT_REDUCING
36  if (op0->type() == Type::Matrix || op1->type() == Type::Matrix) {
37  return replaceWith(new Undefined(), true);
38  }
39 #endif
40  if (op0->type() == Type::Rational) {
41  Rational * r0 = static_cast<Rational *>(op0);
42  if (r0->numerator().isNegative() || Integer::NaturalOrder(r0->numerator(), r0->denominator()) > 0) {
43  return replaceWith(new Undefined(), true);
44  }
45  }
46  if (op1->type() == Type::Rational) {
47  Rational * r1 = static_cast<Rational *>(op1);
48  if (!r1->denominator().isOne() || r1->numerator().isNegative()) {
49  return replaceWith(new Undefined(), true);
50  }
51  }
52  if (op0->type() != Type::Rational || op1->type() != Type::Rational) {
53  return this;
54  }
55  Rational * r0 = static_cast<Rational *>(op0);
56  Rational * r1 = static_cast<Rational *>(op1);
58  // Compute [r0-1/sqr(r1), r0+1/sqr(r1)]
59  Expression * sqr = new Power(r1, new Rational(-1, 2), false);
60  const Expression * newOperands[2] = {new Addition(r0, new Multiplication(new Rational(-1), sqr, false), false), new Addition(r0, sqr, true)};
61  Expression * matrix = replaceWith(new Matrix(newOperands, 1, 2, false), true);
62  return matrix->deepReduce(context, angleUnit);
63 }
64 
65 template<typename T>
66 Expression * ConfidenceInterval::templatedApproximate(Context& context, AngleUnit angleUnit) const {
67  Expression * fInput = operand(0)->approximate<T>(context, angleUnit);
68  Expression * nInput = operand(1)->approximate<T>(context, angleUnit);
69  if (fInput->type() != Type::Complex || nInput->type() != Type::Complex) {
70  return new Complex<T>(Complex<T>::Float(NAN));
71  }
72  T f = static_cast<Complex<T> *>(fInput)->toScalar();
73  T n = static_cast<Complex<T> *>(nInput)->toScalar();
74  delete fInput;
75  delete nInput;
76  if (std::isnan(f) || std::isnan(n) || n != (int)n || n < 0 || f < 0 || f > 1) {
77  return new Complex<T>(Complex<T>::Float(NAN));
78  }
79  Expression * operands[2];
80  operands[0] = new Complex<T>(Complex<T>::Float(f - 1/std::sqrt(n)));
81  operands[1] = new Complex<T>(Complex<T>::Float(f + 1/std::sqrt(n)));
82  return new Matrix(operands, 1, 2, false);
83 }
84 
85 }
86 
friend class Addition
Definition: expression.h:22
#define NAN
Definition: math.h:30
int polynomialDegree(char symbolName) const override
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
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
friend class ConfidenceInterval
Definition: expression.h:37
friend class Matrix
Definition: expression.h:73
const Expression * operand(int i) const
Definition: expression.cpp:78
#define sqrt(x)
Definition: math.h:196
friend class Undefined
Definition: expression.h:17
Expression * clone() const override