当前位置: 首页>>代码示例>>Java>>正文


Java Function类代码示例

本文整理汇总了Java中de.linearbits.newtonraphson.Function的典型用法代码示例。如果您正苦于以下问题:Java Function类的具体用法?Java Function怎么用?Java Function使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Function类属于de.linearbits.newtonraphson包,在下文中一共展示了Function类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getObjectFunction

import de.linearbits.newtonraphson.Function; //导入依赖的package包/类
/**
 * Returns the object function
 * 
 * @param k
 * @param f
 * @param c1
 * @param c2
 * @return
 */
private Function<Vector2D, Vector2D> getObjectFunction(final double k,
                                                       final double f,
                                                       final double c1,
                                                       final double c2) {

    return new Function<Vector2D, Vector2D>() {
        public Vector2D evaluate(Vector2D input) {
            // The derivatives of the following formulas have been obtained using Matlab
            final double a = input.x;
            final double b = input.y;
            final double dividend = (1 - f) * (1 - b);

            // Original equations to determine the value of the parameters alpha and beta in the SNB Model
            Vector2D result = new Vector2D();
            result.x = k * f * Math.pow(b / (1 - dividend), a) *
                       (((a * dividend) / (1 - dividend)) + 1) - c1;
            result.y = k * a * Math.pow(b, a) * (f * f) * (1 - b) / 2 *
                       Math.pow(1 - dividend, a + 2) *
                       (2 - (1 - a) * dividend) - c2;
            return result;
        }
    };
}
 
开发者ID:arx-deidentifier,项目名称:arx,代码行数:33,代码来源:ModelSNB.java

示例2: getMasterFunction

import de.linearbits.newtonraphson.Function; //导入依赖的package包/类
/**
 * Returns the master function
 * @return
 */
private static Function<Vector2D, Pair<Vector2D, SquareMatrix2D>> getMasterFunction() {
    
    // Return function
    return new Function<Vector2D, Pair<Vector2D, SquareMatrix2D>>() {

        private final SquareMatrix2D                 derivatives  = new SquareMatrix2D();
        // Prepare result objects
        private final Vector2D                       object       = new Vector2D();
        private final Pair<Vector2D, SquareMatrix2D> result       = new Pair<Vector2D, SquareMatrix2D>(object, derivatives);

        /**
         * Eval
         * @param input
         * @return
         */
        public Pair<Vector2D, SquareMatrix2D> evaluate(Vector2D input) {
            
            // Prepare
            double xSquare = input.x * input.x;
            double ySquare = input.y * input.y;
            
            // Compute
            object.x = 3d * xSquare + 2d * ySquare - 35d;
            object.y = 4d * xSquare - 3d * ySquare - 24d;
            derivatives.x1 = + 6d * input.x;
            derivatives.x2 = + 4d * input.y;
            derivatives.y1 = + 8d * input.x;
            derivatives.y2 = - 6d * input.y;
            
            // Return
            return result;
        }
    };
}
 
开发者ID:prasser,项目名称:newtonraphson,代码行数:39,代码来源:Tests.java

示例3: main

import de.linearbits.newtonraphson.Function; //导入依赖的package包/类
/**
 * Entry point
 * @param args
 */
public static void main(String[] args) {
    
    // First object function : 3 * x^2 + 2 * y^2 - 35 = 0
    // Second object function: 4 * x^2 - 3 * y^2 - 24 = 0
    //
    // This system has four solutions: (+-3, +-2)
    
    /* ****************************
     *  Solve without derivatives *
     ******************************/

    Function2D object1 = getObjectFunction1();
    Function2D object2 = getObjectFunction2();
    
    NewtonRaphson2D solver = new NewtonRaphson2D(object1, object2)
                                                 .accuracy(1e-6)
                                                 .iterationsPerTry(1000)
                                                 .iterationsTotal(100000);
    
    solve(object1, object2, solver, 1000000);

    /* *************************************************
     *  Solve without derivatives but with constraints *
     ***************************************************/

    // We want a solution in the negative range
    Constraint2D constraint = new Constraint2D(){ 
        public Boolean evaluate(Vector2D input) { 
            return input.x < 0 && input.y < 0; 
        }
    };
    
    solver = new NewtonRaphson2D(object1, object2, constraint)
                                 .accuracy(1e-6)
                                 .iterationsPerTry(1000)
                                 .iterationsTotal(100000);
    
    solve(object1, object2, solver, 1000000);
    
    /* *************************
     *  Solve with derivatives *
     ***************************/
    
    Function2D derivative11 = getDerivativeFunction11();
    Function2D derivative12 = getDerivativeFunction12();
    Function2D derivative21 = getDerivativeFunction21();
    Function2D derivative22 = getDerivativeFunction22();

    solver = new NewtonRaphson2D(object1, object2, 
                                 derivative11, derivative12,
                                 derivative21, derivative22)
                                 .accuracy(1e-6)
                                 .iterationsPerTry(1000)
                                 .iterationsTotal(100000);
    
    Function2DUtil util = new Function2DUtil();
    System.out.println("\nChecking derivatives:");
    System.out.println("Is derivative: " + util.isDerivativeFunction1(object1, derivative11, 0.01, 100, 0.001, 0.1d, 0.01d));
    System.out.println("Is derivative: " + util.isDerivativeFunction2(object1, derivative12, 0.01, 100, 0.001, 0.1d, 0.01d));
    System.out.println("Is derivative: " + util.isDerivativeFunction1(object2, derivative21, 0.01, 100, 0.001, 0.1d, 0.01d));
    System.out.println("Is derivative: " + util.isDerivativeFunction2(object2, derivative22, 0.01, 100, 0.001, 0.1d, 0.01d));
    
    solve(object1, object2, solver, 1000000);
    
    /* *****************************
     *  Solve with master function *
     *******************************/
    
    Function<Vector2D, Pair<Vector2D, SquareMatrix2D>> master = getMasterFunction();
    solver = new NewtonRaphson2D(master)
                                 .accuracy(1e-6)
                                 .iterationsPerTry(1000)
                                 .iterationsTotal(100000);
    
    solve(object1, object2, solver, 1000000);
}
 
开发者ID:prasser,项目名称:newtonraphson,代码行数:81,代码来源:Tests.java

示例4: getMasterFunctionClosed

import de.linearbits.newtonraphson.Function; //导入依赖的package包/类
/**
 * Returns the master function including the object function and the
 * derivative functions
 * 
 * @return
 */
private Function<Vector2D, Pair<Vector2D, SquareMatrix2D>>
        getMasterFunctionClosed(final int[] classes,
                                final double u,
                                final double n) {

    return new Function<Vector2D, Pair<Vector2D, SquareMatrix2D>>() {

        // Init
        private final SquareMatrix2D                 derivatives = new SquareMatrix2D();
        private final Vector2D                       object      = new Vector2D();
        private final Pair<Vector2D, SquareMatrix2D> result      = new Pair<Vector2D, SquareMatrix2D>(object,
                                                                                                      derivatives);

        @Override
        public Pair<Vector2D, SquareMatrix2D> evaluate(Vector2D input) {

            // Prepare
            double t = input.x; // Theta
            double a = input.y; // Alpha

            // These closed forms have been verified with Matlab and Mathematica
            double val0 = u - 1d;
            double val1 = Gamma.digamma(val0 + (t / a) + 1d);
            double val2 = Gamma.trigamma((a + t + (a * val0)) / a);
            double val3 = Gamma.trigamma((t / a) + 1d);
            double val4 = Gamma.digamma((t / a) + 1d);
            double val5 = a * a;

            double d1 = (val3 - val2) / (val5);
            double d5 = (((a * val1) + (t * val2)) - (a * val4) - (t * val3)) / (val5 * a);
            double d3 = (((((val5 * val0) - (t * t * val2)) + (t * t * val3)) - 
                        (2d * a * t * val1)) + (2d * a * t * val4)) / (val5 * val5);
            double o1 = (val1 - val4) / a;
            double o3 = ((-t * val1) + (a * val0) + (t * val4)) / (a * a);
            double o2 = Gamma.digamma(n + t) - Gamma.digamma(t + 1d);
            
            checkInterrupt();

            double d2 = Gamma.trigamma(t + 1d) - Gamma.trigamma(n + t);

            // For each class...
            double d4 = 0;
            double o4 = 0;
            double val6 = Gamma.digamma(1d - a);
            double val7 = Gamma.trigamma(1d - a);
            for (int i = 0; i < classes.length; i += 2) {
                int key = classes[i];
                int value = classes[i + 1];

                if (key != 1) {
                    d4 += value * (val7 - Gamma.trigamma(key - a));
                    o4 += value * (Gamma.digamma(key - a) - val6);
                }
                checkInterrupt();
            }

            // Store
            derivatives.x1 = d2 - d1;
            derivatives.x2 = 0d - d5;
            derivatives.y1 = 0d - d5;
            derivatives.y2 = 0d - d3 - d4;
            object.x = o1 - o2;
            object.y = o3 - o4;

            // Return
            return result;
        }
    };
}
 
