for a large number of params, avoid copying array.

also fixes an incorrect javadoc comment.
This commit is contained in:
adrian 2018-05-17 13:03:47 -04:00
parent eb57d6d457
commit f90d29262b
2 changed files with 14 additions and 16 deletions

View File

@ -64,8 +64,9 @@ public class DifferentiatorMultivariateJacobianFunction implements MultivariateJ
*/ */
@Override @Override
public Pair<RealVector, RealMatrix> value(RealVector point) { public Pair<RealVector, RealMatrix> value(RealVector point) {
double[] testArray = point.toArray();
ArrayRealVector value = new ArrayRealVector(1); 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()); RealMatrix jacobian = new Array2DRowRealMatrix(1, point.getDimension());
for(int column = 0; column < point.getDimension(); column++) { for(int column = 0; column < point.getDimension(); column++) {
@ -73,15 +74,13 @@ public class DifferentiatorMultivariateJacobianFunction implements MultivariateJ
double originalPoint = point.getEntry(column); double originalPoint = point.getEntry(column);
double partialDerivative = getPartialDerivative(testPoint -> { double partialDerivative = getPartialDerivative(testPoint -> {
point.setEntry(columnFinal, testPoint); testArray[columnFinal] = testPoint;
double testPointOutput = function.value(point.toArray()); return function.value(testArray);
point.setEntry(columnFinal, originalPoint); //set it back
return testPointOutput;
}, originalPoint); }, originalPoint);
testArray[column] = originalPoint; //set it back
jacobian.setEntry(0, column, partialDerivative); jacobian.setEntry(0, column, partialDerivative);
} }

View File

@ -51,8 +51,8 @@ public class DifferentiatorVectorMultivariateJacobianFunction implements Multiva
* @param function the function to turn into a jacobian * @param function the function to turn into a jacobian
* @param differentiator the differentiator to find the derivative * @param differentiator the differentiator to find the derivative
* *
* This version that works with MultivariateFunction * This version that works with MultivariateVectorFunction
* @see DifferentiatorVectorMultivariateJacobianFunction for version that works with MultivariateVectorFunction * @see DifferentiatorMultivariateJacobianFunction for version that works with MultivariateFunction
*/ */
public DifferentiatorVectorMultivariateJacobianFunction(MultivariateVectorFunction function, UnivariateVectorFunctionDifferentiator differentiator) { public DifferentiatorVectorMultivariateJacobianFunction(MultivariateVectorFunction function, UnivariateVectorFunctionDifferentiator differentiator) {
this.function = function; this.function = function;
@ -64,7 +64,8 @@ public class DifferentiatorVectorMultivariateJacobianFunction implements Multiva
*/ */
@Override @Override
public Pair<RealVector, RealMatrix> value(RealVector point) { 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()); RealMatrix jacobian = new Array2DRowRealMatrix(value.getDimension(), point.getDimension());
for(int column = 0; column < point.getDimension(); column++) { for(int column = 0; column < point.getDimension(); column++) {
@ -72,15 +73,13 @@ public class DifferentiatorVectorMultivariateJacobianFunction implements Multiva
double originalPoint = point.getEntry(column); double originalPoint = point.getEntry(column);
double[] partialDerivatives = getPartialDerivative(testPoint -> { double[] partialDerivatives = getPartialDerivative(testPoint -> {
point.setEntry(columnFinal, testPoint); testArray[columnFinal] = testPoint;
double[] testPointValue = function.value(point.toArray()); return function.value(testArray);
point.setEntry(columnFinal, originalPoint); //set it back
return testPointValue;
}, originalPoint); }, originalPoint);
testArray[column] = originalPoint; //set it back
jacobian.setColumn(column, partialDerivatives); jacobian.setColumn(column, partialDerivatives);
} }