| 46 | } |
| 47 | |
| 48 | public static void testGradAndHessian(final VectorFn f, final double[] x, final double epsilon, final double tol) { |
| 49 | final VEval fx = f.eval(x,true,true); |
| 50 | //System.out.println("fx: " + fx); |
| 51 | final VEval fn = numericlyBuildGradientAndHessian(f,x,epsilon); |
| 52 | //System.out.println("fn: " + fn); |
| 53 | final int dim = f.dim(); |
| 54 | for(int i=0;i<dim;++i) { |
| 55 | if(Math.abs(fx.gx[i]-fn.gx[i])>=tol) { //extra if lets us set a breakpoint |
| 56 | assertTrue(Math.abs(fx.gx[i]-fn.gx[i])<tol); |
| 57 | } |
| 58 | for(int j=0;j<dim;++j) { |
| 59 | if(Math.abs(fx.hx[i][j]-fn.hx[i][j])>=tol) { //extra if lets us set a breakpoint |
| 60 | assertTrue(Math.abs(fx.hx[i][j]-fn.hx[i][j])<tol); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | public void testNormV() { |