root / pobysoPythonSage / src / sageSLZ.sage @ 63
Historique | Voir | Annoter | Télécharger (9,95 ko)
| 1 |
|
|---|---|
| 2 |
def slz_compute_polynomial_and_interval(functionSo, degreeSo, lowerBoundSa, |
| 3 |
upperBoundSa, approxPrecSa, |
| 4 |
sollyaPrecSa=None): |
| 5 |
""" |
| 6 |
Under the assumptions listed for slz_get_intervals_and_polynomials, compute |
| 7 |
a polynomial that approximates the function on a an interval starting |
| 8 |
at lowerBoundSa and finishing at a value that guarantees that the polynomial |
| 9 |
approximates with the expected precision. |
| 10 |
The interval upper bound is lowered until the expected approximation |
| 11 |
precision is reached. |
| 12 |
The polynomial, the bounds, the center of the interval and the error |
| 13 |
are returned. |
| 14 |
""" |
| 15 |
RRR = lowerBoundSa.parent() |
| 16 |
#goldenRatioSa = RRR(5.sqrt() / 2 - 1/2) |
| 17 |
#intervalShrinkConstFactorSa = goldenRatioSa |
| 18 |
intervalShrinkConstFactorSa = RRR('0.5')
|
| 19 |
absoluteErrorTypeSo = pobyso_absolute_so_so() |
| 20 |
currentRangeSo = pobyso_bounds_to_range_sa_so(lowerBoundSa, upperBoundSa) |
| 21 |
currentUpperBoundSa = upperBoundSa |
| 22 |
currentLowerBoundSa = lowerBoundSa |
| 23 |
# What we want here is the polynomial without the variable change, |
| 24 |
# since our actual variable will be x-intervalCenter defined over the |
| 25 |
# domain [lowerBound-intervalCenter , upperBound-intervalCenter]. |
| 26 |
(polySo, intervalCenterSo, maxErrorSo) = \ |
| 27 |
pobyso_taylor_expansion_no_change_var_so_so(functionSo, degreeSo, |
| 28 |
currentRangeSo, |
| 29 |
absoluteErrorTypeSo) |
| 30 |
maxErrorSa = pobyso_get_constant_as_rn_with_rf_so_sa(maxErrorSo) |
| 31 |
while maxErrorSa > approxPrecSa: |
| 32 |
sollya_lib_clear_obj(maxErrorSo) |
| 33 |
errorRatioSa = 1/(maxErrorSa/approxPrecSa).log2() |
| 34 |
#print "Error ratio: ", errorRatioSa |
| 35 |
if errorRatioSa < intervalShrinkConstFactorSa: |
| 36 |
#currentUpperBoundSa = currentLowerBoundSa + (currentUpperBoundSa - currentLowerBoundSa) * errorRatioSa |
| 37 |
currentUpperBoundSa = currentLowerBoundSa + \ |
| 38 |
(currentUpperBoundSa - currentLowerBoundSa) * \ |
| 39 |
intervalShrinkConstFactorSa |
| 40 |
else: |
| 41 |
currentUpperBoundSa = currentLowerBoundSa + \ |
| 42 |
(currentUpperBoundSa - currentLowerBoundSa) * \ |
| 43 |
intervalShrinkConstFactorSa |
| 44 |
#print lowerBoundSa, currentUpperBoundSa |
| 45 |
sollya_lib_clear_obj(currentRangeSo) |
| 46 |
sollya_lib_clear_obj(polySo) |
| 47 |
currentRangeSo = pobyso_bounds_to_range_sa_so(currentLowerBoundSa, |
| 48 |
currentUpperBoundSa) |
| 49 |
(polySo, intervalCenterSo, maxErrorSo) = \ |
| 50 |
pobyso_taylor_expansion_no_change_var_so_so(functionSo, degreeSo, |
| 51 |
currentRangeSo, |
| 52 |
absoluteErrorTypeSo) |
| 53 |
#maxErrorSa = pobyso_get_constant_as_rn_with_rf_so_sa(maxErrorSo, RRR) |
| 54 |
maxErrorSa = pobyso_get_constant_as_rn_with_rf_so_sa(maxErrorSo) |
| 55 |
sollya_lib_clear_obj(absoluteErrorTypeSo) |
| 56 |
return((polySo, currentRangeSo, intervalCenterSo, maxErrorSo)) |
| 57 |
# End slz_compute_polynomial_and_interval |
| 58 |
|
| 59 |
def slz_get_intervals_and_polynomials(functionSa, variableNameSa, degreeSa, |
| 60 |
lowerBoundSa, |
| 61 |
upperBoundSa, floatingPointPrecSa, |
| 62 |
internalSollyaPrecSa): |
| 63 |
""" |
| 64 |
Under the assumption that: |
| 65 |
- functionSa is monotonic on the [lowerBoundSa, upperBoundSa] interval; |
| 66 |
- lowerBound and upperBound belong to the same binade. |
| 67 |
from a: |
| 68 |
- function; |
| 69 |
- a degree |
| 70 |
- a pair of bounds; |
| 71 |
- the floating-point precision we work on; |
| 72 |
- the internal Sollya precision; |
| 73 |
compute a list of tuples made of: |
| 74 |
- a polynomial approximating the function (a Sollya object); |
| 75 |
- the range for which the polynomial approximates the function |
| 76 |
(a Sollya object); |
| 77 |
- the center of the interval (a Sollya object); |
| 78 |
- the approximation error (a Sage object). |
| 79 |
The initial interval is, possibly, splitted into smaller intervals. |
| 80 |
It return a list of tuples, each made of: |
| 81 |
- a polynomial; |
| 82 |
- the approximation interval; |
| 83 |
- the center, x0, of the interval (the polynomial is defined as p(x-x0)); |
| 84 |
- the corresponding approximation error. |
| 85 |
""" |
| 86 |
x = var(variableNameSa) |
| 87 |
# Scalling the domain -> [1,2[. |
| 88 |
boundsIntervalRifSa = RealIntervalField(floatingPointPrecSa) |
| 89 |
domainBoundsIntervalSa = boundsIntervalRifSa(lowerBoundSa, upperBoundSa) |
| 90 |
(domainScalingExpressionSa, invDomainScalingExpressionSa) = \ |
| 91 |
slz_interval_scaling_expression(domainBoundsIntervalSa, variableNameSa) |
| 92 |
print "domainScalingExpression for argument :", domainScalingExpressionSa |
| 93 |
print "f: ", f |
| 94 |
ff = f.subs({x:domainScalingExpressionSa})
|
| 95 |
scaledLowerBoundSa = invDomainScalingExpressionSa(lowerBoundSa).n() |
| 96 |
scaledUpperBoundSa = invDomainScalingExpressionSa(upperBoundSa).n() |
| 97 |
print 'ff:', ff, "- Domain:", scaledLowerBoundSa, scaledUpperBoundSa |
| 98 |
# |
| 99 |
# Scalling the image -> [1,2[. |
| 100 |
flbSa = f(lowerBoundSa).n() |
| 101 |
fubSa = f(upperBoundSa).n() |
| 102 |
if flbSa <= fubSa: # Increasing |
| 103 |
imageBinadeBottomSa = floor(flbSa.log2()) |
| 104 |
else: # Decreasing |
| 105 |
imageBinadeBottomSa = floor(fubSa.log2()) |
| 106 |
print 'ff:', ff, '- Image:', flbSa, fubSa, imageBinadeBottomSa |
| 107 |
imageBoundsIntervalSa = boundsIntervalRifSa(flbSa, fubSa) |
| 108 |
(imageScalingExpressionSa, invImageScalingExpressionSa) = \ |
| 109 |
slz_interval_scaling_expression(imageBoundsIntervalSa, variableNameSa) |
| 110 |
iis = invImageScalingExpressionSa.function(x) |
| 111 |
fff = iis.subs({x:ff})
|
| 112 |
print "fff:", fff, |
| 113 |
print " - Image:", fff(scaledLowerBoundSa), fff(scaledUpperBoundSa) |
| 114 |
# |
| 115 |
resultArray = [] |
| 116 |
# |
| 117 |
approxPrecSa = 1/(2^(floatingPointPrecSa + 1)) |
| 118 |
print "Approximation precision: ", RR(approxPrecSa) |
| 119 |
# Prepare the arguments for the Taylor expansion computation with Sollya. |
| 120 |
functionSo = pobyso_parse_string_sa_so(fff._assume_str()) |
| 121 |
degreeSo = pobyso_constant_from_int_sa_so(degreeSa) |
| 122 |
scaledBoundsSo = pobyso_bounds_to_range_sa_so(scaledLowerBoundSa, |
| 123 |
scaledUpperBoundSa) |
| 124 |
# Compute the first Taylor expansion. |
| 125 |
(polySo, boundsSo, intervalCenterSo, maxErrorSo) = \ |
| 126 |
slz_compute_polynomial_and_interval(functionSo, degreeSo, |
| 127 |
scaledLowerBoundSa, scaledUpperBoundSa, |
| 128 |
approxPrecSa, internalSollyaPrecSa) |
| 129 |
""" |
| 130 |
changeVarExpressionSo = sollya_lib_build_function_sub( |
| 131 |
sollya_lib_build_function_free_variable(), |
| 132 |
sollya_lib_copy_obj(intervalCenterSo)) |
| 133 |
polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpressionSo) |
| 134 |
""" |
| 135 |
resultArray.append((polySo, boundsSo, intervalCenterSo, maxErrorSo)) |
| 136 |
realIntervalField = RealIntervalField(max(lowerBoundSa.parent().precision(), |
| 137 |
upperBoundSa.parent().precision())) |
| 138 |
boundsSa = pobyso_range_to_interval_so_sa(boundsSo, realIntervalField) |
| 139 |
# Compute the other expansions. |
| 140 |
while boundsSa.endpoints()[1] < scaledUpperBoundSa: |
| 141 |
currentScaledLowerBoundSa = boundsSa.endpoints()[1] |
| 142 |
(polySo, boundsSo, intervalCenterSo, maxErrorSo) = \ |
| 143 |
slz_compute_polynomial_and_interval(functionSo, degreeSo, |
| 144 |
currentScaledLowerBoundSa, |
| 145 |
scaledUpperBoundSa, approxPrecSa, |
| 146 |
internalSollyaPrecSa) |
| 147 |
resultArray.append((polySo, boundsSo, intervalCenterSo, maxErrorSo)) |
| 148 |
boundsSa = pobyso_range_to_interval_so_sa(boundsSo, realIntervalField) |
| 149 |
sollya_lib_clear_obj(functionSo) |
| 150 |
sollya_lib_clear_obj(degreeSo) |
| 151 |
sollya_lib_clear_obj(scaledBoundsSo) |
| 152 |
return(resultArray) |
| 153 |
# End slz_get_intervals_and_polynomials |
| 154 |
|
| 155 |
def slz_interval_scaling_expression(boundsInterval, varName): |
| 156 |
""" |
| 157 |
Compute the scaling expression to map an interval that span only |
| 158 |
a binade to [1, 2) and the inverse expression as well. |
| 159 |
Not very sure that the transformation makes sense for negative numbers. |
| 160 |
""" |
| 161 |
# The scaling offset is only used for negative numbers. |
| 162 |
if abs(boundsInterval.endpoints()[0]) < 1: |
| 163 |
if boundsInterval.endpoints()[0] >= 0: |
| 164 |
scalingCoeff = 2^floor(boundsInterval.endpoints()[0].log2()) |
| 165 |
invScalingCoeff = 1/scalingCoeff |
| 166 |
return((scalingCoeff * eval(varName), |
| 167 |
invScalingCoeff * eval(varName))) |
| 168 |
else: |
| 169 |
scalingCoeff = \ |
| 170 |
2^(floor((-boundsInterval.endpoints()[0]).log2()) - 1) |
| 171 |
scalingOffset = -3 * scalingCoeff |
| 172 |
return((scalingCoeff * eval(varName) + scalingOffset, |
| 173 |
1/scalingCoeff * eval(varName) + 3)) |
| 174 |
else: |
| 175 |
if boundsInterval.endpoints()[0] >= 0: |
| 176 |
scalingCoeff = 2^floor(boundsInterval.endpoints()[0].log2()) |
| 177 |
scalingOffset = 0 |
| 178 |
return((scalingCoeff * eval(varName), |
| 179 |
1/scalingCoeff * eval(varName))) |
| 180 |
else: |
| 181 |
scalingCoeff = \ |
| 182 |
2^(floor((-boundsInterval.endpoints()[1]).log2())) |
| 183 |
scalingOffset = -3 * scalingCoeff |
| 184 |
#scalingOffset = 0 |
| 185 |
return((scalingCoeff * eval(varName) + scalingOffset, |
| 186 |
1/scalingCoeff * eval(varName) + 3)) |
| 187 |
|
| 188 |
|
| 189 |
def slz_polynomial_and_interval_to_sage(polyRangeCenterErrorSo): |
| 190 |
polynomialSa = pobyso_get_poly_so_sa(polyRangeCenterErrorSo[0]) |
| 191 |
intervalSa = \ |
| 192 |
pobyso_get_interval_from_range_so_sa(polyRangeCenterErrorSo[1]) |
| 193 |
centerSa = \ |
| 194 |
pobyso_get_constant_as_rn_with_rf_so_sa(polyRangeCenterErrorSo[2]) |
| 195 |
errorSa = \ |
| 196 |
pobyso_get_constant_as_rn_with_rf_so_sa(polyRangeCenterErrorSo[3]) |
| 197 |
return((polynomialSa, intervalSa, centerSa, errorSa)) |
| 198 |
# End slz_polynomial_and_interval_to_sage |
| 199 |
|
| 200 |
print "sageSLZ loaded..." |