Numworks Epsilon  1.4.1
Graphing Calculator Operating System
sequence_context.cpp
Go to the documentation of this file.
1 #include "sequence_context.h"
2 #include "sequence_store.h"
3 #include <cmath>
4 
5 using namespace Poincare;
6 
7 namespace Sequence {
8 
9 template<typename T>
11  m_rank(-1),
12  m_values{{NAN, NAN, NAN},{NAN, NAN, NAN}}
13 {
14 }
15 
16 template<typename T>
18  return m_values[sequenceIndex][rank];
19 }
20 
21 template<typename T>
23  m_rank = -1;
24 }
25 
26 template<typename T>
28  if (m_rank > n) {
29  m_rank = -1;
30  }
31  if (n < 0 || n-m_rank > k_maxRecurrentRank) {
32  return false;
33  }
34  while (m_rank++ < n) {
35  step(sequenceStore, sqctx);
36  }
37  m_rank--;
38  return true;
39 }
40 
41 template<typename T>
43  /* Shift values */
44  for (int i = 0; i < MaxNumberOfSequences; i++) {
45  for (int j = MaxRecurrenceDepth; j > 0; j--) {
46  m_values[i][j] = m_values[i][j-1];
47  }
48  m_values[i][0] = NAN;
49  }
50 
51  /* Evaluate new u(n) and v(n) */
52  Sequence * u = sequenceStore->numberOfFunctions() > 0 ? sequenceStore->functionAtIndex(0) : nullptr;
53  u = u && u->isDefined() ? u : nullptr;
54  Sequence * v = sequenceStore->numberOfFunctions() > 1 ? sequenceStore->functionAtIndex(1) : nullptr;
55  v = v && v->isDefined() ? v : nullptr;
56  /* Switch u & v if the name of u is v */
57  if (u != nullptr && u->name()[0] == SequenceStore::k_sequenceNames[1][0]) {
58  Sequence * temp = u;
59  u = v;
60  v = temp;
61  }
62 
63  /* Approximate u & v at the new rank. We evaluate u twice in case its
64  * expression depends on v. */
65  m_values[0][0] = u ? u->approximateToNextRank<T>(m_rank, sqctx) : NAN;
66  m_values[1][0] = v ? v->approximateToNextRank<T>(m_rank, sqctx) : NAN;
67  m_values[0][0] = u ? u->approximateToNextRank<T>(m_rank, sqctx) : NAN;
68 }
69 
70 template class TemplatedSequenceContext<float>;
71 template class TemplatedSequenceContext<double>;
72 
73 }
#define NAN
Definition: math.h:30
#define T(x)
Definition: events.cpp:26
Sequence * functionAtIndex(int i) override
Definition: app.cpp:7