File: FunctionDelegates.java

package info (click to toggle)
quantlib-swig 1.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,284 kB
  • sloc: python: 6,056; java: 1,552; cs: 774; makefile: 243; sh: 22
file content (85 lines) | stat: -rw-r--r-- 3,122 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package examples;

import org.quantlib.Brent;
import org.quantlib.Newton;
import org.quantlib.DoubleVector;
import org.quantlib.OdeFctDelegate;
import org.quantlib.GaussKronrodAdaptive;
import org.quantlib.UnaryFunctionDelegate;
import org.quantlib.BinaryFunctionDelegate;
import org.quantlib.RichardsonExtrapolation;
import org.quantlib.RungeKutta;

public class FunctionDelegates {

    public static void main(String[] args) {
        System.out.println("Integration result " +
            new GaussKronrodAdaptive(1e-8).calculate(
                new UnaryFunctionDelegate() {
                    public double value(double x) { return Math.sin(x); }
                }, 0.0, Math.PI
            )
        );
    
        System.out.println("Brent Solver result " +
            new Brent().solve(
                new UnaryFunctionDelegate() {
                    public double value(double x) { return Math.cos(x)-x; }
                }, 1e-8, 0.5, 0.0, Math.PI
            )
        );
        
        System.out.println("Newton Solver result " +
            new Newton().solve(
                new UnaryFunctionDelegate() {
                    public double value(double x) { return x*x-1.0; }
                },
                new UnaryFunctionDelegate() {
                    public double value(double x) { return 2*x; }
                },                
                1e-4, 0.25, 0.1
            )
        );
                
        System.out.println("Richardson Extrapolation, known order " +
            new RichardsonExtrapolation(
                new UnaryFunctionDelegate() {
                    public double value(double x) { return Math.exp(1 + x); }
                }, 0.1, 1.0).getValue(2.0)
            );
            
        System.out.println("Richardson Extrapolation, unknown order " +
            new RichardsonExtrapolation(
                new UnaryFunctionDelegate() {
                    public double value(double x) { return Math.exp(1 + x); }
                }, 0.1).getValue(4.0, 2.0)
            );
        
        System.out.println("One dimensional adaptive Runge-Kutta result " + 
            // y'=y  and y[0] = 1
            new RungeKutta().getValue(
                new BinaryFunctionDelegate() {
                    public double value(double x, double y) { return y; }
                }, 1.0, 0.0, 1.0
            )
        );
        
        DoubleVector startVal = new DoubleVector();
        startVal.add(0.0);
        startVal.add(1.0);

        System.out.println("Two dimensional adaptive Runge-Kutta result " + 
            // y_0'=y_1 & y_1'=-y_0 and y_0[0]=0 & y_1[0]=1
            new RungeKutta().getValue(
                new OdeFctDelegate() {                    
                    public DoubleVector value(double x, DoubleVector y) {
                         DoubleVector retVal = new DoubleVector();
                         retVal.add(y.get(1));
                         retVal.add(-y.get(0));
                        return retVal; 
                    }
                }, startVal, 0.0, 0.5*Math.PI
            ).get(0)
        );        
    }
}