Blame view

Modif/epsilon-master/apps/sequence/sequence_context.h 2.85 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
  #ifndef SEQUENCE_SEQUENCE_CONTEXT_H
  #define SEQUENCE_SEQUENCE_CONTEXT_H
  
  #include <poincare.h>
  
  namespace Sequence {
  
  constexpr static int MaxRecurrenceDepth = 2;
  static constexpr int MaxNumberOfSequences = 2;
  
  class SequenceStore;
  class SequenceContext;
  
  template<typename T>
  class TemplatedSequenceContext {
  public:
    TemplatedSequenceContext();
    T valueOfSequenceAtPreviousRank(int sequenceIndex, int rank) const;
    void resetCache();
    bool iterateUntilRank(int n, SequenceStore * sequenceStore, SequenceContext * sqctx);
  private:
    constexpr static int k_maxRecurrentRank = 10000;
    /* Cache:
     * In order to accelerate the computation of values of recurrent sequences,
     * we memoize the last computed values of the sequence and their associated
     * ranks (n and n+1 for instance). Thereby, when another evaluation at a
     * superior rank k > n+1 is called, we avoid iterating from 0 but can start
     * from n. */
    void step(SequenceStore * sequenceStore, SequenceContext * sqctx);
    int m_rank;
    T m_values[MaxNumberOfSequences][MaxRecurrenceDepth+1];
  };
  
  class SequenceContext : public Poincare::Context {
  public:
    SequenceContext(Poincare::Context * parentContext, SequenceStore * sequenceStore) :
      Context(),
      m_floatSequenceContext(),
      m_doubleSequenceContext(),
      m_sequenceStore(sequenceStore),
      m_parentContext(parentContext) {}
    /* expressionForSymbol & setExpressionForSymbolName directly call the parent
     * context respective methods. Indeed, special chars like n, u(n), u(n+1),
     * v(n), v(n+1) are taken into accound only when evaluating sequences which
     * is done in another context. */
    const Poincare::Expression * expressionForSymbol(const Poincare::Symbol * symbol) override {
      return m_parentContext->expressionForSymbol(symbol);
    }
    void setExpressionForSymbolName(const Poincare::Expression * expression, const Poincare::Symbol * symbol, Poincare::Context & context) override {
      m_parentContext->setExpressionForSymbolName(expression, symbol, context);
    }
    template<typename T> T valueOfSequenceAtPreviousRank(int sequenceIndex, int rank) const {
      if (sizeof(T) == sizeof(float)) {
        return m_floatSequenceContext.valueOfSequenceAtPreviousRank(sequenceIndex, rank);
      }
      return m_doubleSequenceContext.valueOfSequenceAtPreviousRank(sequenceIndex, rank);
    }
    void resetCache() {
      m_floatSequenceContext.resetCache();
      m_doubleSequenceContext.resetCache();
    }
    template<typename T> bool iterateUntilRank(int n) {
      if (sizeof(T) == sizeof(float)) {
        return m_floatSequenceContext.iterateUntilRank(n, m_sequenceStore, this);
      }
      return m_doubleSequenceContext.iterateUntilRank(n, m_sequenceStore, this);
    }
  private:
    TemplatedSequenceContext<float> m_floatSequenceContext;
    TemplatedSequenceContext<double> m_doubleSequenceContext;
    SequenceStore * m_sequenceStore;
    Poincare::Context * m_parentContext;
  };
  
  }
  
  #endif