Révision 84 pobysoPythonSage/src/pobyso.py

pobyso.py (revision 84)
94 94
def pobyso_absolute_so_so():
95 95
    return(sollya_lib_absolute(None))
96 96

  
97
def pobyso_bounds_to_range_sa_so(rnLowerBoundSa, rnUpperBoundSa, \
98
                                 precisionSa=None):
99
    """
100
    Return a Sollya range from to 2 RealField Sage elements.
101
    The Sollya range element has a sufficient precision to hold all
102
    the digits of the Sage bounds.
103
    """
104
    # Sanity check.
105
    if rnLowerBoundSa > rnUpperBoundSa:
106
        return None
107
    if precision is None:
108
        # Check for the largest precision.
109
        lbPrecSa = rnLowerBoundSa.parent().precision()
110
        ubPrecSa = rnLowerBoundSa.parent().precision()
111
        maxPrecSa = max(lbPrecSa, ubPrecSa)
112
    else:
113
        maxPrecSa = precisionSa
114
    sollyaCurrentPrecSo = pobyso_get_prec_so()
115
    sollyaCurrentPrecSa = pobyso_constant_from_int_so_sa(sollyaCurrentPrecSo)
116
    # Change the current Sollya precision only if necessary.
117
    if maxPrecSa > sollyaCurrentPrecSa:
118
        pobyso_set_prec_sa_so(maxPrecSa)
119
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa))
120
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa))
121
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
122
    # Back to original precision.
123
    if maxPrecSa > sollyaCurrentPrecSa:
124
        sollya_lib_set_prec(sollyaCurrentPrecSo)
125
    # Clean up
126
    sollya_lib_clear_obj(sollyaCurrentPrecSo)
127
    sollya_lib_clear_obj(lowerBoundSo)
128
    sollya_lib_clear_obj(upperBoundSo)
129
    return(rangeSo)
130
# End pobyso_bounds_to_range_sa_so
131

  
97 132
def pobyso_build_function_sub_so_so(exp1So, exp2So):
98 133
    return(sollya_lib_build_function_sub(exp1So, exp2So))
99 134

  
......
169 204
    """ Legacy function. See pobyso_constant_sa_so. """
170 205
    return(pobyso_constant_sa_so(rnArg))
171 206
    
172
def pobyso_constant_sa_so(rnArg):
207
def pobyso_constant_sa_so(rnArgSa, precisionSa=None):
173 208
    """
174 209
    Create a Sollya constant from a RealNumber.
175 210
    """
176
    return(sollya_lib_constant(get_rn_value(rnArg)))
211
    # Precision stuff
212
    if precisionSa is None:
213
        precisionSa = rnArgSa.parent().precision()
214
    currentSollyaPrecisionSo = pobyso_get_prec_so()
215
    currentSollyaPrecisionSa = pobyso_get
216
    if precisionSa > currentSollyaPrecisionSa:
217
        pobyso_set_prec_sa_so(precisionSa)
218
        constantSo = sollya_lib_constant(get_rn_value(rnArgSa))
219
        pobyso_set_prec_sa_so(currentSollyaPrecision)
220
    else:
221
        constantSo = sollya_lib_constant(get_rn_value(rnArgSa))
222
    return(constantSo)
177 223
    
178 224
def pobyso_constant_0_sa_so():
179 225
    return(pobyso_constant_from_int_sa_so(0))
......
192 238
def pobyso_constant_from_int_sa_so(anInt):
193 239
    return(sollya_lib_constant_from_int(int(anInt)))
194 240

  
241
def pobyso_constant_from_int_so_sa(constSo):
242
    """
243
    Get a Sollya constant as an int.
244
    Use full for precision or powers in polynomials.
245
    """
246
    constSa = c_int(0)
247
    sollya_lib_get_constant_as_int(byref(constSa), constSo)
248
    return(constSa.value)
249
# End pobyso_constant_from_int_so_sa
250

  
195 251
def pobyso_function_type_as_string(funcType):
196 252
    """ Legacy function. See pobyso_function_type_as_string_so_sa. """
197 253
    return(pobyso_function_type_as_string_so_sa(funcType))
......
296 352
    """ Legacy function. See pobyso_get_constant_so_sa. """
