# Ticket #9958: trac_9958-fixing_numericalnoise-part2.patch

File trac_9958-fixing_numericalnoise-part2.patch, 26.7 KB (added by fbissey, 10 years ago)

fix numerical noise part 2 (refreshed against 4.7.1.alpha3 -20110625)

• ## sage/calculus/calculus.py

```# HG changeset patch
# User Francois Bissey <francois.bissey@canterbury.ac.nz>
# Date 1305760213 -43200
# Node ID 3ae47fe68fe4bb806cdd7753967fef96641030f8
# Parent  f4d90fd5493d56fd18c39aeed61915c21008c62c
#9958 fixing numerical noise part 2

diff --git a/sage/calculus/calculus.py b/sage/calculus/calculus.py```
 a sage: n(z,200) 4.6467837624329358733826155674904591885104869874232887508703 sage: float(z) 4.6467837624329356 4.646783762432936 We test pickling:: sage: f(x) = exp(-sqrt(x)) sage: f.nintegral(x, 0, 1) (0.52848223531423055, 4.163...e-11, 231, 0) (0.5284822353142306, 4.163...e-11, 231, 0) We can also use the ``numerical_integral`` function, which calls the GSL C library. :: sage: numerical_integral(f, 0, 1) (0.52848223225314706, 6.83928460...e-07) (0.528482232253147, 6.83928460...e-07) Note that in exotic cases where floating point evaluation of the expression leads to the wrong value, then the output can be Now numerically integrating, we see why the answer is wrong:: sage: f.nintegrate(x,0,1) (-480.00000000000006, 5.3290705182007538e-12, 21, 0) (-480.00000000000006, 5.329070518200754e-12, 21, 0) It is just because every floating point evaluation of return -480.0 in floating point.
• ## sage/calculus/test_sympy.py

`diff --git a/sage/calculus/test_sympy.py b/sage/calculus/test_sympy.py`
 a sage: pi^2 pi^2 sage: float(pi) 3.1415926535897931 3.141592653589793 sage: RealField(200)(pi) 3.1415926535897932384626433832795028841971693993751058209749 sage: float(pi + exp(1))
• ## sage/libs/mpmath/ext_impl.pyx

`diff --git a/sage/libs/mpmath/ext_impl.pyx b/sage/libs/mpmath/ext_impl.pyx`
 a sage: from sage.libs.mpmath.ext_impl import exp_fixed sage: y = exp_fixed(1<<53, 53) sage: float(y) / 2^53 2.7182818284590442 2.718281828459044 """ cdef Integer v
• ## sage/libs/mwrank/interface.py

`diff --git a/sage/libs/mwrank/interface.py b/sage/libs/mwrank/interface.py`
 a sage: E = mwrank_EllipticCurve([0, 0, 0, -1002231243161, 0]) sage: E.silverman_bound() 18.295452104682472 18.29545210468247 sage: E = mwrank_EllipticCurve([0,0,1,-7,6]) sage: E.silverman_bound() 6.2848333699724028 6.284833369972403 """ return self.__curve.silverman_bound() sage: E = mwrank_EllipticCurve([0,0,1,-7,6]) sage: E.regulator() 0.41714355875838399 0.417143558758384 """ return self.__mw.regulator()
• ## sage/libs/mwrank/mwrank.pyx

`diff --git a/sage/libs/mwrank/mwrank.pyx b/sage/libs/mwrank/mwrank.pyx`
 a sage: D2.getbasis() '[[1:-1:1], [-2:3:1], [-14:25:8]]' sage: D2.regulator() 0.41714355875838399 0.417143558758384 """ sig_on() return float(string_sigoff(two_descent_regulator(self.x)))
• ## sage/libs/pari/gen.pyx

`diff --git a/sage/libs/pari/gen.pyx b/sage/libs/pari/gen.pyx`
 a sage: g = pari(-1.0)^(1/5); g 0.809016994374947 + 0.587785252292473*I sage: g.__complex__() (0.80901699437494745+0.58778525229247314j) (0.8090169943749475+0.5877852522924731j) sage: complex(g) (0.80901699437494745+0.58778525229247314j) (0.8090169943749475+0.5877852522924731j) ::
• ## sage/numerical/optimize.py

