diff --git a/apps/regression/model/cubic_model.cpp b/apps/regression/model/cubic_model.cpp index 42fdb5c7d..393e2120e 100644 --- a/apps/regression/model/cubic_model.cpp +++ b/apps/regression/model/cubic_model.cpp @@ -55,24 +55,21 @@ double CubicModel::evaluate(double * modelCoefficients, double x) const { } 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; + switch (derivateCoefficientIndex) { + case 0: + // Derivate with respect to a: x^3 + return x*x*x; + case 1: + // Derivate with respect to b: x^2 + return x*x; + case 2: + // Derivate with respect to c: x + return x; + default: + // Derivate with respect to d: 1 + assert(derivateCoefficientIndex == 3); + return 1.0; + }; } Expression CubicModel::expression(double * modelCoefficients) { diff --git a/apps/regression/model/exponential_model.cpp b/apps/regression/model/exponential_model.cpp index bffb66c53..aecdc3cae 100644 --- a/apps/regression/model/exponential_model.cpp +++ b/apps/regression/model/exponential_model.cpp @@ -86,18 +86,15 @@ void ExponentialModel::fit(Store * store, int series, double * modelCoefficients } double ExponentialModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const { - double a = modelCoefficients[0]; - double b = modelCoefficients[1]; + const double b = modelCoefficients[1]; if (derivateCoefficientIndex == 0) { - // Derivate: exp(b*x) + // Derivate with respect to a: 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; + assert(derivateCoefficientIndex == 1); + // Derivate with respect to b: a*x*exp(b*x) + double a = modelCoefficients[0]; + return a*x*exp(b*x); } } diff --git a/apps/regression/model/linear_model.cpp b/apps/regression/model/linear_model.cpp index 3de85628f..afe159ea2 100644 --- a/apps/regression/model/linear_model.cpp +++ b/apps/regression/model/linear_model.cpp @@ -38,15 +38,12 @@ void LinearModel::fit(Store * store, int series, double * modelCoefficients, Poi double LinearModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const { if (derivateCoefficientIndex == 0) { - // Derivate: x + // Derivate with respect to a: x return x; } - if (derivateCoefficientIndex == 1) { - // Derivate: 1; - return 1; - } - assert(false); - return 0.0; + assert(derivateCoefficientIndex == 1); + // Derivate with respect to b: 1 + return 1.0; } } diff --git a/apps/regression/model/logarithmic_model.cpp b/apps/regression/model/logarithmic_model.cpp index f5c23a105..7beeb119c 100644 --- a/apps/regression/model/logarithmic_model.cpp +++ b/apps/regression/model/logarithmic_model.cpp @@ -33,16 +33,13 @@ double LogarithmicModel::levelSet(double * modelCoefficients, double xMin, doubl double LogarithmicModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const { if (derivateCoefficientIndex == 0) { - // Derivate: ln(x) - assert(x >0); + // Derivate with respect to a: ln(x) + assert(x > 0); return log(x); } - if (derivateCoefficientIndex == 1) { - // Derivate: 1 - return 1; - } - assert(false); - return 0.0; + assert(derivateCoefficientIndex == 1); + // Derivate with respect to b: 1 + return 1.0; } bool LogarithmicModel::dataSuitableForFit(Store * store, int series) const { diff --git a/apps/regression/model/logistic_model.cpp b/apps/regression/model/logistic_model.cpp index 0876aa70d..ec9c50a48 100644 --- a/apps/regression/model/logistic_model.cpp +++ b/apps/regression/model/logistic_model.cpp @@ -61,21 +61,18 @@ double LogisticModel::partialDerivate(double * modelCoefficients, int derivateCo double a = modelCoefficients[0]; double b = modelCoefficients[1]; double c = modelCoefficients[2]; - double denominator = 1.0+a*exp(-b*x); + double denominator = 1.0 + a * exp(-b * x); if (derivateCoefficientIndex == 0) { - // Derivate: exp(-b*x)*(-1 * c/(1.0+a*exp(-b*x))^2) - return -exp(-b*x) * c/(denominator * denominator); + // Derivate with respect to a: exp(-b*x)*(-1 * c/(1.0+a*exp(-b*x))^2) + return -exp(-b * x) * c / (denominator * denominator); } if (derivateCoefficientIndex == 1) { - // Derivate: (-x)*a*exp(-b*x)*(-1/(1.0+a*exp(-b*x))^2) - return x*a*exp(-b*x)*c/(denominator * denominator); + // Derivate with respect to b: (-x)*a*exp(-b*x)*(-1/(1.0+a*exp(-b*x))^2) + return x * a * exp(-b * x) * c / (denominator * denominator); } - if (derivateCoefficientIndex == 2) { - // Derivate: (-x)*a*exp(-b*x)*(-1/(1.0+a*exp(-b*x))^2) - return 1.0/denominator; - } - assert(false); - return 0.0; + assert(derivateCoefficientIndex == 2); + // Derivate with respect to c: (-x)*a*exp(-b*x)*(-1/(1.0+a*exp(-b*x))^2) + return 1.0 / denominator; } void LogisticModel::specializedInitCoefficientsForFit(double * modelCoefficients, double defaultValue, Store * store, int series) const { @@ -86,7 +83,7 @@ void LogisticModel::specializedInitCoefficientsForFit(double * modelCoefficients * and c. Twice the standard vertical deviation is a rough estimate of c * that is "close enough" to c to seed the coefficient, without being too * dependent on outliers.*/ - modelCoefficients[2] = 2.0*store->standardDeviationOfColumn(series, 1); + modelCoefficients[2] = 2.0 * store->standardDeviationOfColumn(series, 1); } diff --git a/apps/regression/model/power_model.cpp b/apps/regression/model/power_model.cpp index b7c0e09e0..a6b768f09 100644 --- a/apps/regression/model/power_model.cpp +++ b/apps/regression/model/power_model.cpp @@ -44,19 +44,16 @@ double PowerModel::partialDerivate(double * modelCoefficients, int derivateCoeff double a = modelCoefficients[0]; double b = modelCoefficients[1]; if (derivateCoefficientIndex == 0) { - // Derivate: pow(x,b) + // Derivate with respect to a: pow(x,b) return pow(x,b); } - if (derivateCoefficientIndex == 1) { - assert(x >= 0); - /* We assume all xi are positive. - * For x = 0, a*pow(x,b) = 0, the partial derivate along b is 0 - * For x > 0, a*pow(x,b) = a*exp(b*ln(x)), the partial derivate along b is - * ln(x)*a*pow(x,b) */ - return x == 0 ? 0 : log(x)*a*pow(x, b); - } - assert(false); - return 0.0; + assert(derivateCoefficientIndex == 1); + assert(x >= 0); + /* We assume all xi are positive. + * For x = 0, a*pow(x,b) = 0, the partial derivate with respect to b is 0 + * For x > 0, a*pow(x,b) = a*exp(b*ln(x)), the partial derivate with respect + * to b is ln(x)*a*pow(x,b) */ + return x == 0.0 ? 0.0 : log(x) * a * pow(x, b); } void PowerModel::fit(Store * store, int series, double * modelCoefficients, Poincare::Context * context) { diff --git a/apps/regression/model/quadratic_model.cpp b/apps/regression/model/quadratic_model.cpp index 66bc7cf0c..d258116fc 100644 --- a/apps/regression/model/quadratic_model.cpp +++ b/apps/regression/model/quadratic_model.cpp @@ -47,19 +47,16 @@ double QuadraticModel::evaluate(double * modelCoefficients, double x) const { double QuadraticModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const { if (derivateCoefficientIndex == 0) { - // Derivate: x^2 + // Derivate with respect to a: x^2 return x*x; } if (derivateCoefficientIndex == 1) { - // Derivate: x + // Derivate with respect to b: x return x; } - if (derivateCoefficientIndex == 2) { - // Derivate: 1 - return 1; - } - assert(false); - return 0.0; + assert(derivateCoefficientIndex == 2); + // Derivate with respect to c: 1 + return 1.0; } Expression QuadraticModel::expression(double * modelCoefficients) { diff --git a/apps/regression/model/quartic_model.cpp b/apps/regression/model/quartic_model.cpp index 2b37e4423..3015b7f52 100644 --- a/apps/regression/model/quartic_model.cpp +++ b/apps/regression/model/quartic_model.cpp @@ -64,28 +64,24 @@ double QuarticModel::evaluate(double * modelCoefficients, double x) const { } double QuarticModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const { - if (derivateCoefficientIndex == 0) { - // Derivate: x^4 - return x*x*x*x; - } - if (derivateCoefficientIndex == 1) { - // Derivate: x^3 - return x*x*x; - } - if (derivateCoefficientIndex == 2) { - // Derivate: x^2 - return x*x; - } - if (derivateCoefficientIndex == 3) { - // Derivate: x - return x; - } - if (derivateCoefficientIndex == 4) { - // Derivate: 1 - return 1; - } - assert(false); - return 0.0; + switch (derivateCoefficientIndex) { + case 0: + // Derivate with respect to a: x^4 + return x*x*x*x; + case 1: + // Derivate with respect to b: x^3 + return x*x*x; + case 2: + // Derivate with respect to c: x^2 + return x*x; + case 3: + // Derivate with respect to d: x + return x; + default: + assert(derivateCoefficientIndex == 4); + // Derivate with respect to e: 1 + return 1.0; + }; } Expression QuarticModel::expression(double * modelCoefficients) { diff --git a/apps/regression/model/trigonometric_model.cpp b/apps/regression/model/trigonometric_model.cpp index f32f2c66b..b6ddd6359 100644 --- a/apps/regression/model/trigonometric_model.cpp +++ b/apps/regression/model/trigonometric_model.cpp @@ -46,28 +46,27 @@ double TrigonometricModel::evaluate(double * modelCoefficients, double x) const } double TrigonometricModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const { + if (derivateCoefficientIndex == 3) { + // Derivate with respect to d: 1 + return 1.0; + } + double a = modelCoefficients[0]; double b = modelCoefficients[1]; double c = modelCoefficients[2]; double radianX = x * toRadians(Poincare::Preferences::sharedPreferences()->angleUnit()); + if (derivateCoefficientIndex == 0) { - // Derivate: sin(b*x+c) - return sin(b*radianX+c); + // Derivate with respect to a: sin(b*x+c) + return sin(b * radianX + c); } if (derivateCoefficientIndex == 1) { - // Derivate: x*a*cos(b*x+c); - return radianX*a*cos(b*radianX+c); + // Derivate with respect to b: x*a*cos(b*x+c); + return radianX * a * cos(b * radianX + c); } - if (derivateCoefficientIndex == 2) { - // Derivate: a*cos(b*x+c) - return a*cos(b*radianX+c); - } - if (derivateCoefficientIndex == 3) { - // Derivate: 1 - return 1.0; - } - assert(false); - return 0.0; + assert(derivateCoefficientIndex == 2); + // Derivatewith respect to c: a*cos(b*x+c) + return a * cos(b * radianX + c); } void TrigonometricModel::specializedInitCoefficientsForFit(double * modelCoefficients, double defaultValue, Store * store, int series) const {