@@ -452,7 +452,6 @@ cdef class Function(SageObject):
452
452
else :
453
453
symbolic_input = False
454
454
455
-
456
455
cdef Py_ssize_t i
457
456
if coerce :
458
457
try :
@@ -778,7 +777,7 @@ cdef class GinacFunction(BuiltinFunction):
778
777
There is also no need to register these functions.
779
778
"""
780
779
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 ):
782
781
"""
783
782
TESTS::
784
783
@@ -793,7 +792,7 @@ cdef class GinacFunction(BuiltinFunction):
793
792
"""
794
793
self ._ginac_name = ginac_name
795
794
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 )
797
796
798
797
def __call__ (self , *args , **kwds ):
799
798
"""
@@ -900,7 +899,7 @@ cdef class BuiltinFunction(Function):
900
899
of this class.
901
900
"""
902
901
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 ):
904
903
"""
905
904
TESTS::
906
905
@@ -909,6 +908,10 @@ cdef class BuiltinFunction(Function):
909
908
sage: c(pi/2)
910
909
0
911
910
"""
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
+
912
915
# If we have an _evalf_ method, change _eval_ to a
913
916
# wrapper function which first tries to call _evalf_.
914
917
if hasattr (self , ' _evalf_' ):
@@ -969,10 +972,28 @@ cdef class BuiltinFunction(Function):
969
972
res = super (BuiltinFunction, self ).__call__(
970
973
* args, coerce = coerce , hold = hold)
971
974
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
974
976
# Python type if possible.
975
977
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
976
997
return res
977
998
if not isinstance (res, Element):
978
999
return res
0 commit comments