`diff --git a/sage/numerical/optimize.py b/sage/numerical/optimize.py`
 a sage: R. = QQ[] sage: f = (x+17)*(x-3)*(x-1/8)^3 sage: find_root(f, 0,4) 2.9999999999999951 2.999999999999995 sage: find_root(f, 0,1)  # note -- precision of answer isn't very good on some machines. 0.124999... sage: find_root(f, -20,-10)
• ## sage/plot/misc.py

`diff --git a/sage/plot/misc.py b/sage/plot/misc.py`
 a sage: sage.plot.misc.setup_for_eval_on_grid([x+y], [(x,-1,1),(y,-2,2)]) ((,), [(-1.0, 1.0, 2.0), (-2.0, 2.0, 4.0)]) sage: sage.plot.misc.setup_for_eval_on_grid(x+y, [(x,-1,1),(y,-1,1)], plot_points=[4,9]) (, [(-1.0, 1.0, 0.66666666666666663), (-1.0, 1.0, 0.25)]) (, [(-1.0, 1.0, 0.6666666666666666), (-1.0, 1.0, 0.25)]) sage: sage.plot.misc.setup_for_eval_on_grid(x+y, [(x,-1,1),(y,-1,1)], plot_points=[4,9,10]) Traceback (most recent call last): ...
• ## sage/plot/plot.py

`diff --git a/sage/plot/plot.py b/sage/plot/plot.py`
• ## sage/plot/plot3d/base.pyx

`diff --git a/sage/plot/plot3d/base.pyx b/sage/plot/plot3d/base.pyx`
 a sage: G.scale(4).bounding_box() ((-2.0, -2.0, -2.0), (2.0, 2.0, 2.0)) sage: G.rotateZ(pi/4).bounding_box() ((-0.70710678118654746, -0.70710678118654746, -0.5), (0.70710678118654746, 0.70710678118654746, 0.5)) ((-0.7071067811865475, -0.7071067811865475, -0.5), (0.7071067811865475, 0.7071067811865475, 0.5)) """ try: return self._bounding_box
• ## sage/plot/plot3d/index_face_set.pyx

`diff --git a/sage/plot/plot3d/index_face_set.pyx b/sage/plot/plot3d/index_face_set.pyx`
 a sage: x,y=var('x,y') sage: p=plot3d(sqrt(sin(x)*sin(y)), (x,0,2*pi),(y,0,2*pi)) sage: p.bounding_box() ((0.0, 0.0, -0.0), (6.2831853071795862, 6.2831853071795862, 0.9991889981715697)) ((0.0, 0.0, -0.0), (6.283185307179586, 6.283185307179586, 0.9991889981715697)) """ if self.vcount == 0: return ((0,0,0),(0,0,0))
• ## sage/plot/plot3d/parametric_surface.pyx

`diff --git a/sage/plot/plot3d/parametric_surface.pyx b/sage/plot/plot3d/parametric_surface.pyx`
 a sage: from sage.plot.plot3d.parametric_surface import MobiusStrip sage: M = MobiusStrip(7,3,2) sage: M.bounding_box() ((-10.0, -7.53907349250478..., -2.9940801852848145), (10.0, 7.5390734925047846, 2.9940801852848145)) ((-10.0, -7.53907349250478..., -2.9940801852848145), (10.0, 7.53907349250478..., 2.9940801852848145)) """ # We must triangulate before computing the bounding box; otherwise # we'll get an empty bounding box, as the bounding box is computed
• ## sage/plot/plot3d/shapes.pyx

`diff --git a/sage/plot/plot3d/shapes.pyx b/sage/plot/plot3d/shapes.pyx`
 a sage: from sage.plot.plot3d.shapes import Sphere sage: Sphere(1).get_grid(100) ([-10.0, ..., 0.0, ..., 10.0], [0.0, ..., 3.1415926535897931, ..., 0.0]) [0.0, ..., 3.141592653589793, ..., 0.0]) """ cdef int K, u_res, v_res u_res = min(max(int(M_PI*self.radius/ds), 6), 20) sage: from sage.plot.plot3d.shapes import Torus sage: Torus(2, 1).get_grid(100) ([0.0, -1.047..., -3.1415926535897931, ..., 0.0], [0.0, 1.047..., 3.1415926535897931, ..., 0.0]) ([0.0, -1.047..., -3.141592653589793, ..., 0.0], [0.0, 1.047..., 3.141592653589793, ..., 0.0]) """ cdef int k, u_divs, v_divs u_divs = min(max(int(4*M_PI * self.R/ds), 6), 37)
• ## sage/plot/plot3d/shapes2.py

`diff --git a/sage/plot/plot3d/shapes2.py b/sage/plot/plot3d/shapes2.py`
 a sage: from sage.plot.plot3d.shapes2 import Line sage: L = Line([(i,i^2-1,-2*ln(i)) for i in [10,20,30]]) sage: L.bounding_box() ((10.0, 99.0, -6.8023947633243109), (30.0, 899.0, -4.6051701859880918)) ((10.0, 99.0, -6.802394763324311), (30.0, 899.0, -4.605170185988092)) """ try: return self.__bounding_box
