Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jasongrout opened this issue Apr 29, 2010 · 1 comment
Open

Comments

@jasongrout
Copy link
Member

From sage-devel: http://groups.google.com/group/sage-devel/browse_frm/thread/221f569eaba874de

Hello:

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
numpy.float64.lt, 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.

Example:

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
True
MagicOne has control
False

Component: coercion

Issue created by migration from https://trac.sagemath.org/ticket/8824

@sagetrac-dagss
Copy link
Mannequin

sagetrac-dagss mannequin commented Apr 29, 2010

comment:1

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
float64

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!

@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@mkoeppe mkoeppe removed this from the sage-6.4 milestone Dec 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants