Révision 217 pobysoPythonSage/src/pobyso.py

pobyso.py (revision 217)
1114 1114
    return precisionSa
1115 1115
# End pobyso_precision_so_sa
1116 1116

  
1117
def pobyso_polynomial_coefficients_progressive_truncate_so_so(polySo,
1118
                                                              funcSo,
1119
                                                              icSo,
1120
                                                              intervalSo,
1121
                                                              itpSo,
1122
                                                              ftpSo,
1123
                                                              maxPrecSo,
1124
                                                              maxErrSo):
1117
def pobyso_polynomial_coefficients_progressive_round_so_so(polySo,
1118
                                                           funcSo,
1119
                                                           icSo,
1120
                                                           intervalSo,
1121
                                                           itpSo,
1122
                                                           ftpSo,
1123
                                                           maxPrecSo,
1124
                                                           maxErrSo):
1125 1125
    print "Input arguments:"
1126 1126
    pobyso_autoprint(polySo)
1127 1127
    pobyso_autoprint(funcSo)
......
1133 1133
    pobyso_autoprint(maxErrSo)
1134 1134
    print "________________"
1135 1135
    
1136
    precDecayFuncSo = \
1137
        pobyso_parse_string("3/8*(exp(x/2.2)-1) + 2/8*(2*x)^2 + (1-3/8-2/8)*x")
1138
    pobyso_autoprint(precDecayFuncSo)
1136
    ## Higher order function see: 
1137
    #  http://effbot.org/pyfaq/how-do-you-make-a-higher-order-function-in-python.htm
1138
    def precision_decay_ratio_function(degreeSa):
1139
        def outer(x):
1140
            def inner(x):
1141
                we = 3/8
1142
                wq = 2/8
1143
                a  = 2.2
1144
                b  = 2 
1145
                return we*(exp(x/a)-1) +  wq*((b*x)**2) + (1-we-wq)*x
1146
            return  inner(x)/inner(degreeSa)
1147
        return outer
1148
    
1149
    #
1150
    degreeSa        = pobyso_polynomial_degree_so_sa(polySo)
1151
    ratio           = precision_decay_ratio_function(degreeSa)
1152
    itpSa           = pobyso_constant_from_int_so_sa(itpSo)
1153
    ftpSa           = pobyso_constant_from_int_so_sa(ftpSo)
1154
    maxPrecSa       = pobyso_constant_from_int_so_sa(maxPrecSo)
1155
    maxErrSa        = pobyso_get_constant_as_rn_so_sa(maxErrSo)
1156
    resPolySo       = pobyso_constant_0_sa_so()
1157
    lastResPolySo   = None
1158
    while True: 
1159
        pDeltaSa    = ftpSa - itpSa
1160
        for indexSa in reversed(xrange(0,degreeSa+1)):
1161
            print "Index:", indexSa
1162
            indexSo = pobyso_constant_from_int_sa_so(indexSa)
1163
            #pobyso_autoprint(indexSo)
1164
            #print ratio(indexSa)
1165
            ctpSa = floor(ftpSa - (pDeltaSa * ratio(indexSa)))
1166
            ctpSo = pobyso_constant_from_int_sa_so(ctpSa)
1167
            print "Index:", indexSa, " - Target precision:", 
1168
            pobyso_autoprint(ctpSo)
1169
            cmonSo  = \
1170
                sollya_lib_build_function_mul(sollya_lib_coeff(polySo, indexSo),
1171
                                      sollya_lib_build_function_pow( \
1172
                                          sollya_lib_build_function_free_variable(), \
1173
                                          indexSo))
1174
            #pobyso_autoprint(cmonSo)
1175
            cmonrSo = pobyso_round_coefficients_single_so_so(cmonSo, ctpSo)
1176
            sollya_lib_clear_obj(cmonSo)
1177
            #pobyso_autoprint(cmonrSo)
1178
            resPolySo = sollya_lib_build_function_add(resPolySo,
1179
                                                      cmonrSo)
1180
            pobyso_autoprint(resPolySo)
1181
        # End for index