开发者ID:arx-deidentifier,项目名称:risk-benchmark,代码行数:76,代码来源:ModelPitman.java

示例5: getDerivatives

import de.linearbits.newtonraphson.Function; //导入依赖的package包/类
/**
 * Returns the derivatives
 * 
 * @param k
 * @param f
 * @param c1
 * @param c2
 * @return
 */
private Function<Vector2D, SquareMatrix2D> getDerivatives(final double k,
                                                          final double f,
                                                          final double c1,
                                                          final double c2) {

    return new Function<Vector2D, SquareMatrix2D>() {
        public SquareMatrix2D evaluate(Vector2D input) {

            // The derivation of the following formulas has been obtained using Matlab
            final double a = input.x;
            final double b = input.y;

            final double val0 = (b - 1d) * (f - 1d);
            final double val1 = val0 - 1d;
            final double val2 = 1d - val0;
            final double val3 = a * val0 / val1 - 1d;
            final double val4 = Math.pow(-b / val1, a);
            final double val6 = Math.pow(f, 2d);
            final double val7 = Math.pow(b, a);
            final double val8 = val7 * val6 * k;
            final double val9 = a * val8;
            final double val10 = 2d * Math.pow(val2, a + 2d);
            final double val11 = Math.pow((val1), 2d);
            final double val13 = f * k;
            final double val14 = f - 1d;
            final double val15 = a - 1d;
            final double val16 = b - 1d;
            final double val17 = val15 * val0;
            final double val18 = val6 * k;
            final double val19 = (val17 + 2d);
            final double val20 = val18 * val19;

            SquareMatrix2D result = new SquareMatrix2D();

            // Formula 1d, d alpha
            result.x1 = -val13 * Math.log(-b / val1) * val3 * val4 -
                        (val13 * val4 * val0) / val1;

            // Formula 1d, d beta
            result.x2 = a * val13 * (1d / val1 - (b * val14) / val11) *
                        val3 * Math.pow((-b / val1), val15) - val13 *
                        val4 * ((a * val14) / val1 - (a * val16 * Math.pow(val14, 2d)) / val11);

            // Formula 2d, d alpha
            result.y1 = (val9 * Math.log(val2) * val19 * val16) / (val10) -
                        (val9 * Math.pow(val16, 2d) * val14) / (val10) -
                        (val7 * val20 * val16) / (val10) -
                        (a * val7 * val18 * Math.log(b) * val19 * val16) /
                        (val10);
            // Formula 2d, d beta
            result.y2 = -(val9 * val19) / (val10) - (Math.pow(a, 2d) * Math.pow(b, val15) * val20 * val16) /
                        val10 - (a * val7 * val18 * val17) / val10 -
                        (a * val7 * val20 * (a + 2d) * val0) /
                        (2d * Math.pow((val2), (a + 3d)));

            return result;
        }
    };
}
 
开发者ID:arx-deidentifier,项目名称:arx,代码行数:69,代码来源:ModelSNB.java


注:本文中的de.linearbits.newtonraphson.Function类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。