Révision 218 pobysoPythonSage/src/pobyso.py

pobyso.py (revision 218)
90 90
    return(sollya_lib_absolute(None))
91 91

  
92 92
def pobyso_autoprint(arg):
93
    sollya_lib_autoprint(arg,None)
93
    sollya_lib_autoprint(arg, None)
94 94

  
95 95
def pobyso_autoprint_so_so(arg):
96 96
    sollya_lib_autoprint(arg,None)
......
369 369
    ## Precision. If a precision is given, convert the polynomial
370 370
    #  into the right polynomial field. If not convert it straight
371 371
    #  to Sollya.
372
    if not precSa is None:
373
        RRR = RealField(precSa)
374
        ## Create a Sage polynomial in the "right" precision.
375
        P_RRR = RRR[polySa.variables()[0]]
376
        polyFloatSa = P_RRR(polySa)
377
    else:
378
        polyFloatSa = polySa
372
    sollyaPrecChanged = False 
373
    (curSollyaPrecSo, curSollyaPrecSa) = pobyso_get_prec_so_so_sa()
374
    if precSa is None:
379 375
        precSa = polySa.parent().base_ring().precision()
376
    if (precSa != curSollyaPrecSa):
377
        precSo = pobyso_constant_from_int(precSa)
378
        pobyso_set_prec_so_so(precSo)
379
        sollya_lib_clear_obj(precSo)
380
        sollyaPrecChanged = True    
380 381
    ## Get exponents and coefficients.
381
    exponentsSa = polyFloatSa.exponents()
382
    coefficientsSa = polyFloatSa.coefficients()
382
    exponentsSa     = polySa.exponents()
383
    coefficientsSa  = polySa.coefficients()
383 384
    ## Build the polynomial.
384 385
    polySo = None
385 386
    for coefficientSa, exponentSa in zip(coefficientsSa, exponentsSa):
......
393 394
        monomialSo = sollya_lib_build_function_pow(
394 395
                       sollya_lib_build_function_free_variable(),
395 396
                       exponentSo)
397
        polyTermSo = sollya_lib_build_function_mul(coefficientSo,
398
                                                       monomialSo)
396 399
        if polySo is None:
397
            polySo = sollya_lib_build_function_mul(coefficientSo,
398
                                                   monomialSo)
400
            polySo = polyTermSo
399 401
        else:
400
            polyTermSo = sollya_lib_build_function_mul(coefficientSo,
401
                                                       monomialSo)
402 402
            polySo = sollya_lib_build_function_add(polySo, polyTermSo)
403
    if sollyaPrecChanged:
404
        pobyso_set_prec_so_so(curSollyaPrecSo)
405
        sollya_lib_clear_obj(curSollyaPrecSo)
403 406
    return polySo
404 407
# End pobyso_float_poly_sa_so    
405 408

  
406 409
def pobyso_float_poly_so_sa(polySo, realFieldSa=None):
407 410
    """
408 411
    Convert a Sollya polynomial into a Sage floating-point polynomial.
409
    We assume that the polynomial is in canonical form.
410 412
    If no realField is given, a RealField corresponding to the maximum 
411 413
    precision of the coefficients is internally computed.
412 414
    The real field is not returned but can be easily retrieved from 
......
415 417
    - (optional) compute the RealField of the coefficients;
416 418
    - convert the Sollya expression into a Sage expression;
417 419
    - convert the Sage expression into a Sage polynomial
418
    TODO: the canonical thing for the polynomial.
419 420
    """    
420 421
    if realFieldSa is None:
421 422
        expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo)
422
        print "Maximum precision of Sollya polynomial coefficients:", expressionPrecSa
423
        #print "Maximum precision of Sollya polynomial coefficients:", expressionPrecSa
423 424
        realFieldSa      = RealField(expressionPrecSa)
424 425
    #print "Sollya expression before...",
425 426
    #pobyso_autoprint(polySo)
426 427

  
427 428
    expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo,
428 429
                                                             realFieldSa)
429
    #print "...Sollya expression after.",
430
    #print "...Sollya expression after."
430 431
    #pobyso_autoprint(polySo)
431 432
    polyVariableSa = expressionSa.variables()[0]
432 433
    polyRingSa     = realFieldSa[str(polyVariableSa)]
......
575 576
    The precision of the floating-point number returned is that of the Sollya
576 577
    constant.
