Numworks Epsilon  1.4.1
Graphing Calculator Operating System
law.cpp
Go to the documentation of this file.
1 #include "law.h"
2 #include <cmath>
3 #include <float.h>
4 
5 namespace Probability {
6 
8  Shared::CurveViewRange()
9 {
10 }
11 
12 float Law::xGridUnit() {
13  return computeGridUnit(Axis::X, xMin(), xMax());
14 }
15 
17  if (!isContinuous()) {
18  int end = std::round(x);
19  double result = 0.0;
20  for (int k = 0; k <=end; k++) {
21  result += evaluateAtDiscreteAbscissa(k);
22  /* Avoid too long loop */
23  if (k > k_maxNumberOfOperations) {
24  break;
25  }
26  if (result >= k_maxProbability) {
27  result = 1.0;
28  break;
29  }
30 
31  }
32  return result;
33  }
34  return 0.0;
35 }
36 
37 double Law::rightIntegralFromAbscissa(double x) const {
38  if (isContinuous()) {
40  }
41  return 1.0 - cumulativeDistributiveFunctionAtAbscissa(x-1.0);
42 }
43 
44 double Law::finiteIntegralBetweenAbscissas(double a, double b) const {
45  if (b < a) {
46  return 0.0;
47  }
48  if (isContinuous()) {
50  }
51  int start = std::round(a);
52  int end = std::round(b);
53  double result = 0.0;
54  for (int k = start; k <=end; k++) {
55  result += evaluateAtDiscreteAbscissa(k);
56  /* Avoid too long loop */
58  break;
59  }
60  if (result >= k_maxProbability) {
61  result = 1.0;
62  break;
63  }
64  }
65  return result;
66 }
67 
69  if (*probability >= 1.0) {
70  return INFINITY;
71  }
72  if (isContinuous()) {
73  return 0.0;
74  }
75  if (*probability <= 0.0) {
76  return 0.0;
77  }
78  double p = 0.0;
79  int k = 0;
80  double delta = 0.0;
81  do {
82  delta = std::fabs(*probability-p);
84  if (p >= k_maxProbability && std::fabs(*probability-1.0) <= delta) {
85  *probability = 1.0;
86  return k-1.0;
87  }
88  } while (std::fabs(*probability-p) <= delta && k < k_maxNumberOfOperations && p < 1.0);
90  if (k == k_maxNumberOfOperations) {
91  *probability = 1.0;
92  return INFINITY;
93  }
94  *probability = p;
95  if (std::isnan(*probability)) {
96  return NAN;
97  }
98  return k-1.0;
99 }
100 
101 double Law::rightIntegralInverseForProbability(double * probability) {
102  if (isContinuous()) {
103  double f = 1.0 - *probability;
105  }
106  if (*probability >= 1.0) {
107  return 0.0;
108  }
109  if (*probability <= 0.0) {
110  return INFINITY;
111  }
112  double p = 0.0;
113  int k = 0;
114  double delta = 0.0;
115  do {
116  delta = std::fabs(1.0-*probability-p);
117  p += evaluateAtDiscreteAbscissa(k++);
118  if (p >= k_maxProbability && std::fabs(1.0-*probability-p) <= delta) {
119  *probability = 0.0;
120  return k;
121  }
122  } while (std::fabs(1.0-*probability-p) <= delta && k < k_maxNumberOfOperations);
123  if (k == k_maxNumberOfOperations) {
124  *probability = 1.0;
125  return INFINITY;
126  }
127  *probability = 1.0 - (p - evaluateAtDiscreteAbscissa(k-1));
128  if (std::isnan(*probability)) {
129  return NAN;
130  }
131  return k-1.0;
132 }
133 
134 double Law::evaluateAtDiscreteAbscissa(int k) const {
135  return 0.0;
136 }
137 
138 }
virtual bool isContinuous() const =0
#define NAN
Definition: math.h:30
double finiteIntegralBetweenAbscissas(double a, double b) const
Definition: law.cpp:44
virtual double rightIntegralInverseForProbability(double *probability)
Definition: law.cpp:101
virtual float xMax()=0
#define fabs(x)
Definition: math.h:178
virtual double cumulativeDistributiveInverseForProbability(double *probability)
Definition: law.cpp:68
#define round(x)
Definition: math.h:192
static constexpr double k_maxProbability
Definition: law.h:43
#define isnan(x)
Definition: math.h:43
#define INFINITY
Definition: math.h:29
void start()
Definition: rt0.cpp:31
virtual double cumulativeDistributiveFunctionAtAbscissa(double x) const
Definition: law.cpp:16
double rightIntegralFromAbscissa(double x) const
Definition: law.cpp:37
virtual double evaluateAtDiscreteAbscissa(int k) const
Definition: law.cpp:134
float computeGridUnit(Axis axis, float min, float max)
float xGridUnit() override
Definition: law.cpp:12
static constexpr int k_maxNumberOfOperations
Definition: law.h:40
virtual float xMin()=0