| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evolution.coalescent; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class TwoEpochDemographic extends DemographicFunction.Abstract { |
| 36 |
|
| 37 |
private DemographicFunction epoch1; |
| 38 |
private DemographicFunction epoch2; |
| 39 |
private double transitionTime; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public TwoEpochDemographic(DemographicFunction epoch1, DemographicFunction epoch2, Type units) { |
| 45 |
|
| 46 |
super(units); |
| 47 |
|
| 48 |
this.epoch1 = epoch1; |
| 49 |
this.epoch2 = epoch2; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public final double getTransitionTime() { return transitionTime; } |
| 56 |
|
| 57 |
public final void setTransitionTime(double t) { |
| 58 |
if (t < 0.0 || t > Double.MAX_VALUE) { |
| 59 |
throw new IllegalArgumentException("transition time out of bounds."); |
| 60 |
} |
| 61 |
transitionTime = t; |
| 62 |
} |
| 63 |
|
| 64 |
public final DemographicFunction getFirstEpochDemography() { |
| 65 |
return epoch1; |
| 66 |
} |
| 67 |
|
| 68 |
public final DemographicFunction getSecondEpochDemography() { |
| 69 |
return epoch2; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public final double getDemographic(double t) { |
| 75 |
|
| 76 |
if (t < transitionTime) { |
| 77 |
return epoch1.getDemographic(t); |
| 78 |
} else { |
| 79 |
return epoch2.getDemographic(t-transitionTime); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public final double getIntensity(double t) { |
| 84 |
|
| 85 |
if (t < transitionTime) { |
| 86 |
return epoch1.getIntensity(t); |
| 87 |
} else { |
| 88 |
return epoch1.getIntensity(transitionTime) + epoch2.getIntensity(t-transitionTime); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public final double getIntegral(double start, double finish) { |
| 99 |
|
| 100 |
if (start < transitionTime) { |
| 101 |
if (finish < transitionTime) { |
| 102 |
return epoch1.getIntegral(start, finish); |
| 103 |
} else { |
| 104 |
return epoch1.getIntegral(start, transitionTime) + epoch2.getIntegral(0, finish-transitionTime); |
| 105 |
} |
| 106 |
} else { |
| 107 |
return epoch2.getIntegral(start-transitionTime, finish-transitionTime); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public final double getInverseIntensity(double x) { |
| 116 |
|
| 117 |
double time = epoch1.getInverseIntensity(x); |
| 118 |
if (time < transitionTime) { |
| 119 |
return time; |
| 120 |
} |
| 121 |
x -= epoch1.getIntensity(transitionTime); |
| 122 |
return transitionTime + epoch2.getInverseIntensity(x); |
| 123 |
} |
| 124 |
|
| 125 |
public int getNumArguments() { |
| 126 |
return epoch1.getNumArguments() + epoch2.getNumArguments() + 1; |
| 127 |
} |
| 128 |
|
| 129 |
public final String getArgumentName(int n) { |
| 130 |
|
| 131 |
if (n < epoch1.getNumArguments()) { |
| 132 |
return epoch1.getArgumentName(n); |
| 133 |
} |
| 134 |
n -= epoch1.getNumArguments(); |
| 135 |
if (n < epoch2.getNumArguments()) { |
| 136 |
return epoch2.getArgumentName(n); |
| 137 |
} |
| 138 |
n -= epoch2.getNumArguments(); |
| 139 |
if (n == 0) return "transitionTime"; |
| 140 |
throw new IllegalArgumentException(); |
| 141 |
} |
| 142 |
|
| 143 |
public final double getArgument(int n) { |
| 144 |
|
| 145 |
if (n < epoch1.getNumArguments()) { |
| 146 |
return epoch1.getArgument(n); |
| 147 |
} |
| 148 |
n -= epoch1.getNumArguments(); |
| 149 |
if (n < epoch2.getNumArguments()) { |
| 150 |
return epoch2.getArgument(n); |
| 151 |
} |
| 152 |
n -= epoch2.getNumArguments(); |
| 153 |
if (n == 0) return transitionTime; |
| 154 |
throw new IllegalArgumentException(); |
| 155 |
} |
| 156 |
|
| 157 |
public final void setArgument(int n, double value) { |
| 158 |
|
| 159 |
if (n < epoch1.getNumArguments()) { |
| 160 |
epoch1.setArgument(n, value); |
| 161 |
} |
| 162 |
n -= epoch1.getNumArguments(); |
| 163 |
if (n < epoch2.getNumArguments()) { |
| 164 |
epoch2.setArgument(n, value); |
| 165 |
} |
| 166 |
n -= epoch2.getNumArguments(); |
| 167 |
if (n == 0) transitionTime = value; |
| 168 |
throw new IllegalArgumentException(); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public final double getLowerBound(int n) { |
| 175 |
|
| 176 |
if (n < epoch1.getNumArguments()) { |
| 177 |
return epoch1.getLowerBound(n); |
| 178 |
} |
| 179 |
n -= epoch1.getNumArguments(); |
| 180 |
if (n < epoch2.getNumArguments()) { |
| 181 |
return epoch2.getLowerBound(n); |
| 182 |
} |
| 183 |
n -= epoch2.getNumArguments(); |
| 184 |
if (n == 0) return 0.0; |
| 185 |
throw new IllegalArgumentException(); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public final double getUpperBound(int n) { |
| 192 |
|
| 193 |
if (n < epoch1.getNumArguments()) { |
| 194 |
return epoch1.getUpperBound(n); |
| 195 |
} |
| 196 |
n -= epoch1.getNumArguments(); |
| 197 |
if (n < epoch2.getNumArguments()) { |
| 198 |
return epoch2.getUpperBound(n); |
| 199 |
} |
| 200 |
n -= epoch2.getNumArguments(); |
| 201 |
if (n == 0) return Double.MAX_VALUE; |
| 202 |
throw new IllegalArgumentException(); |
| 203 |
} |
| 204 |
|
| 205 |
public final DemographicFunction getCopy() { |
| 206 |
TwoEpochDemographic df = new TwoEpochDemographic(epoch1, epoch2, getUnits()); |
| 207 |
df.setTransitionTime(transitionTime); |
| 208 |
return df; |
| 209 |
} |
| 210 |
} |