577 578
    """
579
    #print "Before computing precision of variable..."
580
    #pobyso_autoprint(constExpSo)
578 581
    precisionSa  = pobyso_get_prec_of_constant_so_sa(constExpSo)
582
    #print "precisionSa:", precisionSa
579 583
    ## If the expression can not be exactly converted, None is returned.
580 584
    #  In this case opt for the Sollya current expression.
581 585
    if precisionSa is None:
......
641 645

  
642 646
def pobyso_get_head_function_so_sa(expressionSo):
643 647
    functionType = c_int(0)
644
    sollya_lib_get_head_function(byref(functionType), expressionSo, None)
648
    sollya_lib_get_head_function(byref(functionType), expressionSo)
645 649
    return int(functionType.value)
646 650

  
647 651
def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ):
......
813 817
    Tries to find a precision to represent ctExpSo without rounding.
814 818
    If not possible, returns None.
815 819
    """
820
    #print "Entering pobyso_get_prec_of_constant_so_sa..."
816 821
    prec = c_int(0)
817 822
    retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
818 823
    if retc == 0:
824
        #print "pobyso_get_prec_of_constant_so_sa failed."
819 825
        return None
826
    #print "...exiting pobyso_get_prec_of_constant_so_sa."
820 827
    return int(prec.value)
821 828

  
822 829
def pobyso_get_prec_of_range_so_sa(rangeSo):
......
876 883
        #print "This is a constant"
877 884
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExpSo, realFieldSa)
878 885
    elif operatorSa == SOLLYA_BASE_FUNC_FREE_VARIABLE:
879
        #print "This is free variable"
886
        #print "This is the free variable"
880 887
        return eval(sollyaLibFreeVariableName)
881 888
    else:
882 889
        print "Unexpected"
......
1123 1130
                                                           maxPrecSo,
1124 1131
                                                           maxErrSo):
1125 1132
    print "Input arguments:"
1126
    pobyso_autoprint(polySo)
1127
    pobyso_autoprint(funcSo)
1128
    pobyso_autoprint(icSo)
1129
    pobyso_autoprint(intervalSo)
1130
    pobyso_autoprint(itpSo)
1131
    pobyso_autoprint(ftpSo)
1132
    pobyso_autoprint(maxPrecSo)
1133
    pobyso_autoprint(maxErrSo)
1134
    print "________________"
1133
    #pobyso_autoprint(polySo)
1134
    #pobyso_autoprint(funcSo)
1135
    #pobyso_autoprint(icSo)
1136
    #pobyso_autoprint(intervalSo)
1137
    #pobyso_autoprint(itpSo)
1138
    #pobyso_autoprint(ftpSo)
1139
    #pobyso_autoprint(maxPrecSo)
1140
    #pobyso_autoprint(maxErrSo)
1141
    #print "________________"
1135 1142
    
1136 1143
    ## Higher order function see: 
1137 1144
    #  http://effbot.org/pyfaq/how-do-you-make-a-higher-order-function-in-python.htm
......
1148 1155
    
1149 1156
    #
1150 1157
    degreeSa        = pobyso_polynomial_degree_so_sa(polySo)
1158
    print "degreeSa:", degreeSa
1151 1159
    ratio           = precision_decay_ratio_function(degreeSa)
1160
    print "ratio:", ratio
1152 1161
    itpSa           = pobyso_constant_from_int_so_sa(itpSo)
1162
    print "itpsSa:", itpSa
1153 1163
    ftpSa           = pobyso_constant_from_int_so_sa(ftpSo)
1164
    print "ftpSa:", ftpSa
1154 1165
    maxPrecSa       = pobyso_constant_from_int_so_sa(maxPrecSo)
1166
    print "maxPrecSa:", maxPrecSa
1155 1167
    maxErrSa        = pobyso_get_constant_as_rn_so_sa(maxErrSo)
1156
    resPolySo       = pobyso_constant_0_sa_so()
1168
    print "maxErrSa:", maxErrSa
1157 1169
    lastResPolySo   = None
1170
    lastInfNormSo   = None
1171
    print "About to enter the while loop..."
1158 1172
    while True: 
1173
        resPolySo   = pobyso_constant_0_sa_so()
1159 1174
        pDeltaSa    = ftpSa - itpSa
1160 1175
        for indexSa in reversed(xrange(0,degreeSa+1)):
1161
            print "Index:", indexSa
1176
            #print "Index:", indexSa
1162 1177
            indexSo = pobyso_constant_from_int_sa_so(indexSa)
1163 1178
            #pobyso_autoprint(indexSo)
1164 1179
            #print ratio(indexSa)
......
1177 1192
            #pobyso_autoprint(cmonrSo)
1178 1193
            resPolySo = sollya_lib_build_function_add(resPolySo,
1179 1194
                                                      cmonrSo)
