Révision 154

pobysoPythonSage/src/testPobyso.sage (revision 154)
131 131
        precSa = pobyso_get_prec_of_constant_so_sa(constSo)
132 132
    wrapped = test_pobyso_wrapper(test)
133 133
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
134
    print "\t...", functionName, "done."
134 135
    sollya_lib_clear_obj(constSo,None)
135
    print "\t...", functionName, "done."
136 136
    return timing
137
    sollya_lib_clear_obj(constSo)
138 137
# End test_pobyso_get_prec_of_constant_so_sa
139 138

  
140
def test_pobyso_guess_degree_so_sa(iterationsNum=10000):
141
    print "Running", inspect.stack()[0][3]
139
def test_pobyso_guess_degree_sa_sa(repeat=1000, number=10):
140
    functionName = inspect.stack()[0][3]
141
    print "Running", functionName, "..."
142
    funcExpSa(x) = exp(x)
143
    RRIF = RealIntervalField(113)
144
    intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68)
145
    print "Interval:", intervalSa.str(style='brackets')
146
    errorSa = RR(2^-227)
147
    weightSa(x) = 1
148
    degreeBoundSa = 150
149
    # Function definition
150
    def test():
151
        pobyso_guess_degree_sa_sa(funcExpSa, 
152
                                  intervalSa, 
153
                                  errorSa, 
154
                                  weightSa, 
155
                                  degreeBoundSa)
156
    wrapped = test_pobyso_wrapper(test)
157
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
158
    print "\t...", functionName, "done."
159
    return timing
160
# End test_pobyso_guess_degree_sa_sa
161

  
162
def test_pobyso_guess_degree_so_sa(repeat=1000, number=10):
163
    functionName = inspect.stack()[0][3]
164
    print "Running", functionName, "..."
142 165
    funcExpSo = pobyso_parse_string_sa_so("exp(x)")
143 166
    RRIF = RealIntervalField(113)
144 167
    intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68)
145
    print ai.str(style='brackets')
168
    print "Interval:", intervalSa.str(style='brackets')
146 169
    intervalSo = pobyso_interval_to_range_sa_so(intervalSa)
147
    errorSo = pobyso_constant_sa_so(2^-227)
148
    # Compute the other arguments...
149
    for index in xrange(0,iterationsNum):
150
        pass
151
# End test_pobyso_guess_degree
170
    errorSo = pobyso_constant_sa_so(RR(2^-227))
171
    weightSo=pobyso_parse_string_sa_so("1")
172
    degreeBoundSo = pobyso_constant_from_int_sa_so(150)
173
    # Function definition
174
    def test():
175
        pobyso_guess_degree_so_sa(funcExpSo, 
176
                                  intervalSo, 
177
                                  errorSo, 
178
                                  weightSo, 
179
                                  degreeBoundSo)
180
    wrapped = test_pobyso_wrapper(test)
181
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
182
    print "\t...", functionName, "done."
183
    sollya_lib_clear_obj(funcExpSo)
184
    sollya_lib_clear_obj(intervalSo)
185
    sollya_lib_clear_obj(errorSo)
186
    sollya_lib_clear_obj(weightSo)
187
    sollya_lib_clear_obj(degreeBoundSo)
188
    return timing