• ## sage/rings/complex_number.pyx

`diff --git a/sage/rings/complex_number.pyx b/sage/rings/complex_number.pyx`
 a sage: a.__abs__() 2.23606797749979 sage: float(sqrt(2^2 + 1^1)) 2.2360679774997898 2.23606797749979 ::
• ## sage/rings/contfrac.py

`diff --git a/sage/rings/contfrac.py b/sage/rings/contfrac.py`
 a sage: a = CFF(-17/389); a [-1, 1, 21, 1, 7, 2] sage: float(a) -0.043701799485861177 -0.04370179948586118 """ return float(self._rational_()) sage: b.value() 4508361/9825745 sage: float(b.value()^2 - a) -5.4514925256726876e-16 -5.451492525672688e-16 sage: b = a.sqrt(prec=100); b [0, 2, 5, 1, 1, 2, 1, 16, 1, 2, 1, 1, 5, 4, 5, 1, 1, 2, 1, 16, 1, 2, 1, 1, 5, 4, 5, 1, 1, 2, 1, 16, 1, 2, 1, 1, 5] sage: b^2
• ## sage/rings/integer.pyx

`diff --git a/sage/rings/integer.pyx b/sage/rings/integer.pyx`
 a sage: n = Integer(17); float(n) 17.0 sage: n = Integer(902834098234908209348209834092834098); float(n) 9.0283409823490813e+35 9.028340982349081e+35 sage: n = Integer(-57); float(n) -57.0 sage: n.__float__()
• ## sage/rings/number_field/totallyreal.pyx

`diff --git a/sage/rings/number_field/totallyreal.pyx b/sage/rings/number_field/totallyreal.pyx`
 a EXAMPLES:: sage: [sage.rings.number_field.totallyreal.odlyzko_bound_totallyreal(n) for n in range(1,5)] [1.0, 2.2229999999999999, 3.6099999999999999, 5.0670000000000002] [1.0, 2.223, 3.61, 5.067] AUTHORS:
• ## sage/rings/polynomial/real_roots.pyx

`diff --git a/sage/rings/polynomial/real_roots.pyx b/sage/rings/polynomial/real_roots.pyx`
 a sage: from sage.rings.polynomial.real_roots import * sage: max_abs_doublevec(vector(RDF, [0.1, -0.767, 0.3, 0.693])) 0.76700000000000002 0.767 """ cdef numpy.ndarray[double, ndim=1] cd = c._vector_numpy
• ## sage/rings/qqbar.py

`diff --git a/sage/rings/qqbar.py b/sage/rings/qqbar.py`
 a ...               return None ...       return map(convert_test, all_vals) sage: convert_test_all(float) [42.0, 3.1428571428571432, 1.6180339887498949, -13.0, 1.6181818181818182, -2.6457513110645907, None] [42.0, 3.1428571428571432, 1.618033988749895, -13.0, 1.6181818181818182, -2.6457513110645907, None] sage: convert_test_all(complex) [(42+0j), (3.1428571428571432+0j), (1.6180339887498949+0j), (-13+0j), (1.6181818181818182+0j), (-2.6457513110645907+0j), (0.30901699437494745+0.95105651629515364j)] [(42+0j), (3.1428571428571432+0j), (1.618033988749895+0j), (-13+0j), (1.6181818181818182+0j), (-2.6457513110645907+0j), (0.30901699437494745+0.9510565162951536j)] sage: convert_test_all(RDF) [42.0, 3.14285714286, 1.61803398875, -13.0, 1.61818181818, -2.64575131106, None] sage: convert_test_all(CDF) EXAMPLES:: sage: QQbar(sqrt(2)).__float__() 1.4142135623730949 1.414213562373095 sage: float(QQbar(-22/7)) -3.1428571428571432 sage: float(QQbar.zeta(3)) EXAMPLES:: sage: QQbar(sqrt(2)).__complex__() (1.4142135623730949+0j) (1.414213562373095+0j) sage: complex(QQbar.zeta(3)) (-0.5+0.8660254037844386j) """ EXAMPLES:: sage: AA(golden_ratio).__float__() 1.6180339887498949 1.618033988749895 sage: float(AA(sqrt(11))) 3.3166247903553998 3.3166247903554 """ return float(RR(self))
• ## sage/rings/quotient_ring_element.py

`diff --git a/sage/rings/quotient_ring_element.py b/sage/rings/quotient_ring_element.py`
 a sage: R. = QQ[]; S. = R.quo(x^2 + y^2); type(a) sage: float(S(2/3)) 0.66666666666666663 0.6666666666666666 sage: float(a) Traceback (most recent call last): ...
• ## sage/rings/real_lazy.pyx

`diff --git a/sage/rings/real_lazy.pyx b/sage/rings/real_lazy.pyx`
 a sage: from sage.rings.real_lazy import LazyConstant sage: a = LazyConstant(RLF, 'pi') sage: float(a) 3.1415926535897931 3.141592653589793 """ interval_field = self._parent.interval_field() return self.eval(interval_field._middle_field())
• ## sage/structure/coerce_dict.pyx

`diff --git a/sage/structure/coerce_dict.pyx b/sage/structure/coerce_dict.pyx`
 a sage: len(L) 1 sage: L.stats()             # min, avg, max (bucket length) (0, 0.032258064516129031, 1) (0, 0.03225806451612903, 1) sage: L.bucket_lens()       # random layout [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] sage: for i in range(1000):
• ## sage/symbolic/constants.py

`diff --git a/sage/symbolic/constants.py b/sage/symbolic/constants.py`
 a EXAMPLES:: sage: float(pi) 3.1415926535897931 3.141592653589793 """ return math.pi sage: grm + grm sqrt(5)+1 sage: float(grm + grm) 3.2360679774997898 3.23606797749979 """ def __init__(self, name='golden_ratio'): """ EXAMPLES:: sage: float(golden_ratio) 1.6180339887498949 1.618033988749895 sage: golden_ratio.__float__() 1.6180339887498949 1.618033988749895 """ return float(0.5)*(float(1.0)+math.sqrt(float(5.0))) sage: log2 log2 sage: float(log2) 0.69314718055994529 0.6931471805599453 sage: RR(log2) 0.693147180559945 sage: R = RealField(200); R EXAMPLES:: sage: float(log2) 0.69314718055994529 0.6931471805599453 sage: log2.__float__() 0.69314718055994529 0.6931471805599453 """ return math.log(2) EXAMPLES:: sage: float(euler_gamma) 0.57721566490153287 0.5772156649015329 """ return 0.57721566490153286060651209008 EXAMPLES:: sage: float(catalan) 0.91596559417721901 0.915965594177219 """ return 0.91596559417721901505460351493252 EXAMPLES:: sage: float(twinprime) 0.66016181584686962 0.6601618158468696 """ return 0.66016181584686957392781211001
• ## sage/symbolic/expression.pyx

`diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx`
 a sage: float(SR(12)) 12.0 sage: float(SR(2/3)) 0.66666666666666663 0.6666666666666666 sage: float(sqrt(SR(2))) 1.4142135623730951 sage: float(x^2 + 1) sage: cmp(sin(SR(2)), sin(SR(1))) 1 sage: float(sin(SR(2))) 0.90929742682568171 0.9092974268256817 sage: float(sin(SR(1))) 0.8414709848078965 """ sage: int(2)^x 2^x sage: float(2.3)^(x^3 - x^2 + 1/3) 2.2999999999999998^(x^3 - x^2 + 1/3) 2.3^(x^3 - x^2 + 1/3) sage: complex(1,3)^(sqrt(2)) (1+3j)^sqrt(2) """ sage: maxima('atan2(0.7,0.6)') .862170054667226... sage: float(SR(0.7).arctan2(-0.6)) 2.2794225989225669 2.279422598922567 sage: maxima('atan2(0.7,-0.6)') 2.279422598922567 sage: float(SR(-0.7).arctan2(0.6)) sage: maxima('atan2(-0.7,0.6)') -.862170054667226... sage: float(SR(-0.7).arctan2(-0.6)) -2.2794225989225669 -2.279422598922567 sage: maxima('atan2(-0.7,-0.6)') -2.279422598922567 sage: float(SR(0).arctan2(-0.6)) sage: SR(0.5).log().exp() 0.500000000000000 sage: math.log(0.5) -0.69314718055994529 -0.6931471805599453 sage: plot(lambda x: SR(x).log(), 0.1,10) To prevent automatic evaluation use the ``hold`` argument:: An example with a square root:: sage: f = 1 + x + sqrt(x+2); f.find_root(-2,10) -1.6180339887498949 -1.618033988749895 Some examples that Ted Kosan came up with::
• ## sage/symbolic/function.pyx

`diff --git a/sage/symbolic/function.pyx b/sage/symbolic/function.pyx`
 a sage: cot(0.5) #indirect doctest 1.83048772171245 sage: cot(complex(1,2)) (0.032797755533752602-0.98432922645819...j) (0.0327977555337526-0.98432922645819...j) """ if isinstance(x, (int, long)): return None sage: arctan(1) 1/4*pi sage: arctan(float(1)) 0.78539816339744828 0.7853981633974483 Precision of the result depends on the precision of the input:: sage: ff.is_pure_c() False sage: ff(1.5) 0.96610514647531076 0.9661051464753108 sage: erf(1.5) 0.966105146475311 """
• ## sage/symbolic/integration/integral.py

`diff --git a/sage/symbolic/integration/integral.py b/sage/symbolic/integration/integral.py`
 a sage: res.subs(y=2).n() 1.46383232641443 sage: nres = numerical_integral(f.subs(y=.5), 0.0001414, 1.); nres (-0.66951170887280698, 7.7686781108547113e-15) (-0.669511708872807, 7.768678110854711e-15) sage: res.subs(y=.5).n() -0.669511708872807
• ## sage/symbolic/pynac.pyx

`diff --git a/sage/symbolic/pynac.pyx b/sage/symbolic/pynac.pyx`
 a TESTS:: sage: sin(float(2)) #indirect doctest 0.90929742682568171 0.9092974268256817 sage: sin(2.) 0.909297426825682 sage: sin(2.*I) """ TESTS:: sage: cos(float(2)) #indirect doctest -0.41614683654714241 -0.4161468365471424 sage: cos(2.) -0.416146836547142 sage: cos(2.*I) sage: py_log(float(0)) -inf sage: py_log(float(-1)) 3.1415926535897931j 3.141592653589793j sage: py_log(int(1)) 0.0 sage: py_log(long(1))