297 353
    return(pobyso_get_constant_so_sa(rnArg, soConst))
298 354

  
299
def pobyso_get_constant_so_sa(rnArg, soConst):
355
def pobyso_get_constant_so_sa(rnArgSa, constSo):
300 356
    """
301 357
    Set the value of rnArg to the value of soConst in MPFR_RNDN mode.
302 358
    rnArg must already exist and belong to some RealField.
303 359
    We assume that soConst points to a Sollya constant.
304 360
    """
305
    return(sollya_lib_get_constant(get_rn_value(rnArg), soConst))
361
    return(sollya_lib_get_constant(get_rn_value(rnArgSa), constSo))
306 362
    
307 363
def pobyso_get_constant_as_rn(ctExpSo):
308 364
    """ 
......
609 665
    """ Legacy function. See pobyso_get_prec_so_sa(). """
610 666
    return(pobyso_get_prec_so_sa())
611 667

  
668
def pobyso_get_prec_so():
669
    """
670
    Get the current default precision in Sollya.
671
    The return value is a Sollya object.
672
    Usefull when modifying the precision back and forth by avoiding
673
    extra conversions.
674
    """
675
    return(sollya_lib_get_prec(None))
676
    
612 677
def pobyso_get_prec_so_sa():
613 678
    """
614 679
    Get the current default precision in Sollya.
......
643 708
    print "Do not use this function. User pobyso_supnorm_so_so instead."
644 709
    return(None)
645 710

  
711
def pobyso_interval_to_range_sa_so(intervalSa, precisionSa=None):
712
    if precisionSa is None:
713
        precisionSa = intervalSa.parent().precision()
714
    intervalSo = pobyso_bounds_to_range_sa_so(intervalSa.lower(),\
715
                                              intervalSa.upper(),\
716
                                              precisionSa)
717
    return(intervalSo)
718
# End pobyso_interval_to_range_sa_so
719

  
646 720
def pobyso_lib_init():
647 721
    sollya_lib_init(None)
648 722
    
......
670 744
    """ Legacy function. See pobyso_range_sa_so. """
671 745
    return(pobyso_range_sa_so(rnLowerBound, rnUpperBound)) 
672 746

  
673
def pobyso_bounds_to_range_sa_so(rnLowerBoundSa, rnUpperBoundSa):
674
    """
675
    Return a Sollya range from to 2 RealField Sage elements.
676
    The Sollya range element has a sufficient precision to hold all
677
    the digits of the Sage bounds.
678
    """
679
    # Sanity check.
680
    if rnLowerBoundSa > rnUpperBoundSa:
681
        return None
682
    # Check for the largest precision.
683
    lbPrec = rnLowerBoundSa.parent().precision()
684
    ubPrec = rnLowerBoundSa.parent().precision()
685
    currentSollyaPrecSa = pobyso_get_prec_so_sa()
686
    maxPrecSa = max(lbPrec, ubPrec, currentSollyaPrecSa)
687
    # Change the current Sollya precision only if necessary.
688
    if maxPrecSa > currentSollyaPrecSa:
689
        currentPrecSo = sollya_lib_get_prec(None)
690
        newPrecSo = solly_lib_constant_from_uint64(maxPrecSa)
691
        sollya_lib_set_prec(newPrecSo)
692
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa))
693
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa))
694
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
695
    if maxPrecSa > currentSollyaPrecSa:
696
        sollya_lib_set_prec(currentPrecSo)
697
        sollya_lib_clear_obj(currentPrecSo)
698
        sollya_lib_clear_obj(newPrecSo)
699
    sollya_lib_clear_obj(lowerBoundSo)
700
    sollya_lib_clear_obj(upperBoundSo)
701
    return(rangeSo)
702 747

  
703 748
def pobyso_range_to_interval_so_sa(rangeSo, realIntervalField = None):
704 749
    """
......
929 974
    """ Legacy function. See ;"""
930 975
    
931 976
def pobyso_taylorform_sa_sa(functionSa, \
932
                            degree, \
933
                            point, \
934
                            precision, \
935
                            interval=None, \
936
                            errorType=None):
977
                            degreeSa, \
978
                            pointSa, \
979
                            intervalSa=None, \
980
                            errorTypeSa=None, \
981
                            precisionSa=None):
937 982
    """
