VectorFieldSecondOrderDifferentiator implements Differentiator<DoubleMatrix1D, DoubleMatrix1D, DoubleMatrix2D[]> { @Override public Function1D<DoubleMatrix1D, DoubleMatrix2D[]> differentiate(final Function1D<DoubleMatrix1D, DoubleMatrix1D> function) { Validate.notNull(function); final Function1D<DoubleMatrix1D, DoubleMatrix2D> jacFunc = _vectorFieldDiff.differentiate(function); final Function1D<DoubleMatrix1D, DoubleMatrix2D[]> hFunc = _maxtrixFieldDiff.differentiate(jacFunc); return new Function1D<DoubleMatrix1D, DoubleMatrix2D[]>() { @SuppressWarnings("synthetic-access") @Override public DoubleMatrix2D[] evaluate(final DoubleMatrix1D x) { DoubleMatrix2D[] gamma = hFunc.evaluate(x); return reshapeTensor(gamma); } }; } VectorFieldSecondOrderDifferentiator(); @Override Function1D<DoubleMatrix1D, DoubleMatrix2D[]> differentiate(final Function1D<DoubleMatrix1D, DoubleMatrix1D> function); @Override Function1D<DoubleMatrix1D, DoubleMatrix2D[]> differentiate(final Function1D<DoubleMatrix1D, DoubleMatrix1D> function, final Function1D<DoubleMatrix1D, Boolean> domain); Function1D<DoubleMatrix1D, DoubleMatrix2D[]> differentiateFull(final Function1D<DoubleMatrix1D, DoubleMatrix1D> function); Function1D<DoubleMatrix1D, DoubleMatrix2D> differentiateNoCross(final Function1D<DoubleMatrix1D, DoubleMatrix1D> function);  }
@Test public void test() { double a = 2.3; double theta = 0.34; DoubleMatrix1D x = new DoubleMatrix1D(new double[] {a, theta }); VectorFieldSecondOrderDifferentiator fd = new VectorFieldSecondOrderDifferentiator(); Function1D<DoubleMatrix1D, DoubleMatrix2D[]> fdFuncs = fd.differentiate(FUNC); DoubleMatrix2D[] fdValues = fdFuncs.evaluate(x); DoubleMatrix2D t1 = DW1.evaluate(x); DoubleMatrix2D t2 = DW2.evaluate(x); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { assertEquals("first observation " + i + " " + j, t1.getEntry(i, j), fdValues[0].getEntry(i, j), 1e-6); assertEquals("second observation " + i + " " + j, t2.getEntry(i, j), fdValues[1].getEntry(i, j), 1e-6); } } }
@Test(expectedExceptions = IllegalArgumentException.class) public void outsideDomainTest() { VectorFieldSecondOrderDifferentiator fd = new VectorFieldSecondOrderDifferentiator(); Function1D<DoubleMatrix1D, DoubleMatrix2D[]> fdFuncs = fd.differentiate(FUNC, DOMAIN); fdFuncs.evaluate(new DoubleMatrix1D(-1.0, 0.3)); }
@Test public void domainTest() { DoubleMatrix1D[] x = new DoubleMatrix1D[4]; x[0] = new DoubleMatrix1D(2.3, 0.34); x[1] = new DoubleMatrix1D(1e-8, 1.45); x[2] = new DoubleMatrix1D(1.2, 0.0); x[3] = new DoubleMatrix1D(1.2, Math.PI); VectorFieldSecondOrderDifferentiator fd = new VectorFieldSecondOrderDifferentiator(); Function1D<DoubleMatrix1D, DoubleMatrix2D[]> fdFuncs = fd.differentiate(FUNC, DOMAIN); for (int k = 0; k < 4; k++) { DoubleMatrix2D[] fdValues = fdFuncs.evaluate(x[k]); DoubleMatrix2D t1 = DW1.evaluate(x[k]); DoubleMatrix2D t2 = DW2.evaluate(x[k]); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { assertEquals("first observation " + i + " " + j, t1.getEntry(i, j), fdValues[0].getEntry(i, j), 1e-6); assertEquals("second observation " + i + " " + j, t2.getEntry(i, j), fdValues[1].getEntry(i, j), 1e-6); } } } }