[apps/regression] Fill column with formula

This commit is contained in:
Léa Saviot
2018-06-01 11:54:25 +02:00
parent b8c74835de
commit 589b0443ea
5 changed files with 110 additions and 2 deletions

View File

@@ -16,6 +16,7 @@ app_objs += $(addprefix apps/regression/,\
prediction_parameter_controller.o\
store.o\
store_controller.o\
regression_context.o\
)
i18n_files += $(addprefix apps/regression/,\

View File

@@ -0,0 +1,37 @@
#include "regression_context.h"
#include <poincare/decimal.h>
#include <assert.h>
#include <string.h>
using namespace Poincare;
using namespace Shared;
namespace Regression {
void RegressionContext::setExpressionForSymbolName(const Expression * expression, const Symbol * symbol, Context & context) {
m_parentContext->setExpressionForSymbolName(expression, symbol, context);
}
const Expression * RegressionContext::expressionForSymbol(const Symbol * symbol) {
if (Symbol::isRegressionSymbol(symbol->name())) {
const char * seriesName = Symbol::textForSpecialSymbols(symbol->name());
assert(strlen(seriesName) == 2);
int series = (int)(seriesName[1] - '0') - 1;
assert(series >= 0 && series < FloatPairStore::k_numberOfSeries);
assert((seriesName[0] == 'X') || (seriesName[0] == 'Y'));
int storeI = seriesName[0] == 'X' ? 0 : 1;
assert(m_seriesPairIndex >= 0);
assert(m_seriesPairIndex < m_store->numberOfPairsOfSeries(series));
Expression * result = new Decimal(m_store->get(series, storeI, m_seriesPairIndex));
assert(result != nullptr);
return result;
} else {
return m_parentContext->expressionForSymbol(symbol);
}
}
}

View File

@@ -0,0 +1,28 @@
#ifndef REGRESSION_REGRESSION_CONTEXT_H
#define REGRESSION_REGRESSION_CONTEXT_H
#include <poincare/context.h>
#include "../shared/float_pair_store.h"
namespace Regression {
class RegressionContext : public Poincare::Context {
public:
RegressionContext(Shared::FloatPairStore * store, Poincare::Context * parentContext = nullptr) :
Poincare::Context(),
m_store(store),
m_seriesPairIndex(-1),
m_parentContext(parentContext)
{}
void setSeriesPairIndex(int j) { m_seriesPairIndex = j; }
void setExpressionForSymbolName(const Poincare::Expression * expression, const Poincare::Symbol * symbol, Poincare::Context & context) override;
const Poincare::Expression * expressionForSymbol(const Poincare::Symbol * symbol) override;
private:
Shared::FloatPairStore * m_store;
int m_seriesPairIndex;
Poincare::Context * m_parentContext;
};
}
#endif

View File

@@ -1,5 +1,6 @@
#include "store_controller.h"
#include "app.h"
#include "regression_context.h"
#include "../apps_container.h"
#include "../constant.h"
#include "../../poincare/src/layout/char_layout.h"
@@ -18,6 +19,47 @@ StoreController::StoreController(Responder * parentResponder, Store * store, But
{
}
void StoreController::setFormulaLabel() {
int series = selectedColumn() / Store::k_numberOfColumnsPerSeries;
int isXColumn = selectedColumn() % Store::k_numberOfColumnsPerSeries == 0;
char text[] = {isXColumn ? 'X' : 'Y', static_cast<char>('1' + series), '=', 0};
static_cast<ContentView *>(view())->formulaInputView()->setBufferText(text);
}
void StoreController::fillColumnWithFormula(Expression * formula) {
int currentColumn = selectedColumn();
// Fetch the series used in the formula to compute the size of the filled in series
char variables[7] = {0, 0, 0, 0, 0, 0, 0};
formula->getVariables(Symbol::isRegressionSymbol, variables);
int numberOfValuesToCompute = -1;
int index = 0;
while (variables[index] != 0) {
const char * seriesName = Symbol::textForSpecialSymbols(variables[index]);
assert(strlen(seriesName) == 2);
int series = (int)(seriesName[1] - '0') - 1;
assert(series >= 0 && series < FloatPairStore::k_numberOfSeries);
if (numberOfValuesToCompute == -1) {
numberOfValuesToCompute = m_store->numberOfPairsOfSeries(series);
} else {
numberOfValuesToCompute = min(numberOfValuesToCompute, m_store->numberOfPairsOfSeries(series));
}
index++;
}
if (numberOfValuesToCompute == -1) {
numberOfValuesToCompute = m_store->numberOfPairsOfSeries(selectedColumn()/FloatPairStore::k_numberOfColumnsPerSeries);
}
RegressionContext regressionContext(m_store, const_cast<AppsContainer *>(static_cast<const AppsContainer *>(app()->container()))->globalContext());
for (int j = 0; j < numberOfValuesToCompute; j++) {
// Set the context
regressionContext.setSeriesPairIndex(j);
// Compute the new value using the formula
double evaluation = formula->approximateToScalar<double>(regressionContext);
setDataAtLocation(evaluation, currentColumn, j + 1);
}
selectableTableView()->reloadData();
}
void StoreController::willDisplayCellAtLocation(HighlightCell * cell, int i, int j) {
::StoreController::willDisplayCellAtLocation(cell, i, j);
if (cellAtLocationIsEditable(i, j)) {

View File

@@ -11,8 +11,8 @@ namespace Regression {
class StoreController : public Shared::StoreController {
public:
StoreController(Responder * parentResponder, Store * store, ButtonRowController * header);
void setFormulaLabel() override {} //TODO
void fillColumnWithFormula(Poincare::Expression * formula) override {} //TODO
void setFormulaLabel() override;
void fillColumnWithFormula(Poincare::Expression * formula) override;
void willDisplayCellAtLocation(HighlightCell * cell, int i, int j) override;
private:
HighlightCell * titleCells(int index) override;