| 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 BinomialDistribution extends Distribution |
| 14 | { |
| 15 | |
| 16 | public BinomialDistribution() |
| 17 | { |
| 18 | super(2); |
| 19 | parameterType[0] = 0; // integer |
| 20 | parameterType[1] = 1; // real |
| 21 | } |
| 22 | |
| 23 | public ArrayList<String> getIntroduction() |
| 24 | { |
| 25 | ArrayList<String> result = new ArrayList<String>(); |
| 26 | |
| 27 | result.add("INTEGER G05EYF"); |
| 28 | result.add("EXTERNAL G05EYF"); |
| 29 | result.add("EXTERNAL G05EDF"); |
| 30 | result.add("EXTERNAL G01BJF"); |
| 31 | |
| 32 | return result; |
| 33 | } |
| 34 | |
| 35 | public ArrayList<String> getFreqCode(String[] parameters) throws IllegalParametersException |
| 36 | { |
| 37 | // Parameters are: n, p, point, result |
| 38 | if (parameters.length != 4) |
| 39 | { |
| 40 | throw new IllegalParametersException("Trying to use binomial distribution with illegal parameters."); |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | The code we want to generate looks like this: |
| 45 | ifail=0 |
| 46 | CALL G01BJF(n, p, point, temp_real, temp_real, result, ifail) |
| 47 | IF (ifail /= 0) THEN |
| 48 | WRITE (*, *) 'Error when trying to calculate frequency of the binomial distribution, parameters: n = ', n, 'p = ', p |
| 49 | STOP |
| 50 | END IF |
| 51 | */ |
| 52 | |
| 53 | ArrayList<String> result = new ArrayList<String>(); |
| 54 | System.out.println("Using binomial distribution"); |
| 55 | result.add("ifail=0"); |
| 56 | result.add("CALL G01BJF(" + parameters[0] + ", " + parameters[1] + ", " + |
| 57 | parameters[2] + ", temp_real, temp_real, " + parameters[3] + ", ifail)"); |
| 58 | result.add("IF (ifail /= 0) THEN"); |
| 59 | result.add("WRITE (*, *) 'Error when trying to calculate frequency of the binomial distribution, " + |
| 60 | "parameters: n = '," + parameters[0] + ", 'p = '," + parameters[1]); |
| 61 | result.add("STOP"); |
| 62 | result.add("END IF"); |
| 63 | return result; |
| 64 | |
| 65 | /* |
| 66 | Note: |
| 67 | The code generated by this method uses the following already introduced variables: |
| 68 | INTEGER :: ifail |
| 69 | REAL :: temp_real |
| 70 | */ |
| 71 | |
| 72 | } |
| 73 | |
| 74 | public ArrayList<String> getGenCode(String[] parameters) throws IllegalParametersException |
| 75 | { |
| 76 | // Parameters are: n, p, result |
| 77 | if (parameters.length != 3) |
| 78 | { |
| 79 | throw new IllegalParametersException("Trying to use binomial distribution with illegal parameters."); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | The code we want to generate looks like this: |
| 84 | ifail=0 |
| 85 | CALL G05EDF(n, p, binomial_reference, SIZE(binomial_reference), ifail) |
| 86 | IF (ifail /= 0) THEN |
| 87 | WRITE (*, *) 'Error when trying to generate random numbers from binomial distribution, parameters: n = ', n, 'p = ', p |
| 88 | STOP |
| 89 | END IF |
| 90 | DO k=0, SIZE(result) |
| 91 | result(k)=G05EYF(binomial_reference, SIZE(binomial_reference)) |
| 92 | END DO |
| 93 | */ |
| 94 | |
| 95 | ArrayList<String> result = new ArrayList<String>(); |
| 96 | |
| 97 | result.add("ifail = 0"); |
| 98 | result.add("ALLOCATE(binomial_reference(1 : NINT(20 + 20 * SQRT(" + parameters[0] + |
| 99 | " * " + parameters[1] + " * (1 - " + parameters[1] +")))))"); |
| 100 | result.add("CALL G05EDF(" + parameters[0] + ", " + parameters[1] + |
| 101 | ", binomial_reference, SIZE(binomial_reference), ifail)"); |
| 102 | result.add("IF (ifail /= 0) THEN"); |
| 103 | result.add("WRITE (*, *) 'Error when trying to generate random numbers from binomial distribution, parameters: n = ', " |
| 104 | + parameters[0] + ", ' p = ', " + parameters[1]); |
| 105 | result.add("STOP"); |
| 106 | result.add("END IF"); |
| 107 | result.add("DO k=1, SIZE(" + parameters[2] +")"); |
| 108 | result.add(parameters[2] + "(k)=G05EYF(binomial_reference, SIZE(binomial_reference))"); |
| 109 | result.add("END DO"); |
| 110 | |
| 111 | return result; |
| 112 | |
| 113 | /* |
| 114 | Note: |
| 115 | The code generated by this method uses the following already introduced variables: |
| 116 | INTEGER :: k, ifail |
| 117 | REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: binomial_reference |
| 118 | |
| 119 | NOTE: the dimension must be large enough! |
| 120 | Optimum: NR = 20 + 20 * sqrt( N * P(1 -P)) |
| 121 | |
| 122 | */ |
| 123 | } |
| 124 | } |