Symbolic functions in sympy.

6 views
Skip to first unread message

and...@hotmail.com

unread,
May 12, 2007, 5:53:45 PM5/12/07
to sympy

Dear All,

I've recently discovered sympy, and I'm very impressed with it. I hope
to use it within a project of mine to save me a lot of coding, and the
lightweight format of sympy as pure Python code library (without
complicated compilation and dependency issues) will be very valuable
to me. So many thanks for your work!

However, within my work I wish to be able to work with general
symbolic functions, which I cannot find within the current SVN version
of sympy. I will try and explain what I mean below with an example
(but please reply with questions if my explanation is not clear!). I
would like to be able to define an expression, called "a" for example,
which involves an unknown function, which I will call "f" in my
example which depends on various variables. For example, in Maple to
do this I would write:

restart;
a := f(u,v^4) + u^3 + 27 + 8*u*v;

to represent an expression involving an unknown function "f" evaluated
at the point (u,v^4). Then I would like to be able to manipulate the
expression "a", with the unknown function. For example, in Maple I
could differentiate "a" symbolically, so that:

b := diff(a,v);

would return:

b = 4*v^3*D(2)(f)(u,v^4) + 8*u

where the symbol D(2)(f)(u,v^4) represents the derivative of "f" with
respect to it's second argument evaluated at the point (u,v^4). Later,
if "f" became defined in the Maple session, the value of "b" would
return a specific answer. For example, if "f" is later defined as
"f(x,y):=x^2+y^2", then evaluating "b" would return:

b = 8*v^7 + 8*u

So, I have a few questions:
- do you have any functionality like this in sympy already? Perhaps I
just haven't found it yet!
- do you have any desire to support this type of syntax in sympy?
- do you have a plan for how this should work?

I would be happy to try and contribute code to implement this in sympy
if it does not exist already (instead of just as an extension of sympy
within my own project). However I would certainly wish to discuss the
design carefully with you first (as I am not an expert in object
orientated design!).

Best wishes, and thanks for sympy,
andy.

Fabian Seoane

unread,
May 12, 2007, 6:54:25 PM5/12/07
to sy...@googlegroups.com
On 5/12/07, and...@hotmail.com <and...@hotmail.com> wrote:
>
>
> Dear All,
>
> I've recently discovered sympy, and I'm very impressed with it. I hope
> to use it within a project of mine to save me a lot of coding, and the
> lightweight format of sympy as pure Python code library (without
> complicated compilation and dependency issues) will be very valuable
> to me. So many thanks for your work!
>
> However, within my work I wish to be able to work with general
> symbolic functions, which I cannot find within the current SVN version
> of sympy. I will try and explain what I mean below with an example
> (but please reply with questions if my explanation is not clear!). I
> would like to be able to define an expression, called "a" for example,
> which involves an unknown function, which I will call "f" in my
> example which depends on various variables. For example, in Maple to
> do this I would write:
>
> restart;
> a := f(u,v^4) + u^3 + 27 + 8*u*v;
>
> to represent an expression involving an unknown function "f" evaluated
> at the point (u,v^4). Then I would like to be able to manipulate the
> expression "a", with the unknown function. For example, in Maple I
> could differentiate "a" symbolically, so that:
>
> b := diff(a,v);

you can do something similar, but just for one variable:

>>> from sympy import *
>>> x = Symbol('x')
>>> f = Function(x)+x+1
>>> diff(f, x)
1+Function'(x)

implementing multiple variables for Function would be very
appreciated, just take a look in sympy.core.functions and ask
questions!

>
> would return:
>
> b = 4*v^3*D(2)(f)(u,v^4) + 8*u
>
> where the symbol D(2)(f)(u,v^4) represents the derivative of "f" with
> respect to it's second argument evaluated at the point (u,v^4). Later,
> if "f" became defined in the Maple session, the value of "b" would
> return a specific answer. For example, if "f" is later defined as
> "f(x,y):=x^2+y^2", then evaluating "b" would return:
>
> b = 8*v^7 + 8*u
>
> So, I have a few questions:
> - do you have any functionality like this in sympy already? Perhaps I
> just haven't found it yet!
> - do you have any desire to support this type of syntax in sympy?
> - do you have a plan for how this should work?
>
> I would be happy to try and contribute code to implement this in sympy
> if it does not exist already (instead of just as an extension of sympy
> within my own project). However I would certainly wish to discuss the
> design carefully with you first (as I am not an expert in object
> orientated design!).
>
> Best wishes, and thanks for sympy,
> andy.
>
>
> >
>


--
Fabian, http://grupododo.com/seo/

Ondrej Certik

unread,
May 12, 2007, 6:58:33 PM5/12/07
to sy...@googlegroups.com, and...@hotmail.com
Hi Andy!

Thanks for the email and you suggestions.

> I've recently discovered sympy, and I'm very impressed with it. I hope
> to use it within a project of mine to save me a lot of coding, and the
> lightweight format of sympy as pure Python code library (without
> complicated compilation and dependency issues) will be very valuable
> to me. So many thanks for your work!

We are glad you like it. :)

> So, I have a few questions:
> - do you have any functionality like this in sympy already? Perhaps I
> just haven't found it yet!

Yes, we do, but currently we only have functions of one variable. It's
the class Function() from which classes like sin,cos, etc. inherit.
Look into the examples/relativity.py

basically you just do (in isympy):

In [2]: class myf(Function): pass
...:

In [3]: f = myf(x)

In [4]: f
Out[4]: myf(x)

In [5]: f.diff(x)
Out[5]: myf'(x)

In [8]: e = exp(myf(x)*x**2)

In [9]: e.diff(x)
Out[9]: exp(myf(x)*x**2)*(x**2*myf'(x)+2*myf(x)*x)


if you prefer your new function to have some specific functionality,
just inherit some more methods, look into cos, sin classes for
example.

> - do you have any desire to support this type of syntax in sympy?
> - do you have a plan for how this should work?

Well, we want SymPy to be a regular python library, thus we can only
use syntax, that Python allows us. We also would like to support
functions of more variables.
If you would like to help - just subscribe to the mailing list and the
Issues list, read:

http://code.google.com/p/sympy/wiki/SympyDevelopment

submit a few patches and we'll give you a svn access.

> I would be happy to try and contribute code to implement this in sympy
> if it does not exist already (instead of just as an extension of sympy
> within my own project). However I would certainly wish to discuss the
> design carefully with you first (as I am not an expert in object
> orientated design!).

If you want to contribute several variables, just create an Issue for
that and possibly submit your patch there. We'll discuss the details
there.

If you have some code or module with SymPy and it could be useful to
others, we are interested in that. We can add it to sympy.modules.

Have a nice day,
Ondrej

Reply all
Reply to author
Forward
0 new messages