Search in sources :

Example 1 with Gradient1Procedure

use of gdsc.smlm.function.Gradient1Procedure in project GDSC-SMLM by aherbert.

the class Gaussian2DFunction method computeValuesAndJacobian.

/*
	 * (non-Javadoc)
	 * 
	 * @see gdsc.smlm.function.ExtendedNonLinearFunction#computeValuesAndJacobian(double[])
	 */
public Pair<double[], double[][]> computeValuesAndJacobian(double[] variables) {
    initialise1(variables);
    final int n = size();
    final double[][] jacobian = new double[n][];
    final double[] values = new double[n];
    forEach(new Gradient1Procedure() {

        int i = 0;

        public void execute(double value, double[] dy_da) {
            values[i] = value;
            jacobian[i++] = dy_da.clone();
        }
    });
    return new Pair<double[], double[][]>(values, jacobian);
}
Also used : Gradient1Procedure(gdsc.smlm.function.Gradient1Procedure) Pair(org.apache.commons.math3.util.Pair)

Example 2 with Gradient1Procedure

use of gdsc.smlm.function.Gradient1Procedure in project GDSC-SMLM by aherbert.

the class ErfGaussian2DFunctionTest method functionComputesGradientForEachWith2Peaks.

@Test
public void functionComputesGradientForEachWith2Peaks() {
    org.junit.Assume.assumeNotNull(f2);
    final ErfGaussian2DFunction f2 = (ErfGaussian2DFunction) this.f2;
    final int n = f2.size();
    double[] du_da = new double[f2.getNumberOfGradients()];
    double[] du_db = new double[f2.getNumberOfGradients()];
    final double[] d2u_da2 = new double[f2.getNumberOfGradients()];
    final double[] values = new double[n];
    final double[][] jacobian = new double[n][];
    final double[][] jacobian2 = new double[n][];
    for (double background : testbackground) // Peak 1
    for (double amplitude1 : testamplitude1) for (double shape1 : testshape1) for (double cx1 : testcx1) for (double cy1 : testcy1) for (double[] w1 : testw1) // Peak 2
    for (double amplitude2 : testamplitude2) for (double shape2 : testshape2) for (double cx2 : testcx2) for (double cy2 : testcy2) for (double[] w2 : testw2) {
        double[] a = createParameters(background, amplitude1, shape1, cx1, cy1, w1[0], w1[1], amplitude2, shape2, cx2, cy2, w2[0], w2[1]);
        f2.initialiseExtended2(a);
        // Compute single
        for (int i = 0; i < n; i++) {
            double o1 = f2.eval(i, du_da);
            double o2 = f2.eval(i, du_db, d2u_da2);
            Assert.assertEquals("Value", o1, o2, 1e-10);
            Assert.assertArrayEquals("Jacobian!=Jacobian", du_da, du_db, 1e-10);
            values[i] = o1;
            jacobian[i] = du_da.clone();
            jacobian2[i] = d2u_da2.clone();
        }
        // Use procedures
        f2.forEach(new ValueProcedure() {

            int i = 0;

            public void execute(double value) {
                Assert.assertEquals("Value ValueProcedure", values[i], value, 1e-10);
                i++;
            }
        });
        f2.forEach(new Gradient1Procedure() {

            int i = 0;

            public void execute(double value, double[] dy_da) {
                Assert.assertEquals("Value Gradient1Procedure", values[i], value, 1e-10);
                Assert.assertArrayEquals("du_da Gradient1Procedure", jacobian[i], dy_da, 1e-10);
                i++;
            }
        });
        f2.forEach(new Gradient2Procedure() {

            int i = 0;

            public void execute(double value, double[] dy_da, double[] d2y_da2) {
                Assert.assertEquals("Value Gradient2Procedure", values[i], value, 1e-10);
                Assert.assertArrayEquals("du_da Gradient2Procedure", jacobian[i], dy_da, 1e-10);
                Assert.assertArrayEquals("d2u_da2 Gradient2Procedure", jacobian2[i], d2y_da2, 1e-10);
                i++;
            }
        });
        f2.forEach(new ExtendedGradient2Procedure() {

            int i = 0;

            public void executeExtended(double value, double[] dy_da, double[] d2y_dadb) {
                Assert.assertEquals("Value ExtendedGradient2Procedure", values[i], value, 1e-10);
                Assert.assertArrayEquals("du_da ExtendedGradient2Procedure", jacobian[i], dy_da, 1e-10);
                for (int j = 0, k = 0; j < d2u_da2.length; j++, k += d2u_da2.length + 1) d2u_da2[j] = d2y_dadb[k];
                Assert.assertArrayEquals("d2u_da2 Gradient2Procedure", jacobian2[i], d2u_da2, 1e-10);
                i++;
            }
        });
    }
}
Also used : ValueProcedure(gdsc.smlm.function.ValueProcedure) Gradient2Procedure(gdsc.smlm.function.Gradient2Procedure) ExtendedGradient2Procedure(gdsc.smlm.function.ExtendedGradient2Procedure) ExtendedGradient2Procedure(gdsc.smlm.function.ExtendedGradient2Procedure) Gradient1Procedure(gdsc.smlm.function.Gradient1Procedure) Gaussian2DFunctionTest(gdsc.smlm.function.gaussian.Gaussian2DFunctionTest) Test(org.junit.Test)

Aggregations

Gradient1Procedure (gdsc.smlm.function.Gradient1Procedure)2 ExtendedGradient2Procedure (gdsc.smlm.function.ExtendedGradient2Procedure)1 Gradient2Procedure (gdsc.smlm.function.Gradient2Procedure)1 ValueProcedure (gdsc.smlm.function.ValueProcedure)1 Gaussian2DFunctionTest (gdsc.smlm.function.gaussian.Gaussian2DFunctionTest)1 Pair (org.apache.commons.math3.util.Pair)1 Test (org.junit.Test)1