forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumericalIntegration.java
More file actions
201 lines (173 loc) · 5.95 KB
/
NumericalIntegration.java
File metadata and controls
201 lines (173 loc) · 5.95 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package symjava.examples;
import symjava.relational.Ge;
import symjava.relational.Le;
import symjava.symbolic.*;
import static symjava.math.SymMath.*;
import static symjava.symbolic.Symbol.*;
import symjava.bytecode.BytecodeFunc;
import symjava.domains.Domain;
import symjava.domains.Domain2D;
import symjava.domains.Domain3D;
import symjava.domains.DomainND;
import symjava.domains.Interval;
import symjava.symbolic.utils.JIT;
public class NumericalIntegration {
public static void main(String[] args) {
test_1D();
test_2D();
test_ND();
//Expr i = Integrate.apply(exp(pow(x,2)), Interval.apply(a, b).setStepSize(0.001));
//BytecodeFunc fi = JIT.compile(new Expr[]{a,b}, i);
//System.out.println(fi.apply(1,2));
test_paper_example1();
test_paper_example2();
}
public static void test_1D() {
//Define the interal
Domain I = Interval.apply(-10, 0).setStepSize(0.01);
//Define the integral: cumulative distribution function for standard normal distribution
Expr cdf = Integrate.apply(exp(-0.5*pow(x,2))/sqrt(2*PI), I);
System.out.println(cdf); //\int_{-10.0}^{10.0}{1/\sqrt{2*\pi}*e^{-0.5*x^2}}dx
//Compile cdf to perform numerical integration
BytecodeFunc f = JIT.compile(cdf);
System.out.println(f.apply()); //0.5
//Define the integral: cumulative distribution function for a general normal distribution
Symbol mu = new Symbol("\\mu");
Symbol sigma = new Symbol("\\sigma");
Expr cdf2 = Integrate.apply(exp(-pow(x-mu,2)/(2*sigma*sigma))/(sigma*sqrt(2*PI)), I);
System.out.println(cdf2); //\int_{-10.0}^{0.0}{1/(\sigma*\sqrt{2*\pi})*e^{-(-\mu + x)^2/(2*\sigma*\sigma)}}dx
//Compile cdf2 to perform numerical integration
BytecodeFunc f2 = JIT.compile(new Expr[]{mu, sigma}, cdf2);
System.out.println(f2.apply(-5.0, 1.0)); //~1.0
}
public static void test_2D() {
//http://turing.une.edu.au/~amth142/Lectures/Lecture_14.pdf
/*
Example:
We will evaluate the integral
I = \int_{\Omega} sin(sqrt(log(x+y+1))) dxdy
where \Omega is the disk
(x-1/2)^2 + (y-1/2)^2 <= 1/4
Since the disk Ω is contained within the square [0, 1] × [0, 1], we can
generate x and y as uniform [0, 1] random numbers, and keep those which
lie in the disk Ω.
Matlab code:
function ii = monte2da(n)
k = 0 // count no. of points in disk
sumf = 0 // keep running sum of function values
while (k < n) // keep going until we get n points
x = rand(1,1)
y = rand(1,1)
if ((x-0.5)^2 + (y-0.5)^2 <= 0.25) then // (x,y) is in disk
k = k + 1 // increment count
sumf = sumf + sin(sqrt(log(x+y+1))) // increment sumf
end
end
ii = (%pi/4)*(sumf/n) // %pi/4 = volume of disk
endfunction
-->monte2da(100000)
ans =
0.5679196
*/
Domain omega = new Domain2D("\\Omega", x, y)
.setConstraint( Le.apply((x-0.5)*(x-0.5) + (y-0.5)*(y-0.5), 0.25) )
.setBound(x, 0, 1)
.setBound(y, 0, 1);
Expr I = Integrate.apply(sin(sqrt(log(x+y+1))), omega);
System.out.println(I);
BytecodeFunc fI = JIT.compile(I);
System.out.println(fI.apply());
test_2D_verifiy();
}
public static void test_2D_verifiy() {
double xMin=0, xMax=1, xStep=0.001;
double yMin=0, yMax=1, yStep=0.001;
double sum = 0.0;
for(double x=xMin; x<=xMax; x+=xStep) {
for(double y=yMin; y<=yMax; y+=yStep) {
if((x-0.5)*(x-0.5) + (y-0.5)*(y-0.5) < 0.5*0.5)
sum += Math.sin(Math.sqrt(Math.log(x+y+1)))*xStep*yStep;
}
}
System.out.println("verify="+sum);
}
public static void test_ND() {
double r = 1.0;
Domain omega = new Domain3D("\\Omega", x, y, z)
.setConstraint( Le.apply(x*x + y*y + z*z, r*r) )
.setBound(x, -1, 1)
.setBound(y, -1, 1)
.setBound(z, -1, 1);
Expr ii = Integrate.apply(1, omega);
System.out.println(ii);
BytecodeFunc f = JIT.compile(ii);
System.out.println(f.apply());
System.out.println(4.0*Math.PI/3.0*Math.pow(r, 3));
Domain omega2 = new DomainND("\\Omega", x, y, z, t)
.setConstraint( Le.apply(x*x + y*y + z*z + t*t, r*r) )
.setBound(x, -1, 1)
.setBound(y, -1, 1)
.setBound(z, -1, 1)
.setBound(t, -1, 1);
Expr ii2 = Integrate.apply(1, omega2);
System.out.println(ii);
BytecodeFunc f2 = JIT.compile(ii2);
System.out.println(f2.apply());
System.out.println(0.5*Math.PI*Math.PI*Math.pow(r, 4));
}
/**
* I = \int_{\Omega} sin(sqrt(log(x+y+1))) dxdy
* where
* \Omega={ (x,y), where (x-1/2)^2 + (y-1/2)^2 <= 0.25 }
*/
public static void test_paper_example1() {
Domain omega = new Domain2D("\\Omega", x, y)
.setBound(x, 0.5-sqrt(0.25-(y-0.5)*(y-0.5)), 0.5+sqrt(0.25-(y-0.5)*(y-0.5)))
.setBound(y, 0, 1)
.setStepSize(0.001);
Expr I = Integrate.apply( sin(sqrt(log(x+y+1)) ), omega);
System.out.println(I);
BytecodeFunc fI = JIT.compile(I);
System.out.println(fI.apply());
}
/**
* I = \int_{\Omega} sin(sqrt(log(x+y+1))) dxdy
* where
* \Omega= { (x,y), where
* a^2 <= (x-1/2)^2 + (y-1/2)^2 <= b^2 or
* c^2 <= (x-1/2)^2 + (y-1/2)^2 <= d^2
* }
* we choose
* a=0.25, b=0.5, c=0.75, d=1.0
*/
public static void test_paper_example2() {
Expr eq = (x-0.5)*(x-0.5) + (y-0.5)*(y-0.5);
Domain omega = new Domain2D("\\Omega", x, y)
.setConstraint(
( Ge.apply(eq, a*a) & Le.apply(eq, b*b)) |
( Ge.apply(eq, c*c) & Le.apply(eq, d*d) )
).setBound(x, 0, 1).setBound(y, 0, 1);
Expr I = Integrate.apply( sin(sqrt(log(x+y+1)) ), omega);
System.out.println(I);
BytecodeFunc fI = JIT.compile(new Expr[]{a, b, c, d}, I);
System.out.println(fI.apply(0.25, 0.5, 0.75, 1.0));
test_paper_example_verifiy();
}
public static void test_paper_example_verifiy() {
double xMin=0, xMax=1, xStep=0.001;
double yMin=0, yMax=1, yStep=0.001;
double sum = 0.0;
double a=0.25, b=0.5, c=0.75, d=1.0;
for(double x=xMin; x<=xMax; x+=xStep) {
for(double y=yMin; y<=yMax; y+=yStep) {
double disk = (x-0.5)*(x-0.5) + (y-0.5)*(y-0.5);
if((a*a <= disk && disk <= b*b) ||
(c*c <= disk && disk <= d*d )
) {
sum += Math.sin(Math.sqrt(Math.log(x+y+1)))*xStep*yStep;
}
}
}
System.out.println("verify="+sum);
}
}