Writing a symbolic version of numpy.clip for use by lambdify

207 views
Skip to first unread message

Vladimir Perić

unread,
Feb 3, 2016, 4:11:28 PM2/3/16
to sy...@googlegroups.com
Hi,

I'm trying to write a symbolic numpy.clip function[1], to be used in
expressions (it's ok if it doesn't get simplified) and in lambdify
(and consequently evaluated by numpy). Doing this in pure numpy would
be trivial, but it doesn't work well with my current code (and would
be inelegant).

I've tried something like this, in the hope that the function will
remain unevaluated except when an numpy array is passed to it, but
it's not working: (inspired by [2])

class MyClip(sympy.Function):
@classmethod
def eval(cls, arg1, arg2, arg3):
if isinstance(arg1, np.ndarray):
return np.clip(arg1, arg2, arg3)

Am I missing something obvious here, or not specifying "eval" in the
correct way? Perhaps my whole approach is wrong, but I cannot see a
better solution.

Thanks for any help!

[1] Clip limits the values in an array: given an interval, values
outside the interval are clipped to the interval edges: np.clip(a,
a_min, a_max)
[2] How to define a symbolic function:
https://groups.google.com/forum/#!topic/sympy/sk4K-VWBRRQ

Aaron Meurer

unread,
Feb 4, 2016, 3:21:43 PM2/4/16
to sy...@googlegroups.com
What happens when you use this?

eval is generally used only for defining output for SymPy arguments, but I believe what you are doing should work, since lambdify should map MyClip to itself.  You might have to do something to make sure it is included in the namespace. 

Aaron Meurer


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAFu6gYbqsROfy9WnZ5VaocVjdcV3-Te3dQO3f_ZQ2UHdRa6YDQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages