[Context] Modifying context method signature for sequences

This allows sequences to be used in functions by calling u(x)

Change-Id: I336e84a19bf9b3dd0f2e435d1aaebda3c9e71ec8
This commit is contained in:
Arthur Camouseigt
2020-09-10 16:19:14 +02:00
committed by Émilie Feral
parent 3dca515441
commit 1d71a14d2c
13 changed files with 30 additions and 21 deletions

View File

@@ -9,7 +9,7 @@ using namespace Shared;
namespace Regression { namespace Regression {
const Expression RegressionContext::expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone) { const Expression RegressionContext::expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone, float unknownSymbolValue ) {
if (symbol.type() == ExpressionNode::Type::Symbol && Symbol::isRegressionSymbol(symbol.name(), nullptr)) { if (symbol.type() == ExpressionNode::Type::Symbol && Symbol::isRegressionSymbol(symbol.name(), nullptr)) {
const char * seriesName = symbol.name(); const char * seriesName = symbol.name();
assert(strlen(seriesName) == 2); assert(strlen(seriesName) == 2);

View File

@@ -9,7 +9,7 @@ namespace Regression {
class RegressionContext : public Shared::StoreContext { class RegressionContext : public Shared::StoreContext {
public: public:
using Shared::StoreContext::StoreContext; using Shared::StoreContext::StoreContext;
const Poincare::Expression expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone) override; const Poincare::Expression expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone, float unknownSymbolValue = NAN) override;
}; };
} }

View File

@@ -19,7 +19,7 @@ CacheContext<T>::CacheContext(Context * parentContext) :
} }
template<typename T> template<typename T>
const Expression CacheContext<T>::expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone) { const Expression CacheContext<T>::expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone, float unknownSymbolValue ) {
// [u|v|w](n(+1)?) // [u|v|w](n(+1)?)
if (symbol.type() == ExpressionNode::Type::Sequence) { if (symbol.type() == ExpressionNode::Type::Sequence) {
int index = nameIndexForSymbol(const_cast<Symbol &>(static_cast<const Symbol &>(symbol))); int index = nameIndexForSymbol(const_cast<Symbol &>(static_cast<const Symbol &>(symbol)));

View File

@@ -12,7 +12,7 @@ template<typename T>
class CacheContext : public Poincare::ContextWithParent { class CacheContext : public Poincare::ContextWithParent {
public: public:
CacheContext(Poincare::Context * parentContext); CacheContext(Poincare::Context * parentContext);
const Poincare::Expression expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone) override; const Poincare::Expression expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone, float unknownSymbolValue = NAN) override;
void setValueForSymbol(T value, const Poincare::Symbol & symbol); void setValueForSymbol(T value, const Poincare::Symbol & symbol);
void setNValue(int n) { m_nValue = n; } void setNValue(int n) { m_nValue = n; }
void setSequenceContext(SequenceContext * sequenceContext) { m_sequenceContext = sequenceContext;} void setSequenceContext(SequenceContext * sequenceContext) { m_sequenceContext = sequenceContext;}

View File

@@ -2,6 +2,7 @@
#include "continuous_function.h" #include "continuous_function.h"
#include "sequence.h" #include "sequence.h"
#include "poincare_helpers.h" #include "poincare_helpers.h"
#include <poincare/serialization_helper.h>
#include <poincare/undefined.h> #include <poincare/undefined.h>
#include <assert.h> #include <assert.h>
@@ -56,9 +57,9 @@ Context::SymbolAbstractType GlobalContext::expressionTypeForIdentifier(const cha
} }
} }
const Expression GlobalContext::expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone) { const Expression GlobalContext::expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone, float unknownSymbolValue ) {
Ion::Storage::Record r = SymbolAbstractRecordWithBaseName(symbol.name()); Ion::Storage::Record r = SymbolAbstractRecordWithBaseName(symbol.name());
return ExpressionForSymbolAndRecord(symbol, r, this); return ExpressionForSymbolAndRecord(symbol, r, this, unknownSymbolValue);
} }
void GlobalContext::setExpressionForSymbolAbstract(const Expression & expression, const SymbolAbstract & symbol) { void GlobalContext::setExpressionForSymbolAbstract(const Expression & expression, const SymbolAbstract & symbol) {
@@ -83,14 +84,14 @@ void GlobalContext::setExpressionForSymbolAbstract(const Expression & expression
} }
} }
const Expression GlobalContext::ExpressionForSymbolAndRecord(const SymbolAbstract & symbol, Ion::Storage::Record r, Context * ctx) { const Expression GlobalContext::ExpressionForSymbolAndRecord(const SymbolAbstract & symbol, Ion::Storage::Record r, Context * ctx, float unknownSymbolValue ) {
if (symbol.type() == ExpressionNode::Type::Symbol) { if (symbol.type() == ExpressionNode::Type::Symbol) {
return ExpressionForActualSymbol(r); return ExpressionForActualSymbol(r);
} else if (symbol.type() == ExpressionNode::Type::Function) { } else if (symbol.type() == ExpressionNode::Type::Function) {
return ExpressionForFunction(symbol, r); return ExpressionForFunction(symbol, r);
} }
assert(symbol.type() == ExpressionNode::Type::Sequence); assert(symbol.type() == ExpressionNode::Type::Sequence);
return ExpressionForSequence(symbol, r, ctx); return ExpressionForSequence(symbol, r, ctx, unknownSymbolValue);
} }
const Expression GlobalContext::ExpressionForActualSymbol(Ion::Storage::Record r) { const Expression GlobalContext::ExpressionForActualSymbol(Ion::Storage::Record r) {
@@ -115,14 +116,17 @@ const Expression GlobalContext::ExpressionForFunction(const SymbolAbstract & sym
return e; return e;
} }
const Expression GlobalContext::ExpressionForSequence(const SymbolAbstract & symbol, Ion::Storage::Record r, Context * ctx) { const Expression GlobalContext::ExpressionForSequence(const SymbolAbstract & symbol, Ion::Storage::Record r, Context * ctx, float unknownSymbolValue) {
if (!Ion::Storage::FullNameHasExtension(r.fullName(), Ion::Storage::seqExtension, strlen(Ion::Storage::seqExtension))) { if (!Ion::Storage::FullNameHasExtension(r.fullName(), Ion::Storage::seqExtension, strlen(Ion::Storage::seqExtension))) {
return Expression(); return Expression();
} }
/* An function record value has metadata before the expression. To get the /* An function record value has metadata before the expression. To get the
* expression, use the function record handle. */ * expression, use the function record handle. */
Sequence seq(r); Sequence seq(r);
double rank = PoincareHelpers::ApproximateToScalar<float>(symbol.childAtIndex(0), ctx); constexpr int bufferSize = CodePoint::MaxCodePointCharLength + 1;
char unknownN[bufferSize];
Poincare::SerializationHelper::CodePoint(unknownN, bufferSize, UCodePointUnknown);
float rank = symbol.childAtIndex(0).approximateWithValueForSymbol<float>(unknownN, unknownSymbolValue, ctx, Preferences::sharedPreferences()->complexFormat(),Preferences::sharedPreferences()->angleUnit());
if (std::floor(rank) == rank) { if (std::floor(rank) == rank) {
SequenceContext sqctx(ctx, sequenceStore()); SequenceContext sqctx(ctx, sequenceStore());
return Float<double>::Builder(seq.evaluateXYAtParameter(rank, &sqctx).x2()); return Float<double>::Builder(seq.evaluateXYAtParameter(rank, &sqctx).x2());

View File

@@ -29,15 +29,15 @@ public:
* The expression recorded in global context is already an expression. * The expression recorded in global context is already an expression.
* Otherwise, we would need the context and the angle unit to evaluate it */ * Otherwise, we would need the context and the angle unit to evaluate it */
SymbolAbstractType expressionTypeForIdentifier(const char * identifier, int length) override; SymbolAbstractType expressionTypeForIdentifier(const char * identifier, int length) override;
const Poincare::Expression expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone) override; const Poincare::Expression expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone, float unknownSymbolValue = NAN) override;
void setExpressionForSymbolAbstract(const Poincare::Expression & expression, const Poincare::SymbolAbstract & symbol) override; void setExpressionForSymbolAbstract(const Poincare::Expression & expression, const Poincare::SymbolAbstract & symbol) override;
static SequenceStore * sequenceStore(); static SequenceStore * sequenceStore();
private: private:
// Expression getters // Expression getters
static const Poincare::Expression ExpressionForSymbolAndRecord(const Poincare::SymbolAbstract & symbol, Ion::Storage::Record r, Context * ctx); static const Poincare::Expression ExpressionForSymbolAndRecord(const Poincare::SymbolAbstract & symbol, Ion::Storage::Record r, Context * ctx, float unknownSymbolValue = NAN);
static const Poincare::Expression ExpressionForActualSymbol(Ion::Storage::Record r); static const Poincare::Expression ExpressionForActualSymbol(Ion::Storage::Record r);
static const Poincare::Expression ExpressionForFunction(const Poincare::SymbolAbstract & symbol, Ion::Storage::Record r); static const Poincare::Expression ExpressionForFunction(const Poincare::SymbolAbstract & symbol, Ion::Storage::Record r);
static const Poincare::Expression ExpressionForSequence(const Poincare::SymbolAbstract & symbol, Ion::Storage::Record r, Context * ctx); static const Poincare::Expression ExpressionForSequence(const Poincare::SymbolAbstract & symbol, Ion::Storage::Record r, Context * ctx, float unknownSymbolValue = NAN);
// Expression setters // Expression setters
static Ion::Storage::Record::ErrorStatus SetExpressionForActualSymbol(const Poincare::Expression & expression, const Poincare::SymbolAbstract & symbol, Ion::Storage::Record previousRecord); static Ion::Storage::Record::ErrorStatus SetExpressionForActualSymbol(const Poincare::Expression & expression, const Poincare::SymbolAbstract & symbol, Ion::Storage::Record previousRecord);
static Ion::Storage::Record::ErrorStatus SetExpressionForFunction(const Poincare::Expression & expression, const Poincare::SymbolAbstract & symbol, Ion::Storage::Record previousRecord); static Ion::Storage::Record::ErrorStatus SetExpressionForFunction(const Poincare::Expression & expression, const Poincare::SymbolAbstract & symbol, Ion::Storage::Record previousRecord);

View File

@@ -9,7 +9,7 @@ using namespace Shared;
namespace Statistics { namespace Statistics {
const Expression StatisticsContext::expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone) { const Expression StatisticsContext::expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone, float unknownSymbolValue ) {
if (symbol.type() == ExpressionNode::Type::Symbol && Symbol::isSeriesSymbol(symbol.name(), nullptr)) { if (symbol.type() == ExpressionNode::Type::Symbol && Symbol::isSeriesSymbol(symbol.name(), nullptr)) {
const char * seriesName = symbol.name(); const char * seriesName = symbol.name();
assert(strlen(seriesName) == 2); assert(strlen(seriesName) == 2);

View File

@@ -8,7 +8,7 @@ namespace Statistics {
class StatisticsContext : public Shared::StoreContext { class StatisticsContext : public Shared::StoreContext {
public: public:
using Shared::StoreContext::StoreContext; using Shared::StoreContext::StoreContext;
const Poincare::Expression expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone) override; const Poincare::Expression expressionForSymbolAbstract(const Poincare::SymbolAbstract & symbol, bool clone, float unknownSymbolValue = NAN) override;
}; };
} }

View File

@@ -3,6 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <assert.h> #include <assert.h>
#include <cmath>
namespace Poincare { namespace Poincare {
@@ -18,7 +19,7 @@ public:
Symbol Symbol
}; };
virtual SymbolAbstractType expressionTypeForIdentifier(const char * identifier, int length) = 0; virtual SymbolAbstractType expressionTypeForIdentifier(const char * identifier, int length) = 0;
virtual const Expression expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone) = 0; virtual const Expression expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone, float unknownSymbolValue = NAN) = 0;
virtual void setExpressionForSymbolAbstract(const Expression & expression, const SymbolAbstract & symbol) = 0; virtual void setExpressionForSymbolAbstract(const Expression & expression, const SymbolAbstract & symbol) = 0;
}; };

View File

@@ -14,7 +14,7 @@ public:
// Context // Context
SymbolAbstractType expressionTypeForIdentifier(const char * identifier, int length) override { return m_parentContext->expressionTypeForIdentifier(identifier, length); } SymbolAbstractType expressionTypeForIdentifier(const char * identifier, int length) override { return m_parentContext->expressionTypeForIdentifier(identifier, length); }
void setExpressionForSymbolAbstract(const Expression & expression, const SymbolAbstract & symbol) override { m_parentContext->setExpressionForSymbolAbstract(expression, symbol); } void setExpressionForSymbolAbstract(const Expression & expression, const SymbolAbstract & symbol) override { m_parentContext->setExpressionForSymbolAbstract(expression, symbol); }
const Expression expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone) override { return m_parentContext->expressionForSymbolAbstract(symbol, clone); } const Expression expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone, float unknownSymbolValue = NAN) override { return m_parentContext->expressionForSymbolAbstract(symbol, clone, unknownSymbolValue); }
private: private:
Context * m_parentContext; Context * m_parentContext;

