njfy410
njfy410

Reputation: 157

sympy error: 'Symbol' object has no attribute 'pi'

I am attempting to solve an equation using sympy, however for some reason I am getting the following error:

'Symbol' object has no attribute 'pi'

The code looks like:

solveset(Eq(parse_expr("sin(math.pi/6)+sin(a)"),parse_expr("1")),a,domain=S.Reals)

Anyone understand what to do in this case?

My import statements are as follows:

import re
from sympy import *
from sympy.parsing.sympy_parser import parse_expr 
import math

Upvotes: 0

Views: 1461

Answers (1)

smichr
smichr

Reputation: 19115

You don't need to use parse_expr here (or math).

>>> from sympy import pi, solveset, sin, Eq
>>> from sympy.abc import a
>>> solveset(Eq(sin(pi/6)+sin(a), 1), a, domain=S.Reals)
Union(ImageSet(Lambda(_n, 2*_n*pi + 5*pi/6), Integers), ImageSet(Lambda(_n, 2*_n*pi + pi/6), Integers))

Otherwise, if you do then you need to tell parse_expr what to use for 'math' (otherwise it creates a Symbol named 'math' and Symbols don't have an attribute of pi...thus the error):

>>> import math
>>> parse_expr('math.pi', {'math': math})  # local dict: "for "math" use math
3.14159265359

You can probably reconstruct your example but you should be aware that (currently) solveset does not like working with floating point numbers in this case:

>>> solveset(sin(x) - .5, domain=S.Reals)
EmptySet()

But it works if you replace the 0.5 with S(1)/2 (which creates SymPy Rational, 1/2).

Upvotes: 2

Related Questions