-1

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
user21819
  • 11
  • 1
  • 1
  • 4
  • It works with no problems with python 3.8.2. Perhaps more information is needed, including versions, exact error messages. – hellork Mar 01 '20 at 08:15
  • I have added the information you asked for – user21819 Mar 01 '20 at 08:27
  • Did you import a? from sympy.abc import x, y, z, a, b – hellork Mar 01 '20 at 08:40
  • Actually I don't want to define symbols in code itself. I want to take input from a user and then assign them as symbols just like we do: x = Symbol('x') But here 'x' will be the user input – user21819 Mar 01 '20 at 08:43
  • it seems to work with strings as well. In [17]: sm.solve('a**3 - 1', a) Out[17]: [1, -1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2] – hellork Mar 01 '20 at 08:45
  • Oh! yeah! It worked with a string. But in sympy document equation was entered without quotes. Nevermind! It worked. Thanks alot! – user21819 Mar 01 '20 at 08:47
  • I guess you're supposed to use sm.solve(sm.symbols('a**3 - 1'), a) but I dunno :) Um, one might assume that sm.symbols would do some linting or cleaning of user input, but needs research. – hellork Mar 01 '20 at 08:48
  • This is a near duplicate of the question to which [this answer](https://stackoverflow.com/a/60471208/1089161) was given. – smichr Mar 01 '20 at 11:32

2 Answers2

0

You should define your symbols just the way you want to use it.

ex. [a,b,c] = sm.symbols('a b c')

Now you can use a,b,c in your code.

The way you are using it in L[j] = sm.symbols(L[j]), L[j] doesn't really take a as a variable.

Or in your case you can use it:

from sympy import sm

L=['a','b']
print(L)

for j in range(len(L)):
    locals()[L[j]] = sm.symbols(L[j])
Billie
  • 35
  • 8
0

You can try this way:

>>> L = ['a', 'b']
>>> a, b = map(Symbol, L)
>>> type(a)
<class′sympy.core.symbol.Symbol′>

And then use a and b wherever you want.

namannimmo
  • 136
  • 1
  • 6