How to convert a normal list of variable names and functions into a suitable input for the commands in sympy

I want to use the code for computing Jacobian by sympy given in this answer.

But I want to give the variable and function lists as follows;

v_list=[x,y,z]
f_list=[f1,f2,f3]

However, the sympy commands there need v_list and f_list to be given as following;

v_list='x y z'
f_list=['f1','f2','f3']

Is there a way to write a python code that automatically convert v_list and f_list from the first shape that I gave to the shape suitable for the sympy command in that Jacobian function?

Upvotes: 0

Views: 129

Answers (1)

smichr
smichr

Reputation: 19115

The direct answer is

>>> sv_list = ' '.join([i.name for i in v_list])
>>> sf_list = [i.name for i in f_list]
>>> repr(sv_list)
'x y z'
>>> repr(sf_list)
['f1', 'f2', 'f3']

But a question is: why not use the built-in jacobian method of SymPy's Matrix?

>>> v_list = u1, u2 = symbols('u1:3')
>>> f_list = [2*u1 + 3*u2, 2*u1 - 3*u2]
>>> Matrix(f_list).jacobian(v_list)
Matrix([
[2,  3],
[2, -3]])

Note: the use of strings is not necessary; it is just a way to avoid creating variables.

Upvotes: 1

Related Questions