Opened 12 years ago

Last modified 8 years ago

#8824 new defect

Make it so that numpy datatypes are integrated into the coercion model

Reported by: jason Owned by: robertwb
Priority: major Milestone: sage-6.4
Component: coercion Keywords:
Cc: Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges


From sage-devel:


Tracking a weird bug I've discovered the following: For a symbolic variable x and a numpy.float64 y, the code 'x<y' evals

to a Symbolic expression, while 'y<x' evals to a numpy.bool.

I'm afraid I'm stacked, as it is the responsability of the method, and I can't assign it to a custom method, for example.

Any idea what can I try so that 'y<x' evals to a Symbolic Expression

too (if you agree this should be the result)?

Sage should set the array_priority attribute to something very high in its base class(es), then let the coercion model decide how NumPy? objects should be handled (in this case, coerce to RDF or CDF).

NumPy? uses the custom convention that array_priority decides which operand gets to handle the operation.


import numpy as np

class MagicOne:
    __array_priority__ = 1000
    def __cmp__(self, other):
        print 'MagicOne has control'
        return cmp(1, other)

one = MagicOne()

print one < np.float64(63.3)
print np.float64(63.3) < one

This prints

MagicOne has control
MagicOne has control

Change History (5)

comment:1 Changed 12 years ago by dagss

This might be a fitting place to record a wish: If Sage decides to do something with NumPy? arrays (not just scalars), I think the behaviour should be something like:

sage: M = random_matrix(RDF, 4, 3)
sage: A = np.random.normal(size=(12, 10, 4)).astype(np.float32)
sage: type(A * M)
<type np.ndarray...>
sage: (A * M).shape
(12, 10, 3)
sage: (A * M).dtype

I.e. let matrices be operators acting on data, operating along the vectors along the rightmost dimension (matrix on right) or leftmost dimension (matrix on left).

In particular, I think it would be very bad to coerce NumPy? arrays to Sage matrices!

comment:2 Changed 9 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:3 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:4 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:5 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4
Note: See TracTickets for help on using tickets.