1182
        freeVarSo     = sollya_lib_build_function_free_variable()
1183
        changeVarSo   = sollya_lib_sub(freeVarSo, icSo)
1184
        resPolyCvSo   = sollya_lib_evaluate(resPolySo, changeVarSo)
1185
        errFuncSo = sollya_lib_build_function_sub(funcSo, resPolyCvSo)
1186
        infNormSo = sollya_lib_dirtyinform(errFuncSo, intervalSo)
1187
        print "Infnorm (Sollya):", ; pobyso_autoprint(infNormSo)
1188
        cerrSa    = pobyso_get_constant_as_rn_so_sa(infNormSo)
1189
        sollya_lib_clear_obj(resPolyCvSo)
1190
        sollya_lib_clear_obj(infNormSo)
1191
        print "Infnorm  (Sage):", cerrSa
1192
        if (cerrSa > maxErrSa):
1193
            print "Error is too large."
1194
            if not lastResPolySo is None:
1195
                print "Enlarging prec."
1196
                ntpSa = floor(ftpSa + ftpSa/50)
1197
                ## Can't enlarge (numerical)
1198
                if ntpSa == ftpSa:
1199
                    sollya_lib_clear_obj(lastResPolySo)
1200
                    sollya_lib_clear_obj(resPolySo)
1201
                    return None
1202
                ## Can't enlarge (not enough precision left)
1203
                if ntpSa > maxPrecSa:
1204
                    sollya_lib_clear_obj(lastResPolySo)
1205
                    sollya_lib_clear_obj(resPolySo)
1206
                    return None
1207
                ftpSa = ntpSa
1208
                lastResPolySo = resPolySo
1209
                continue
1210
            ## One enlargement took place.
1211
            else:
1212
                sollya_lib_clear_obj(resPolySo)
1213
                return lastResPolySo
1214
        # cerrSa <= maxErrSa: scrap more bits.
1215
        else:
1216
            print "Shrinking prec."
1217
            ntpSa = floor(ftpSa - ftpSa/50)
1218
            ## Can't shrink (numerical)
1219
            if ntpSa == ftpSa:
1220
                if not lastResPolySo is None:
1221
                    sollya_lib_clear_obj(lastResPolySo)
1222
                return resPolySo
1223
            ## Can't enlarge (not enough precision left)
1224
            if ntpSa <= itpSa:
1225
                if not lastResPolySo is None:
1226
                    sollya_lib_clear_obj(lastResPolySo)
1227
                return resPolySo
1228
            ftpSa = ntpSa
1229
            continue
1230
    # End wile True
1139 1231
    return None
1140
    
1232
# End pobyso_polynomial_coefficients_progressive_truncate_so_so.
1233

  
1234
def pobyso_polynomial_degree_so_sa(polySo):
1235
    """
1236
    Return the degree of a Sollya polynomial as a Sage int.
1237
    """
1238
    degreeSo = sollya_lib_degree(polySo)
1239
    return pobyso_constant_from_int_so_sa(degreeSo)
1240
# End pobyso_polynomial_degree_so_sa
1241

  
1242
def pobyso_polynomial_degree_so_so(polySo):
1243
    """
1244
    Thin wrapper around lib_sollya_degree().
1245
    """
1246
    return sollya_lib_degree(polySo)
1247
# End pobyso_polynomial_degree_so_so
1248
                                                                  
1141 1249
def pobyso_range(rnLowerBound, rnUpperBound):
1142 1250
    """ Legacy function. See pobyso_range_sa_so. """
1143 1251
    return pobyso_range_sa_so(rnLowerBound, rnUpperBound) 
......
1324 1432
    ## TODO: check arguments.
1325 1433
    endEllipListSo = pobyso_build_end_elliptic_list_so_so(precSo)
1326 1434
    polySo = sollya_lib_roundcoefficients(polySo, endEllipListSo, None)
1435
    sollya_lib_clear_obj(endEllipListSo)
1327 1436
    #sollya_lib_clear_obj(endEllipListSo)
1328 1437
    return polySo
1329 1438
    

Formats disponibles : Unified diff