Skip to content

Commit 5fb1c31

Browse files
committed
refactor(ets): use std::min/std::max for reductions and clamp
1 parent 8fefe40 commit 5fb1c31

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/etsTargetFunction.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <R.h>
44

5+
#include <algorithm>
56
#include <cmath>
67

78
void EtsTargetFunction::init(const std::vector<double> &p_y,
@@ -119,7 +120,7 @@ void EtsTargetFunction::eval(const double *p_par, int p_par_length) {
119120
if (trendtype != 0) start = 2;
120121

121122
for (int i = start; i < state.size(); i++) {
122-
if (state[i] < min) min = state[i];
123+
min = std::min(min, state[i]);
123124
}
124125

125126
if (min < 0) {
@@ -135,7 +136,7 @@ void EtsTargetFunction::eval(const double *p_par, int p_par_length) {
135136
this->amse.data(), this->nmse);
136137

137138
// Avoid perfect fits
138-
if (this->lik < -1e10) this->lik = -1e10;
139+
this->lik = std::max(this->lik, -1e10);
139140

140141
if (ISNAN(this->lik)) this->lik = R_PosInf;
141142

@@ -242,7 +243,7 @@ bool EtsTargetFunction::admissible() {
242243
double max = 0;
243244
for (int i = 0; i < zeror.size(); i++) {
244245
const double abs_val = std::sqrt(zeror[i] * zeror[i] + zeroi[i] * zeroi[i]);
245-
if (abs_val > max) max = abs_val;
246+
max = std::max(max, abs_val);
246247
}
247248

248249
if (max > 1 + 1e-10) return false;

0 commit comments

Comments
 (0)