for a large number of params, avoid copying array.
authoradrian <adrian-sw@aporter.org>
Thu, 17 May 2018 17:03:47 +0000 (13:03 -0400)
committeradrian <adrian-sw@aporter.org>
Thu, 28 Jun 2018 18:40:09 +0000 (14:40 -0400)
also fixes an incorrect javadoc comment.

src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorMultivariateJacobianFunction.java
src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorVectorMultivariateJacobianFunction.java

index 313571d..12ebcf5 100644 (file)
@@ -64,8 +64,9 @@ public class DifferentiatorMultivariateJacobianFunction implements MultivariateJ
      */
     @Override
     public Pair<RealVector, RealMatrix> 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);
         }
 
index 2f8295a..05e1267 100644 (file)
@@ -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<RealVector, RealMatrix> 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);
         }