CoCalc Public Filesscratch / sympy.sagewsOpen with one click!
Author: Harald Schilly
Views : 57
Compute Environment: Ubuntu 18.04 (Deprecated)

SymPy solveset and unevaluated expressions

This is running in Anaconda Python, in Sympy 1.0

%auto %default_mode python3
%auto %python3 from sympy import * x = Symbol('x') init_printing()
with evaluate(False): y = x/x y

Note, ** for power in pure Python

x**2/x

sympify converts a string to an expression and evaluate=False suppresses simplification

sympify('x^2 / x', evaluate=False)

solveset in action (like solve, but returns all solutions or a set of solutions)

ex1 = sympify('x^2 / x', evaluate=False) solveset(ex1)
ex2 = sympify('(x+1)^2/(x+1)', evaluate=False) solveset(ex2)
ex3 = sympify('(x+1)^2', evaluate=False) solveset(ex3)

roots for polynomials shows the multiplicity

roots(ex3)
ex4 = sympify('(x+1)/((x+1) * (x-1))', evaluate=False) solveset(ex4)
ex5 = sympify('(x * (x-2) * (x + 3)^2) / (x - 2)', evaluate=False) solveset(ex5)
ex6 = Eq(x - 1, (x + 1) / (x - 1)) ex6
solveset(ex6)
ex7 = Eq(x**3 - 2*x + 1, 1+x) solveset(ex7)
solveset(sympify('(x^2+2*x+1)/(x+1)', evaluate = False))
solveset(sympify('(x^2+2*x+1)/(x-1)^2', evaluate = False))

Below, 0 is not a solution

solveset(sympify('(x^3 - x^2) / x', evaluate=False))
simplify((x**3 - x**2) / x)
solveset(simplify((x**3 - x**2) / x))

use root to see that 1-1 has multiplicity of 2 (only works for polynomial-like expressions)

roots(sympify('x^2+2*x+1'), x)
sympify('x/x')
sympify('x/x', evaluate=False)