How do I create a number of sympy symbols from a list?

Question:

I have a list in the following form:
[‘C_k’, ‘c_f’, ‘m_1’, ‘T_1’, ‘T_m’]

I wanna creat a sympy symbol for every "variable" in this list, which is possible with the following function:

a,k,m_n=symbols(‘a k m_n’)

How would this work?
The end goal is to do (Guassian-)error propagation, which requires me to evaluate a derivative of an expression at a given point.

What my code currently only does is convert a latex (math) string to a sympy expression and then extract a list of symbols that I need to differentiate by

import matplotlib.pyplot as plt
from latex2sympy2 import latex2sympy
import numpy as np
from sympy import *
import re

# task: perform error propagation for a given formula and a given dataset regarding some associated uncertainty 

Formula=r'(m_1cdot c_f+C_k)cdot (T_1-T_m)' # Formula is given as latex code
Formula=latex2sympy(Formula) # Formula converted to sympy

print(re.findall(r'[A-Za-z_]+d*',str(Formula))) # this creates a list of symbols in the expression using regex. task is to differentiate the function by these variables

output: [‘C_k’, ‘c_f’, ‘m_1’, ‘T_1’, ‘T_m’]

Asked By: Maris Baier

||

Answers:

Use the symbols function, like this:

tokens = ['C_k', 'c_f', 'm_1', 'T_1', 'T_m']
symb = symbols(" ".join(tokens))
print(symb)
# out: (C_k, c_f, m_1, T_1, T_m)
Answered By: Davide_sd
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.