5

I am have a problem when trying to solve an equation using sympy. Some of the variables have specific values assigned. I am trying to solve the equation for b,c and d. This is my attempt:

from random import randint
import sympy
from sympy.abc import b,c,d,B,C,r,x

B=10
C=20
r=123

# Equation: x^3+b*x^2+c*x+d=x^3+(B−r)x^2+(C−B*r)x−C*r
equation = sympy.Eq(x**3+b*x**2+c*x+d,x**3+(B−r)*x**2+(C−B*r)*x−C*r)

print(sympy.solve(equation,"b"))
print(sympy.solve(equation,"c"))    
print(sympy.solve(equation,"d"))

Python prints me the following error:

    [user@user Python Scripts]$ python polygen.py 
  File "polygen.py", line 10
    equation = sympy.Eq(x**3+b*x**2+c*x+d,x**3+(B−r)*x**2+(C−B*r)*x−C*r)
                                                  ^
SyntaxError: invalid character in identifier

What am I missing?

4
  • Are you missing a * there after (B-r)? Commented Jun 29, 2018 at 22:44
  • I added the * but the error is the exact same.
    – Kyu96
    Commented Jun 29, 2018 at 22:47
  • try adding a * after (C-B*r) too, python doesnt handle inference of multiplication with parenthesis
    – Tissuebox
    Commented Jun 29, 2018 at 23:17
  • @Tissuebox I added it everywhere (See updated start post). Also I tried both, ** and ^ for power as expression doesn't change anything.
    – Kyu96
    Commented Jun 29, 2018 at 23:19

2 Answers 2

7

Something is wrong with your - character. It's not a regular character for some reason and python isn't recognizing it. I replaced all your minus signs with hyphens on my machine and it works:

from random import randint
import sympy
from sympy.abc import b,c,d,B,C,r,x

B=10
C=20
r=123

# Equation: x^3+b*x^2+c*x+d=x^3+(B−r)x^2+(C−B*r)x−C*r
equation = sympy.Eq(x**3+b*x**2+c*x+d,x**3+(B-r)*x**2+(C-B*r)*x-C*r)

print(sympy.solve(equation,"b"))
print(sympy.solve(equation,"c"))
print(sympy.solve(equation,"d"))

I'm not sure what kind of character the wrong - sign is:

a = 2−2 does not work in my machine

a = 2-2 does however

2
  • Oh my god.. That must be have happened by copying the equation from a pdf file. That such a dumb error :D Thanks !
    – Kyu96
    Commented Jun 29, 2018 at 23:41
  • 1
    @Kyu96 That wrong sign is U+2212, a Unicode MINUS SIGN.
    – PM 2Ring
    Commented Jun 30, 2018 at 0:00
2

- IS a MINUS IS NOT. in Python interprets them differently, the first one encodes to u"\u002D" but the second encodes to u"\u2212" and clearly they won't do the same thing. To be sure use your numeric keyboard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.