| 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 DiscreteUniformDistribution extends Distribution |
| 14 | { |
| 15 | |
| 16 | public DiscreteUniformDistribution() |
| 17 | { |
| 18 | super(2); |
| 19 | parameterType[0] = 0; // integer |
| 20 | parameterType[1] = 0; // integer |
| 21 | } |
| 22 | |
| 23 | public ArrayList<String> getIntroduction() |
| 24 | { |
| 25 | ArrayList<String> result = new ArrayList<String>(); |
| 26 | result.add("INTEGER G05DYF"); |
| 27 | result.add("EXTERNAL G05DYF"); |
| 28 | return result; |
| 29 | } |
| 30 | |
| 31 | public ArrayList<String> getFreqCode(String[] parameters) throws IllegalParametersException |
| 32 | { |
| 33 | // This is not done with NAG because it's a trivial calculation |
| 34 | // Parameters are: m, n, point, result |
| 35 | |
| 36 | if (parameters.length != 4) |
| 37 | { |
| 38 | throw new IllegalParametersException("Trying to use discrete uniform distribution with illegal parameters."); |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | The code we want to generate looks like this: |
| 43 | IF (point < m .OR. point > n) THEN |
| 44 | result = 0 |
| 45 | ELSE |
| 46 | result = 1 / (n - (m) +1) |
| 47 | END IF |
| 48 | */ |
| 49 | |
| 50 | ArrayList<String> result = new ArrayList<String>(); |
| 51 | |
| 52 | result.add("IF (" + parameters[2] + " < " + parameters[0] + " .OR. " + |
| 53 | parameters[2] + " > " + parameters[1] + ") THEN"); |
| 54 | result.add(parameters[3] + " = 0"); |
| 55 | result.add("ELSE"); |
| 56 | result.add(parameters[3] + " = 1.0 / (REAL("+parameters[1]+") - (REAL(" + parameters[0] + ")) + 1.0)"); |
| 57 | result.add("END IF"); |
| 58 | |
| 59 | return result; |
| 60 | } |
| 61 | |
| 62 | public ArrayList<String> getGenCode(String[] parameters) throws IllegalParametersException |
| 63 | { |
| 64 | // Parameters are: m, n, result |
| 65 | if (parameters.length != 3) |
| 66 | { |
| 67 | throw new IllegalParametersException("Trying to use discrete uniform distribution with illegal parameters."); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | The code we want to generate looks like this: |
| 72 | DO k=1, SIZE(result) |
| 73 | result(k) = G05DYF(m, n) |
| 74 | END DO |
| 75 | */ |
| 76 | |
| 77 | ArrayList<String> result = new ArrayList<String>(); |
| 78 | |
| 79 | result.add("DO k=1, SIZE(" + parameters[2] + ")"); |
| 80 | result.add(parameters[2] + "(k) = G05DYF(" + parameters[0] + ", " + parameters[1] + ")"); |
| 81 | result.add("END DO"); |
| 82 | |
| 83 | return result; |
| 84 | |
| 85 | /* |
| 86 | Note: |
| 87 | The code generated by this method uses the following already introduced variables: |
| 88 | INTEGER :: k |
| 89 | */ |
| 90 | } |
| 91 | } |