Ticket #9769 (new defect)

Opened 3 years ago

Last modified 2 years ago

symbolic function do not work with numpy.int64 arguments

Reported by: maldun Owned by: burcin
Priority: major Milestone: sage-5.10
Component: symbolics Keywords:
Cc: Work issues:
Report Upstream: N/A Reviewers:
Authors: Merged in:
Dependencies: Stopgaps:

Description (last modified by maldun) (diff)

There seems to be some problems with the coercion of some datatypes to the symbolic ring:

sage: cos(MatrixSpace(ZZ, 2)([1, 2, -4, 7]))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
.......
TypeError: cannot coerce arguments: no canonical coercion from Full MatrixSpace of 2 by 2 dense matrices over Integer Ring to Symbolic Ring

sage: import numpy
sage: vec = numpy.array([1,2])
sage: sin(vec)
array([ 0.84147098,  0.90929743])
sage: sin(vec[0])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
....
TypeError: cannot coerce arguments: no canonical coercion from <type 'numpy.int64'> to Symbolic Ring
----

sage: x = PolynomialRing(QQ, 'x').gen()
sage: sin(x)
sin(x)
sage: x = PolynomialRing(RR, 'x').gen()
sage: sin(x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
.....
TypeError: cannot coerce arguments: __call__() takes exactly 1 positional argument (0 given)
sage: x = PolynomialRing(CC, 'x').gen()
sage: sin(x)
sin(x)

Change History

comment:1 Changed 3 years ago by maldun

  • Description modified (diff)

comment:2 Changed 2 years ago by burcin

  • Summary changed from Coercon problems to symbolic ring to symbolic function do not work with numpy.int64 arguments
  • Milestone set to sage-4.7.1

Note that there is no coercion when you call

sage: import numpy
sage: vec = numpy.array([1,2])
sage: sin(vec)
array([ 0.84147098,  0.90929743])

The __call__() function for sin checks if the argument is a numpy array and calls the right numpy function on this input. See line 349 of sage/symbolic/function.pyx. We can handle other numpy types there.

We cannot work with matrices as numeric objects in symbolics. I suppose you expect the sin() function to be applied to each entry of the matrix. The apply_map() method should be used for this purpose:

sage: t = Matrix(ZZ, 2,2)
sage: t.randomize()
sage: t.apply_map(lambda x: sin(x))
[      0 -sin(1)]
[ sin(4)       0]

sage: x = PolynomialRing(RR, 'x').gen()
sage: sin(x)
<boom>

The problem here is really coercion, but it should be copied to another ticket (in the basic_arithmetic component):

The __call__() function of RR[x] doesn't conform to the generic definition. You should be able to give the parameters as a keyword argument as well. This should be made to work:

sage: R.<x> = RR[]
sage: (x^2+1)(x=5)
11
Note: See TracTickets for help on using tickets.