wishlist: numpy ufuncs and Sage symbolic callable functions

63 views
Skip to first unread message

William Stein

unread,
May 20, 2016, 5:39:27 PM5/20/16
to sage-devel
Hi Sage-Devel,

I just spent the week teaching numpy and matplotlib in the context of
Sage. Walking around and watching why/how students got frustrated on
certain homework problems revealed one way we could improve Sage,
which would make Sage much more useful and natural overall. It
seems to me that this shouldn't be impossibly hard.

Consider:

sag: import numpy as np
sage: t = np.arange(5); t
array([0, 1, 2, 3, 4])
sage: t^2 + sin(2*t)
array([ 0. , 1.90929743, 3.2431975 , 8.7205845 , 16.98935825])

So far this is great -- it's awesome that basic sage symbolic
functions like sin, cos, exp, etc., can take numpy arrays as input.
Much better than the situation with python:

sage: math.sin(t) # BOOM

However, we have the following, which frustrated my students a lot:

sage: f(x) = x^2 + sin(2*x)
sage: f(t)
BOOM ...
NotImplementedError: Numpy arrays are not supported as arguments for
symbolic expressions

Yep, it's not implemented. I think it would be great if this were
implemented. The semantics are pretty clear -- apply component-wise
and get a numpy array. Under the hood this should use fast_callable.

Anyway, just a wishlist thing, but it would be nice to make Sage a bit
more usable in a way that doesn't raise any subtle design questions...

-- William


https://cloud.sagemath.com/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/files/support/2016-05-20-142756-numpy-ufunc.sagews




--
William (http://wstein.org)

Erik Bray

unread,
May 23, 2016, 10:44:07 AM5/23/16
to sage-...@googlegroups.com
Yikes! I'd say that should be more than just a wishlist thing. Is
there a ticket for this?

I've put a lot of effort in the past into making expressions like
these work on Numpy arrays (not in Sage but in other projects). I
haven't played around enough with fast_callable to know how much
functionality it gets you for free, but it seems like a lot.

Erik
Reply all
Reply to author
Forward
0 new messages