mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
61 lines
1.7 KiB
C++
61 lines
1.7 KiB
C++
#include "exponential_model.h"
|
|
#include <math.h>
|
|
#include <assert.h>
|
|
|
|
using namespace Poincare;
|
|
|
|
namespace Regression {
|
|
|
|
LayoutReference ExponentialModel::layout() {
|
|
if (m_layout.isUninitialized()) {
|
|
const LayoutReference layoutChildren[] = {
|
|
CharLayoutRef('a', KDText::FontSize::Small),
|
|
CharLayoutRef(Ion::Charset::MiddleDot, KDText::FontSize::Small),
|
|
CharLayoutRef('e', KDText::FontSize::Small),
|
|
VerticalOffsetLayoutRef(
|
|
HorizontalLayoutRef(
|
|
CharLayoutRef('b', KDText::FontSize::Small),
|
|
CharLayoutRef(Ion::Charset::MiddleDot, KDText::FontSize::Small),
|
|
CharLayoutRef('X', KDText::FontSize::Small)
|
|
),
|
|
VerticalOffsetLayoutNode::Type::Superscript
|
|
)
|
|
};
|
|
m_layout = HorizontalLayoutRef(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;
|
|
}
|
|
|
|
}
|