Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 258b0fc

Browse filesBrowse files
committed
add Monte Carlo ND
1 parent 839add8 commit 258b0fc
Copy full SHA for 258b0fc

File tree

Expand file treeCollapse file tree

13 files changed

+350
-102
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

13 files changed

+350
-102
lines changed
Open diff view settings
Collapse file

‎src/symjava/domains/Domain.java‎

Copy file name to clipboardExpand all lines: src/symjava/domains/Domain.java
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ public Domain setBound(Expr x, Expr minBound, Expr maxBound) {
148148
info.maxBound = maxBound;
149149
return this;
150150
}
151+
public Domain setBound(Expr x, double minBound, Expr maxBound) {
152+
setBound(x, Expr.valueOf(minBound), maxBound);
153+
return this;
154+
}
155+
public Domain setBound(Expr x, Expr minBound, double maxBound) {
156+
setBound(x, minBound, Expr.valueOf(maxBound));
157+
return this;
158+
}
159+
public Domain setBound(Expr x, double minBound, double maxBound) {
160+
setBound(x, Expr.valueOf(minBound), Expr.valueOf(maxBound));
161+
return this;
162+
}
151163

152164
/**
153165
* Set a logic expression of equations to repreent the domain
Collapse file

‎src/symjava/domains/DomainND.java‎

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package symjava.domains;
2+
3+
import symjava.math.Transformation;
4+
import symjava.symbolic.Expr;
5+
6+
public class DomainND extends Domain {
7+
/**
8+
* Construct a N Dimentional domain with a given label(name) and
9+
* a list of coordinate variables
10+
* @param label
11+
* @param coordVars
12+
*/
13+
public DomainND(String label, Expr ...coordVars) {
14+
this.label = label;
15+
this.coordVars = coordVars;
16+
}
17+
18+
@Override
19+
public Domain transform(String label, Transformation trans) {
20+
return new DomainND(label, trans.getToVars());
21+
}
22+
23+
@Override
24+
public int getDim() {
25+
return coordVars.length;
26+
}
27+
}
Collapse file

‎src/symjava/examples/NumericalIntegration.java‎

Copy file name to clipboardExpand all lines: src/symjava/examples/NumericalIntegration.java
+42-15Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
import symjava.bytecode.BytecodeFunc;
88
import symjava.domains.Domain;
99
import symjava.domains.Domain2D;
10+
import symjava.domains.Domain3D;
11+
import symjava.domains.DomainND;
1012
import symjava.domains.Interval;
1113
import symjava.symbolic.utils.JIT;
1214

