Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ArpegiusWhooves/1409697599d706067c726d320d1406e4 to your computer and use it in GitHub Desktop.
Save ArpegiusWhooves/1409697599d706067c726d320d1406e4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
},
"orig_nbformat": 4,
"kernelspec": {
"name": "python3",
"display_name": "Python 3.9.5 64-bit"
},
"interpreter": {
"hash": "ac59ebe37160ed0dfa835113d9b8498d9f09ceb179beaac4002f036b9467c963"
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"source": [
"from sympy import Poly,prod,Eq,symbols,Symbol\r\n",
"from sympy.abc import x \r\n",
"from IPython.display import display, Latex,HTML"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 15,
"source": [
"RootDegre=3\r\n",
"Coeffs=symbols(\"f e d c b a\")"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 16,
"source": [
"display(Latex(\"$$x \\\\rightarrow x^{}$$\".format(RootDegre)))\r\n",
"P=Poly.from_list([1] + list(reversed(Coeffs)), gens=x)\r\n",
"P = P.as_poly(x)\r\n",
"Eq(P.as_expr(),0)"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Latex object>"
],
"text/latex": [
"$$x \\rightarrow x^3$$"
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(a*x**5 + b*x**4 + c*x**3 + d*x**2 + e*x + f + x**6, 0)"
],
"text/latex": [
"$\\displaystyle a x^{5} + b x^{4} + c x^{3} + d x^{2} + e x + f + x^{6} = 0$"
]
},
"metadata": {},
"execution_count": 16
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 17,
"source": [
"left1=sum([ (b*x**a) for ((a,),b) in P.all_terms() if a%RootDegre!=0 ]) \r\n",
"right1=-sum([ (b*x**a) for ((a,),b) in P.all_terms() if a%RootDegre==0 ]) \r\n",
"Eq(left1,right1)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(a*x**5 + b*x**4 + d*x**2 + e*x, -c*x**3 - f - x**6)"
],
"text/latex": [
"$\\displaystyle a x^{5} + b x^{4} + d x^{2} + e x = - c x^{3} - f - x^{6}$"
]
},
"metadata": {},
"execution_count": 17
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 18,
"source": [
"left_pol=sum([ (b*x**a) for ((a,),b) in (left1**RootDegre).as_poly(x).all_terms() if a%RootDegre!=0 ]).as_poly(x)\r\n",
"left_pol.as_expr()"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"3*a**2*b*x**14 + 3*a*b**2*x**13 + 3*d**2*e*x**5 + 3*d*e**2*x**4 + x**11*(3*a**2*e + 6*a*b*d) + x**10*(6*a*b*e + 3*b**2*d) + x**8*(6*a*d*e + 3*b*d**2) + x**7*(3*a*e**2 + 6*b*d*e)"
],
"text/latex": [
"$\\displaystyle 3 a^{2} b x^{14} + 3 a b^{2} x^{13} + 3 d^{2} e x^{5} + 3 d e^{2} x^{4} + x^{11} \\left(3 a^{2} e + 6 a b d\\right) + x^{10} \\left(6 a b e + 3 b^{2} d\\right) + x^{8} \\left(6 a d e + 3 b d^{2}\\right) + x^{7} \\left(3 a e^{2} + 6 b d e\\right)$"
]
},
"metadata": {},
"execution_count": 18
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 19,
"source": [
"common,leftrem = left_pol.div(left1.as_poly(x)) \r\n",
"if leftrem != 0: \r\n",
" #display(leftrem.as_expr())\r\n",
" raise SystemExit(\"Not exists!\")\r\n",
"Eq(common.as_expr()*left1,common.as_expr()*right1)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq((3*a*b*x**9 + 3*d*e*x**3 + x**6*(3*a*e + 3*b*d))*(a*x**5 + b*x**4 + d*x**2 + e*x), (-c*x**3 - f - x**6)*(3*a*b*x**9 + 3*d*e*x**3 + x**6*(3*a*e + 3*b*d)))"
],
"text/latex": [
"$\\displaystyle \\left(3 a b x^{9} + 3 d e x^{3} + x^{6} \\left(3 a e + 3 b d\\right)\\right) \\left(a x^{5} + b x^{4} + d x^{2} + e x\\right) = \\left(- c x^{3} - f - x^{6}\\right) \\left(3 a b x^{9} + 3 d e x^{3} + x^{6} \\left(3 a e + 3 b d\\right)\\right)$"
]
},
"metadata": {},
"execution_count": 19
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 20,
"source": [
"Eq((common.as_expr()*left1).expand(),common.as_expr()*right1)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(3*a**2*b*x**14 + 3*a**2*e*x**11 + 3*a*b**2*x**13 + 6*a*b*d*x**11 + 6*a*b*e*x**10 + 6*a*d*e*x**8 + 3*a*e**2*x**7 + 3*b**2*d*x**10 + 3*b*d**2*x**8 + 6*b*d*e*x**7 + 3*d**2*e*x**5 + 3*d*e**2*x**4, (-c*x**3 - f - x**6)*(3*a*b*x**9 + 3*d*e*x**3 + x**6*(3*a*e + 3*b*d)))"
],
"text/latex": [
"$\\displaystyle 3 a^{2} b x^{14} + 3 a^{2} e x^{11} + 3 a b^{2} x^{13} + 6 a b d x^{11} + 6 a b e x^{10} + 6 a d e x^{8} + 3 a e^{2} x^{7} + 3 b^{2} d x^{10} + 3 b d^{2} x^{8} + 6 b d e x^{7} + 3 d^{2} e x^{5} + 3 d e^{2} x^{4} = \\left(- c x^{3} - f - x^{6}\\right) \\left(3 a b x^{9} + 3 d e x^{3} + x^{6} \\left(3 a e + 3 b d\\right)\\right)$"
]
},
"metadata": {},
"execution_count": 20
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 21,
"source": [
"left2 = left1**RootDegre\r\n",
"right2 = right1**RootDegre\r\n",
"Eq(left2,right2)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq((a*x**5 + b*x**4 + d*x**2 + e*x)**3, (-c*x**3 - f - x**6)**3)"
],
"text/latex": [
"$\\displaystyle \\left(a x^{5} + b x^{4} + d x^{2} + e x\\right)^{3} = \\left(- c x^{3} - f - x^{6}\\right)^{3}$"
]
},
"metadata": {},
"execution_count": 21
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 22,
"source": [
"Eq(left2.expand(),right2)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(a**3*x**15 + 3*a**2*b*x**14 + 3*a**2*d*x**12 + 3*a**2*e*x**11 + 3*a*b**2*x**13 + 6*a*b*d*x**11 + 6*a*b*e*x**10 + 3*a*d**2*x**9 + 6*a*d*e*x**8 + 3*a*e**2*x**7 + b**3*x**12 + 3*b**2*d*x**10 + 3*b**2*e*x**9 + 3*b*d**2*x**8 + 6*b*d*e*x**7 + 3*b*e**2*x**6 + d**3*x**6 + 3*d**2*e*x**5 + 3*d*e**2*x**4 + e**3*x**3, (-c*x**3 - f - x**6)**3)"
],
"text/latex": [
"$\\displaystyle a^{3} x^{15} + 3 a^{2} b x^{14} + 3 a^{2} d x^{12} + 3 a^{2} e x^{11} + 3 a b^{2} x^{13} + 6 a b d x^{11} + 6 a b e x^{10} + 3 a d^{2} x^{9} + 6 a d e x^{8} + 3 a e^{2} x^{7} + b^{3} x^{12} + 3 b^{2} d x^{10} + 3 b^{2} e x^{9} + 3 b d^{2} x^{8} + 6 b d e x^{7} + 3 b e^{2} x^{6} + d^{3} x^{6} + 3 d^{2} e x^{5} + 3 d e^{2} x^{4} + e^{3} x^{3} = \\left(- c x^{3} - f - x^{6}\\right)^{3}$"
]
},
"metadata": {},
"execution_count": 22
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 23,
"source": [
"left2 = left1**RootDegre - common.as_expr()*left1\r\n",
"right2 = right1**RootDegre - common.as_expr()*right1\r\n",
"Eq(left2.expand(),right2)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(a**3*x**15 + 3*a**2*d*x**12 + 3*a*d**2*x**9 + b**3*x**12 + 3*b**2*e*x**9 + 3*b*e**2*x**6 + d**3*x**6 + e**3*x**3, (-c*x**3 - f - x**6)**3 - (-c*x**3 - f - x**6)*(3*a*b*x**9 + 3*d*e*x**3 + x**6*(3*a*e + 3*b*d)))"
],
"text/latex": [
"$\\displaystyle a^{3} x^{15} + 3 a^{2} d x^{12} + 3 a d^{2} x^{9} + b^{3} x^{12} + 3 b^{2} e x^{9} + 3 b e^{2} x^{6} + d^{3} x^{6} + e^{3} x^{3} = \\left(- c x^{3} - f - x^{6}\\right)^{3} - \\left(- c x^{3} - f - x^{6}\\right) \\left(3 a b x^{9} + 3 d e x^{3} + x^{6} \\left(3 a e + 3 b d\\right)\\right)$"
]
},
"metadata": {},
"execution_count": 23
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 24,
"source": [
"P2 = (left2-right2).as_poly(x)\r\n",
"Eq(P2.as_expr(),0)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(f**3 + x**18 + x**15*(a**3 - 3*a*b + 3*c) + x**12*(3*a**2*d - 3*a*b*c - 3*a*e + b**3 - 3*b*d + 3*c**2 + 3*f) + x**9*(-3*a*b*f - 3*a*c*e + 3*a*d**2 + 3*b**2*e - 3*b*c*d + c**3 + 6*c*f - 3*d*e) + x**6*(-3*a*e*f - 3*b*d*f + 3*b*e**2 + 3*c**2*f - 3*c*d*e + d**3 + 3*f**2) + x**3*(3*c*f**2 - 3*d*e*f + e**3), 0)"
],
"text/latex": [
"$\\displaystyle f^{3} + x^{18} + x^{15} \\left(a^{3} - 3 a b + 3 c\\right) + x^{12} \\left(3 a^{2} d - 3 a b c - 3 a e + b^{3} - 3 b d + 3 c^{2} + 3 f\\right) + x^{9} \\left(- 3 a b f - 3 a c e + 3 a d^{2} + 3 b^{2} e - 3 b c d + c^{3} + 6 c f - 3 d e\\right) + x^{6} \\left(- 3 a e f - 3 b d f + 3 b e^{2} + 3 c^{2} f - 3 c d e + d^{3} + 3 f^{2}\\right) + x^{3} \\left(3 c f^{2} - 3 d e f + e^{3}\\right) = 0$"
]
},
"metadata": {},
"execution_count": 24
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 25,
"source": [
"for a,b in zip(Coeffs[::-1],P2.terms()[1:]):\r\n",
" display(Eq(symbols(a.name.upper()),b[1]))"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"Eq(A, a**3 - 3*a*b + 3*c)"
],
"text/latex": [
"$\\displaystyle A = a^{3} - 3 a b + 3 c$"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Eq(B, 3*a**2*d - 3*a*b*c - 3*a*e + b**3 - 3*b*d + 3*c**2 + 3*f)"
],
"text/latex": [
"$\\displaystyle B = 3 a^{2} d - 3 a b c - 3 a e + b^{3} - 3 b d + 3 c^{2} + 3 f$"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Eq(C, -3*a*b*f - 3*a*c*e + 3*a*d**2 + 3*b**2*e - 3*b*c*d + c**3 + 6*c*f - 3*d*e)"
],
"text/latex": [
"$\\displaystyle C = - 3 a b f - 3 a c e + 3 a d^{2} + 3 b^{2} e - 3 b c d + c^{3} + 6 c f - 3 d e$"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Eq(D, -3*a*e*f - 3*b*d*f + 3*b*e**2 + 3*c**2*f - 3*c*d*e + d**3 + 3*f**2)"
],
"text/latex": [
"$\\displaystyle D = - 3 a e f - 3 b d f + 3 b e^{2} + 3 c^{2} f - 3 c d e + d^{3} + 3 f^{2}$"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Eq(E, 3*c*f**2 - 3*d*e*f + e**3)"
],
"text/latex": [
"$\\displaystyle E = 3 c f^{2} - 3 d e f + e^{3}$"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Eq(F, f**3)"
],
"text/latex": [
"$\\displaystyle F = f^{3}$"
]
},
"metadata": {}
}
],
"metadata": {}
}
]
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment