/** * Creates an instance using the default values. */ public VectorFieldSecondOrderDifferentiator() { this.eps = DEFAULT_EPS; this.epsSqr = eps * eps; this.vectorFieldDiff = new VectorFieldFirstOrderDifferentiator(eps); this.maxtrixFieldDiff = new MatrixFieldFirstOrderDifferentiator(eps); }
/** * Create an instance of the finite difference calculator. * * @param fdType the finite difference type * @param shift the shift to be applied to the curves */ private CurveGammaCalculator(FiniteDifferenceType fdType, double shift) { this.fd = new VectorFieldFirstOrderDifferentiator(fdType, shift); }
@Override public Function<DoubleArray, DoubleMatrix[]> differentiate( Function<DoubleArray, DoubleArray> function, Function<DoubleArray, Boolean> domain) { ArgChecker.notNull(function, "function"); Function<DoubleArray, DoubleMatrix> jacFunc = vectorFieldDiff.differentiate(function, domain); Function<DoubleArray, DoubleMatrix[]> hFunc = maxtrixFieldDiff.differentiate(jacFunc, domain); return new Function<DoubleArray, DoubleMatrix[]>() { @SuppressWarnings("synthetic-access") @Override public DoubleMatrix[] apply(DoubleArray x) { DoubleMatrix[] gamma = hFunc.apply(x); return reshapeTensor(gamma); } }; }
@Override public DoubleArray findRoot(Function<DoubleArray, DoubleArray> function, DoubleArray startPosition) { VectorFieldFirstOrderDifferentiator jac = new VectorFieldFirstOrderDifferentiator(); return findRoot(function, jac.differentiate(function), startPosition); }
@Override public DoubleArray apply(Double x) { Function<DoubleArray, Double> f = asFunctionOfParameters(x); Function<DoubleArray, DoubleArray> g = FIRST_ORDER_DIFF.differentiate(f); return g.apply(params); } };
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction() { CENTRAL.differentiate((Function<DoubleArray, DoubleArray>) null); }
public void testNullFunction() { assertThrowsIllegalArg(() -> CALC.differentiate((Function<Double, Double>) null)); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction() { CENTRAL.differentiate((Function<Double, Double>) null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullDifferenceType() { new ScalarFirstOrderDifferentiator(null); }
/** * Use this when the model is given as a function of its parameters only (i.e. a function that takes a set of * parameters and return a set of model values, * so the measurement points are already known to the function), and analytic parameter sensitivity is not available * @param observedValues Set of measurement values * @param sigma Set of measurement errors * @param func The model as a function of its parameters only * @param startPos Initial value of the parameters * @return value of the fitted parameters */ public LeastSquareResults solve( DoubleArray observedValues, DoubleArray sigma, Function<DoubleArray, DoubleArray> func, DoubleArray startPos) { VectorFieldFirstOrderDifferentiator jac = new VectorFieldFirstOrderDifferentiator(); return solve(observedValues, sigma, func, jac.differentiate(func), startPos, null); }
/** * This computes the second derivative of a vector field, which is a rank 3 tensor field. * The tensor is represented as an array of DoubleMatrix, where each matrix is * a Hessian (for the dependent variable y_i), so the indexing is * H^i_{j,k} =\partial^2y_i/\partial x_j \partial x_k * * @param function the function representing the vector field * @return a function representing the second derivative of the vector field (i.e. a rank 3 tensor field) */ @Override public Function<DoubleArray, DoubleMatrix[]> differentiate( Function<DoubleArray, DoubleArray> function) { ArgChecker.notNull(function, "function"); Function<DoubleArray, DoubleMatrix> jacFunc = vectorFieldDiff.differentiate(function); Function<DoubleArray, DoubleMatrix[]> hFunc = maxtrixFieldDiff.differentiate(jacFunc); return new Function<DoubleArray, DoubleMatrix[]>() { @SuppressWarnings("synthetic-access") @Override public DoubleMatrix[] apply(DoubleArray x) { DoubleMatrix[] gamma = hFunc.apply(x); return reshapeTensor(gamma); } }; }
@Override public DoubleArray apply(DoublesPair xy) { Function<DoubleArray, Double> f = asFunctionOfParameters(xy); Function<DoubleArray, DoubleArray> g = FIRST_ORDER_DIFF.differentiate(f); return g.apply(params); } };
public void testDomainOut() { Function<Double, Boolean> domain = new Function<Double, Boolean>() { @Override public Boolean apply(final Double x) { return x >= 0d && x <= 1.0e-8; } }; assertThrowsIllegalArg(() -> CALC.differentiate(F, domain).apply(1.0e-9)); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullDifferenceType() { new ScalarFirstOrderDifferentiator(null); }
/** * Use this when the model is given as a function of its parameters only (i.e. a function that takes a set of * parameters and return a set of model values, * so the measurement points are already known to the function), and analytic parameter sensitivity is not available * @param observedValues Set of measurement values * @param sigma Set of measurement errors * @param func The model as a function of its parameters only * @param startPos Initial value of the parameters * @param maxJumps A vector containing the maximum absolute allowed step in a particular direction in each iteration. * Can be null, in which case no constant * on the step size is applied. * @return value of the fitted parameters */ public LeastSquareResults solve( DoubleArray observedValues, DoubleArray sigma, Function<DoubleArray, DoubleArray> func, DoubleArray startPos, DoubleArray maxJumps) { VectorFieldFirstOrderDifferentiator jac = new VectorFieldFirstOrderDifferentiator(); return solve(observedValues, sigma, func, jac.differentiate(func), startPos, maxJumps); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction() { CENTRAL.differentiate((Function<DoubleArray, Double>) null); }
public void domainTest() { final double[] x = new double[] {1.2, 0, Math.PI }; final Function<Double, Double> alFunc = CALC.differentiate(F, DOMAIN); for (int i = 0; i < 3; i++) { assertEquals(alFunc.apply(x[i]), DX_ANALYTIC.apply(x[i]), EPS); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullDifferenceType() { new ScalarFirstOrderDifferentiator(null); }
public void analyticTest() { final double x = 0.2245; assertEquals(CALC.differentiate(F).apply(x), DX_ANALYTIC.apply(x), EPS); }
public void testNullDifferenceType() { assertThrowsIllegalArg(() -> new ScalarFirstOrderDifferentiator(null)); }