View File

@@ -12,7 +12,7 @@ public:
// Context // Context
SymbolAbstractType expressionTypeForIdentifier(const char * identifier, int length) override { return SymbolAbstractType::None; } SymbolAbstractType expressionTypeForIdentifier(const char * identifier, int length) override { return SymbolAbstractType::None; }
void setExpressionForSymbolAbstract(const Expression & expression, const SymbolAbstract & symbol) override { assert(false); } void setExpressionForSymbolAbstract(const Expression & expression, const SymbolAbstract & symbol) override { assert(false); }
const Expression expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone) override { return Expression(); } const Expression expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone, float unknownSymbolValue = NAN) override { return Expression(); }
}; };
} }

View File

@@ -18,7 +18,7 @@ public:
// Context // Context
void setExpressionForSymbolAbstract(const Expression & expression, const SymbolAbstract & symbol) override; void setExpressionForSymbolAbstract(const Expression & expression, const SymbolAbstract & symbol) override;
const Expression expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone) override; const Expression expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone, float unknownSymbolValue = NAN) override;
private: private:
const char * m_name; const char * m_name;

View File

@@ -23,14 +23,18 @@ void VariableContext::setExpressionForSymbolAbstract(const Expression & expressi
} }
} }
const Expression VariableContext::expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone) { const Expression VariableContext::expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone, float unknownSymbolValue ) {
if (m_name != nullptr && strcmp(symbol.name(), m_name) == 0) { if (m_name != nullptr && strcmp(symbol.name(), m_name) == 0) {
if (symbol.type() == ExpressionNode::Type::Symbol) { if (symbol.type() == ExpressionNode::Type::Symbol) {
return clone ? m_value.clone() : m_value; return clone ? m_value.clone() : m_value;
} }
return Undefined::Builder(); return Undefined::Builder();
} else { } else {
return ContextWithParent::expressionForSymbolAbstract(symbol, clone); Symbol unknownSymbol = Symbol::Builder(UCodePointUnknown);
if (m_name != nullptr && strcmp(m_name, unknownSymbol.name()) == 0) {
unknownSymbolValue = m_value.approximateToScalar<float>(this, Preferences::sharedPreferences()->complexFormat(),Preferences::sharedPreferences()->angleUnit());
}
return ContextWithParent::expressionForSymbolAbstract(symbol, clone, unknownSymbolValue);
} }
} }