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

Time:11-11

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_1\cdot 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']

CodePudding user response:

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)
  • Related