189
# End test_pobyso_guess_degree_so_sa
152 190

  
153 191
def test_pobyso_lib_init(iterationsNum=10000):
154 192
    """
pobysoPythonSage/src/pobyso.py (revision 154)
112 112
        maxPrecSa = max(lbPrecSa, ubPrecSa)
113 113
    else:
114 114
        maxPrecSa = precisionSa
115
    sollyaCurrentPrecSo = pobyso_get_prec_so()
116
    sollyaCurrentPrecSa = pobyso_constant_from_int_so_sa(sollyaCurrentPrecSo)
117
    # Change the current Sollya precision only if necessary.
118
    if maxPrecSa > sollyaCurrentPrecSa:
119
        pobyso_set_prec_sa_so(maxPrecSa)
120 115
    # From Sage to Sollya bounds.
121
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa))
122
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa))
116
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa),
117
                                       maxPrecSa)
118
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa),
119
                                       maxPrecSa)
123 120
    # From Sollya bounds to range.
124 121
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
125 122
    # Back to original precision.
126
    if maxPrecSa > sollyaCurrentPrecSa:
127
        sollya_lib_set_prec(sollyaCurrentPrecSo)
128 123
    # Clean up
129
    sollya_lib_clear_obj(sollyaCurrentPrecSo)
130 124
    sollya_lib_clear_obj(lowerBoundSo)
131 125
    sollya_lib_clear_obj(upperBoundSo)
132
    return(rangeSo)
126
    return rangeSo
133 127
# End pobyso_bounds_to_range_sa_so
134 128

  
135 129
def pobyso_build_function_sub_so_so(exp1So, exp2So):
......
242 236
    if precisionSa > currentSollyaPrecisionSa:
243 237
        pobyso_set_prec_sa_so(precisionSa)
244 238
        constantSo = sollya_lib_constant(get_rn_value(rnArgSa))
245
        pobyso_set_prec_sa_so(currentSollyaPrecision)
239
        pobyso_set_prec_so_so(currentSollyaPrecisionSo)
246 240
    else:
247 241
        constantSo = sollya_lib_constant(get_rn_value(rnArgSa))
248 242
    sollya_lib_clear_obj(currentSollyaPrecisionSo)
249
    return(constantSo)
243
    return constantSo
250 244
# End pobyso_constant_sa_so
251 245
     
252 246
def pobyso_constant_0_sa_so():
......
745 739
    precSa = c_int(0)
746 740
    sollya_lib_get_constant_as_int(byref(precSa), precSo)
747 741
    sollya_lib_clear_obj(precSo)
748
    return(int(precSa.value))
742
    return int(precSa.value)
749 743
# End pobyso_get_prec_so_sa.
750 744

  
751 745
def pobyso_get_prec_of_constant(ctExpSo):
......
767 761
    return(int(prec.value))
768 762
# End pobyso_get_prec_of_range_so_sa()
769 763

  
764
def pobyso_guess_degree_sa_sa(functionSa, intervalSa, errorSa, weightSa=None, \
765
                              degreeBoundSa=None):
766
    functionAsStringSa = functionSa._assume_str()
767
    functionSo = pobyso_parse_string_sa_so(functionAsStringSa)
768
    rangeSo = pobyso_interval_to_range_sa_so(intervalSa)
769
    errorSo = pobyso_constant_sa_so(errorSa)
770
    if not weightSa is None:
771
        weightAsStringSa = weightSa._assume_str()
772
        weightSo = pobyso_parse_string_sa_so(weightAsStringSa)
773
    else:
774
        weightSo = None
775
    if not degreeBoundSa is None:
776
        degreeBoundSo = pobyso_constant_from_int_sa_so(degreeBoundSa)
777
    else:
778
        degreeBoundSo = None
779
    guessedDegreeSa = pobyso_guess_degree_so_sa(functionSo,
780
                                              rangeSo,
781
                                              errorSo,
782
                                              weightSo,
783
                                              degreeBoundSo)
784
    sollya_lib_clear_obj(functionSo)
785
    sollya_lib_clear_obj(rangeSo)
786
    sollya_lib_clear_obj(errorSo)
787
    if not weightSo is None:
788
        sollya_lib_clear_obj(weightSo)
789
    if not degreeBoundSo is None:
790
        sollya_lib_clear_obj(degreeBoundSo)
791
    return guessedDegreeSa
792
# End poyso_guess_degree_sa_sa
793

  
770 794
def pobyso_guess_degree_so_sa(functionSo, rangeSo, errorSo, weightSo=None, \
771
                              degreeBoundsSo=None):
795
                              degreeBoundSo=None):
796
    """
797
    Thin wrapper around the guessdegree function.
798
    Nevertheless, some precision control stuff has been appended.
799
    """
800
    # Deal with Sollya internal precision issues: if it is too small, 
801
    # compared with the error, increases it to about twice -log2(error).
802
    errorSa = pobyso_get_constant_as_rn_with_rf_so_sa(errorSo)
803
    log2ErrorSa = errorSa.log2()
804
    if log2ErrorSa < 0:
805
        neededPrecisionSa = int(2 * int(-log2ErrorSa) / 64) * 64
806
    else:
807
        neededPrecisionSa = int(2 * int(log2ErrorSa) / 64) * 64
808
    #print "Needed precision:", neededPrecisionSa
809
    currentPrecSa = pobyso_get_prec_so_sa()
810
    if neededPrecisionSa > currentPrecSa:
811
        currentPrecSo = pobyso_get_prec_so()
812
        pobyso_set_prec_sa_so(neededPrecisionSa)
772 813
    # weightSo and degreeBoundsSo are optional arguments.
773 814
    if weightSo is None:
774
        degreeRangeSo = sollya_lib_guessdegree(functionSo, rangeSo, errorSo)
775
    elif degreeBounds is None:
815
        degreeRangeSo = sollya_lib_guessdegree(functionSo, rangeSo, errorSo, None)
816
    elif degreeBoundSo is None:
776 817
        degreeRangeSo =  sollya_lib_guessdegree(functionSo, rangeSo, \
777
                                                errorSo, weightSo)
818
                                                errorSo, weightSo, None)
778 819
    else:
779 820
        degreeRangeSo =  sollya_lib_guessdegree(functionSo, rangeSo, errorSo, \
780
                                                weightSo, degreeBoundsSo)
781
    pobyso_range_to_interval_so_sa(degreeRangeSo)
782
# End pobyso_guess_degree_so_sa
821
                                                weightSo, degreeBoundSo, None)
822
    # Restore internal precision, if applicable.
823
    if neededPrecisionSa > currentPrecSa:
824
        pobyso_set_prec_so_so(currentPrecSo)
825
        sollya_lib_clear_obj(currentPrecSo)
826
    degreeIntervalSa = pobyso_range_to_interval_so_sa(degreeRangeSo)
827
    sollya_lib_clear_obj(degreeRangeSo)
828
    # When ok, both bounds match.
829
    # When the degree bound is too low, the upper bound is the degree
830
    # for which the error can be honored.
831
    # When it really goes wrong, the upper bound is infinity. 
832
    if degreeIntervalSa.lower() == degreeIntervalSa.upper():
833
        return int(degreeIntervalSa.lower())
834
    else:
835
        if degreeIntervalSa.upper().is_infinity():
836
            return None
837
        else:
838
            return int(degreeIntervalSa.upper())
839
    # End pobyso_guess_degree_so_sa
783 840

  
784 841
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None):
785 842
    print "Do not use this function. User pobyso_supnorm_so_so instead."

Formats disponibles : Unified diff