Opened 9 years ago

Closed 4 years ago

#9769 closed defect (duplicate)

symbolic function do not work with numpy.int64 arguments

Reported by: maldun Owned by: burcin
Priority: major Milestone: sage-duplicate/invalid/wontfix
Component: symbolics Keywords:
Cc: Merged in:
Authors: Reviewers: Jeroen Demeyer
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by rws)

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

Change History (11)

comment:1 Changed 9 years ago by maldun

  • Description modified (diff)

comment:2 follow-ups: Changed 8 years ago by burcin

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

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

comment:3 Changed 6 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:4 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:5 in reply to: ↑ 2 Changed 5 years ago by rws

  • Description modified (diff)

Replying to burcin:

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):

Incidentally the same with power series is part of #16197.

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

Edit: confused poly with series, sorry

Last edited 5 years ago by rws (previous) (diff)

comment:6 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:7 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:8 in reply to: ↑ 2 Changed 5 years ago by rws

Replying to burcin:

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

This is now #17311

comment:9 Changed 4 years ago by vdelecroix

  • Milestone changed from sage-6.4 to sage-duplicate/invalid/wontfix
  • Status changed from new to needs_review

Hello,

I propose to close this ticket as duplicate because of #18076. With the branch applied

sage: import numpy
sage: cos(numpy.int32('12'))
cos(12)
sage: cos(numpy.float32('1.1'))
0.453596100177538

comment:10 Changed 4 years ago by jdemeyer

  • Reviewers set to Jeroen Demeyer
  • Status changed from needs_review to positive_review

comment:11 Changed 4 years ago by vbraun

  • Resolution set to duplicate
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.