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 bb87de2

Browse filesBrowse files
committed
fix post_order problem, add trapezoidal rule
1 parent c537621 commit bb87de2
Copy full SHA for bb87de2

File tree

Expand file treeCollapse file tree

5 files changed

+177
-151
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+177
-151
lines changed
Open diff view settings
Collapse file

‎src/symjava/examples/BlackScholez.java‎

Copy file name to clipboardExpand all lines: src/symjava/examples/BlackScholez.java
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public class BlackScholez {
1313

1414
public static void main(String[] args) {
15-
//Construct the Balck-Scholez equation
15+
// Define symbols to construct the Black-Scholes formula
1616
Symbol spot = new Symbol("spot"); //spot price
1717
Symbol strike = new Symbol("strike"); //strike price
1818
Symbol rd = new Symbol("rd");
@@ -29,14 +29,13 @@ public static void main(String[] args) {
2929
Expr dp = (log(fwd/strike)+0.5*pow(stdDev,2))/stdDev;
3030
Expr dm = (log(fwd/strike)-0.5*pow(stdDev,2))/stdDev;
3131

32-
//Domain I1 = Interval.apply(-oo, phi*dp, z);
33-
Domain I1 = Interval.apply(-10, phi*dp, z); //Good enough for -10, it will take a long time to use -oo
34-
I1.setStep(1e-5);
32+
//we use -10 instead of -oo for numerical computation
33+
Domain I1 = Interval.apply(-10, phi*dp, z);
34+
Domain I2 = Interval.apply(-10, phi*dm, z);
35+
double stepSize = 1e-3;
36+
I1.setStep(stepSize);
37+
I2.setStep(stepSize);
3538
Expr cdf1 = Integrate.apply(exp(-0.5*pow(z,2)), I1)/sqrt(PI2);
36-
37-
//Domain I2 = Interval.apply(-oo, phi*dm, z);
38-
Domain I2 = Interval.apply(-10, phi*dm, z);
39-
I2.setStep(1e-5);
4039
Expr cdf2 = Integrate.apply(exp(-0.5*pow(z,2)), I2)/sqrt(PI2);
4140

4241
Expr res = phi*domDf*(fwd*cdf1-strike*cdf2);
@@ -56,6 +55,7 @@ public static void main(String[] args) {
5655
new Eq(res-price, C0, freeVars, params)
5756
};
5857

58+
// Use Newton's method to find the root
5959
double[] guess = new double[]{ 0.10 };
6060
double[] constParams = new double[] {100.0, 110.0, 0.002, 0.01, 0.5, 1};
6161
Newton.solve(eq, guess, constParams, 100, 1e-5);
Collapse file

‎src/symjava/examples/Example0.java‎

Copy file name to clipboardExpand all lines: src/symjava/examples/Example0.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static void main(String[] args) {
1010
Expr R = 0.127-(x*0.194/(y+0.194));
1111
Expr Rdy = R.diff(y);
1212
System.out.println(Rdy);
13+
1314
BytecodeFunc func = JIT.compile(new Expr[]{x,y}, Rdy);
1415
System.out.println(func.apply(0.362, 0.556));
1516
}
Collapse file

‎src/symjava/symbolic/utils/BytecodeSupport.java‎

Copy file name to clipboardExpand all lines: src/symjava/symbolic/utils/BytecodeSupport.java
+19-4Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,26 @@ public static double numIntegrate1D(double begin, double end, double step, Strin
4141
getMethod("apply", new Class[] {double[].class});
4242
double[] args = { 0 };
4343
double sum = 0.0;
44-
for(double i=begin; i<=end; i+=step) {
44+
// for(double i=begin; i<=end; i+=step) {
45+
// args[0] = i;
46+
// Double val = (Double)method.invoke(null, args);
47+
// //Double val = test_pdf(args);
48+
// sum += val*step;
49+
// }
50+
51+
args[0] = begin;
52+
Double val1 = (Double)method.invoke(null, args);
53+
double i = begin + step;
54+
for(; i<=end; i+=step) {
4555
args[0] = i;
46-
Double val = (Double)method.invoke(null, args);
47-
//Double val = test_pdf(args);
48-
sum += val*step;
56+
Double val2 = (Double)method.invoke(null, args);
57+
sum += (val1+val2)*step/2.0;
58+
val1 = val2;
59+
}
60+
if(i - end > 0.0) {
61+
args[0] = end;
62+
Double val2 = (Double)method.invoke(null, args);
63+
sum += (val1+val2)*(step-(i-end))/2.0;
4964
}
5065
return sum;
5166
} catch (Exception e) {
Collapse file

‎src/symjava/symbolic/utils/BytecodeUtils.java‎

Copy file name to clipboardExpand all lines: src/symjava/symbolic/utils/BytecodeUtils.java
+24-14Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ public static void post_order(Expr e, List<Expr> outList) {
8989
Interval I = (Interval)INT.domain;
9090
post_order(I.getStart(), outList);
9191
post_order(I.getEnd(), outList);
92+
//Integrand will not be added to the outList since we don't want the dummy variable to be exposed
9293
//outList.add(new Func("integrand"+java.util.UUID.randomUUID().toString().replaceAll("-", ""),INT.integrand));
9394
} else {
94-
throw new RuntimeException("Unsupported Integrate: "+e);
95+
//TODO
96+
//Support multi-variable integrate
9597
}
9698
}
9799
outList.add(e);
@@ -259,23 +261,31 @@ public static ClassGen genClass(Func fun, boolean writeClassFile, boolean static
259261
} else if(ins instanceof Negate) {
260262
il.append(new PUSH(cp, -1.0));
261263
il.append(new DMUL());
262-
// } else if(ins instanceof Infinity) {
263-
// il.append(new PUSH(cp, Double.NaN));
264+
} else if(ins instanceof Infinity) {
265+
throw new RuntimeException(ins.getClass() + "Infinity cannot be used in numerical computation, use a proper number instead!");
264266
} else if(ins instanceof Integrate) {
265267
Integrate INT = (Integrate)ins;
266-
Func f = new Func("integrand_"+java.util.UUID.randomUUID().toString().replaceAll("-", ""),INT.integrand);
267-
//System.out.println(f);
268-
f.toBytecodeFunc(true, true); //Load class, could be better method to load a class
269-
//TODO read this: http://stackoverflow.com/questions/19119702/injecting-code-in-an-existing-method-using-bcel/19219759#19219759
270-
if(INT.domain.getStep() == null) {
271-
throw new RuntimeException("Please specifiy the step size for you integral: "+INT);
268+
if(INT.domain instanceof Interval) {
269+
//Compile the integrand
270+
Func f = new Func("integrand_"+java.util.UUID.randomUUID().toString().replaceAll("-", ""),INT.integrand);
271+
//System.out.println(f);
272+
f.toBytecodeFunc(true, true); //Load class, could be better method to load a class
273+
274+
//TODO read this: http://stackoverflow.com/questions/19119702/injecting-code-in-an-existing-method-using-bcel/19219759#19219759
275+
if(INT.domain.getStep() == null) {
276+
throw new RuntimeException("Please specifiy the step size for you integral: "+INT);
277+
}
278+
279+
il.append(new PUSH(cp, INT.domain.getStep()));
280+
il.append(new PUSH(cp, f.getName()));
281+
il.append(factory.createInvoke("symjava.symbolic.utils.BytecodeSupport", "numIntegrate1D",
282+
Type.DOUBLE, new Type[] { Type.DOUBLE, Type.DOUBLE, Type.DOUBLE, Type.STRING }, Constants.INVOKESTATIC));
283+
} else {
284+
//TODO
285+
throw new RuntimeException("Unsupported Integrate: "+INT);
272286
}
273-
il.append(new PUSH(cp, INT.domain.getStep())); //
274-
il.append(new PUSH(cp, f.getName()));
275-
il.append(factory.createInvoke("symjava.symbolic.utils.BytecodeSupport", "numIntegrate1D",
276-
Type.DOUBLE, new Type[] { Type.DOUBLE, Type.DOUBLE, Type.DOUBLE, Type.STRING }, Constants.INVOKESTATIC));
277287
} else {
278-
throw new RuntimeException(ins.getClass() + " is not allowed when generating bytecode function!");
288+
throw new RuntimeException(ins.getClass() + " is not supported in this version when generating bytecode function!");
279289
}
280290
}
281291

Collapse file

‎triangle_neumann.dat‎

Copy file name to clipboardExpand all lines: triangle_neumann.dat
+125-125Lines changed: 125 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -36,131 +36,131 @@ ZONE F=FEPOINT ET=TRIANGLE N=161 E=284
3636
3.000000 -1.000000 0.000000
3737
3.000000 -1.666667 0.000000
3838
3.000000 -2.333333 0.000000
39-
-1.996795 1.996795 -1308.027316
40-
-1.786509 1.395641 -1286.967460
41-
-1.381602 1.747001 -1292.910091
42-
2.687000 2.687000 -722.614477
43-
2.414665 2.414665 -1165.968666
44-
2.687000 -2.687000 -722.085189
45-
2.414665 -2.414665 -1164.131086
46-
-2.687000 2.687000 -722.461498
47-
-2.414665 2.414665 -1165.437554
48-
-2.687000 -2.687000 -719.185178
49-
-2.414665 -2.414665 -1154.062831
50-
2.439027 -1.396248 -1193.476944
51-
2.558251 -1.924005 -1069.221028
52-
-2.591268 1.865444 -1054.911307
53-
-2.709003 1.333932 -831.528402
54-
0.821309 -2.027533 -1256.321270
55-
1.358555 -2.289309 -1220.214057
56-
1.347485 -1.741104 -1285.949911
57-
1.996795 -1.996795 -1309.133434
58-
-0.788934 2.048816 -1251.306357
59-
-1.394140 2.433171 -1197.579032
60-
-1.491504 -2.317436 -1244.487306
61-
-1.924005 -2.558251 -1036.330624
62-
2.085412 0.847887 -1247.818909
63-
2.339115 0.228740 -1183.632863
64-
2.583168 0.867246 -1010.490267
65-
2.714035 0.000000 -732.643918
66-
2.464298 -0.369310 -1078.742622
67-
0.191070 -2.295472 -1207.191002
68-
0.411710 -1.805212 -1263.013714
69-
1.813941 0.419244 -1256.505439
70-
-2.085412 -0.847887 -1248.304797
71-
-2.442360 -1.389242 -1187.914625
72-
-1.811324 -1.347319 -1292.638263
73-
-1.996795 -1.996795 -1303.060793
74-
-0.041330 -2.676601 -782.215161
75-
-0.415534 -2.412628 -1133.662447
76-
0.846082 -2.562099 -1027.638517
77-
0.228740 2.339115 -1183.766478
78-
-0.362727 2.469069 -1072.516649
79-
-0.281857 2.031116 -1233.099617
80-
1.345644 2.702400 -846.042044
81-
1.855583 2.566671 -1087.752387
82-
2.289558 1.388149 -1224.341019
83-
1.736746 1.348401 -1284.170466
84-
1.996795 1.996795 -1310.136150
85-
1.376393 2.286163 -1223.919204
86-
0.855333 2.568848 -1025.668807
87-
0.847887 2.085412 -1246.969983
88-
-0.787617 2.597531 -959.802880
89-
-2.587741 -0.785370 -965.729766
90-
-2.410932 -0.356389 -1122.754762
91-
-2.288023 0.206717 -1211.199559
92-
-1.913731 -0.332610 -1253.013576
93-
-1.491079 -0.767388 -1267.571196
94-
-0.966913 -0.955370 -1262.632610
95-
-0.924722 -0.469911 -1252.024623
96-
-1.454343 -1.792452 -1300.384221
97-
-0.030042 -0.798192 -1248.574762
98-
-0.070874 -1.224309 -1256.889355
99-
0.375039 -1.018424 -1252.883260
100-
1.787992 -1.396357 -1294.133415
101-
-2.291776 1.354897 -1217.806489
102-
-2.562369 0.846128 -1025.071478
103-
-2.085412 0.847887 -1245.260302
104-
-0.144650 0.585519 -1242.747811
105-
0.017951 1.199874 -1250.506111
106-
-0.480120 1.086735 -1251.493954
107-
1.473252 0.774028 -1263.109821
108-
0.953551 0.944175 -1256.189245
109-
0.912896 0.465330 -1248.400914
110-
1.384444 1.773673 -1286.016729
111-
-0.590643 0.111098 -1244.026717
112-
-1.166446 0.087339 -1252.895928
113-
-1.368369 0.599574 -1260.217427
114-
-1.688483 0.133677 -1262.271945
115-
-0.880851 1.454021 -1266.404479
116-
-0.926033 0.880849 -1255.782810
117-
0.598148 1.355920 -1257.764785
118-
0.112610 1.731900 -1257.288860
119-
0.609961 -0.014911 -1243.381753
120-
1.163893 -0.045010 -1250.744613
121-
1.466650 -0.631237 -1261.423175
122-
1.710317 -0.000000 -1257.159158
123-
0.772952 -1.472005 -1266.350982
124-
0.873109 -0.905285 -1256.127565
125-
-0.631237 -1.466650 -1269.290709
126-
0.000000 -1.710317 -1264.528636
127-
2.567236 1.857542 -1087.349607
128-
1.852610 -2.567195 -1086.372994
129-
-1.924005 2.558251 -1068.197240
130-
-2.558251 -1.924005 -1065.167846
131-
-0.968334 -2.626306 -997.620933
132-
0.450331 0.821385 -1247.011097
133-
-1.669107 0.926944 -1269.676795
134-
-1.329698 1.178059 -1271.833605
135-
0.303307 -0.466573 -1243.930680
136-
1.676346 -1.024268 -1272.783163
137-
1.320728 -1.206746 -1272.883124
138-
0.855159 -0.394841 -1248.570938
139-
0.925381 1.664115 -1268.560876
140-
1.191017 1.309872 -1269.698743
141-
-0.945234 0.433707 -1250.635640
142-
-1.024268 -1.676346 -1283.189714
143-
-1.222157 -1.311644 -1279.062216
144-
1.345644 -2.702400 -844.088466
145-
2.702400 1.345644 -847.790087
146-
-0.433747 1.609805 -1256.620189
147-
2.597747 -0.789356 -958.072676
148-
-0.609578 0.583016 -1246.931925
149-
-1.481222 -0.284110 -1258.864551
150-
-0.286010 -0.425521 -1244.428216
151-
1.428376 0.305601 -1255.569222
152-
0.364093 0.377490 -1242.436640
153-
0.305601 -1.428376 -1260.623354
154-
0.546426 1.855568 -1259.676592
155-
2.085412 -0.847887 -1248.105776
156-
-1.855243 0.541617 -1262.273610
157-
-0.902965 -2.027769 -1276.541889
158-
2.002485 -0.321355 -1238.683477
159-
0.000000 2.714035 -732.510303
160-
-0.351733 -1.982567 -1254.214970
161-
-2.714035 0.000000 -707.851311
162-
-0.471669 -0.856131 -1252.556911
163-
0.000000 -0.000000 -1240.720163
39+
-1.996795 1.996795 25.520632
40+
-1.786509 1.395641 40.948450
41+
-1.381602 1.747001 42.244250
42+
2.687000 2.687000 3.204465
43+
2.414665 2.414665 10.355239
44+
2.687000 -2.687000 3.191939
45+
2.414665 -2.414665 10.311753
46+
-2.687000 2.687000 3.184532
47+
-2.414665 2.414665 10.286036
48+
-2.687000 -2.687000 3.177874
49+
-2.414665 -2.414665 10.262920
50+
2.439027 -1.396248 21.895350
51+
2.558251 -1.924005 12.893389
52+
-2.591268 1.865444 12.551084
53+
-2.709003 1.333932 12.003944
54+
0.821309 -2.027533 40.803573
55+
1.358555 -2.289309 27.038654
56+
1.347485 -1.741104 42.866249
57+
1.996795 -1.996795 25.489815
58+
-0.788934 2.048816 40.333959
59+
-1.394140 2.433171 22.112389
60+
-1.491504 -2.317436 24.841395
61+
-1.924005 -2.558251 13.014498
62+
2.085412 0.847887 38.608280
63+
2.339115 0.228740 31.776046
64+
2.583168 0.867246 19.336907
65+
2.714035 0.000000 14.433707
66+
2.464298 -0.369310 25.835176
67+
0.191070 -2.295472 33.635743
68+
0.411710 -1.805212 50.452125
69+
1.813941 0.419244 50.111056
70+
-2.085412 -0.847887 38.503812
71+
-2.442360 -1.389242 21.857794
72+
-1.811324 -1.347319 41.100377
73+
-1.996795 -1.996795 25.478650
74+
-0.041330 -2.676601 16.154792
75+
-0.415534 -2.412628 27.914657
76+
0.846082 -2.562099 20.281043
77+
0.228740 2.339115 31.724182
78+
-0.362727 2.469069 25.642301
79+
-0.281857 2.031116 43.234973
80+
1.345644 2.702400 12.167832
81+
1.855583 2.566671 13.314946
82+
2.289558 1.388149 26.692551
83+
1.736746 1.348401 42.930422
84+
1.996795 1.996795 25.358348
85+
1.376393 2.286163 26.891977
86+
0.855333 2.568848 19.948148
87+
0.847887 2.085412 38.519837
88+
-0.787617 2.597531 18.819553
89+
-2.587741 -0.785370 19.208383
90+
-2.410932 -0.356389 28.213422
91+
-2.288023 0.206717 33.951666
92+
-1.913731 -0.332610 47.438060
93+
-1.491079 -0.767388 57.134469
94+
-0.966913 -0.955370 65.063699
95+
-0.924722 -0.469911 71.695923
96+
-1.454343 -1.792452 39.964900
97+
-0.030042 -0.798192 74.937473
98+
-0.070874 -1.224309 67.292074
99+
0.375039 -1.018424 70.338823
100+
1.787992 -1.396357 40.945606
101+
-2.291776 1.354897 26.951240
102+
-2.562369 0.846128 20.253104
103+
-2.085412 0.847887 38.561220
104+
-0.144650 0.585519 77.850957
105+
0.017951 1.199874 67.981281
106+
-0.480120 1.086735 68.401245
107+
1.473252 0.774028 57.388562
108+
0.953551 0.944175 65.315382
109+
0.912896 0.465330 71.737189
110+
1.384444 1.773673 41.431751
111+
-0.590643 0.111098 77.902113
112+
-1.166446 0.087339 68.606763
113+
-1.368369 0.599574 61.575274
114+
-1.688483 0.133677 55.191589
115+
-0.880851 1.454021 56.538629
116+
-0.926033 0.880849 66.780758
117+
0.598148 1.355920 61.897548
118+
0.112610 1.731900 53.854354
119+
0.609961 -0.014911 77.527139
120+
1.163893 -0.045010 68.645166
121+
1.466650 -0.631237 59.179027
122+
1.710317 -0.000000 54.608136
123+
0.772952 -1.472005 57.470278
124+
0.873109 -0.905285 67.363692
125+
-0.631237 -1.466650 59.161784
126+
0.000000 -1.710317 54.586747
127+
2.567236 1.857542 13.301943
128+
1.852610 -2.567195 13.332587
129+
-1.924005 2.558251 12.898566
130+
-2.558251 -1.924005 12.872511
131+
-0.968334 -2.626306 16.964120
132+
0.450331 0.821385 73.134987
133+
-1.669107 0.926944 50.242047
134+
-1.329698 1.178059 54.920269
135+
0.303307 -0.466573 78.314594
136+
1.676346 -1.024268 48.795778
137+
1.320728 -1.206746 54.625739
138+
0.855159 -0.394841 72.828656
139+
0.925381 1.664115 50.371905
140+
1.191017 1.309872 55.101347
141+
-0.945234 0.433707 71.106298
142+
-1.024268 -1.676346 48.863898
143+
-1.222157 -1.311644 54.589592
144+
1.345644 -2.702400 12.200057
145+
2.702400 1.345644 12.177502
146+
-0.433747 1.609805 56.346666
147+
2.597747 -0.789356 18.754670
148+
-0.609578 0.583016 74.384192
149+
-1.481222 -0.284110 60.500534
150+
-0.286010 -0.425521 78.635744
151+
1.428376 0.305601 61.670063
152+
0.364093 0.377490 78.223581
153+
0.305601 -1.428376 61.643591
154+
0.546426 1.855568 48.096963
155+
2.085412 -0.847887 38.518010
156+
-1.855243 0.541617 48.179900
157+
-0.902965 -2.027769 40.076374
158+
2.002485 -0.321355 44.195071
159+
0.000000 2.714035 14.421741
160+
-0.351733 -1.982567 44.790095
161+
-2.714035 0.000000 14.365843
162+
-0.471669 -0.856131 72.490166
163+
0.000000 -0.000000 80.965887
164164
37 38 39
165165
40 5 41
166166
42 36 43

0 commit comments

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