Numworks Epsilon  1.4.1
Graphing Calculator Operating System
global_context.cpp
Go to the documentation of this file.
2 #include <poincare/matrix.h>
3 #include <poincare/matrix.h>
4 #include <assert.h>
5 #include <cmath>
6 #include <ion.h>
7 
8 namespace Poincare {
9 
11  m_pi(Complex<double>::Float(M_PI)),
12  m_e(Complex<double>::Float(M_E)),
13  m_i(Complex<double>::Cartesian(0.0, 1.0))
14 {
15  for (int i = 0; i < k_maxNumberOfScalarExpressions; i++) {
16  m_expressions[i] = nullptr;
17  }
18  for (int i = 0; i < k_maxNumberOfMatrixExpressions ; i++) {
19  m_matrixExpressions[i] = nullptr;
20  m_matrixLayout[i] = nullptr;
21  }
22 }
23 
25  for (int i = 0; i < k_maxNumberOfScalarExpressions; i++) {
26  if (m_expressions[i] != nullptr) {
27  delete m_expressions[i];
28  }
29  m_expressions[i] = nullptr;
30  }
31  for (int i = 0; i < k_maxNumberOfMatrixExpressions; i++) {
32  if (m_matrixExpressions[i] != nullptr) {
33  delete m_matrixExpressions[i];
34  }
35  m_matrixExpressions[i] = nullptr;
36  if (m_matrixLayout[i] != nullptr) {
37  delete m_matrixLayout[i];
38  }
39  m_matrixLayout[i] = nullptr;
40  }
41 }
42 
43 Complex<double> * GlobalContext::defaultExpression() {
44  static Complex<double> defaultExpression(Complex<double>::Float(0.0));
45  return &defaultExpression;
46 }
47 
48 int GlobalContext::symbolIndex(const Symbol * symbol) const {
49  if (symbol->isMatrixSymbol()) {
50  return symbol->name() - (char)Symbol::SpecialSymbols::M0;
51  }
52  if (symbol->isScalarSymbol()) {
53  return symbol->name() - 'A';
54  }
55  return -1;
56 }
57 
59  if (symbol->name() == Ion::Charset::SmallPi) {
60  return &m_pi;
61  }
62  if (symbol->name() == Ion::Charset::Exponential) {
63  return &m_e;
64  }
65  if (symbol->name() == Ion::Charset::IComplex) {
66  return &m_i;
67  }
68  int index = symbolIndex(symbol);
69  if (symbol->isMatrixSymbol()) {
70  return m_matrixExpressions[index];
71  }
72  if (index < 0 || index >= k_maxNumberOfScalarExpressions) {
73  return nullptr;
74  }
75  if (m_expressions[index] == nullptr) {
76  return defaultExpression();
77  }
78  return m_expressions[index];
79 }
80 
82  if (symbol->isMatrixSymbol()) {
83  int index = symbolIndex(symbol);
84  if (m_matrixLayout[index] == nullptr && m_matrixExpressions[index] != nullptr) {
85  m_matrixLayout[index] = m_matrixExpressions[index]->createLayout();
86  }
87  return m_matrixLayout[index];
88  }
89  return nullptr;
90 }
91 
92 void GlobalContext::setExpressionForSymbolName(const Expression * expression, const Symbol * symbol, Context & context) {
93  int index = symbolIndex(symbol);
94  if (symbol->isMatrixSymbol()) {
95  int indexMatrix = symbol->name() - (char)Symbol::SpecialSymbols::M0;
96  assert(indexMatrix >= 0 && indexMatrix < k_maxNumberOfMatrixExpressions);
97  Expression * evaluation = expression ? expression->approximate<double>(context) : nullptr; // evaluate before deleting anything (to be able to evaluate M1+2->M1)
98  if (m_matrixExpressions[indexMatrix] != nullptr) {
99  delete m_matrixExpressions[indexMatrix];
100  m_matrixExpressions[indexMatrix] = nullptr;
101  }
102  if (m_matrixLayout[indexMatrix] != nullptr) {
103  delete m_matrixLayout[indexMatrix];
104  m_matrixLayout[indexMatrix] = nullptr;
105  }
106  if (evaluation != nullptr) {
107  if (evaluation->type() == Expression::Type::Complex) {
108  m_matrixExpressions[indexMatrix] = new Matrix(&evaluation, 1, 1, false);
109  } else {
110  m_matrixExpressions[indexMatrix] = static_cast<Matrix *>(evaluation);
111  }
112  }
113  return;
114  }
115  if (index < 0 || index >= k_maxNumberOfScalarExpressions) {
116  return;
117  }
118  Expression * evaluation = expression ? expression->approximate<double>(context) : nullptr; // evaluate before deleting anything (to be able to evaluate A+2->A)
119  if (m_expressions[index] != nullptr) {
120  delete m_expressions[index];
121  m_expressions[index] = nullptr;
122  }
123  if (evaluation == nullptr) {
124  return;
125  }
126  if (evaluation->type() == Expression::Type::Complex) {
127  m_expressions[index] = static_cast<Complex<double> *>(evaluation);
128  } else {
129  m_expressions[index] = new Complex<double>(Complex<double>::Float(NAN));
130  delete evaluation;
131  }
132 }
133 
134 }
ExpressionLayout * expressionLayoutForSymbol(const Symbol *symbol)
#define NAN
Definition: math.h:30
const Expression * expressionForSymbol(const Symbol *symbol) override
#define assert(e)
Definition: assert.h:9
static constexpr uint16_t k_maxNumberOfMatrixExpressions
Expression * approximate(Context &context, AngleUnit angleUnit=AngleUnit::Default) const
Definition: expression.cpp:338
#define M_PI
Definition: math.h:17
char name() const
Definition: symbol.cpp:185
void setExpressionForSymbolName(const Expression *expression, const Symbol *symbol, Context &context) override
constexpr Expression::ComplexFormat Cartesian
#define M_E
Definition: math.h:12
static constexpr uint16_t k_maxNumberOfScalarExpressions
ExpressionLayout * createLayout(PrintFloat::Mode floatDisplayMode=PrintFloat::Mode::Default, ComplexFormat complexFormat=ComplexFormat::Default) const
Definition: expression.cpp:244
static Complex< T > Float(T x)
Definition: complex.cpp:23
bool isMatrixSymbol() const
Definition: symbol.cpp:231
virtual Type type() const =0