Skip to content

Commit 33f3324

Browse files
committed
refine some codes dealing with filling an array with its first and second elements.
1 parent ba8a267 commit 33f3324

2 files changed

Lines changed: 19 additions & 21 deletions

File tree

src/main/java/org/apache/commons/math4/analysis/differentiation/DSCompiler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,11 +1497,11 @@ public void cosh(final double[] operand, final int operandOffset,
14971497

14981498
// create the function value and derivatives
14991499
double[] function = new double[1 + order];
1500-
function[0] = FastMath.cosh(operand[operandOffset]);
1500+
final double function0 = function[0] = FastMath.cosh(operand[operandOffset]);
15011501
if (order > 0) {
1502-
function[1] = FastMath.sinh(operand[operandOffset]);
1502+
final double function1 = function[1] = FastMath.sinh(operand[operandOffset]);
15031503
for (int i = 2; i <= order; ++i) {
1504-
function[i] = function[i - 2];
1504+
function[i] = (i & 1) == 0 ? function0 : function1;
15051505
}
15061506
}
15071507

@@ -1523,11 +1523,11 @@ public void sinh(final double[] operand, final int operandOffset,
15231523

15241524
// create the function value and derivatives
15251525
double[] function = new double[1 + order];
1526-
function[0] = FastMath.sinh(operand[operandOffset]);
1526+
final double function0 = function[0] = FastMath.sinh(operand[operandOffset]);
15271527
if (order > 0) {
1528-
function[1] = FastMath.cosh(operand[operandOffset]);
1528+
final double function1 = function[1] = FastMath.cosh(operand[operandOffset]);
15291529
for (int i = 2; i <= order; ++i) {
1530-
function[i] = function[i - 2];
1530+
function[i] = (i & 1) == 0 ? function0 : function1;
15311531
}
15321532
}
15331533

src/main/java/org/apache/commons/math4/analysis/function/Logit.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,20 @@ public DerivativeStructure value(final DerivativeStructure t)
170170
double[] f = new double[t.getOrder() + 1];
171171

172172
// function value
173-
f[0] = FastMath.log((x - lo) / (hi - x));
174-
175-
if (Double.isInfinite(f[0])) {
176-
177-
if (f.length > 1) {
178-
f[1] = Double.POSITIVE_INFINITY;
173+
final double f0 = f[0] = FastMath.log((x - lo) / (hi - x));
174+
final int fLen = f.length;
175+
if (Double.isInfinite(f0)) {
176+
if (fLen > 1) {
177+
final double f1 = f[1] = Double.POSITIVE_INFINITY;
178+
// fill the array with infinities
179+
// (for x close to lo the signs will flip between -inf and +inf,
180+
// for x close to hi the signs will always be +inf)
181+
// this is probably overkill, since the call to compose at the end
182+
// of the method will transform most infinities into NaN ...
183+
for (int i = 2; i < fLen; ++i) {
184+
f[i] = (i & 1) == 0 ? f0 : f1;
185+
}
179186
}
180-
// fill the array with infinities
181-
// (for x close to lo the signs will flip between -inf and +inf,
182-
// for x close to hi the signs will always be +inf)
183-
// this is probably overkill, since the call to compose at the end
184-
// of the method will transform most infinities into NaN ...
185-
for (int i = 2; i < f.length; ++i) {
186-
f[i] = f[i - 2];
187-
}
188-
189187
} else {
190188

191189
// function derivatives

0 commit comments

Comments
 (0)