In [1]:
import sympy as sym
import numpy as np
import math

from IPython.display import display, Math
from sympy.abc import w,x,y,z,a,b,c,d
sym.init_printing()
In [2]:
p1 = 4*x**5 - x
p2 = 2*x**3 - x

p1
Out[2]:
$$4 x^{5} - x$$
In [3]:
p2
Out[3]:
$$2 x^{3} - x$$
In [4]:
display(Math('\\frac{%s}{%s} = %s' %(sym.latex(p1),sym.latex(p2),sym.latex(p1/p2))))
display(Math('\\frac{%s}{%s} = %s' %(sym.latex(p1),sym.latex(p2),sym.latex(sym.expand(p1/p2)))))
display(Math('\\frac{%s}{%s} = %s' %(sym.latex(p1),sym.latex(p2),sym.latex(sym.simplify(p1/p2)))))
$\displaystyle \frac{4 x^{5} - x}{2 x^{3} - x} = \frac{4 x^{5} - x}{2 x^{3} - x}$
$\displaystyle \frac{4 x^{5} - x}{2 x^{3} - x} = \frac{4 x^{5}}{2 x^{3} - x} - \frac{x}{2 x^{3} - x}$
$\displaystyle \frac{4 x^{5} - x}{2 x^{3} - x} = 2 x^{2} + 1$
In [5]:
display(Math('\\frac{%s}{%s} = %s' %(sym.latex(p1),sym.latex(p2),sym.latex(sym.sympify(p1/p2)))))
display(Math('\\frac{%s}{%s} = %s' %(sym.latex(p1),sym.latex(p2),sym.latex(sym.solve(p1/p2)))))
$\displaystyle \frac{4 x^{5} - x}{2 x^{3} - x} = \frac{4 x^{5} - x}{2 x^{3} - x}$
$\displaystyle \frac{4 x^{5} - x}{2 x^{3} - x} = \left [ - \frac{\sqrt{2} i}{2}, \quad \frac{\sqrt{2} i}{2}\right ]$
In [6]:
f = 3/4
f
Out[6]:
$$0.75$$
In [7]:
f = sym.sympify(3)/4
finfo = sym.fraction(f)
finfo
Out[7]:
$$\left ( 3, \quad 4\right )$$
In [8]:
type(finfo)
Out[8]:
tuple
In [9]:
f = sym.sympify(3)
finfo = sym.fraction(f)
finfo
Out[9]:
$$\left ( 3, \quad 1\right )$$
In [10]:
pNum = x**6 + 2*x**4 + 6*x -y
pDen = x**3 + 3

pNum
Out[10]:
$$x^{6} + 2 x^{4} + 6 x - y$$
In [11]:
pDen
Out[11]:
$$x^{3} + 3$$
In [12]:
for yi in range(5,16):
    tempnum = pNum.subs(y,yi)
    display(Math('%s = %s' %(sym.latex(tempnum/pDen), sym.latex(sym.simplify(tempnum/pDen)))))
    #display(tempnum)
    
    #display(sym.fraction(sym.simplify(tempnum/pDen))[1]) # extract denominator
    if sym.fraction(sym.simplify(tempnum/pDen))[1]==1:
        rightanswer = yi
        
print('The answer the satisfies our goal is y=%g' %rightanswer)
$\displaystyle \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 5\right) = \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 5\right)$
$\displaystyle \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 6\right) = \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 6\right)$
$\displaystyle \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 7\right) = \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 7\right)$
$\displaystyle \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 8\right) = \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 8\right)$
$\displaystyle \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 9\right) = x^{3} + 2 x - 3$
$\displaystyle \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 10\right) = \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 10\right)$
$\displaystyle \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 11\right) = \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 11\right)$
$\displaystyle \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 12\right) = \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 12\right)$
$\displaystyle \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 13\right) = \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 13\right)$
$\displaystyle \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 14\right) = \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 14\right)$
$\displaystyle \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 15\right) = \frac{1}{x^{3} + 3} \left(x^{6} + 2 x^{4} + 6 x - 15\right)$
The answer the satisfies our goal is y=9