Blame view

emulateur/epsilon-nofrendo/apps/sequence/test/sequence.cpp 8.35 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
  #include <quiz.h>
  #include <string.h>
  #include <assert.h>
  #include <cmath>
  #include "../sequence_store.h"
  #include "../sequence_context.h"
  
  using namespace Poincare;
  
  namespace Sequence {
  
  void check_sequences_defined_by(double result[2][10], Sequence::Type typeU, const char * definitionU, const char * conditionU1 = nullptr, const char * conditionU2 = nullptr, Sequence::Type typeV = Sequence::Type::Explicit, const char * definitionV = nullptr, const char * conditionV1 = nullptr, const char * conditionV2 = nullptr) {
    GlobalContext globalContext;
    SequenceStore store;
    SequenceContext sequenceContext(&globalContext, &store);
  
    Sequence * u = nullptr;
    Sequence * v = nullptr;
    if (definitionU) {
      u = static_cast<Sequence *>(store.addEmptyModel());
      assert(u->name()[0] == 'u');
      u->setType(typeU);
      u->setContent(definitionU);
      if (conditionU1) {
        u->setFirstInitialConditionContent(conditionU1);
      }
      if (conditionU2) {
        u->setSecondInitialConditionContent(conditionU2);
      }
    }
    if (definitionV) {
      if (store.numberOfModels() == 0) {
        Sequence * tempU = static_cast<Sequence *>(store.addEmptyModel());
        v = static_cast<Sequence *>(store.addEmptyModel());
        store.removeModel(tempU);
        v = store.modelAtIndex(0);
      } else {
        assert(store.numberOfModels() == 1);
        v = static_cast<Sequence *>(store.addEmptyModel());
      }
      v->setType(typeV);
      v->setContent(definitionV);
      if (conditionV1) {
        v->setFirstInitialConditionContent(conditionV1);
      }
      if (conditionV2) {
        v->setSecondInitialConditionContent(conditionV2);
      }
    }
    for (int j = 0; j < 10; j++) {
      if (u && u->isDefined()) {
        double un = u->evaluateAtAbscissa((double)j, &sequenceContext);
        assert((std::isnan(un) && std::isnan(result[0][j])) || (un == result[0][j]));
      }
      if (v && v->isDefined()) {
        double vn = v->evaluateAtAbscissa((double)j, &sequenceContext);
        assert((std::isnan(vn) && std::isnan(result[1][j])) || (vn == result[1][j]));
      }
    }
  }
  
  QUIZ_CASE(sequence_evaluation) {
    // u(n) = n
    double result0[2][10] = {{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, {}};
    check_sequences_defined_by(result0, Sequence::Type::Explicit, "n");
  
    // u(n+1) = u(n)+n, u(0) = 0
    double result1[2][10] = {{0.0, 0.0, 1.0, 3.0, 6.0, 10.0, 15.0, 21.0, 28.0, 36.0}, {}};
    check_sequences_defined_by(result1, Sequence::Type::SingleRecurrence, "u(n)+n", "0");
  
    // u(n+2) = u(n+1)+u(n)+n, u(0) = 0, u(1) = 0
    double result2[2][10] = {{0.0, 0.0, 0.0, 1.0, 3.0, 7.0, 14.0, 26.0, 46.0, 79.0}, {}};
    check_sequences_defined_by(result2, Sequence::Type::DoubleRecurrence, "u(n)+u(n+1)+n", "0", "0");
  
    // u independent, v defined with u
    // u(n) = n; v(n) = u(n)+n
    double result3[2][10] = {{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0},
      {0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0}};
    check_sequences_defined_by(result3, Sequence::Type::Explicit, "n", nullptr, nullptr, Sequence::Type::Explicit, "u(n)+n");
  
    // u(n+1) = u(n)+n, u(0) = 0; v(n) = u(n)+n
    double result4[2][10] = {{0.0, 0.0, 1.0, 3.0, 6.0, 10.0, 15.0, 21.0, 28.0, 36.0},
      {0.0, 1.0, 3.0, 6.0, 10.0, 15.0, 21.0, 28.0, 36.0, 45.0}};
    check_sequences_defined_by(result4, Sequence::Type::SingleRecurrence, "n+u(n)", "0", nullptr, Sequence::Type::Explicit, "u(n)+n");
  
    // u(n+2) = u(n+1)+u(n)+n, u(0) = 0, u(1) = 0; v(n) = u(n)+n
    double result5[2][10] = {{0.0, 0.0, 0.0, 1.0, 3.0, 7.0, 14.0, 26.0, 46.0, 79.0},
      {0.0, 1.0, 2.0, 4.0, 7.0, 12.0, 20.0, 33.0, 54.0, 88.0}};
    check_sequences_defined_by(result5, Sequence::Type::DoubleRecurrence, "n+u(n)+u(n+1)", "0", "0", Sequence::Type::Explicit, "u(n)+n");
  
    // u(n) = n; v(n+1) = u(n)+u(n+1)+n, v(0) = 0
    double result6[2][10] = {{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0},
      {0.0, 1.0, 4.0, 7.0, 10.0, 13.0, 16.0, 19.0, 22.0, 25.0}};
    check_sequences_defined_by(result6, Sequence::Type::Explicit, "n", nullptr, nullptr, Sequence::Type::SingleRecurrence, "u(n)+u(n+1)+n", "0");
  
    // u(n+1) = u(n)+n, u(0) = 0; v(n+1) = u(n)+u(n+1)+n, v(0) = 0
    double result7[2][10] = {{0.0, 0.0, 1.0, 3.0, 6.0, 10.0, 15.0, 21.0, 28.0, 36.0},
      {0.0, 0.0, 2.0, 6.0, 12.0, 20.0, 30.0, 42.0, 56.0, 72.0}};
    check_sequences_defined_by(result7, Sequence::Type::SingleRecurrence, "n+u(n)", "0", nullptr, Sequence::Type::SingleRecurrence, "u(n)+u(n+1)+n", "0");
  
    // u(n+2) = u(n+1)+u(n)+n, u(0) = 0, u(1) = 0; v(n+1) = u(n)+u(n+1)+n, v(0) = 0
    double result8[2][10] = {{0.0, 0.0, 0.0, 1.0, 3.0, 7.0, 14.0, 26.0, 46.0, 79.0},
      {0.0, 0.0, 1.0, 3.0, 7.0, 14.0, 26.0, 46.0, 79.0, 133.0}};
    check_sequences_defined_by(result8, Sequence::Type::DoubleRecurrence, "n+u(n)+u(n+1)", "0", "0", Sequence::Type::SingleRecurrence, "u(n)+u(n+1)+n", "0");
  
    // u(n) = n; v(n+2) = u(n)+u(n+1)+n, v(0) = 0, v(1)=0
    double result9[2][10] = {{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0},
      {0.0, 0.0, 1.0, 4.0, 7.0, 10.0, 13.0, 16.0, 19.0, 22.0}};
    check_sequences_defined_by(result9, Sequence::Type::Explicit, "n", nullptr, nullptr, Sequence::Type::DoubleRecurrence, "u(n)+u(n+1)+n", "0", "0");
  
    // u(n+1) = u(n)+n, u(0) = 0; v(n+2) = u(n)+u(n+1)+n, v(0) = 0, v(1)=0
    double result10[2][10] = {{0.0, 0.0, 1.0, 3.0, 6.0, 10.0, 15.0, 21.0, 28.0, 36.0},
      {0.0, 0.0, 0.0, 2.0, 6.0, 12.0, 20.0, 30.0, 42.0, 56.0}};
    check_sequences_defined_by(result10, Sequence::Type::SingleRecurrence, "n+u(n)", "0", nullptr, Sequence::Type::DoubleRecurrence, "u(n)+u(n+1)+n", "0", "0");
  
    // u(n+2) = u(n+1)+u(n)+n, u(0) = 0, u(1) = 0; v(n+2) = u(n)+u(n+1)+n, v(0) = 0, v(1)=0
    double result11[2][10] = {{0.0, 0.0, 0.0, 1.0, 3.0, 7.0, 14.0, 26.0, 46.0, 79.0},
      {0.0, 0.0, 0.0, 1.0, 3.0, 7.0, 14.0, 26.0, 46.0, 79.0}};
    check_sequences_defined_by(result11, Sequence::Type::DoubleRecurrence, "n+u(n)+u(n+1)", "0", "0", Sequence::Type::DoubleRecurrence, "u(n)+u(n+1)+n", "0", "0");
  
    // v independent, u defined with v
  
    // u(n) = v(n); v(n) = u(n)
    double result12[2][10] = {{NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN},
      {NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN}};
    check_sequences_defined_by(result12, Sequence::Type::Explicit, "v(n)", nullptr, nullptr, Sequence::Type::Explicit, "u(n)");
  
    // u(n+1) = v(n)+n, u(0)=0; v(n) = u(n)+n
    double result13[2][10] = {{0.0, 0.0, 2.0, 6.0, 12.0, 20.0, 30.0, 42.0, 56.0, 72.0}, {0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0, 81}};
    check_sequences_defined_by(result13, Sequence::Type::SingleRecurrence, "v(n)+n", "0", nullptr, Sequence::Type::Explicit, "u(n)+n");
  
    // u(n+2) = v(n+1)+u(n+1)+v(n)+u(n)+n, u(1) = 0; u(0)=0; v(n) = u(n)+n
    double result14[2][10] = {{0.0, 0.0, 1.0, 6.0, 21.0, 64.0, 183.0, 510.0, 1405.0, 3852.0}, {0.0, 1.0, 3.0, 9.0, 25.0, 69.0, 189.0, 517.0, 1413.0, 3861.0}};
    check_sequences_defined_by(result14, Sequence::Type::DoubleRecurrence, "v(n+1)+v(n)+u(n+1)+u(n)+n", "0", "0", Sequence::Type::Explicit, "u(n)+n");
  
    // u(n) = v(n)+n; v(n+1) = u(n)+n, v(0)=0
    double result15[2][10] = {{0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0, 81},
      {0.0, 0.0, 2.0, 6.0, 12.0, 20.0, 30.0, 42.0, 56.0, 72.0}};
    check_sequences_defined_by(result15, Sequence::Type::Explicit, "v(n)+n", nullptr, nullptr, Sequence::Type::SingleRecurrence, "u(n)+n", "0");
  
    // u(n+1) = v(n)+n, u(0)=0; v(n+1) = u(n)+n, v(0)=0
    double result16[2][10] = {{0.0, 0.0, 1.0, 3.0, 6.0, 10.0, 15.0, 21.0, 28.0, 36.0}, {0.0, 0.0, 1.0, 3.0, 6.0, 10.0, 15.0, 21.0, 28.0, 36.0}};
    check_sequences_defined_by(result16, Sequence::Type::SingleRecurrence, "v(n)+n", "0", nullptr, Sequence::Type::SingleRecurrence, "u(n)+n", "0");
  
    // u(n+2) = v(n+1)+u(n+1)+v(n)+u(n)+n, u(1) = 0, u(0) = 0; v(n+1) = u(n)+n, v(0)=0
    double result17[2][10] = {{0.0, 0.0, 0.0, 2.0, 7.0, 19.0, 46.0, 105.0, 233.0, 509.0}, {0.0, 0.0, 1.0, 2.0, 5.0, 11.0, 24.0, 52.0, 112.0, 241.0}};
    check_sequences_defined_by(result17, Sequence::Type::DoubleRecurrence, "v(n+1)+v(n)+u(n+1)+u(n)+n", "0", "0", Sequence::Type::SingleRecurrence, "u(n)+n", "0");
  
    // u(n) = n; v undefined
    double result18[2][10] = {{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, {NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN}};
    check_sequences_defined_by(result18, Sequence::Type::Explicit, "n");
  
    // u undefined; v(n) = n
    double result19[2][10] = {{NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN}, {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}};
    check_sequences_defined_by(result19, Sequence::Type::Explicit, nullptr, nullptr, nullptr, Sequence::Type::Explicit, "n");
  }
  
  }