Help with defining functions

28 views
Skip to first unread message

Ben Lucato

unread,
Jan 14, 2013, 8:14:21 PM1/14/13
to sy...@googlegroups.com
Hi I have looked through the docs and other places online, but I haven't found an answer!

Currently when working with functions I do this:

from sympy import *
f = Function('f')
x = Symbol('x')
f = 100 - 2 * x

to evaluate f I have to do this: 
f.subs({x: 2})
>>> 96

which is a pain. Is there some way to define my function such that I can do:
f(2)
>>> 96
???

Matthew Rocklin

unread,
Jan 14, 2013, 9:12:26 PM1/14/13
to sy...@googlegroups.com
You can transform an expression into a Python callable function with lambdify 

In [1]: f = lambdify(x, 100-2*x)

In [2]: f(2)
Out[2]: 96



--
You received this message because you are subscribed to the Google Groups "sympy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sympy/-/By7WV9mZh8wJ.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.

Ben Lucato

unread,
Jan 14, 2013, 9:34:34 PM1/14/13
to sy...@googlegroups.com
Thanks for your response. Is there a way to do this whilst preserving other methods of functions. For example, so I can still do things like f.diff()

Aaron Meurer

unread,
Jan 14, 2013, 9:43:12 PM1/14/13
to sy...@googlegroups.com
We don't have support for that exact syntax, because it doesn't make
sense (with it, f is both a function and the evaluation of itself,
i.e., f == f(x)). You could probably emulate it by writing a custom
class with minimal code, though.

If you want the more reasonable syntax f(x).diff(x), just define a
Python function

>>> def f(y):
... return (100 - 2*x).subs(x, y)
...
>>> f(2)
96
>>> f(x).diff(x)
-2

lambdify is designed for efficient evaluation using numpy. If you are
only evaulating at a couple of points, or you still want to use the
output with SymPy, just use a Python function.

For completeness, other options are implemented_function and
subclassing Function. These are useful if you want the function to
remain unevaluated for some inputs. implemented_function is designed
to be used in conjunction with lambdify. Subclassing Function lets
you write arbitrary code (in the eval method) to decide when the
function becomes evaluated or not (not evaluating means it is just
left as symbolic f(x)).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/i0oibTlceh4J.

Ben Lucato

unread,
Jan 14, 2013, 11:57:18 PM1/14/13
to sy...@googlegroups.com
That's perfect and exactly what I was looking for with defining a function. I'm not in need with efficient code, I'm looking for the simple version. Thanks - and sympy fucking ROCKS. I love what you guys have created
Reply all
Reply to author
Forward
0 new messages