[poincare] Fix GlobalContext

This commit is contained in:
Émilie Feral
2018-08-10 09:44:56 +02:00
parent fc93907d51
commit 42d7806cae
5 changed files with 54 additions and 88 deletions

View File

@@ -34,6 +34,7 @@ objs += $(addprefix poincare/src/,\
approximation_helper.o\
layout_helper.o\
simplification_helper.o\
global_context.o\
)
objsExpected += $(addprefix poincare/src/,\

View File

@@ -83,11 +83,11 @@ public:
static int Exponent(const char * integralPart, int integralPartLength, const char * fractionalPart, int fractionalPartLength, const char * exponent, int exponentLength, bool exponentNegative);
Decimal(const char * integralPart, int integralPartLength, const char * fractionalPart, int fractionalPartLength, bool negative, int exponent);
Decimal(const DecimalNode * node) : Number(node) {}
Decimal(Integer m, int e);
constexpr static int k_maxExponentLength = 4;
private:
DecimalNode * node() const override { return static_cast<DecimalNode *>(Number::node()); }
template <typename T> Decimal(T f);
Decimal(Integer m, int e);
Decimal(size_t size) : Number(nullptr) {
TreeNode * node = TreePool::sharedPool()->createTreeNode<DecimalNode>(size);
m_identifier = node->identifier();

View File

@@ -2,9 +2,10 @@
#define POINCARE_GLOBAL_CONTEXT_H
#include <poincare/context.h>
#include <poincare/matrix.h>
#include <poincare/approximation.h>
//#include <poincare/matrix.h>
#include <poincare/float.h>
#include <poincare/decimal.h>
#include <poincare/symbol.h>
namespace Poincare {
@@ -17,22 +18,21 @@ public:
GlobalContext();
/* The expression recorded in global context is already a expression.
* Otherwise, we would need the context and the angle unit to evaluate it */
const Expression expressionForSymbol(const SymbolReference symbol) override;
LayoutRef layoutForSymbol(const SymbolReference symbol, int numberOfSignificantDigits);
void setExpressionForSymbolName(const Expression expression, const SymbolReference symbol, Context & context) override;
const Expression expressionForSymbol(const Symbol symbol) override;
//LayoutRef layoutForSymbol(const Symbol symbol, int numberOfSignificantDigits);
void setExpressionForSymbolName(const Expression expression, const Symbol symbol, Context & context) override;
static constexpr uint16_t k_maxNumberOfScalarExpressions = 26;
static constexpr uint16_t k_maxNumberOfListExpressions = 10;
static constexpr uint16_t k_maxNumberOfMatrixExpressions = 10;
private:
static Decimal * defaultExpression();
int symbolIndex(const Symbol * symbol) const;
static Decimal defaultExpression();
int symbolIndex(const Symbol symbol) const;
Expression m_expressions[k_maxNumberOfScalarExpressions];
MatrixReference m_matrixExpressions[k_maxNumberOfMatrixExpressions];
//Matrix m_matrixExpressions[k_maxNumberOfMatrixExpressions];
/* Matrix layout memoization */
LayoutRef m_matrixLayouts[k_maxNumberOfMatrixExpressions];
Approximation<double> m_pi;
Approximation<double> m_e;
Approximation<double> m_i;
//LayoutRef m_matrixLayouts[k_maxNumberOfMatrixExpressions];
Float<double> m_pi;
Float<double> m_e;
};
}

View File

@@ -107,9 +107,11 @@ public:
Expression shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const;
Expression replaceSymbolWithExpression(char symbol, Expression expression) const;
int getPolynomialCoefficients(char symbolName, Expression coefficients[]) const;
// Symbol
char name() const { return node()->name(); }
private:
SymbolNode * node() const override { return static_cast<SymbolNode *>(Expression::node()); }
char name() const { return node()->name(); }
};
}

View File

@@ -1,5 +1,4 @@
#include <poincare/global_context.h>
#include <poincare/matrix.h>
#include <poincare/undefined.h>
#include <poincare/preferences.h>
#include <assert.h>
@@ -9,7 +8,7 @@
namespace Poincare {
GlobalContext::GlobalContext() :
m_matrixLayouts{
/* m_matrixLayouts{
LayoutRef(nullptr),
LayoutRef(nullptr),
LayoutRef(nullptr),
@@ -19,74 +18,50 @@ GlobalContext::GlobalContext() :
LayoutRef(nullptr),
LayoutRef(nullptr),
LayoutRef(nullptr),
LayoutRef(nullptr)}, //TODO find better way to initialize
LayoutRef(nullptr)},*/ //TODO find better way to initialize
m_pi(M_PI),
m_e(M_E),
m_i(0.0, 1.0)
m_e(M_E)
{
for (int i = 0; i < k_maxNumberOfScalarExpressions; i++) {
m_expressions[i] = nullptr;
}
for (int i = 0; i < k_maxNumberOfMatrixExpressions ; i++) {
m_matrixExpressions[i] = nullptr;
}
}
GlobalContext::~GlobalContext() {
for (int i = 0; i < k_maxNumberOfScalarExpressions; i++) {
if (m_expressions[i] != nullptr) {
delete m_expressions[i];
}
m_expressions[i] = nullptr;
}
for (int i = 0; i < k_maxNumberOfMatrixExpressions; i++) {
if (m_matrixExpressions[i] != nullptr) {
delete m_matrixExpressions[i];
}
m_matrixExpressions[i] = nullptr;
}
}
Decimal * GlobalContext::defaultExpression() {
Decimal GlobalContext::defaultExpression() {
static Decimal defaultExpression(Integer(0), 0);
return &defaultExpression;
return defaultExpression;
}
int GlobalContext::symbolIndex(const Symbol * symbol) const {
if (SymbolReference::isMatrixSymbol(symbol->name())) {
return symbol->name() - (char)Symbol::SpecialSymbols::M0;
int GlobalContext::symbolIndex(const Symbol symbol) const {
if (Symbol::isMatrixSymbol(symbol.name())) {
return symbol.name() - (char)Symbol::SpecialSymbols::M0;
}
if (SymbolReference::isScalarSymbol(symbol->name())) {
return symbol->name() - 'A';
if (Symbol::isScalarSymbol(symbol.name())) {
return symbol.name() - 'A';
}
return -1;
}
const Expression * GlobalContext::expressionForSymbol(const Symbol * symbol) {
if (symbol->name() == Ion::Charset::IComplex) {
return &m_i;
const Expression GlobalContext::expressionForSymbol(const Symbol symbol) {
if (symbol.name() == Ion::Charset::SmallPi) {
return m_pi;
}
if (symbol->name() == Ion::Charset::SmallPi) {
return &m_pi;
}
if (symbol->name() == Ion::Charset::Exponential) {
return &m_e;
if (symbol.name() == Ion::Charset::Exponential) {
return m_e;
}
int index = symbolIndex(symbol);
if (SymbolReference::isMatrixSymbol(symbol->name()) {
return m_matrixExpressions[index];
if (Symbol::isMatrixSymbol(symbol.name())) {
return Expression();
//return m_matrixExpressions[index]; // TODO: implement when matrix is done
}
if (index < 0 || index >= k_maxNumberOfScalarExpressions) {
return nullptr;
return Expression();
}
if (m_expressions[index] == nullptr) {
return defaultExpression();
if (m_expressions[index].isDefined()) {
return m_expressions[index];
}
return m_expressions[index];
return defaultExpression();
}
LayoutRef GlobalContext::layoutForSymbol(const Symbol * symbol, int numberOfSignificantDigits) {
if (SymbolReference::isMatrixSymbol(symbol->name()) {
/*LayoutRef GlobalContext::layoutForSymbol(const Symbol * symbol, int numberOfSignificantDigits) {
if (Symbol::isMatrixSymbol(symbol.name()) {
int index = symbolIndex(symbol);
if (!m_matrixLayouts[index].isDefined() && m_matrixExpressions[index] != nullptr) {
m_matrixLayouts[index] = m_matrixExpressions[index]->createLayout(Preferences::PrintFloatMode::Decimal, numberOfSignificantDigits);
@@ -94,26 +69,19 @@ LayoutRef GlobalContext::layoutForSymbol(const Symbol * symbol, int numberOfSign
return m_matrixLayouts[index];
}
return nullptr;
}
}*/
void GlobalContext::setExpressionForSymbolName(const Expression * expression, const Symbol * symbol, Context & context) {
void GlobalContext::setExpressionForSymbolName(const Expression expression, const Symbol symbol, Context & context) {
int index = symbolIndex(symbol);
if (SymbolReference::isMatrixSymbol(symbol->name()) {
int indexMatrix = symbol->name() - (char)Symbol::SpecialSymbols::M0;
if (Symbol::isMatrixSymbol(symbol.name())) {
int indexMatrix = symbol.name() - (char)Symbol::SpecialSymbols::M0;
assert(indexMatrix >= 0 && indexMatrix < k_maxNumberOfMatrixExpressions);
Expression * evaluation = expression ? expression->approximate<double>(context, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->complexFormat()) : nullptr; // evaluate before deleting anything (to be able to evaluate M1+2->M1)
if (m_matrixExpressions[indexMatrix] != nullptr) {
delete m_matrixExpressions[indexMatrix];
m_matrixExpressions[indexMatrix] = nullptr;
}
if (m_matrixLayouts[indexMatrix] != nullptr) {
m_matrixLayouts[indexMatrix] = LayoutRef(nullptr);
}
if (evaluation != nullptr) {
if (evaluation->type() != Expression::Type::Matrix) {
m_matrixExpressions[indexMatrix] = new Matrix(&evaluation, 1, 1, false);
Expression evaluation = expression.isDefined() ? expression.approximate<double>(context, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->complexFormat()) : Expression(); // evaluate before deleting anything (to be able to evaluate M1+2->M1)
if (evaluation.isDefined()) {
if (evaluation.type() != ExpressionNode::Type::Matrix) {
//m_matrixExpressions[indexMatrix] = new Matrix(&evaluation, 1, 1, false);
} else {
m_matrixExpressions[indexMatrix] = static_cast<Matrix *>(evaluation);
//m_matrixExpressions[indexMatrix] = static_cast<Matrix *>(evaluation);
}
}
return;
@@ -121,17 +89,12 @@ void GlobalContext::setExpressionForSymbolName(const Expression * expression, co
if (index < 0 || index >= k_maxNumberOfScalarExpressions) {
return;
}
Expression * evaluation = expression ? expression->approximate<double>(context, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->complexFormat()) : nullptr; // evaluate before deleting anything (to be able to evaluate A+2->A)
if (m_expressions[index] != nullptr) {
delete m_expressions[index];
m_expressions[index] = nullptr;
}
if (evaluation == nullptr) {
Expression evaluation = expression.isDefined() ? expression.approximate<double>(context, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->complexFormat()) : Expression(); // evaluate before deleting anything (to be able to evaluate A+2->A)
if (!evaluation.isDefined()) {
return;
}
if (evaluation->type() == Expression::Type::Matrix) {
m_expressions[index] = new Undefined();
delete evaluation;
if (evaluation.type() == ExpressionNode::Type::Matrix) {
m_expressions[index] = Undefined();
} else {
m_expressions[index] = evaluation;
}