Blame view

build5/epsilon-master/poincare/test/function.cpp 15.3 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  #include <quiz.h>
  #include <poincare.h>
  #include <cmath>
  #include <ion.h>
  #include <assert.h>
  #include "helper.h"
  
  using namespace Poincare;
  
  template<typename T>
  void assert_exp_is_bounded(Expression * exp, T lowBound, T upBound, bool upBoundIncluded = false) {
    GlobalContext globalContext;
    T result = exp->approximateToScalar<T>(globalContext, Radian);
    assert(result >= lowBound);
    assert(result < upBound || (result == upBound && upBoundIncluded));
  }
  
  QUIZ_CASE(poincare_parse_function) {
    assert_parsed_expression_type("abs(-1)", Expression::Type::AbsoluteValue);
    assert_parsed_expression_type("arg(2+I)", Expression::Type::ComplexArgument);
    assert_parsed_expression_type("binomial(10, 4)", Expression::Type::BinomialCoefficient);
    assert_parsed_expression_type("ceil(0.2)", Expression::Type::Ceiling);
    assert_parsed_expression_type("diff(2*x, 2)", Expression::Type::Derivative);
  #if MATRICES_ARE_DEFINED
    assert_parsed_expression_type("dim([[2]])", Expression::Type::MatrixDimension);
    assert_parsed_expression_type("det([[1,2,3][4,5,6][7,8,9]])", Expression::Type::Determinant);
  #endif
    assert_parsed_expression_type("confidence(0.1, 100)", Expression::Type::ConfidenceInterval);
    assert_parsed_expression_type("conj(2)", Expression::Type::Conjugate);
    assert_parsed_expression_type("factor(23/42)", Expression::Type::Factor);
    assert_parsed_expression_type("floor(2.3)", Expression::Type::Floor);
    assert_parsed_expression_type("frac(2.3)", Expression::Type::FracPart);
    assert_parsed_expression_type("gcd(2,3)", Expression::Type::GreatCommonDivisor);
    assert_parsed_expression_type("im(2+I)", Expression::Type::ImaginaryPart);
    assert_parsed_expression_type("int(x, 2, 3)", Expression::Type::Integral);
  #if MATRICES_ARE_DEFINED
    assert_parsed_expression_type("inverse([[1,2,3][4,5,6][7,8,9]])", Expression::Type::MatrixInverse);
  #endif
    assert_parsed_expression_type("lcm(2,3)", Expression::Type::LeastCommonMultiple);
    assert_parsed_expression_type("ln(2)", Expression::Type::NaperianLogarithm);
    assert_parsed_expression_type("log(2)", Expression::Type::Logarithm);
    assert_parsed_expression_type("permute(10, 4)", Expression::Type::PermuteCoefficient);
    assert_parsed_expression_type("prediction(0.1, 100)", Expression::Type::ConfidenceInterval);
    assert_parsed_expression_type("prediction95(0.1, 100)", Expression::Type::PredictionInterval);
    assert_parsed_expression_type("product(n, 4, 10)", Expression::Type::Product);
    assert_parsed_expression_type("quo(29, 10)", Expression::Type::DivisionQuotient);
    assert_parsed_expression_type("random()", Expression::Type::Random);
    assert_parsed_expression_type("randint(1, 2)", Expression::Type::Randint);
    assert_parsed_expression_type("re(2+I)", Expression::Type::RealPart);
    assert_parsed_expression_type("rem(29, 10)", Expression::Type::DivisionRemainder);
    assert_parsed_expression_type("root(2,3)", Expression::Type::NthRoot);
    assert_parsed_expression_type("R(2)", Expression::Type::SquareRoot);
    assert_parsed_expression_type("round(2,3)", Expression::Type::Round);
    assert_parsed_expression_type("sum(n, 4, 10)", Expression::Type::Sum);
  #if MATRICES_ARE_DEFINED
    assert_parsed_expression_type("trace([[1,2,3][4,5,6][7,8,9]])", Expression::Type::MatrixTrace);
    assert_parsed_expression_type("transpose([[1,2,3][4,5,6][7,8,9]])", Expression::Type::MatrixTranspose);
  #endif
    assert_parsed_expression_type("6!", Expression::Type::Factorial);
  }
  
  
  QUIZ_CASE(poincare_function_evaluate) {
    assert_parsed_expression_evaluates_to<float>("abs(-1)", "1");
    assert_parsed_expression_evaluates_to<double>("abs(-1)", "1");
  
    assert_parsed_expression_evaluates_to<float>("abs(3+2I)", "3.605551");
    assert_parsed_expression_evaluates_to<double>("abs(3+2I)", "3.605551275464");
  
    assert_parsed_expression_evaluates_to<float>("abs([[1,-2][3,-4]])", "[[1,2][3,4]]");
    assert_parsed_expression_evaluates_to<double>("abs([[1,-2][3,-4]])", "[[1,2][3,4]]");
  
    assert_parsed_expression_evaluates_to<float>("abs([[3+2I,3+4I][5+2I,3+2I]])", "[[3.605551,5][5.385165,3.605551]]");
    assert_parsed_expression_evaluates_to<double>("abs([[3+2I,3+4I][5+2I,3+2I]])", "[[3.605551275464,5][5.3851648071345,3.605551275464]]");
  
    assert_parsed_expression_evaluates_to<float>("binomial(10, 4)", "210");
    assert_parsed_expression_evaluates_to<double>("binomial(10, 4)", "210");
  
    assert_parsed_expression_evaluates_to<float>("ceil(0.2)", "1");
    assert_parsed_expression_evaluates_to<double>("ceil(0.2)", "1");
  
    assert_parsed_expression_evaluates_to<float>("diff(2*x, 2)", "2");
    assert_parsed_expression_evaluates_to<double>("diff(2*x, 2)", "2");
  
  #if MATRICES_ARE_DEFINED
    assert_parsed_expression_evaluates_to<float>("det([[1,23,3][4,5,6][7,8,9]])", "126", Degree, Cartesian, 6); // FIXME: the determinant computation is not precised enough to be displayed with 7 significant digits
    assert_parsed_expression_evaluates_to<double>("det([[1,23,3][4,5,6][7,8,9]])", "126");
    assert_parsed_expression_evaluates_to<float>("det([[I,23-2I,3*I][4+I,5*I,6][7,8*I+2,9]])", "126-231*I", Degree, Cartesian, 6); // FIXME: the determinant computation is not precised enough to be displayed with 7 significant digits
    assert_parsed_expression_evaluates_to<double>("det([[I,23-2I,3*I][4+I,5*I,6][7,8*I+2,9]])", "126-231*I");
  #endif
  
    assert_parsed_expression_evaluates_to<float>("floor(2.3)", "2");
    assert_parsed_expression_evaluates_to<double>("floor(2.3)", "2");
  
    assert_parsed_expression_evaluates_to<float>("frac(2.3)", "0.3");
    assert_parsed_expression_evaluates_to<double>("frac(2.3)", "0.3");
  
    assert_parsed_expression_evaluates_to<float>("gcd(234,394)", "2");
    assert_parsed_expression_evaluates_to<double>("gcd(234,394)", "2");
  
    assert_parsed_expression_evaluates_to<float>("im(2+3I)", "3");
    assert_parsed_expression_evaluates_to<double>("im(2+3I)", "3");
  
    assert_parsed_expression_evaluates_to<float>("int(x, 1, 2)", "1.5");
    assert_parsed_expression_evaluates_to<double>("int(x, 1, 2)", "1.5");
  
    assert_parsed_expression_evaluates_to<float>("lcm(234,394)", "46098");
    assert_parsed_expression_evaluates_to<double>("lcm(234,394)", "46098");
  
    assert_parsed_expression_evaluates_to<float>("ln(2)", "0.6931472");
    assert_parsed_expression_evaluates_to<double>("ln(2)", "6.9314718055995E-1");
  
    assert_parsed_expression_evaluates_to<float>("log(2)", "0.30103");
    assert_parsed_expression_evaluates_to<double>("log(2)", "3.0102999566398E-1");
  
    assert_parsed_expression_evaluates_to<float>("permute(10, 4)", "5040");
    assert_parsed_expression_evaluates_to<double>("permute(10, 4)", "5040");
  
    assert_parsed_expression_evaluates_to<float>("product(n, 4, 10)", "604800");
    assert_parsed_expression_evaluates_to<double>("product(n, 4, 10)", "604800");
  
    assert_parsed_expression_evaluates_to<float>("re(2+I)", "2");
    assert_parsed_expression_evaluates_to<double>("re(2+I)", "2");
  
    assert_parsed_expression_evaluates_to<float>("rem(29, 10)", "9");
    assert_parsed_expression_evaluates_to<double>("rem(29, 10)", "9");
  
    assert_parsed_expression_evaluates_to<float>("root(2,3)", "1.259921");
    assert_parsed_expression_evaluates_to<double>("root(2,3)", "1.2599210498949");
  
    assert_parsed_expression_evaluates_to<float>("R(2)", "1.414214");
    assert_parsed_expression_evaluates_to<double>("R(2)", "1.4142135623731");
  
    assert_parsed_expression_evaluates_to<float>("R(-1)", "I");
    assert_parsed_expression_evaluates_to<double>("R(-1)", "I");
  
    assert_parsed_expression_evaluates_to<float>("sum(n, 4, 10)", "49");
    assert_parsed_expression_evaluates_to<double>("sum(n, 4, 10)", "49");
  
  #if MATRICES_ARE_DEFINED
    assert_parsed_expression_evaluates_to<float>("trace([[1,2,3][4,5,6][7,8,9]])", "15");
    assert_parsed_expression_evaluates_to<double>("trace([[1,2,3][4,5,6][7,8,9]])", "15");
  #endif
  
    assert_parsed_expression_evaluates_to<float>("confidence(0.1, 100)", "[[0,0.2]]");
    assert_parsed_expression_evaluates_to<double>("confidence(0.1, 100)", "[[0,0.2]]");
  
  #if MATRICES_ARE_DEFINED
    assert_parsed_expression_evaluates_to<float>("dim([[1,2,3][4,5,-6]])", "[[2,3]]");
    assert_parsed_expression_evaluates_to<double>("dim([[1,2,3][4,5,-6]])", "[[2,3]]");
  #endif
  
    assert_parsed_expression_evaluates_to<float>("conj(3+2*I)", "3-2*I");
    assert_parsed_expression_evaluates_to<double>("conj(3+2*I)", "3-2*I");
  
  #if MATRICES_ARE_DEFINED
    assert_parsed_expression_evaluates_to<float>("inverse([[1,2,3][4,5,-6][7,8,9]])", "[[-1.2917,-0.083333,0.375][1.0833,0.16667,-0.25][0.041667,-0.083333,0.041667]]", Degree, Cartesian, 5); // inverse is not precise enough to display 7 significative digits
    assert_parsed_expression_evaluates_to<double>("inverse([[1,2,3][4,5,-6][7,8,9]])", "[[-1.2916666666667,-8.3333333333333E-2,0.375][1.0833333333333,1.6666666666667E-1,-0.25][4.1666666666667E-2,-8.3333333333333E-2,4.1666666666667E-2]]");
    assert_parsed_expression_evaluates_to<float>("inverse([[I,23-2I,3*I][4+I,5*I,6][7,8*I+2,9]])", "[[(-0.01183)-0.0455*I,(-0.5005)-0.727*I,0.3185+0.4886*I][0.04095+0.00364*I,0.04004-0.02184*I,(-0.02548)+0.0009099*I][0.003336-0.00182*I,0.3609+0.5347*I,(-0.1301)-0.3576*I]]", Degree, Cartesian, 4); // inverse is not precise enough to display 7 significative digits
    assert_parsed_expression_evaluates_to<double>("inverse([[I,23-2I,3*I][4+I,5*I,6][7,8*I+2,9]])", "[[(-0.0118289353958)-0.0454959053685*I,(-0.500454959054)-0.727024567789*I,0.31847133758+0.488626023658*I][0.0409463148317+3.63967242948E-3*I,0.0400363967243-0.0218380345769*I,(-0.0254777070064)+9.0991810737E-4*I][3.33636639369E-3-1.81983621474E-3*I,0.36093418259+0.534728541098*I,(-0.130118289354)-0.357597816197*I]]", Degree, Cartesian, 12); // FIXME: inverse is not precise enough to display 14 significative digits
  
  #endif
  
    assert_parsed_expression_evaluates_to<float>("prediction(0.1, 100)", "[[0,0.2]]");
    assert_parsed_expression_evaluates_to<double>("prediction(0.1, 100)", "[[0,0.2]]");
  
    assert_parsed_expression_evaluates_to<float>("prediction95(0.1, 100)", "[[0.0412,0.1588]]");
    assert_parsed_expression_evaluates_to<double>("prediction95(0.1, 100)", "[[0.0412,0.1588]]");
  
    assert_parsed_expression_evaluates_to<float>("product(2+n*I, 1, 5)", "(-100)-540*I");
    assert_parsed_expression_evaluates_to<double>("product(2+n*I, 1, 5)", "(-100)-540*I");
  
    assert_parsed_expression_evaluates_to<float>("root(3+I, 3)", "1.459366+0.1571201*I");
    assert_parsed_expression_evaluates_to<double>("root(3+I, 3)", "1.4593656008684+1.5712012294394E-1*I");
  
    assert_parsed_expression_evaluates_to<float>("root(3, 3+I)", "1.382007-0.1524428*I");
    assert_parsed_expression_evaluates_to<double>("root(3, 3+I)", "1.3820069623326-0.1524427794159*I");
  
    assert_parsed_expression_evaluates_to<float>("root(5^(-I)3^9,I)", "3.504", Degree, Cartesian, 4);
    assert_parsed_expression_evaluates_to<double>("root(5^(-I)3^9,I)", "3.5039410843", Degree, Cartesian, 11);
  
    assert_parsed_expression_evaluates_to<float>("R(3+I)", "1.755317+0.2848488*I");
    assert_parsed_expression_evaluates_to<double>("R(3+I)", "1.7553173018244+2.8484878459314E-1*I");
  
    assert_parsed_expression_evaluates_to<double>("sum(2+n*I,1,5)", "10+15*I");
    assert_parsed_expression_evaluates_to<double>("sum(2+n*I,1,5)", "10+15*I");
  #if MATRICES_ARE_DEFINED
    assert_parsed_expression_evaluates_to<float>("transpose([[1,2,3][4,5,-6][7,8,9]])", "[[1,4,7][2,5,8][3,-6,9]]");
    assert_parsed_expression_evaluates_to<float>("transpose([[1,7,5][4,2,8]])", "[[1,4][7,2][5,8]]");
    assert_parsed_expression_evaluates_to<float>("transpose([[1,2][4,5][7,8]])", "[[1,4,7][2,5,8]]");
    assert_parsed_expression_evaluates_to<double>("transpose([[1,2,3][4,5,-6][7,8,9]])", "[[1,4,7][2,5,8][3,-6,9]]");
    assert_parsed_expression_evaluates_to<double>("transpose([[1,7,5][4,2,8]])", "[[1,4][7,2][5,8]]");
    assert_parsed_expression_evaluates_to<double>("transpose([[1,2][4,5][7,8]])", "[[1,4,7][2,5,8]]");
  #endif
  
    assert_parsed_expression_evaluates_to<float>("round(2.3246,3)", "2.325");
    assert_parsed_expression_evaluates_to<double>("round(2.3245,3)", "2.325");
  
    assert_parsed_expression_evaluates_to<float>("6!", "720");
    assert_parsed_expression_evaluates_to<double>("6!", "720");
  
    assert_parsed_expression_evaluates_to<float>("R(-1)", "I");
    assert_parsed_expression_evaluates_to<double>("R(-1)", "I");
  
    assert_parsed_expression_evaluates_to<float>("root(-1,3)", "0.5+0.8660254*I");
    assert_parsed_expression_evaluates_to<double>("root(-1,3)", "0.5+8.6602540378444E-1*I");
  
    assert_parsed_expression_evaluates_to<float>("factor(-23/4)", "-5.75");
    assert_parsed_expression_evaluates_to<double>("factor(-123/24)", "-5.125");
  
    assert_parsed_expression_evaluates_to<float>("int(int(x*x,0,x),0,4)", "21.33333");
    assert_parsed_expression_evaluates_to<double>("int(int(x*x,0,x),0,4)", "21.333333333333");
  
    assert_parsed_expression_evaluates_to<float>("int(1+cos(x), 0, 180)", "180");
    assert_parsed_expression_evaluates_to<double>("int(1+cos(x), 0, 180)", "180");
  
    Expression * exp = parse_expression("random()");
    assert_exp_is_bounded(exp, 0.0f, 1.0f);
    assert_exp_is_bounded(exp, 0.0, 1.0);
    delete exp;
  
    exp = parse_expression("randint(4,45)");
    assert_exp_is_bounded(exp, 4.0f, 45.0f, true);
    assert_exp_is_bounded(exp, 4.0, 45.0, true);
    delete exp;
  }
  
  QUIZ_CASE(poincare_function_simplify) {
    assert_parsed_expression_simplify_to("abs(P)", "P");
    assert_parsed_expression_simplify_to("abs(-P)", "P");
    assert_parsed_expression_simplify_to("binomial(20,3)", "1140");
    assert_parsed_expression_simplify_to("binomial(20,10)", "184756");
    assert_parsed_expression_simplify_to("ceil(-1.3)", "-1");
    assert_parsed_expression_simplify_to("conj(1/2)", "1/2");
    assert_parsed_expression_simplify_to("quo(19,3)", "6");
    assert_parsed_expression_simplify_to("quo(19,0)", "undef");
    assert_parsed_expression_simplify_to("quo(-19,3)", "-7");
    assert_parsed_expression_simplify_to("rem(19,3)", "1");
    assert_parsed_expression_simplify_to("rem(-19,3)", "2");
    assert_parsed_expression_simplify_to("rem(19,0)", "undef");
    assert_parsed_expression_simplify_to("99!", "933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000");
    assert_parsed_expression_simplify_to("factor(-10008/6895)", "-(2^3*3^2*139)/(5*7*197)");
    assert_parsed_expression_simplify_to("factor(1008/6895)", "(2^4*3^2)/(5*197)");
    assert_parsed_expression_simplify_to("factor(10007)", "10007");
    assert_parsed_expression_simplify_to("factor(10007^2)", "undef");
    assert_parsed_expression_simplify_to("floor(-1.3)", "-2");
    assert_parsed_expression_simplify_to("frac(-1.3)", "7/10");
    assert_parsed_expression_simplify_to("gcd(123,278)", "1");
    assert_parsed_expression_simplify_to("gcd(11,121)", "11");
    assert_parsed_expression_simplify_to("lcm(123,278)", "34194");
    assert_parsed_expression_simplify_to("lcm(11,121)", "121");
    assert_parsed_expression_simplify_to("R(4)", "2");
    assert_parsed_expression_simplify_to("root(4,3)", "root(4,3)");
    assert_parsed_expression_simplify_to("root(4,P)", "4^(1/P)");
    assert_parsed_expression_simplify_to("root(27,3)", "3");
    assert_parsed_expression_simplify_to("round(4.235,2)", "106/25");
    assert_parsed_expression_simplify_to("round(4.23,0)", "4");
    assert_parsed_expression_simplify_to("round(4.9,0)", "5");
    assert_parsed_expression_simplify_to("round(12.9,-1)", "10");
    assert_parsed_expression_simplify_to("round(12.9,-2)", "0");
    assert_parsed_expression_simplify_to("permute(99,4)", "90345024");
    assert_parsed_expression_simplify_to("permute(20,-10)", "undef");
    assert_parsed_expression_simplify_to("re(1/2)", "1/2");
  }