[poincare] Move the xcontext to poincare to be used by derivative object

Change-Id: Ib950c7db7d864e0ff56c7964f4591bce180d627d
This commit is contained in:
Émilie Feral
2017-01-09 16:32:26 +01:00
parent 3536607f7a
commit 6e01c55f1a
10 changed files with 27 additions and 35 deletions

View File

@@ -13,7 +13,6 @@ app_objs += $(addprefix apps/graph/,\
list/new_function_cell.o\
list/list_controller.o\
list/parameter_controller.o\
local_context.o\
values/abscissa_parameter_controller.o\
values/derivative_parameter_controller.o\
values/function_parameter_controller.o\

View File

@@ -6,7 +6,7 @@ namespace Graph {
App::App(Container * container, Context * context) :
TextFieldDelegateApp(container, &m_inputViewController, "Graph", ImageStore::GraphIcon),
m_functionStore(FunctionStore()),
m_localContext(LocalContext(context)),
m_xContext(XContext(context)),
m_listController(ListController(&m_listHeader, &m_functionStore, &m_listHeader)),
m_listHeader(HeaderViewController(nullptr, &m_listController, &m_listController)),
m_listStackViewController(StackViewController(&m_tabViewController, &m_listHeader)),
@@ -28,7 +28,7 @@ InputViewController * App::inputViewController() {
}
Context * App::localContext() {
return &m_localContext;
return &m_xContext;
}
}

View File

@@ -2,8 +2,8 @@
#define GRAPH_APP_H
#include <escher.h>
#include <poincare.h>
#include "function_store.h"
#include "local_context.h"
#include "graph/graph_controller.h"
#include "list/list_controller.h"
#include "values/values_controller.h"
@@ -18,7 +18,7 @@ public:
Context * localContext() override;
private:
FunctionStore m_functionStore;
LocalContext m_localContext;
XContext m_xContext;
ListController m_listController;
HeaderViewController m_listHeader;
StackViewController m_listStackViewController;

View File

@@ -5,7 +5,6 @@
#include "../../curve_view.h"
#include "../../constant.h"
#include "../function_store.h"
#include "../local_context.h"
#include "../../interactive_curve_view_range.h"
namespace Graph {

View File

@@ -6,7 +6,6 @@ objs += $(addprefix poincare/src/,\
absolute_value.o\
addition.o\
binary_operation.o\
global_context.o\
cosine.o\
derivative.o\
expression.o\
@@ -15,6 +14,7 @@ objs += $(addprefix poincare/src/,\
float.o\
fraction.o\
function.o\
global_context.o\
integer.o\
list_data.o\
leaf_expression.o\
@@ -29,6 +29,7 @@ objs += $(addprefix poincare/src/,\
subtraction.o\
symbol.o\
tangent.o\
x_context.o\
)
objs += $(addprefix poincare/src/layout/,\
absolute_value_layout.o\

View File

@@ -24,5 +24,6 @@
#include <poincare/subtraction.h>
#include <poincare/symbol.h>
#include <poincare/tangent.h>
#include <poincare/x_context.h>
#endif

View File

@@ -11,8 +11,9 @@ public:
Expression * cloneWithDifferentOperands(Expression ** newOperands,
int numberOfOperands, bool cloneOperands = true) const override;
private:
//TODO: do something smarter to compute epsilon to gain in precision
constexpr static float k_epsilon = 0.0001f;
constexpr static float k_precision = 0.01f;
constexpr static float k_precision = 0.1f;
};
#endif

View File

@@ -1,13 +1,12 @@
#ifndef GRAPH_LOCAL_CONTEXT_H
#define GRAPH_LOCAL_CONTEXT_H
#ifndef POINCARE_LOCAL_CONTEXT_H
#define POINCARE_LOCAL_CONTEXT_H
#include <poincare.h>
#include <poincare/context.h>
#include <poincare/float.h>
namespace Graph {
class LocalContext : public Context {
class XContext : public Context {
public:
LocalContext(Context * parentContext);
XContext(Context * parentContext = nullptr);
void setExpressionForSymbolName(Expression * expression, const Symbol * symbol) override;
const Expression * expressionForSymbol(const Symbol * symbol) override;
private:
@@ -15,6 +14,4 @@ private:
Context * m_parentContext;
};
}
#endif

View File

@@ -1,7 +1,8 @@
#include <poincare/derivative.h>
#include <poincare/symbol.h>
#include <poincare/float.h>
#include <poincare/context.h>
#include <poincare/x_context.h>
extern "C" {
#include <assert.h>
#include <math.h>
@@ -26,17 +27,14 @@ Expression * Derivative::cloneWithDifferentOperands(Expression** newOperands,
}
float Derivative::approximate(Context& context) const {
XContext xContext = XContext(&context);
Symbol xSymbol = Symbol('x');
const Expression * previousValueForXSymbol = context.expressionForSymbol(&xSymbol);
Float e = Float(m_args[1]->approximate(context)+k_epsilon);
context.setExpressionForSymbolName(&e, &xSymbol);
float expressionPlus = m_args[0]->approximate(context);
xContext.setExpressionForSymbolName(&e, &xSymbol);
float expressionPlus = m_args[0]->approximate(xContext);
e = Float(m_args[1]->approximate(context)-k_epsilon);
context.setExpressionForSymbolName(&e, &xSymbol);
float expressionMinus = m_args[0]->approximate(context);
xContext.setExpressionForSymbolName(&e, &xSymbol);
float expressionMinus = m_args[0]->approximate(xContext);
float growthRate = (expressionPlus - expressionMinus)/(2*k_epsilon);
growthRate = roundf(growthRate/k_precision)*k_precision;
e = Float(NAN);
context.setExpressionForSymbolName((Expression *)previousValueForXSymbol, &xSymbol);
return growthRate;
return roundf(growthRate/k_precision)*k_precision;
}

View File

@@ -1,15 +1,12 @@
#include "local_context.h"
#include <string.h>
#include <poincare/x_context.h>
namespace Graph {
LocalContext::LocalContext(::Context * parentContext) :
XContext::XContext(::Context * parentContext) :
m_xValue(Float(0.0f)),
m_parentContext(parentContext)
{
}
void LocalContext::setExpressionForSymbolName(Expression * expression, const Symbol * symbol) {
void XContext::setExpressionForSymbolName(Expression * expression, const Symbol * symbol) {
if (symbol->name() == 'x') {
m_xValue = Float(expression->approximate(*m_parentContext));
} else {
@@ -17,7 +14,7 @@ void LocalContext::setExpressionForSymbolName(Expression * expression, const Sym
}
}
const Expression * LocalContext::expressionForSymbol(const Symbol * symbol) {
const Expression * XContext::expressionForSymbol(const Symbol * symbol) {
if (symbol->name() == 'x') {
return &m_xValue;
} else {
@@ -25,4 +22,3 @@ const Expression * LocalContext::expressionForSymbol(const Symbol * symbol) {
}
}
}