Files
Upsilon/apps/regression/model/exponential_model.cpp
Lionel Debroux 1a8c6b6ae9 [poincare, escher, ion, apps] Split the huge umbrella header poincare.h, to reduce build time.
This should be a NFC, but surprisingly, it also reduces size... so what does it change ?
2018-10-23 11:49:09 +02:00

64 lines
1.7 KiB
C++

#include "exponential_model.h"
#include <math.h>
#include <assert.h>
#include <poincare/char_layout.h>
#include <poincare/horizontal_layout.h>
#include <poincare/vertical_offset_layout.h>
using namespace Poincare;
namespace Regression {
Layout ExponentialModel::layout() {
if (m_layout.isUninitialized()) {
const Layout layoutChildren[] = {
CharLayout('a', KDFont::SmallFont),
CharLayout(Ion::Charset::MiddleDot, KDFont::SmallFont),
CharLayout('e', KDFont::SmallFont),
VerticalOffsetLayout(
HorizontalLayout(
CharLayout('b', KDFont::SmallFont),
CharLayout(Ion::Charset::MiddleDot, KDFont::SmallFont),
CharLayout('X', KDFont::SmallFont)
),
VerticalOffsetLayoutNode::Type::Superscript
)
};
m_layout = HorizontalLayout(layoutChildren, 4);
}
return m_layout;
}
double ExponentialModel::evaluate(double * modelCoefficients, double x) const {
double a = modelCoefficients[0];
double b = modelCoefficients[1];
// a*e^(bx)
return a*exp(b*x);
}
double ExponentialModel::levelSet(double * modelCoefficients, double xMin, double step, double xMax, double y, Poincare::Context * context) {
double a = modelCoefficients[0];
double b = modelCoefficients[1];
if (a == 0 || b == 0 || y/a <= 0) {
return NAN;
}
return log(y/a)/b;
}
double ExponentialModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const {
double a = modelCoefficients[0];
double b = modelCoefficients[1];
if (derivateCoefficientIndex == 0) {
// Derivate: exp(b*x)
return exp(b*x);
}
if (derivateCoefficientIndex == 1) {
// Derivate: a*x*exp(b*x)
return a*x*exp(b*x);
}
assert(false);
return 0.0;
}
}