diff --git a/src/main/java/org/apache/commons/math4/analysis/differentiation/DSCompiler.java b/src/main/java/org/apache/commons/math4/analysis/differentiation/DSCompiler.java index 03898ac821..a5160efc5b 100644 --- a/src/main/java/org/apache/commons/math4/analysis/differentiation/DSCompiler.java +++ b/src/main/java/org/apache/commons/math4/analysis/differentiation/DSCompiler.java @@ -1497,11 +1497,11 @@ public void cosh(final double[] operand, final int operandOffset, // create the function value and derivatives double[] function = new double[1 + order]; - function[0] = FastMath.cosh(operand[operandOffset]); + final double function0 = function[0] = FastMath.cosh(operand[operandOffset]); if (order > 0) { - function[1] = FastMath.sinh(operand[operandOffset]); + final double function1 = function[1] = FastMath.sinh(operand[operandOffset]); for (int i = 2; i <= order; ++i) { - function[i] = function[i - 2]; + function[i] = (i & 1) == 0 ? function0 : function1; } } @@ -1523,11 +1523,11 @@ public void sinh(final double[] operand, final int operandOffset, // create the function value and derivatives double[] function = new double[1 + order]; - function[0] = FastMath.sinh(operand[operandOffset]); + final double function0 = function[0] = FastMath.sinh(operand[operandOffset]); if (order > 0) { - function[1] = FastMath.cosh(operand[operandOffset]); + final double function1 = function[1] = FastMath.cosh(operand[operandOffset]); for (int i = 2; i <= order; ++i) { - function[i] = function[i - 2]; + function[i] = (i & 1) == 0 ? function0 : function1; } } diff --git a/src/main/java/org/apache/commons/math4/analysis/function/Logit.java b/src/main/java/org/apache/commons/math4/analysis/function/Logit.java index 55b1fd9a8d..cd722e4064 100644 --- a/src/main/java/org/apache/commons/math4/analysis/function/Logit.java +++ b/src/main/java/org/apache/commons/math4/analysis/function/Logit.java @@ -170,22 +170,20 @@ public DerivativeStructure value(final DerivativeStructure t) double[] f = new double[t.getOrder() + 1]; // function value - f[0] = FastMath.log((x - lo) / (hi - x)); - - if (Double.isInfinite(f[0])) { - - if (f.length > 1) { - f[1] = Double.POSITIVE_INFINITY; + final double f0 = f[0] = FastMath.log((x - lo) / (hi - x)); + final int fLen = f.length; + if (Double.isInfinite(f0)) { + if (fLen > 1) { + final double f1 = f[1] = Double.POSITIVE_INFINITY; + // fill the array with infinities + // (for x close to lo the signs will flip between -inf and +inf, + // for x close to hi the signs will always be +inf) + // this is probably overkill, since the call to compose at the end + // of the method will transform most infinities into NaN ... + for (int i = 2; i < fLen; ++i) { + f[i] = (i & 1) == 0 ? f0 : f1; + } } - // fill the array with infinities - // (for x close to lo the signs will flip between -inf and +inf, - // for x close to hi the signs will always be +inf) - // this is probably overkill, since the call to compose at the end - // of the method will transform most infinities into NaN ... - for (int i = 2; i < f.length; ++i) { - f[i] = f[i - 2]; - } - } else { // function derivatives