Lambdify utilization

33 views
Skip to first unread message

Junjie Shi

unread,
May 20, 2022, 1:35:57 AM5/20/22
to sympy
Hi,
I am a new hand in sympy. I have some questions when I using it. Could you please help me?

When I use "x = symbols('x:(n+1)')" to claim (n+1) variables, after the calculations, I can get the results represented by x0, x1, x2,...xn, i.e., f(x0, x1, ..., xn), where n can change from time to time.
Now, I have several sets of input data, and I want to know the results of the calculated function, i.e., f(input), where each set of input data has n elements.
I am trying to convert the  f(x0, x1, ..., xn) by using lambdify. For example, I can only do obj = lambdify(x, f(x[0], x[1], x[2], ..., x[n])) rather than lambdify(x, f(x0, x1, ..., xn)).
How can I use the previous calculated f(x0, x1, ..., xn) in lambdify directly?

In addition, when I calculate the result, I can only do obj(input[0], input[1], ..., input[n]), can I read the input array directly, i.e., obj(input)?

Thanks for your time.
 
Best,
Junjie
 

Junjie Shi

unread,
May 20, 2022, 5:05:03 AM5/20/22
to sympy
I can write a wrapper function and use subs to realize the obj(input) functionality.
However, I still have no idea how can I convert f(x0, x1, ..., xn) to f(x[0], x[1], x[2], ..., x[n]) :(

emanuel.c...@gmail.com

unread,
May 20, 2022, 8:47:26 AM5/20/22
to sympy

Thanks to Python’s function definition facilities, you can collect your arguments as a list and use them as such in your function definition :

>>> from sympy import symbols
>>> X=symbols("x:5") ; X
(x0, x1, x2, x3, x4)
>>> def foo(*u): return "I got %d arguments."%len(u)
... 
>>> foo(X) # Call foo with the single argument X
'I got 1 arguments.'
>>> foo(*X) # Call fo with the LIST of arguments (x0,...,x4)
'I got 5 arguments.'

You can also have fixed arguments before the list argument :

>>> def bar(u, *v): return "u=%s, v=%s"%(u, v)
... 
>>> bar(*X)
'u=x0, v=(x1, x2, x3, x4)'

HTH,

Junjie Shi

unread,
May 23, 2022, 3:30:47 AM5/23/22
to sympy
Thank you very much :)
Reply all
Reply to author
Forward
0 new messages