1 | package PIANOS.datastructures; |
2 | |
3 | import PIANOS.exceptions.*; |
4 | import PIANOS.datastructures.*; |
5 | |
6 | import java.util.*; |
7 | |
8 | /* |
9 | Note: The thing calling generaterFreqCode and generateGenCode |
10 | must add _dp to real constants. For example, "1.0_dp", not "1.0". |
11 | */ |
12 | |
13 | public class PoissonDistribution extends Distribution { |
14 | public PoissonDistribution() { |
15 | super(1); |
16 | parameterType[0] = 1; // real |
17 | } |
18 | |
19 | public ArrayList<String> getIntroduction() { |
20 | ArrayList<String> result = new ArrayList<String>(); |
21 | result.add("EXTERNAL G01BKF"); |
22 | result.add("EXTERNAL G05ECF"); |
23 | result.add("INTEGER G05EYF"); |
24 | result.add("EXTERNAL G05EYF"); |
25 | return result; |
26 | } |
27 | |
28 | public ArrayList<String> getFreqCode(String[] parameters) throws IllegalParametersException { |
29 | // Paramters are: mu, point, result |
30 | |
31 | if (parameters.length != 3) { |
32 | throw new IllegalParametersException("Trying to use poisson distribution with illegal parameters."); |
33 | } |
34 | |
35 | /* |
36 | The code we want to generate looks like this: |
37 | ifail=0 |
38 | CALL G01BKF(mu, point, temp_real, temp_real, result, ifail) |
39 | IF (ifail /= 0) THEN |
40 | WRITE (*, *) 'Error when trying to calculate frequency of the poisson distribution, parameters: mu = ', mu |
41 | STOP |
42 | END IF |
43 | */ |
44 | |
45 | // BJF? I hope that was only a TYPO.... |
46 | ArrayList<String> result = new ArrayList<String>(); |
47 | |
48 | result.add("ifail=0"); |
49 | result.add("IF ("+ parameters[0] + " > 1.0D+6) THEN"); |
50 | result.add("CALL G01BKF( 1.0D+6, " + parameters[1] + |
51 | ", temp_real, temp_real, " + parameters[2] + ", ifail)"); |
52 | result.add("ELSE"); |
53 | result.add("CALL G01BKF(" + parameters[0] + ", " + parameters[1] + |
54 | ", temp_real, temp_real, " + parameters[2] + ", ifail)"); |
55 | result.add("END IF"); |
56 | result.add("IF (ifail /= 0) THEN"); |
57 | result.add("WRITE (*, *) 'Error when trying to calculate frequency of the poisson distribution, " + |
58 | "parameters: mu = ', " + parameters[0]); |
59 | result.add("STOP"); |
60 | result.add("END IF"); |
61 | |
62 | return result; |
63 | |
64 | /* |
65 | Note: |
66 | The code generated by this method uses the following already introduced variables: |
67 | INTEGER :: ifail |
68 | REAL :: temp_real |
69 | */ |
70 | } |
71 | |
72 | public ArrayList<String> getGenCode(String[] parameters) throws IllegalParametersException { |
73 | // Parameters are: mu, result |
74 | |
75 | if (parameters.length != 2) { |
76 | throw new IllegalParametersException("Trying to use poisson distribution with illegal parameters."); |
77 | } |
78 | |
79 | /* |
80 | The code we want to generate looks like this: |
81 | ifail=0 |
82 | CALL G05ECF(mu, poisson_reference, SIZE(poisson_reference), ifail) |
83 | IF (ifail /= 0) THEN |
84 | WRITE (*, *) 'Error when trying to generate random numbers from poisson distribution, parameters: mu = ', mu |
85 | STOP |
86 | END IF |
87 | DO k=0, SIZE(result) |
88 | result(k)=G05EYF(poisson_reference, SIZE(poisson_reference)) |
89 | END DO |
90 | */ |
91 | |
92 | ArrayList<String> result = new ArrayList<String>(); |
93 | |
94 | result.add("ifail = 0"); |
95 | result.add("ALLOCATE(poisson_reference(1 : NINT(20 + 20 * SQRT(" + parameters[0] + "))))"); |
96 | /* |
97 | optimal value: 20 +20 sqrt(T) |
98 | */ |
99 | result.add("CALL G05ECF(" + parameters[0] + ", poisson_reference, SIZE(poisson_reference), ifail)"); |
100 | result.add("IF (ifail /= 0) THEN"); |
101 | result.add("WRITE (*, *) 'Error when trying to generate random numbers from poisson distribution, parameters: mu = ', " |
102 | + parameters[0]); |
103 | result.add("STOP"); |
104 | result.add("END IF"); |
105 | result.add("DO k=1, SIZE(" + parameters[1] +")"); |
106 | result.add(parameters[1] + "(k)=G05EYF(poisson_reference, SIZE(poisson_reference))"); |
107 | result.add("END DO"); |
108 | |
109 | return result; |
110 | |
111 | /* |
112 | Note: |
113 | The code generated by this method uses the following already introduced variables: |
114 | INTEGER :: k, ifail |
115 | REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: poisson_reference |
116 | |
117 | |
118 | */ |
119 | } |
120 | } |