Defining arbitrary functions

43 views
Skip to first unread message

benjamin

unread,
Dec 23, 2013, 6:38:49 AM12/23/13
to sy...@googlegroups.com

Hi there!

I am trying to create list of N arbitrary functions of the next form :

x is list of symbols [x_1,x_2,.....,x_M]

f is list of functions [f_1,f_2,.....,f_N]

and every function f_i is a multivariable function of all symbols (x_1,x_2,.....x_M)

NOTE: i just wrote arbitrary M and N but in my calculations they are specified integers.


benjamin

unread,
Dec 23, 2013, 11:56:53 AM12/23/13
to sy...@googlegroups.com
This is example

from sympy import Symbol,Function

x1=Symbol ('x1')
x2=Symbol ('x2')
x3=Symbol ('x3')

f1=Function ('f1')
f2=Function ('f2')
f3=Function ('f3')

X=[]
X.append(x1)
X.append(x2)
X.append(x3)

F=[]
F.append(f1(x1,x2,x3))
F.append(f2(x1,x2,x3))
F.append(f3(x1,x2,x3))

is there any smarter way to do it?
thank you

Jason Moore

unread,
Dec 23, 2013, 1:56:47 PM12/23/13
to sy...@googlegroups.com
You could use list comprehensions and argument expansion (*):

x = symbols('x1, x2, x3')
functions = [Function(name) for name in ['f1', 'f2', 'f3]]
F = [f(*x) for f in functions]

--
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 http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.

Aaron Meurer

unread,
Dec 23, 2013, 11:59:48 PM12/23/13
to sy...@googlegroups.com
Also take a look at the numbered_symbols function.

benjamin

unread,
Dec 24, 2013, 3:56:49 AM12/24/13
to sy...@googlegroups.com
Thank you guys a lot.

Is there something like numbered_Functions function?

Christophe Bal

unread,
Dec 24, 2013, 5:40:10 AM12/24/13
to sympy-list
Hello,
following the advice of Aaron, here is a more generic way to do that.


=== CODE ===

from sympy import *

variables = [
    x for x, _ in zip(
        numbered_symbols(prefix = 'x', start = 1), 
        range(1,4)
    )
]

functions = [
    f for f, _ in zip(
        numbered_symbols(prefix = 'f', cls = Function, start = 1), 
        range(1,4)
    )
]

F = [f(*variables) for f in functions]


2013/12/24 benjamin <benjami...@gmail.com>
Thank you guys a lot.

Is there something like numbered_Functions function?

--

Aaron Meurer

unread,
Dec 24, 2013, 3:19:08 PM12/24/13
to sy...@googlegroups.com
Just set the cls parameter in numbered_symbols to Function.

Aaron Meurer

On Tue, Dec 24, 2013 at 1:56 AM, benjamin <benjami...@gmail.com> wrote:
> Thank you guys a lot.
>
> Is there something like numbered_Functions function?
>

Benjamin Mesić

unread,
Dec 24, 2013, 4:43:50 PM12/24/13
to sy...@googlegroups.com
Exactly what i was looking for, thank you all.

Benjamin


You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/tqSnuX9dByM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages