Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit f9d60c8

Browse files
eviatarbachrwst
authored andcommitted
Trac #14778: Fix numerical_approx with algorithm keyword
1 parent 6452f9d commit f9d60c8

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

src/sage/matrix/matrix2.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12067,7 +12067,7 @@ cdef class Matrix(matrix1.Matrix):
1206712067
if p == 'frob':
1206812068
return sum([i**2 for i in A.list()]).sqrt()
1206912069

12070-
def _numerical_approx(self,prec=None,digits=None):
12070+
def _numerical_approx(self, prec=None, digits=None, algorithm=None):
1207112071
r"""
1207212072
Return a numerical approximation of ``self`` as either
1207312073
a real or complex number with at least the requested number of bits

src/sage/misc/functional.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,10 +1210,10 @@ def numerator(x):
12101210
return x.numerator()
12111211

12121212
# Following is the top-level numerical_approx function.
1213-
# Implement a ._numerical_approx(prec, digits) method for your
1213+
# Implement a ._numerical_approx(prec, digits, algorithm) method for your
12141214
# objects to enable the three top-level functions and three methods
12151215

1216-
def numerical_approx(x, prec=None, digits=None):
1216+
def numerical_approx(x, prec=None, digits=None, algorithm=None):
12171217
r"""
12181218
Returns a numerical approximation of an object ``x`` with at
12191219
least ``prec`` bits (or decimal ``digits``) of precision.
@@ -1228,10 +1228,13 @@ def numerical_approx(x, prec=None, digits=None):
12281228
12291229
- ``x`` - an object that has a numerical_approx
12301230
method, or can be coerced into a real or complex field
1231-
- ``prec (optional)`` - an integer (bits of
1231+
- ``prec`` (optional) - an integer (bits of
12321232
precision)
1233-
- ``digits (optional)`` - an integer (digits of
1233+
- ``digits`` (optional) - an integer (digits of
12341234
precision)
1235+
- ``algorithm`` (optional) - a string specifying
1236+
the algorithm to use for functions that implement
1237+
more than one
12351238
12361239
If neither the ``prec`` or ``digits`` are specified,
12371240
the default is 53 bits of precision. If both are
@@ -1384,14 +1387,18 @@ def numerical_approx(x, prec=None, digits=None):
13841387
sage: len(str(n(golden_ratio, digits=5000000))) # long time (4s on sage.math, 2012)
13851388
5000001
13861389
1390+
Check that :trac:`14778` is fixed::
1391+
1392+
sage: n(0, algorithm='foo')
1393+
0.000000000000000
13871394
"""
13881395
if prec is None:
13891396
if digits is None:
13901397
prec = 53
13911398
else:
13921399
prec = int((digits+1) * LOG_TEN_TWO_PLUS_EPSILON) + 1
13931400
try:
1394-
return x._numerical_approx(prec)
1401+
return x._numerical_approx(prec, algorithm=algorithm)
13951402
except AttributeError:
13961403
from sage.rings.complex_double import is_ComplexDoubleElement
13971404
from sage.rings.complex_number import is_ComplexNumber

src/sage/modules/free_module_element.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ cdef class FreeModuleElement(element_Vector): # abstract base class
996996
return sib.name('vector')(self.base_ring(),
997997
[sib(e, 2) for e in self])
998998

999-
def _numerical_approx(self, prec=None, digits=None):
999+
def _numerical_approx(self, prec=None, digits=None, algorithm=None):
10001000
r"""
10011001
Implements numerical approximation of a free module element
10021002
by calling the ``n()`` method on all of its entries.
@@ -1114,7 +1114,7 @@ cdef class FreeModuleElement(element_Vector): # abstract base class
11141114
sage: u
11151115
(0.5000, 0.0000, 0.0000, 0.3333, 0.0000, 0.0000, 0.0000, 0.2500)
11161116
"""
1117-
return vector([e.n(prec, digits) for e in self])
1117+
return vector([e.n(prec, digits, algorithm) for e in self])
11181118

11191119
def transpose(self):
11201120
r"""
@@ -4512,7 +4512,7 @@ cdef class FreeModuleElement_generic_sparse(FreeModuleElement):
45124512
"""
45134513
return len(self._entries)
45144514
4515-
def _numerical_approx(self, prec=None, digits=None):
4515+
def _numerical_approx(self, prec=None, digits=None, algorithm=None):
45164516
"""
45174517
Returns a numerical approximation of self by calling the n() method
45184518
on all of its entries.
@@ -4529,5 +4529,6 @@ cdef class FreeModuleElement_generic_sparse(FreeModuleElement):
45294529
sage: _.parent()
45304530
Sparse vector space of dimension 3 over Real Field with 75 bits of precision
45314531
"""
4532-
return vector(dict([(e[0],e[1].n(prec, digits)) for e in self._entries.iteritems()]), sparse=True)
4532+
return vector(dict([(e[0], e[1].n(prec, digits, algorithm)) for e in
4533+
self._entries.iteritems()]), sparse=True)
45334534

src/sage/schemes/elliptic_curves/heegner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3124,7 +3124,7 @@ def quadratic_form(self):
31243124
return self.__x.quadratic_form()
31253125

31263126
@cached_method
3127-
def numerical_approx(self, prec=53):
3127+
def numerical_approx(self, prec=53, algorithm=None):
31283128
"""
31293129
Return a numerical approximation to this Heegner point
31303130
computed using a working precision of prec bits.

src/sage/structure/element.pyx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ cdef class Element(sage_object.SageObject):
628628
variables.append(gen)
629629
return self(*variables)
630630

631-
def numerical_approx (self, prec=None, digits=None):
631+
def numerical_approx(self, prec=None, digits=None, algorithm=None):
632632
"""
633633
Return a numerical approximation of x with at least prec bits of
634634
precision.
@@ -641,11 +641,20 @@ cdef class Element(sage_object.SageObject):
641641
3.141592654
642642
sage: pi.n(prec=20) # 20 bits
643643
3.1416
644+
645+
TESTS:
646+
647+
Check that :trac:`14778` is fixed::
648+
649+
sage: (0).n(algorithm='foo')
650+
0.000000000000000
644651
"""
645652
import sage.misc.functional
646-
return sage.misc.functional.numerical_approx(self, prec=prec, digits=digits)
647-
n=numerical_approx
648-
N=n
653+
return sage.misc.functional.numerical_approx(self, prec=prec,
654+
digits=digits,
655+
algorithm=algorithm)
656+
n = numerical_approx
657+
N = n
649658

650659
def _mpmath_(self, prec=53, rounding=None):
651660
"""

0 commit comments

Comments
 (0)