Search in sources :

Example 1 with DblDbl_Dbl

use of suite.primitive.DblDbl_Dbl in project suite by stupidsing.

the class PlotMain method run.

@Override
protected boolean run(String[] args) {
    int size = 1024;
    double scale = 1d / (size + 1);
    DblDbl_Dbl variety = (x, y) -> {
        return y * y - (x + .25f) * (x + .15f) * (x + .05f) * (x - .05f) * (x - .15f) * (x - .25f);
    };
    IntInt_Int fp = (fx, fy) -> {
        double x0 = fx * scale - .5d;
        double y0 = fy * scale - .5d;
        double value = variety.apply(x0, y0);
        if (Double.isNaN(value))
            return -2;
        else if (value < 0)
            return -1;
        else
            return 1;
    };
    return // 
    new Render().renderPixels(size, size, (fx, fy) -> {
        int b0 = fp.apply(fx, fy);
        int b1 = fp.apply(fx + 1, fy);
        int b2 = fp.apply(fx, fy + 1);
        int b3 = fp.apply(fx + 1, fy + 1);
        double c = b0 != b1 || b1 != b2 || b2 != b3 ? 1d : 0d;
        return new R3(c, c, c);
    }).view();
}
Also used : IntInt_Int(suite.primitive.IntInt_Int) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) DblDbl_Dbl(suite.primitive.DblDbl_Dbl) Render(suite.image.Render) RunUtil(suite.util.RunUtil) Render(suite.image.Render) DblDbl_Dbl(suite.primitive.DblDbl_Dbl) IntInt_Int(suite.primitive.IntInt_Int)

Example 2 with DblDbl_Dbl

use of suite.primitive.DblDbl_Dbl in project suite by stupidsing.

the class Bfgs method lineSearch.

private double lineSearch(Dbl_Dbl phi, Dbl_Dbl phiGradient, double alphax) {
    double c1 = .0001d;
    double c2 = .1d;
    double alpha0 = 0d;
    double v0 = phi.apply(alpha0);
    double g0 = phiGradient.apply(alpha0);
    // TODO
    DblDbl_Dbl interpolate = (a0, a1) -> (a0 + a1) * .5d;
    // TODO
    DblDbl_Dbl choose = (a0, a1) -> (a0 + a1) * .5d;
    DblDbl_Dbl zoom = (alphaLo, alphaHi) -> {
        for (int iter = 0; iter < 16; iter++) {
            double alpha = interpolate.apply(alphaLo, alphaHi);
            double v = phi.apply(alpha);
            double g;
            if (v0 + c1 * alpha * g0 < v || phi.apply(alphaLo) <= v)
                alphaHi = alpha;
            else if (Math.abs(g = phiGradient.apply(alpha)) <= -c2 * g0)
                return alpha;
            else {
                if (0d <= g * (alphaHi - alphaLo))
                    alphaHi = alphaLo;
                alphaLo = alpha;
            }
        }
        return alphaLo;
    };
    double alphap = alpha0;
    double vp = v0;
    double alpha = choose.apply(alphap, alphax);
    for (int iter = 0; iter < 16; iter++) {
        double v = phi.apply(alpha);
        if (v0 + c1 * alpha * g0 < v || 0 < iter && vp <= v)
            return zoom.apply(alphap, alpha);
        double g = phiGradient.apply(alpha);
        if (Math.abs(g) <= -c2 * g0)
            break;
        else if (0d <= g)
            return zoom.apply(alpha, alphap);
        else {
            alphap = alpha;
            vp = v;
            alpha = choose.apply(alpha, alphax);
        }
    }
    return alpha;
}
Also used : Dbl_Obj(suite.primitive.DblPrimitives.Dbl_Obj) Dbl_Dbl(suite.primitive.Dbl_Dbl) Vector(suite.math.linalg.Vector) Matrix(suite.math.linalg.Matrix) DblDbl_Dbl(suite.primitive.DblDbl_Dbl) FiniteDifference(suite.math.FiniteDifference) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Fun(suite.util.FunUtil.Fun) DblDbl_Dbl(suite.primitive.DblDbl_Dbl)

Aggregations

DblDbl_Dbl (suite.primitive.DblDbl_Dbl)2 Render (suite.image.Render)1 FiniteDifference (suite.math.FiniteDifference)1 Matrix (suite.math.linalg.Matrix)1 Vector (suite.math.linalg.Vector)1 Dbl_Obj (suite.primitive.DblPrimitives.Dbl_Obj)1 Obj_Dbl (suite.primitive.DblPrimitives.Obj_Dbl)1 Dbl_Dbl (suite.primitive.Dbl_Dbl)1 IntInt_Int (suite.primitive.IntInt_Int)1 Fun (suite.util.FunUtil.Fun)1 RunUtil (suite.util.RunUtil)1 ExecutableProgram (suite.util.RunUtil.ExecutableProgram)1