938 983
    Compute the Taylor form of 'degree' for 'functionSa' at 'point' 
939
    for 'interval' with 'errorType'. 
984
    for 'interval' with 'errorType' (a string). 
940 985
    point: must be a Real or a Real interval.
941 986
    return the Taylor form as an array
942 987
    TODO: take care of the interval and of the point when it is an interval;
......
945 990
          errors and delta.
946 991
    """
947 992
    # Absolute as the default error.
948
    if errorType is None:
993
    if errorTypeSa is None:
949 994
        errorTypeSo = sollya_lib_absolute()
995
    elif errorTypeSa == "relative":
996
        errorTypeSo = sollya_lib_relative()
997
    elif errortypeSa == "absolute":
998
        errorTypeSo = sollya_lib_absolute()
950 999
    else:
951
        #TODO: deal with the other case.
952
        pass
1000
        # No clean up needed.
1001
        return None
1002
    # Global precision stuff
1003
    precisionChangedSa = False
1004
    currentSollyaPrecSo = pobyso_get_prec_so()
1005
    currentSollyaPrecSa = pobyso_constant_from_int_so_sa(currentSollyaPrecSo)
1006
    if not precisionSa is None:
1007
        if precisionSa > currentSollyaPrecSa:
1008
            pobyso_set_prec_sa_so(precisionSa)
1009
            precisionChangedSa = True
1010
            
953 1011
    varSa = functionSa.variables()[0]
954
    pointBaseRingString = str(point.base_ring())
955
    if not re.search('Real', pointBaseRingString):
956
        return None
957
    # Call Sollya but first "sollyafy" the arguments.
958 1012
    pobyso_name_free_variable_sa_so(str(varSa))
959
    #pobyso_set_prec_sa_so(300)
1013
    # In any case (point or interval) the parent of pointSa has a precision
1014
    # method.
1015
    pointPrecSa = pointSa.parent().precision()
1016
    if precisionSa > pointPrecSa:
1017
        pointPrecSa = precisionSa
1018
    # In any case (point or interval) pointSa has a base_ring() method.
1019
    pointBaseRingString = str(pointSa.base_ring())
1020
    if re.search('Interval', pointBaseRingString) is None: # Point
1021
        pointSo = pobyso_constant_sa_so(pointSa, pointPrecSa)
1022
    else: # Interval.
1023
        pointSo = pobyso_interval_to_range_sa_so(pointSa, pointPrecSa)
960 1024
    # Sollyafy the function.
961 1025
    functionSo = pobyso_parse_string_sa_so(functionSa._assume_str())
962 1026
    if sollya_lib_obj_is_error(functionSo):
963 1027
        print "pobyso_tailorform: function string can't be parsed!"
964 1028
        return None
965 1029
    # Sollyafy the degree
966
    degreeSo = sollya_lib_constant_from_int(int(degree))
1030
    degreeSo = sollya_lib_constant_from_int(int(degreeSa))
967 1031
    # Sollyafy the point
968
    if not re.search('Interval', pointBaseRingString):
969
        pointSo  = pobyso_constant_sa_so(point)
970
    else:
971
        # TODO: deal with the interval case.
972
        pass
973 1032
    # Call Sollya
974 1033
    taylorFormSo = \
975 1034
        sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
......
980 1039
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
981 1040
    polyRealField = RealField(maxPrecision)
982 1041
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField)
983
    sollya_lib_close()
1042
    if precisionChangedSa:
1043
        sollya_lib_set_prec(currentSollyaPrecSo)
1044
        sollya_lib_clear_obj(currentSollyaPrecSo)
1045
    # Clean up.
1046
    sollya_lib_clear_obj(pointSo) # Works, whatever the type of pointSo is.
1047
    sollya_lib_clear_obj(errorTypeSo)
1048
    sollya_lib_clear_obj(degreeSo)
984 1049
    polynomialRing = polyRealField[str(varSa)]
985 1050
    polySa = polynomial(expSa, polynomialRing)
986 1051
    taylorFormSa = [polySa]

Formats disponibles : Unified diff