### Question:
VectorFieldSecondOrderDifferentiator implements Differentiator<DoubleArray, DoubleArray, DoubleMatrix[]> { @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); } }; } VectorFieldSecondOrderDifferentiator(); @Override Function<DoubleArray, DoubleMatrix[]> differentiate(
      Function<DoubleArray, DoubleArray> function); @Override Function<DoubleArray, DoubleMatrix[]> differentiate(
      Function<DoubleArray, DoubleArray> function,
      Function<DoubleArray, Boolean> domain); Function<DoubleArray, DoubleMatrix[]> differentiateFull(
      Function<DoubleArray, DoubleArray> function); Function<DoubleArray, DoubleMatrix> differentiateNoCross(Function<DoubleArray, DoubleArray> function);  }### Answer:
@Test public void test() { double a = 2.3; double theta = 0.34; DoubleArray x = DoubleArray.of(a, theta); VectorFieldSecondOrderDifferentiator fd = new VectorFieldSecondOrderDifferentiator(); Function<DoubleArray, DoubleMatrix[]> fdFuncs = fd.differentiate(FUNC); DoubleMatrix[] fdValues = fdFuncs.apply(x); DoubleMatrix t1 = DW1.apply(x); DoubleMatrix t2 = DW2.apply(x); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { assertThat(t1.get(i, j)).as("first observation " + i + " " + j).isCloseTo(fdValues[0].get(i, j), offset(1e-6)); assertThat(t2.get(i, j)).as("second observation " + i + " " + j).isCloseTo(fdValues[1].get(i, j), offset(1e-6)); } } }
@Test public void outsideDomainTest() { VectorFieldSecondOrderDifferentiator fd = new VectorFieldSecondOrderDifferentiator(); Function<DoubleArray, DoubleMatrix[]> fdFuncs = fd.differentiate(FUNC, DOMAIN); assertThatIllegalArgumentException() .isThrownBy(() -> fdFuncs.apply(DoubleArray.of(-1.0, 0.3))); }
@Test public void domainTest() { DoubleArray[] x = new DoubleArray[4]; x[0] = DoubleArray.of(2.3, 0.34); x[1] = DoubleArray.of(1e-8, 1.45); x[2] = DoubleArray.of(1.2, 0.0); x[3] = DoubleArray.of(1.2, Math.PI); VectorFieldSecondOrderDifferentiator fd = new VectorFieldSecondOrderDifferentiator(); Function<DoubleArray, DoubleMatrix[]> fdFuncs = fd.differentiate(FUNC, DOMAIN); for (int k = 0; k < 4; k++) { DoubleMatrix[] fdValues = fdFuncs.apply(x[k]); DoubleMatrix t1 = DW1.apply(x[k]); DoubleMatrix t2 = DW2.apply(x[k]); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { assertThat(t1.get(i, j)).as("first observation " + i + " " + j).isCloseTo(fdValues[0].get(i, j), offset(1e-6)); assertThat(t2.get(i, j)).as("second observation " + i + " " + j).isCloseTo(fdValues[1].get(i, j), offset(1e-6)); } } } }