Skip to content

Commit

Permalink
use System.arraycopy instead of loop for better performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
XenoAmess committed Jun 6, 2020
1 parent 715d89d commit 10d0186
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 60 deletions.
Expand Up @@ -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;
}
}

Expand All @@ -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;
}
}

Expand Down
28 changes: 13 additions & 15 deletions src/main/java/org/apache/commons/math4/analysis/function/Logit.java
Expand Up @@ -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
Expand Down
Expand Up @@ -275,9 +275,7 @@ public Complex[] solveAll(Complex coefficients[], Complex initial)
}
// Coefficients for deflated polynomial.
final Complex c[] = new Complex[n + 1];
for (int i = 0; i <= n; i++) {
c[i] = coefficients[i];
}
System.arraycopy(coefficients, 0, c, 0, n + 1);

// Solve individual roots successively.
final Complex root[] = new Complex[n];
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/org/apache/commons/math4/dfp/Dfp.java
Expand Up @@ -668,8 +668,8 @@ public Dfp getTwo() {
/** Shift the mantissa left, and adjust the exponent to compensate.
*/
protected void shiftLeft() {
for (int i = mant.length - 1; i > 0; i--) {
mant[i] = mant[i-1];
if (mant.length - 1 >= 0) {
System.arraycopy(mant, 0, mant, 1, mant.length - 1);
}
mant[0] = 0;
exp--;
Expand All @@ -680,8 +680,8 @@ uses shiftRight() */
/** Shift the mantissa right, and adjust the exponent to compensate.
*/
protected void shiftRight() {
for (int i = 0; i < mant.length - 1; i++) {
mant[i] = mant[i+1];
if (mant.length - 1 >= 0) {
System.arraycopy(mant, 1, mant, 0, mant.length - 1);
}
mant[mant.length - 1] = 0;
exp++;
Expand Down Expand Up @@ -1863,9 +1863,7 @@ public Dfp divide(Dfp divisor) {

/* move the remainder into the dividend while left shifting */
dividend[0] = 0;
for (int i = 0; i < mant.length; i++) {
dividend[i + 1] = remainder[i];
}
System.arraycopy(remainder, 0, dividend, 1, mant.length);
}

/* Find the most sig digit */
Expand Down
Expand Up @@ -348,9 +348,8 @@ public Optimum optimize(final LeastSquaresProblem problem) {

//residuals already have weights applied
double[] weightedResidual = currentResiduals;
for (int i = 0; i < nR; i++) {
qtf[i] = weightedResidual[i];
}
if (nR >= 0)
System.arraycopy(weightedResidual, 0, qtf, 0, nR);

// compute Qt.res
qTy(qtf, internalData);
Expand Down
Expand Up @@ -150,9 +150,7 @@ public RealMatrix getH() {
}

// copy upper triangular part of the matrix
for (int j = i; j < m; ++j) {
h[i][j] = householderVectors[i][j];
}
System.arraycopy(householderVectors[i], i, h[i], i, m - i);
}
cachedH = MatrixUtils.createRealMatrix(h);
}
Expand Down
Expand Up @@ -237,9 +237,8 @@ private NeuronSquareMesh2D(boolean wrapRowDim,
public synchronized NeuronSquareMesh2D copy() {
final long[][] idGrid = new long[numberOfRows][numberOfColumns];
for (int r = 0; r < numberOfRows; r++) {
for (int c = 0; c < numberOfColumns; c++) {
idGrid[r][c] = identifiers[r][c];
}
if (numberOfColumns >= 0)
System.arraycopy(identifiers[r], 0, idGrid[r], 0, numberOfColumns);
}

return new NeuronSquareMesh2D(wrapRows,
Expand Down
Expand Up @@ -170,9 +170,9 @@ private AdamsNordsieckFieldTransformer(final Field<T> field, final int n) {
// Nordsieck to multistep, then shifting rows to represent step advance
// then applying inverse transform
T[][] shiftedP = bigP.getData();
for (int i = shiftedP.length - 1; i > 0; --i) {
// shift rows
shiftedP[i] = shiftedP[i - 1];
// shift rows
if (shiftedP.length - 1 >= 0) {
System.arraycopy(shiftedP, 0, shiftedP, 1, shiftedP.length - 1);
}
shiftedP[0] = MathArrays.buildArray(field, rows);
Arrays.fill(shiftedP[0], field.getZero());
Expand Down
Expand Up @@ -811,8 +811,8 @@ private void updateBD(double negccov) {
* @param val Current best fitness value.
*/
private static void push(double[] vals, double val) {
for (int i = vals.length-1; i > 0; i--) {
vals[i] = vals[i-1];
if (vals.length - 1 >= 0) {
System.arraycopy(vals, 0, vals, 1, vals.length - 1);
}
vals[0] = val;
}
Expand Down
Expand Up @@ -406,9 +406,7 @@ public void testSetSubVectorSameType() {
final int index = 2;
actual.setSubVector(index, create(sub));

for (int i = 0; i < sub.length; i++){
expected[index + i] = sub[i];
}
System.arraycopy(sub, 0, expected, 2, sub.length);
TestUtils.assertEquals("", expected, actual, 0d);
}

Expand All @@ -421,9 +419,7 @@ public void testSetSubVectorMixedType() {
final int index = 2;
actual.setSubVector(index, createAlien(sub));

for (int i = 0; i < sub.length; i++){
expected[index + i] = sub[i];
}
System.arraycopy(sub, 0, expected, 2, sub.length);
TestUtils.assertEquals("", expected, actual, 0d);
}

Expand Down
Expand Up @@ -61,9 +61,7 @@ public void testSumSinc() {
double[] init = new double[dim];

// Initial is minimum.
for (int i = 0; i < dim; i++) {
init[i] = minPoint[i];
}
System.arraycopy(minPoint, 0, init, 0, dim);
doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-9);

// Initial is far from minimum.
Expand Down Expand Up @@ -96,9 +94,7 @@ public double value(double[] x) {
double[] init = new double[dim];

// Initial is minimum.
for (int i = 0; i < dim; i++) {
init[i] = minPoint[i];
}
System.arraycopy(minPoint, 0, init, 0, dim);
doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-8);

// Initial is far from minimum.
Expand Down Expand Up @@ -128,9 +124,7 @@ public double value(double[] x) {
double[] init = new double[dim];

// Initial is minimum.
for (int i = 0; i < dim; i++) {
init[i] = maxPoint[i];
}
System.arraycopy(maxPoint, 0, init, 0, dim);
doTest(func, maxPoint, init, GoalType.MAXIMIZE, 1e-9, 1e-8);

// Initial is far from minimum.
Expand Down

0 comments on commit 10d0186

Please sign in to comment.