Merge changes Ida82d33d,Icce8f8fe

* changes:
  [poincare] Create a class sum
  [poincare] Create a class n_context
This commit is contained in:
Émilie Feral
2017-01-27 10:06:06 +01:00
committed by Gerrit
9 changed files with 104 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ objs += $(addprefix poincare/src/,\
logarithm.o\
matrix.o\
matrix_data.o\
n_context.o\
nth_root.o\
opposite.o\
parenthesis.o\
@@ -30,6 +31,7 @@ objs += $(addprefix poincare/src/,\
product.o\
sine.o\
subtraction.o\
sum.o\
symbol.o\
tangent.o\
x_context.o\

View File

@@ -18,6 +18,7 @@
#include <poincare/logarithm.h>
#include <poincare/matrix.h>
#include <poincare/matrix_data.h>
#include <poincare/n_context.h>
#include <poincare/nth_root.h>
#include <poincare/opposite.h>
#include <poincare/parenthesis.h>
@@ -25,6 +26,7 @@
#include <poincare/product.h>
#include <poincare/sine.h>
#include <poincare/subtraction.h>
#include <poincare/sum.h>
#include <poincare/symbol.h>
#include <poincare/tangent.h>
#include <poincare/x_context.h>

View File

@@ -26,6 +26,7 @@ class Expression {
Power,
Product,
Sine,
Sum,
Subtraction,
Symbol,
Tangent,

View File

@@ -0,0 +1,17 @@
#ifndef POINCARE_N_CONTEXT_H
#define POINCARE_N_CONTEXT_H
#include <poincare/context.h>
#include <poincare/float.h>
class NContext : public Context {
public:
NContext(Context * parentContext = nullptr);
void setExpressionForSymbolName(Expression * expression, const Symbol * symbol) override;
const Expression * expressionForSymbol(const Symbol * symbol) override;
private:
Float m_nValue;
Context * m_parentContext;
};
#endif

View File

@@ -0,0 +1,16 @@
#ifndef POINCARE_SUM_H
#define POINCARE_SUM_H
#include <poincare/function.h>
#include <poincare/n_context.h>
class Sum : public Function {
public:
Sum();
float approximate(Context & context) const override;
Type type() const override;
Expression * cloneWithDifferentOperands(Expression ** newOperands,
int numberOfOperands, bool cloneOperands = true) const override;
};
#endif

View File

@@ -1,5 +1,5 @@
#ifndef POINCARE_LOCAL_CONTEXT_H
#define POINCARE_LOCAL_CONTEXT_H
#ifndef POINCARE_X_CONTEXT_H
#define POINCARE_X_CONTEXT_H
#include <poincare/context.h>
#include <poincare/float.h>

View File

@@ -89,6 +89,7 @@ int { poincare_expression_yylval.expression = new Integral(); return FUNCTION; }
tan { poincare_expression_yylval.expression = new Tangent(); return FUNCTION; }
log { poincare_expression_yylval.expression = new Logarithm(); return FUNCTION; }
root { poincare_expression_yylval.expression = new NthRoot(); return FUNCTION; }
sum { poincare_expression_yylval.expression = new Sum(); return FUNCTION; }
\+ { return PLUS; }
\- { return MINUS; }
\* { return MULTIPLY; }

View File

@@ -0,0 +1,24 @@
#include <poincare/n_context.h>
NContext::NContext(::Context * parentContext) :
m_nValue(Float(0.0f)),
m_parentContext(parentContext)
{
}
void NContext::setExpressionForSymbolName(Expression * expression, const Symbol * symbol) {
if (symbol->name() == 'n') {
m_nValue = Float((int)expression->approximate(*m_parentContext));
} else {
m_parentContext->setExpressionForSymbolName(expression, symbol);
}
}
const Expression * NContext::expressionForSymbol(const Symbol * symbol) {
if (symbol->name() == 'n') {
return &m_nValue;
} else {
return m_parentContext->expressionForSymbol(symbol);
}
}

39
poincare/src/sum.cpp Normal file
View File

@@ -0,0 +1,39 @@
#include <poincare/sum.h>
#include <poincare/symbol.h>
#include <poincare/float.h>
extern "C" {
#include <assert.h>
}
Sum::Sum() :
Function("sum")
{
}
Expression::Type Sum::type() const {
return Type::Sum;
}
Expression * Sum::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
assert(numberOfOperands == 2);
assert(newOperands != nullptr);
Sum * s = new Sum();
s->setArgument(newOperands, numberOfOperands, cloneOperands);
return s;
}
float Sum::approximate(Context& context) const {
NContext nContext = NContext(&context);
Symbol nSymbol = Symbol('n');
int start = m_args[1]->approximate(context);
int end = m_args[2]->approximate(context);
float result = 0.0f;
for (int i = start; i <= end; i++) {
Float iExpression = Float(i);
nContext.setExpressionForSymbolName(&iExpression, &nSymbol);
result += m_args[0]->approximate(nContext);
}
return result;
}