1180
            pobyso_autoprint(resPolySo)
1195
            #pobyso_autoprint(resPolySo)
1181 1196
        # End for index
1182 1197
        freeVarSo     = sollya_lib_build_function_free_variable()
1183 1198
        changeVarSo   = sollya_lib_sub(freeVarSo, icSo)
1184 1199
        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)
1200
        errFuncSo = sollya_lib_build_function_sub(sollya_lib_copy_obj(funcSo), 
1201
                                                  resPolyCvSo)
1202
        infNormSo = sollya_lib_dirtyinfnorm(errFuncSo, intervalSo)
1188 1203
        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
1204
        print "Infnorm (Sollya):", pobyso_autoprint(infNormSo)
1205
        sollya_lib_clear_obj(errFuncSo)
1206
        #print "Infnorm  (Sage):", cerrSa
1192 1207
        if (cerrSa > maxErrSa):
1193 1208
            print "Error is too large."
1194
            if not lastResPolySo is None:
1209
            if lastResPolySo is None:
1195 1210
                print "Enlarging prec."
1196 1211
                ntpSa = floor(ftpSa + ftpSa/50)
1197 1212
                ## Can't enlarge (numerical)
1198 1213
                if ntpSa == ftpSa:
1199
                    sollya_lib_clear_obj(lastResPolySo)
1200 1214
                    sollya_lib_clear_obj(resPolySo)
1201 1215
                    return None
1202 1216
                ## Can't enlarge (not enough precision left)
1203 1217
                if ntpSa > maxPrecSa:
1204
                    sollya_lib_clear_obj(lastResPolySo)
1205 1218
                    sollya_lib_clear_obj(resPolySo)
1206 1219
                    return None
1207 1220
                ftpSa = ntpSa
1208
                lastResPolySo = resPolySo
1209 1221
                continue
1210 1222
            ## One enlargement took place.
1211 1223
            else:
1224
                print "Exit with the last before last polynomial."
1212 1225
                sollya_lib_clear_obj(resPolySo)
1213
                return lastResPolySo
1214
        # cerrSa <= maxErrSa: scrap more bits.
1226
                sollya_lib_clear_obj(infNormSo)
1227
                return (lastResPolySo, lastInfNormSo)
1228
        # cerrSa <= maxErrSa: scrap more bits, possibly.
1215 1229
        else:
1216
            print "Shrinking prec."
1217
            ntpSa = floor(ftpSa - ftpSa/50)
1218
            ## Can't shrink (numerical)
1219
            if ntpSa == ftpSa:
1230
            print "Error is too small"
1231
            if cerrSa <= (maxErrSa/2): 
1232
                print "Shrinking prec."
1233
                ntpSa = floor(ftpSa - ftpSa/50)
1234
                ## Can't shrink (numerical)
1235
                if ntpSa == ftpSa:
1236
                    if not lastResPolySo is None:
1237
                        sollya_lib_clear_obj(lastResPolySo)
1238
                    if not lastInfNormSo is None:
1239
                        sollya_lib_clear_obj(lastInfNormSo)
1240
                    return (resPolySo, infNormSo)
1241
                ## Can't shrink (not enough precision left)
1242
                if ntpSa <= itpSa:
1243
                    if not lastResPolySo is None:
1244
                        sollya_lib_clear_obj(lastResPolySo)
1245
                    if not lastInfNormSo is None:
1246
                        sollya_lib_clear_obj(lastInfNormSo)
1247
                    return (resPolySo, infNormSo)
1248
                ftpSa = ntpSa
1220 1249
                if not lastResPolySo is None:
1221 1250
                    sollya_lib_clear_obj(lastResPolySo)
1222
                return resPolySo
1223
            ## Can't enlarge (not enough precision left)
1224
            if ntpSa <= itpSa:
1251
                if not lastInfNormSo is None:
1252
                    sollya_lib_clear_obj(lastInfNormSo)
1253
                lastResPolySo = resPolySo
1254
                lastInfNormSo = infNormSo
1255
                continue
1256
            else: # Error is not that small, just return
1225 1257
                if not lastResPolySo is None:
1226 1258
                    sollya_lib_clear_obj(lastResPolySo)
1227
                return resPolySo
1228
            ftpSa = ntpSa
1229
            continue
1259
                if not lastInfNormSo is None:
1260
                    sollya_lib_clear_obj(lastInfNormSo)
1261
                return (resPolySo, infNormSo)                
1230 1262
    # End wile True
1231 1263
    return None
1232 1264
# End pobyso_polynomial_coefficients_progressive_truncate_so_so.

Formats disponibles : Unified diff