sympy / sympy

A computer algebra system written in pure Python

Home Page:https://sympy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sp.solve: expected an integer, got rational

Titaninchen opened this issue · comments

Hello, I am new to sympy and wanted to solve an equation to x. I´ve got an Error: "expected an integer, got 1/16"
My code I tested with is:

import sympy as sp
x, y = sp.symbols("x y")

equation = sp.sympify("sqrt(x) - y + sqrt(x*(2 - x))")
try: solutions = sp.solve(equation, x)
except Exception as e:
    print("Testcase 1 failed:", e)

equation = sp.sympify("sqrt(x) - y + sqrt(x*(2 - x))")
try: solutions = sp.solveset(equation, x)
except Exception as e:
    print("Testcase 2 failed:", e)

equation = sp.sympify("sqrt(x) - y + sqrt(x*(2 - x))")
try: solutions = sp.solve(equation, x, manual=True)
except Exception as e:
    print("Testcase 3 failed:", e)

equation = sp.sympify("sqrt(x) - y + sqrt(x*(2 - x))")
try: solutions = sp.solve(equation, x, dict=True)
except Exception as e:
    print("Testcase 4 failed:", e)

equation = sp.Eq(sp.sympify("sqrt(x) + sqrt(x*(2 - x))"), sp.sympify("y"))
try: solutions = sp.solve(equation, x)
except Exception as e:
    print("Testcase 5 failed:", e)

The output of this code is:

Testcase 1 failed: expected an integer, got 1/16
Testcase 2 failed: expected an integer, got 1/16
Testcase 3 failed: expected an integer, got 1/16
Testcase 4 failed: expected an integer, got 1/16
Testcase 5 failed: expected an integer, got 1/16
Press any key to continue . . .

The problem is this:

In [1]: poly(y*(y+1) + S(1)/3, y)
---------------------------------------------------------------------------
CoercionFailed

Somehow this ends up making a polynomial over ZZ and then trying to convert 1/3 to ZZ.

I seem to remember a very similar issue before.

duplicate of gh-19755.

Thanks for reporting.