1315
public class NumericalIntegration {
1416

1517
public static void main(String[] args) {
16-
test_1D();
18+
//test_1D();
1719
//test_2D();
20+
test_ND();
1821
}
1922

2023
public static void test_1D() {
@@ -68,36 +71,60 @@ public static void test_2D() {
6871
ans =
6972
0.5679196
7073
*/
71-
// Domain omega = new Domain2D("\\Omega", x, y)
72-
// .setConstraint( Le.apply((x-0.5)*(x-0.5) + (y-0.5)*(y-0.5), C(1/4)) )
73-
// .setBound(x, Cm1, C1)
74-
// .setBound(y, Cm1, C1);
74+
7575
Domain omega = new Domain2D("\\Omega", x, y)
76-
//.setConstraint( Le.apply((x-0.5)*(x-0.5) + (y-0.5)*(y-0.5), C(1/4)) )
77-
.setBound(x, Cm1, C1)
78-
.setBound(y, Cm1, C1) //TODO How to deal with parameters? e.g. setBound(x, a, b); call f.apply(-1,1)
79-
.setStepSize(0.001);
76+
.setConstraint( Le.apply((x-0.5)*(x-0.5) + (y-0.5)*(y-0.5), 0.25) )
77+
.setBound(x, 0, 1)
78+
.setBound(y, 0, 1);
8079

81-
Expr ii = Integrate.apply(sin(x*x+y*y), omega);
80+
Expr ii = Integrate.apply(sin(sqrt(log(x+y+1))), omega);
8281
System.out.println(ii);
8382
BytecodeFunc f = JIT.compile(ii);
8483
System.out.println(f.apply());
8584
test_2D_verifiy();
86-
87-
8885
}
8986

9087
public static void test_2D_verifiy() {
91-
double xMin=-1, xMax=1, xStep=0.0001;
92-
double yMin=-1, yMax=1, yStep=0.0001;
88+
double xMin=-1, xMax=1, xStep=0.001;
89+
double yMin=-1, yMax=1, yStep=0.001;
9390
double sum = 0.0;
9491
for(double x=xMin; x<=xMax; x+=xStep) {
9592
for(double y=yMin; y<=yMax; y+=yStep) {
96-
sum += Math.sin(x*x+y*y)*xStep*yStep;
93+
if((x-0.5)*(x-0.5) + (y-0.5)*(y-0.5) < 0.5*0.5)
94+
sum += Math.sin(Math.sqrt(Math.log(x+y+1)))*xStep*yStep;
9795
}
9896
}
9997
System.out.println("verify="+sum);
10098
}
99+
100+
public static void test_ND() {
101+
double r = 1.0;
102+
Domain omega = new Domain3D("\\Omega", x, y, z)
103+
.setConstraint( Le.apply(x*x + y*y + z*z, r*r) )
104+
.setBound(x, -1, 1)
105+
.setBound(y, -1, 1)
106+
.setBound(z, -1, 1);
107+
108+
Expr ii = Integrate.apply(1, omega);
109+
System.out.println(ii);
110+
BytecodeFunc f = JIT.compile(ii);
111+
System.out.println(f.apply());
112+
System.out.println(4.0*Math.PI/3.0*Math.pow(r, 3));
113+
114+
Domain omega2 = new DomainND("\\Omega", x, y, z, t)
115+
.setConstraint( Le.apply(x*x + y*y + z*z + t*t, r*r) )
116+
.setBound(x, -1, 1)
117+
.setBound(y, -1, 1)
118+
.setBound(z, -1, 1)
119+
.setBound(t, -1, 1);
120+
121+
Expr ii2 = Integrate.apply(1, omega2);
122+
System.out.println(ii);
123+
BytecodeFunc f2 = JIT.compile(ii2);
124+
System.out.println(f2.apply());
125+
System.out.println(0.5*Math.PI*Math.PI*Math.pow(r, 4));
126+
127+
}
101128

102129
}
103130

Collapse file

‎src/symjava/relational/Eq.java‎

Copy file name to clipboardExpand all lines: src/symjava/relational/Eq.java
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ private void computeUnknowns() {
148148
public static Eq apply(Expr lhs, Expr rhs) {
149149
return new Eq(lhs, rhs);
150150
}
151-
151+
public static Eq apply(double lhs, Expr rhs) {
152+
return new Eq(Expr.valueOf(lhs), rhs);
153+
}
154+
public static Eq apply(Expr lhs, double rhs) {
155+
return new Eq(lhs, Expr.valueOf(rhs));
156+
}
157+
152158
public static Eq apply(Expr lhs, Expr rhs, Expr[] freeVars) {
153159
return new Eq(lhs, rhs, freeVars);
154160
}
Collapse file

‎src/symjava/relational/Ge.java‎

Copy file name to clipboardExpand all lines: src/symjava/relational/Ge.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ public Expr diff(Expr expr) {
3131
public static Ge apply(Expr lhs, Expr rhs) {
3232
return new Ge(lhs, rhs);
3333
}
34+
public static Ge apply(double lhs, Expr rhs) {
35+
return new Ge(Expr.valueOf(lhs), rhs);
36+
}
37+
public static Ge apply(Expr lhs, double rhs) {
38+
return new Ge(lhs, Expr.valueOf(rhs));
39+
}
3440
}
Collapse file

‎src/symjava/relational/Gt.java‎

Copy file name to clipboardExpand all lines: src/symjava/relational/Gt.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ public Expr diff(Expr expr) {
3131
public static Gt apply(Expr lhs, Expr rhs) {
3232
return new Gt(lhs, rhs);
3333
}
34+
public static Gt apply(double lhs, Expr rhs) {
35+
return new Gt(Expr.valueOf(lhs), rhs);
36+
}
37+
public static Gt apply(Expr lhs, double rhs) {
38+
return new Gt(lhs, Expr.valueOf(rhs));
39+
}
3440
}
Collapse file

‎src/symjava/relational/Le.java‎

Copy file name to clipboardExpand all lines: src/symjava/relational/Le.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ public Expr diff(Expr expr) {
3030
public static Le apply(Expr lhs, Expr rhs) {
3131
return new Le(lhs, rhs);
3232
}
33+
public static Le apply(double lhs, Expr rhs) {
34+
return new Le(Expr.valueOf(lhs), rhs);
35+
}
36+
public static Le apply(Expr lhs, double rhs) {
37+
return new Le(lhs, Expr.valueOf(rhs));
38+
}
3339
}
Collapse file

‎src/symjava/relational/Lt.java‎

Copy file name to clipboardExpand all lines: src/symjava/relational/Lt.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ public Expr diff(Expr expr) {
3030
public static Lt apply(Expr lhs, Expr rhs) {
3131
return new Lt(lhs, rhs);
3232
}
33+
public static Lt apply(double lhs, Expr rhs) {
34+
return new Lt(Expr.valueOf(lhs), rhs);
35+
}
36+
public static Lt apply(Expr lhs, double rhs) {
37+
return new Lt(lhs, Expr.valueOf(rhs));
38+
}
3339
}
Collapse file

‎src/symjava/relational/Neq.java‎

Copy file name to clipboardExpand all lines: src/symjava/relational/Neq.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ public Expr diff(Expr expr) {
2929
public static Neq apply(Expr lhs, Expr rhs) {
3030
return new Neq(lhs, rhs);
3131
}
32+
public static Neq apply(double lhs, Expr rhs) {
33+
return new Neq(Expr.valueOf(lhs), rhs);
34+
}
35+
public static Neq apply(Expr lhs, double rhs) {
36+
return new Neq(lhs, Expr.valueOf(rhs));
37+
}
3238
}
Collapse file

‎src/symjava/symbolic/Integrate.java‎

Copy file name to clipboardExpand all lines: src/symjava/symbolic/Integrate.java
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public static Expr apply(Expr integrand, Domain domain) {
3434
return new Integrate(integrand, domain);
3535
}
3636

37+
public static Expr apply(double integrand, Domain domain) {
38+
return new Integrate(Expr.valueOf(integrand), domain);
39+
}
40+
3741
@Override
3842
public Expr subs(Expr from, Expr to) {
3943
return new Integrate(this.integrand.subs(from, to), this.domain);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.