diff --git a/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorMultivariateJacobianFunction.java b/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorMultivariateJacobianFunction.java index 313571d38..12ebcf5f9 100644 --- a/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorMultivariateJacobianFunction.java +++ b/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorMultivariateJacobianFunction.java @@ -64,8 +64,9 @@ public class DifferentiatorMultivariateJacobianFunction implements MultivariateJ */ @Override public Pair value(RealVector point) { + double[] testArray = point.toArray(); ArrayRealVector value = new ArrayRealVector(1); - value.setEntry(0, function.value(point.toArray())); + value.setEntry(0, function.value(testArray)); RealMatrix jacobian = new Array2DRowRealMatrix(1, point.getDimension()); for(int column = 0; column < point.getDimension(); column++) { @@ -73,15 +74,13 @@ public class DifferentiatorMultivariateJacobianFunction implements MultivariateJ double originalPoint = point.getEntry(column); double partialDerivative = getPartialDerivative(testPoint -> { - point.setEntry(columnFinal, testPoint); + testArray[columnFinal] = testPoint; - double testPointOutput = function.value(point.toArray()); - - point.setEntry(columnFinal, originalPoint); //set it back - - return testPointOutput; + return function.value(testArray); }, originalPoint); + testArray[column] = originalPoint; //set it back + jacobian.setEntry(0, column, partialDerivative); } diff --git a/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorVectorMultivariateJacobianFunction.java b/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorVectorMultivariateJacobianFunction.java index 2f8295aa8..05e1267c2 100644 --- a/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorVectorMultivariateJacobianFunction.java +++ b/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorVectorMultivariateJacobianFunction.java @@ -51,8 +51,8 @@ public class DifferentiatorVectorMultivariateJacobianFunction implements Multiva * @param function the function to turn into a jacobian * @param differentiator the differentiator to find the derivative * - * This version that works with MultivariateFunction - * @see DifferentiatorVectorMultivariateJacobianFunction for version that works with MultivariateVectorFunction + * This version that works with MultivariateVectorFunction + * @see DifferentiatorMultivariateJacobianFunction for version that works with MultivariateFunction */ public DifferentiatorVectorMultivariateJacobianFunction(MultivariateVectorFunction function, UnivariateVectorFunctionDifferentiator differentiator) { this.function = function; @@ -64,7 +64,8 @@ public class DifferentiatorVectorMultivariateJacobianFunction implements Multiva */ @Override public Pair value(RealVector point) { - RealVector value = new ArrayRealVector(function.value(point.toArray())); + double[] testArray = point.toArray(); + RealVector value = new ArrayRealVector(function.value(testArray)); RealMatrix jacobian = new Array2DRowRealMatrix(value.getDimension(), point.getDimension()); for(int column = 0; column < point.getDimension(); column++) { @@ -72,15 +73,13 @@ public class DifferentiatorVectorMultivariateJacobianFunction implements Multiva double originalPoint = point.getEntry(column); double[] partialDerivatives = getPartialDerivative(testPoint -> { - point.setEntry(columnFinal, testPoint); + testArray[columnFinal] = testPoint; - double[] testPointValue = function.value(point.toArray()); - - point.setEntry(columnFinal, originalPoint); //set it back - - return testPointValue; + return function.value(testArray); }, originalPoint); + testArray[column] = originalPoint; //set it back + jacobian.setColumn(column, partialDerivatives); }