mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
88 lines
2.5 KiB
C++
88 lines
2.5 KiB
C++
#include "quadratic_model.h"
|
|
#include "../../shared/poincare_helpers.h"
|
|
#include <math.h>
|
|
#include <assert.h>
|
|
#include <poincare/code_point_layout.h>
|
|
#include <poincare/horizontal_layout.h>
|
|
#include <poincare/vertical_offset_layout.h>
|
|
#include <poincare/decimal.h>
|
|
#include <poincare/number.h>
|
|
#include <poincare/symbol.h>
|
|
#include <poincare/addition.h>
|
|
#include <poincare/multiplication_explicite.h>
|
|
#include <poincare/power.h>
|
|
|
|
using namespace Poincare;
|
|
using namespace Shared;
|
|
|
|
namespace Regression {
|
|
|
|
Layout QuadraticModel::layout() {
|
|
if (m_layout.isUninitialized()) {
|
|
constexpr int size = 10;
|
|
Layout layoutChildren[size] = {
|
|
CodePointLayout::Builder('a', k_layoutFont),
|
|
CodePointLayout::Builder(UCodePointMiddleDot, k_layoutFont),
|
|
CodePointLayout::Builder('X', k_layoutFont),
|
|
VerticalOffsetLayout::Builder(
|
|
CodePointLayout::Builder('2', k_layoutFont),
|
|
VerticalOffsetLayoutNode::Position::Superscript
|
|
),
|
|
CodePointLayout::Builder('+', k_layoutFont),
|
|
CodePointLayout::Builder('b', k_layoutFont),
|
|
CodePointLayout::Builder(UCodePointMiddleDot, k_layoutFont),
|
|
CodePointLayout::Builder('X', k_layoutFont),
|
|
CodePointLayout::Builder('+', k_layoutFont),
|
|
CodePointLayout::Builder('c', k_layoutFont),
|
|
};
|
|
m_layout = HorizontalLayout::Builder(layoutChildren, size);
|
|
}
|
|
return m_layout;
|
|
}
|
|
|
|
double QuadraticModel::evaluate(double * modelCoefficients, double x) const {
|
|
double a = modelCoefficients[0];
|
|
double b = modelCoefficients[1];
|
|
double c = modelCoefficients[2];
|
|
return a*x*x+b*x+c;
|
|
}
|
|
|
|
double QuadraticModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const {
|
|
if (derivateCoefficientIndex == 0) {
|
|
// Derivate: x^2
|
|
return x*x;
|
|
}
|
|
if (derivateCoefficientIndex == 1) {
|
|
// Derivate: x
|
|
return x;
|
|
}
|
|
if (derivateCoefficientIndex == 2) {
|
|
// Derivate: 1
|
|
return 1;
|
|
}
|
|
assert(false);
|
|
return 0.0;
|
|
}
|
|
|
|
Expression QuadraticModel::expression(double * modelCoefficients) {
|
|
double a = modelCoefficients[0];
|
|
double b = modelCoefficients[1];
|
|
double c = modelCoefficients[2];
|
|
// a*x^2+b*x+c
|
|
Expression addChildren[] = {
|
|
MultiplicationExplicite::Builder(
|
|
Number::DecimalNumber(a),
|
|
Power::Builder(
|
|
Symbol::Builder('x'),
|
|
Decimal::Builder(2.0))),
|
|
MultiplicationExplicite::Builder(
|
|
Number::DecimalNumber(b),
|
|
Symbol::Builder('x')),
|
|
Number::DecimalNumber(c)
|
|
};
|
|
Expression result = Addition::Builder(addChildren, 3);
|
|
return result;
|
|
}
|
|
|
|
}
|