I am using python 3.7 on Spyder. Here is my simple code to store string elements ['a','b'] in a list L as sympy symbols. As output, I have new list L with two Symbols [a,b] in it. But when I try to use these symbols in my calculation I get an error saying a & b are not defined. Any suggestions on how can I fix this?
Basically, what I want to do is use string elements in a list as symbols for sympy calculations. Any suggestions on other methods to do this are welcomed. Thank you.
import sympy as sm
L=['a','b']
print(L)
for j in range(len(L)):
L[j] = sm.symbols(L[j])
B=sm.solve(a**2 - 1, a)
print(B)
Here is the error:
runfile('C:/Users/bhise/.spyder-py3/temp.py', wdir='C:/Users/bhise/.spyder-py3')
['a', 'b']
Traceback (most recent call last):
File "<ipython-input-43-6826047bb7df>", line 1, in <module>
runfile('C:/Users/bhise/.spyder-py3/temp.py',
wdir='C:/Users/bhise/.spyder-py3')
File "C:\Users\bhise\Anaconda3\lib\site-
packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\bhise\Anaconda3\lib\site-
packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/bhise/.spyder-py3/temp.py", line 10, in <module>
B=sm.solve(a**2 - 1, a)
NameError: name 'a' is not defined