Files
Upsilon/apps/regression/model/cubic_model.cpp
2018-07-18 17:09:07 +02:00

103 lines
3.1 KiB
C++

#include "cubic_model.h"
#include "../../shared/poincare_helpers.h"
#include <math.h>
#include <assert.h>
#include "../../poincare/include/poincare_layouts.h"
using namespace Poincare;
using namespace Shared;
namespace Regression {
ExpressionLayout * CubicModel::layout() {
static ExpressionLayout * layout = nullptr;
if (layout == nullptr) {
const ExpressionLayout * layoutChildren[] = {
new CharLayout('a', KDText::FontSize::Small),
new CharLayout(Ion::Charset::MiddleDot, KDText::FontSize::Small),
new CharLayout('X', KDText::FontSize::Small),
new VerticalOffsetLayout(
new CharLayout('3', KDText::FontSize::Small),
VerticalOffsetLayout::Type::Superscript,
false),
new CharLayout('+', KDText::FontSize::Small),
new CharLayout('b', KDText::FontSize::Small),
new CharLayout(Ion::Charset::MiddleDot, KDText::FontSize::Small),
new CharLayout('X', KDText::FontSize::Small),
new VerticalOffsetLayout(
new CharLayout('2', KDText::FontSize::Small),
VerticalOffsetLayout::Type::Superscript,
false),
new CharLayout('+', KDText::FontSize::Small),
new CharLayout('c', KDText::FontSize::Small),
new CharLayout(Ion::Charset::MiddleDot, KDText::FontSize::Small),
new CharLayout('X', KDText::FontSize::Small),
new CharLayout('+', KDText::FontSize::Small),
new CharLayout('d', KDText::FontSize::Small),
};
layout = new HorizontalLayout(layoutChildren, 15, false);
}
return layout;
}
Expression * CubicModel::simplifiedExpression(double * modelCoefficients, Poincare::Context * context) {
double a = modelCoefficients[0];
double b = modelCoefficients[1];
double c = modelCoefficients[2];
double d = modelCoefficients[3];
Expression * ax3Expression = new Multiplication(
new Decimal(a),
new Power(
new Symbol('x'),
new Decimal(3.0),
false),
false);
Expression * bx2Expression = new Multiplication(
new Decimal(b),
new Power(
new Symbol('x'),
new Decimal(2.0),
false),
false);
Expression * cxExpression = new Multiplication(
new Decimal(c),
new Symbol('x'),
false);
Expression * dExpression = new Decimal(d);
Expression * const operands[] = {ax3Expression, bx2Expression, cxExpression, dExpression};
Expression * result = new Addition(operands, 4, false);
PoincareHelpers::Simplify(&result, *context);
return result;
}
double CubicModel::evaluate(double * modelCoefficients, double x) const {
double a = modelCoefficients[0];
double b = modelCoefficients[1];
double c = modelCoefficients[2];
double d = modelCoefficients[3];
return a*x*x*x+b*x*x+c*x+d;
}
double CubicModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const {
if (derivateCoefficientIndex == 0) {
// Derivate: x^3
return x*x*x;
}
if (derivateCoefficientIndex == 1) {
// Derivate: x^2
return x*x;
}
if (derivateCoefficientIndex == 2) {
// Derivate: x
return x;
}
if (derivateCoefficientIndex == 3) {
// Derivate: 1
return 1;
}
assert(false);
return 0.0;
}
}