I would use the class MyFunction(Function) version unless you really need something quick and dirty. Lambda(), like Python's lambda, should really only be used for unnamed one-time use functions.
Aaron Meurer On Thu, Jun 9, 2011 at 5:38 PM, Mateusz Paprocki <[email protected]> wrote: > Hi, > > On 9 June 2011 16:33, [email protected] > <[email protected]> wrote: >> >> On 10 June 2011 01:13, Mateusz Paprocki <[email protected]> wrote: >>> >>> Hi, >>> >>> On 9 June 2011 16:05, Tomo Lazovich <[email protected]> wrote: >>>> >>>> Hi everyone, >>>> >>>> I'm thinking of implementing a Wavefunction class in >>>> sympy.physics.quantum, and as a start I'm wondering about functions in >>>> sympy >>>> in general. Disclaimer is that this is a potentially very noobish question. >>>> >>>> Is there already a way in sympy to define an arbitrary functional form? >>>> I.e. to be able to easily say something like f(x) = x**2 and then evaluate >>>> f(2) or whatever else you'd like to do. >>> >>> The simplest way is to use Lambda: >>> In [1]: f = Lambda(x, x**2) >>> In [2]: f(2) >>> Out[2]: 4 >>> You can also define your own (named) function (I mean subclass Function >>> and provide eval() class method), but this may be unnecessary. >> >> How robust is this if I want to define a multivariable function? Is it >> done anywhere in sympy? > > Lambda is a SymPy's version of Python's lambda, so multivariate Lambdas are > supported as well, e.g.: > In [3]: g = Lambda((x, y, z), x + sin(y) + cos(z)) > In [4]: g(1, 2, 3) > Out[4]: cos(3) + sin(2) + 1 > If you need something more then you can create your own functions, e.g.: > In [5]: class MyFunction(Function): > ...: nargs = 3 > ...: @classmethod > ...: def eval(cls, x, y, z): > ...: return x + sin(y) + cos(z) > ...: > ...: > In [6]: MyFunction(1, 2, 3) > Out[6]: cos(3) + sin(2) + 1 > >> >> >>>> >>>> Apologies if this is obvious! >>>> >>>> Tomo >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "sympy" group. >>>> To post to this group, send email to [email protected]. >>>> To unsubscribe from this group, send email to >>>> [email protected]. >>>> For more options, visit this group at >>>> http://groups.google.com/group/sympy?hl=en. >>> >>> Mateusz >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "sympy" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> For more options, visit this group at >>> http://groups.google.com/group/sympy?hl=en. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "sympy" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/sympy?hl=en. > > Mateusz > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/sympy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