• ## sage/symbolic/random_tests.py

`diff --git a/sage/symbolic/random_tests.py b/sage/symbolic/random_tests.py`
 a sage: B = [(1, 'B1'), (2, 'B2'), (2, 'B3')] sage: top = [(50, A, 'Group A'), (50, B, 'Group B')] sage: normalize_prob_list(top) [(0.250000000000000, 'A1', 'Group A'), (0.250000000000000, 'A2', 'Group A'), (0.10000000000000001, 'B1', 'Group B'), (0.20000000000000001, 'B2', 'Group B'), (0.20000000000000001, 'B3', 'Group B')] [(0.250000000000000, 'A1', 'Group A'), (0.250000000000000, 'A2', 'Group A'), (0.1, 'B1', 'Group B'), (0.2, 'B2', 'Group B'), (0.2, 'B3', 'Group B')] """ if len(pl) == 0: return pl
• ## sage/symbolic/relation.py

`diff --git a/sage/symbolic/relation.py b/sage/symbolic/relation.py`
 a sage: (x == sin(x)).find_root(-2,2) 0.0 sage: (x^5 + 3*x + 2 == 0).find_root(-2,2,x) -0.63283452024215225 -0.6328345202421523 sage: (cos(x) == sin(x)).find_root(10,20) 19.634954084936208