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

Commit 8f91703

Browse files
committed
Merge branch 'u/rws/parent_of_argument_lost_with_functions' of git://trac.sagemath.org/sage into t/20475/upgrade_to_pynac_0_6_5
2 parents e06a26c + eb5bf93 commit 8f91703

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/sage/functions/other.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ def __init__(self):
15541554
sage: loads(dumps(binomial(n,k)))
15551555
binomial(n, k)
15561556
"""
1557-
GinacFunction.__init__(self, "binomial", nargs=2,
1557+
GinacFunction.__init__(self, "binomial", nargs=2, preserved_arg=1,
15581558
conversions=dict(maxima='binomial',
15591559
mathematica='Binomial',
15601560
sympy='binomial'))

src/sage/symbolic/function.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cdef class Function(SageObject):
1212
cdef _register_function(self)
1313

1414
cdef class BuiltinFunction(Function):
15+
cdef object _preserved_arg
1516
cdef _is_registered(self)
1617

1718
cdef class GinacFunction(BuiltinFunction):

src/sage/symbolic/function.pyx

+27-6
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ cdef class Function(SageObject):
452452
else:
453453
symbolic_input = False
454454

455-
456455
cdef Py_ssize_t i
457456
if coerce:
458457
try:
@@ -778,7 +777,7 @@ cdef class GinacFunction(BuiltinFunction):
778777
There is also no need to register these functions.
779778
"""
780779
def __init__(self, name, nargs=1, latex_name=None, conversions=None,
781-
ginac_name=None, evalf_params_first=True):
780+
ginac_name=None, evalf_params_first=True, preserved_arg=None):
782781
"""
783782
TESTS::
784783
@@ -793,7 +792,7 @@ cdef class GinacFunction(BuiltinFunction):
793792
"""
794793
self._ginac_name = ginac_name
795794
BuiltinFunction.__init__(self, name, nargs, latex_name, conversions,
796-
evalf_params_first=evalf_params_first)
795+
evalf_params_first=evalf_params_first, preserved_arg=preserved_arg)
797796

798797
def __call__(self, *args, **kwds):
799798
"""
@@ -900,7 +899,7 @@ cdef class BuiltinFunction(Function):
900899
of this class.
901900
"""
902901
def __init__(self, name, nargs=1, latex_name=None, conversions=None,
903-
evalf_params_first=True, alt_name=None):
902+
evalf_params_first=True, alt_name=None, preserved_arg=None):
904903
"""
905904
TESTS::
906905
@@ -909,6 +908,10 @@ cdef class BuiltinFunction(Function):
909908
sage: c(pi/2)
910909
0
911910
"""
911+
self._preserved_arg = preserved_arg
912+
if preserved_arg and (preserved_arg < 1 or preserved_arg > nargs):
913+
raise ValueError("preserved_arg must be between 1 and nargs")
914+
912915
# If we have an _evalf_ method, change _eval_ to a
913916
# wrapper function which first tries to call _evalf_.
914917
if hasattr(self, '_evalf_'):
@@ -969,10 +972,28 @@ cdef class BuiltinFunction(Function):
969972
res = super(BuiltinFunction, self).__call__(
970973
*args, coerce=coerce, hold=hold)
971974

972-
# If none of the input arguments was a Sage Element but the
973-
# output is, then convert the output back to the corresponding
975+
# Convert the output back to the corresponding
974976
# Python type if possible.
975977
if any(isinstance(x, Element) for x in args):
978+
if (self._preserved_arg
979+
and isinstance(args[self._preserved_arg-1], Element)):
980+
from sage.structure.all import parent
981+
arg_parent = parent(args[self._preserved_arg-1])
982+
if arg_parent is SR:
983+
return res
984+
from sage.rings.polynomial.polynomial_ring import PolynomialRing_commutative
985+
from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain
986+
if (isinstance(arg_parent, PolynomialRing_commutative)
987+
or isinstance(arg_parent, MPolynomialRing_polydict_domain)):
988+
try:
989+
return res.polynomial(ring=arg_parent)
990+
except TypeError:
991+
return res
992+
else:
993+
try:
994+
return arg_parent(res)
995+
except TypeError:
996+
return res
976997
return res
977998
if not isinstance(res, Element):
978999
return res

0 commit comments

Comments
 (0)