shiroindev/.ipynb_checkpoints/sandbox-checkpoint.ipynb

7075 lines
228 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def extrema(fr):\n",
" t=[]\n",
" for x in fr.free_symbols:\n",
" t+=[fr.diff(x)]\n",
" print(t)\n",
" return solve(t)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from shiroindev import *\n",
"from sympy import *\n",
"from itertools import permutations, combinations\n",
"sVars.seed=1\n",
"from IPython.display import Latex\n",
"sVars.display=lambda x:display(Latex(x))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"numerator: $12a^3-18a^2b+6a^2c+6ab^2-18ac^2+12b^3-18b^2c+6bc^2+12c^3$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"denominator: $4a^2b+2a^2c+2ab^2+9abc+4ac^2+4b^2c+2bc^2$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"status: 0"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"From weighted AM-GM inequality:"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$18a^2b \\le 10a^3+6ab^2+2b^3$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$18ac^2 \\le 2a^3+6a^2c+10c^3$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$18b^2c \\le 10b^3+6bc^2+2c^3$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$ 0 \\le 0 $$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"The sum of all inequalities gives us a proof of the inequality."
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"formula=cyclize('(a+b)/(2*b+c)')-2\n",
"prove(formula*6)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Substitute $a\\to s-s/(a+1)$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $b\\to s-s/(b+1)$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $c\\to s-s/(c+1)$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"numerator: $abcs^2+s^2$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"denominator: $abc+ab+ac+a+bc+b+c+1$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"status: 0"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$ 0 \\le abcs^2+s^2 $$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"The sum of all inequalities gives us a proof of the inequality."
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prove(makesubs(Sm('-a(s-b)-b(s-c)-c(s-a)+ s^2'),'[0,s],[0,s],[0,s]'))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def nonneg(formula):\n",
" formula=expand(formula)\n",
" for addend in formula.as_ordered_terms():\n",
" coef,facts=addend.as_coeff_mul()\n",
" if coef<0:\n",
" return False\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def symprove(formula,n):\n",
" formula=S(formula)\n",
" if n==0:\n",
" return\n",
" ls=list(formula.free_symbols)\n",
" for i in range(len(ls)):\n",
" a=ls[i]\n",
" for j in range(i+1,len(ls)):\n",
" b=ls[j]\n",
" if expand(formula-formula.subs({a:b, b:a}, simultaneous=True))==S(0):\n",
" formula=makesubs(formula,[[b,S('oo')]],variables=[a,b])\n",
" sVars.display('$$'+latex(formula)+'$$')\n",
" symprove(formula,n-1)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Substitute $y\\to x+y$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$y^{2}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"symprove('x^2-2*x*y+y^2',4)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def provesym(formula,n):\n",
" formula=S(formula)\n",
" if n==0:\n",
" return\n",
" fs=list(formula.free_symbols)\n",
" print \n",
" for i in range(2,len(fs)+1):\n",
" for fs2 in combinations(fs,i):\n",
" for fsp in permutations(fs2[1:]):\n",
" if expand(formula-formula.subs(zip((fs2[0],)+fsp,fsp+(fs2[0],)), simultaneous=True))==S(0):\n",
" newformula=makesubs(formula,[[fs2[0],oo]]*(len(fsp)),variables=fsp)\n",
" sVars.display(str(n)+' $$'+latex(newformula)+'$$')\n",
" provesym(newformula,n-1)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Substitute $x\\to x+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"4 $$x^{2} \\left(x + z\\right)^{t} - x y y^{t} + x y z^{t} - x y \\left(x + z\\right)^{t} + x y^{t} z - x z z^{t} + x z \\left(x + z\\right)^{t} + y^{2} y^{t} - 2 y y^{t} z + y^{t} z^{2}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to y+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"4 $$x^{2} x^{t} - x x^{t} y - 2 x x^{t} z + x y z^{t} - x y \\left(y + z\\right)^{t} + x^{t} y z + x^{t} z^{2} + y^{2} \\left(y + z\\right)^{t} - y z z^{t} + y z \\left(y + z\\right)^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to x+y$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"4 $$x^{2} z^{t} - x x^{t} y + x y z^{t} + x y \\left(x + y\\right)^{t} - 2 x z z^{t} + x^{t} y z + y^{2} \\left(x + y\\right)^{t} - y z z^{t} - y z \\left(x + y\\right)^{t} + z^{2} z^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $x\\to x+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to y+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"4 $$x^{2} \\left(x + z\\right)^{t} + x y z^{t} - x y \\left(x + z\\right)^{t} - x y \\left(y + z\\right)^{t} + y^{2} \\left(y + z\\right)^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to x+y$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"3 $$x^{2} z^{t} + x y z^{t} - x y \\left(x + z\\right)^{t} + x y \\left(x + y + z\\right)^{t} + y^{2} \\left(x + y + z\\right)^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to y+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $x\\to x+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"4 $$x^{2} \\left(x + z\\right)^{t} + x y z^{t} - x y \\left(x + z\\right)^{t} - x y \\left(y + z\\right)^{t} + y^{2} \\left(y + z\\right)^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to x+y$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"3 $$x^{2} z^{t} + x y z^{t} - x y \\left(x + z\\right)^{t} + x y \\left(x + y + z\\right)^{t} + y^{2} \\left(x + y + z\\right)^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"provesym(Sm('x^t(x-y)(x-z) + y^t (y-z)(y-x) + z^t (z-x)(z-y)'),4)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Substitute $x\\to x+y$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle x^{2} \\left(x + y\\right)^{t} - x y y^{t} + x y z^{t} + x y \\left(x + y\\right)^{t} + x y^{t} z - x z z^{t} - x z \\left(x + y\\right)^{t} + y^{2} z^{t} - 2 y z z^{t} + z^{2} z^{t}$"
],
"text/plain": [
"x**2*(x + y)**t - x*y*y**t + x*y*z**t + x*y*(x + y)**t + x*y**t*z - x*z*z**t - x*z*(x + y)**t + y**2*z**t - 2*y*z*z**t + z**2*z**t"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"makesubs(Sm('x^t(x-y)(x-z) + y^t (y-z)(y-x) + z^t (z-x)(z-y)'),'[y,oo]',variables='x')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Substitute $x\\to x+y$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle x^{2} \\left(x + y\\right)^{t} - x y y^{t} + x y z^{t} + x y \\left(x + y\\right)^{t} + x y^{t} z - x z z^{t} - x z \\left(x + y\\right)^{t} + y^{2} z^{t} - 2 y z z^{t} + z^{2} z^{t}$"
],
"text/plain": [
"x**2*(x + y)**t - x*y*y**t + x*y*z**t + x*y*(x + y)**t + x*y**t*z - x*z*z**t - x*z*(x + y)**t + y**2*z**t - 2*y*z*z**t + z**2*z**t"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"makesubs(Sm('x^t*(x-y)(x-z) + y^t*(y-z)(y-x) + z^t*(z-x)(z-y)'),'[y,oo]',variables='x')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Substitute $x\\to x+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"4 $$x^{2} \\left(x + z\\right)^{t} - x y y^{t} + x y z^{t} - x y \\left(x + z\\right)^{t} + x y^{t} z - x z z^{t} + x z \\left(x + z\\right)^{t} + y^{2} y^{t} - 2 y y^{t} z + y^{t} z^{2}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to y+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"4 $$x^{2} x^{t} - x x^{t} y - 2 x x^{t} z + x y z^{t} - x y \\left(y + z\\right)^{t} + x^{t} y z + x^{t} z^{2} + y^{2} \\left(y + z\\right)^{t} - y z z^{t} + y z \\left(y + z\\right)^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to x+y$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"4 $$x^{2} z^{t} - x x^{t} y + x y z^{t} + x y \\left(x + y\\right)^{t} - 2 x z z^{t} + x^{t} y z + y^{2} \\left(x + y\\right)^{t} - y z z^{t} - y z \\left(x + y\\right)^{t} + z^{2} z^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $x\\to x+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to y+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"4 $$x^{2} \\left(x + z\\right)^{t} + x y z^{t} - x y \\left(x + z\\right)^{t} - x y \\left(y + z\\right)^{t} + y^{2} \\left(y + z\\right)^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to x+y$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"3 $$x^{2} z^{t} + x y z^{t} - x y \\left(x + z\\right)^{t} + x y \\left(x + y + z\\right)^{t} + y^{2} \\left(x + y + z\\right)^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to y+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $x\\to x+z$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"4 $$x^{2} \\left(x + z\\right)^{t} + x y z^{t} - x y \\left(x + z\\right)^{t} - x y \\left(y + z\\right)^{t} + y^{2} \\left(y + z\\right)^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $y\\to x+y$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"3 $$x^{2} z^{t} + x y z^{t} - x y \\left(x + z\\right)^{t} + x y \\left(x + y + z\\right)^{t} + y^{2} \\left(x + y + z\\right)^{t}$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"provesym(Sm('x^t(x-y)(x-z) + y^t (y-z)(y-x) + z^t (z-x)(z-y)'),4)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle x^{t} \\left(x - y\\right) \\left(x - z\\right) + y^{t} \\left(- x + y\\right) \\left(y - z\\right) + z^{t} \\left(- x + z\\right) \\left(- y + z\\right)$"
],
"text/plain": [
"x**t*(x - y)*(x - z) + y**t*(-x + y)*(y - z) + z**t*(-x + z)*(-y + z)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Sm('x^t(x-y)(x-z) + y^t (y-z)(y-x) + z^t (z-x)(z-y)')"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle 3 x + y + 2 z$"
],
"text/plain": [
"3*x + y + 2*z"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"S('x+2*y+3*z').subs(zip(S('[x,y,z]'),S('[y,z,x]')),simultaneous=True)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[x, y, z]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"S('[x,y,z]')"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle - t x y^{2} + t x z^{2} - x^{2} \\left(x + y\\right)^{t} + x^{2} \\left(x + z\\right)^{t} + x y y^{t} - x y \\left(x + y\\right)^{t} - x y \\left(x + z\\right)^{t} - x z z^{t} + x z \\left(x + y\\right)^{t} + x z \\left(x + z\\right)^{t} + y^{2} y^{t} - y^{2} z^{t} - 2 y y^{t} z + 2 y z z^{t} + y^{t} z^{2} - z^{2} z^{t}$"
],
"text/plain": [
"-t*x*y**2 + t*x*z**2 - x**2*(x + y)**t + x**2*(x + z)**t + x*y*y**t - x*y*(x + y)**t - x*y*(x + z)**t - x*z*z**t + x*z*(x + y)**t + x*z*(x + z)**t + y**2*y**t - y**2*z**t - 2*y*y**t*z + 2*y*z*z**t + y**t*z**2 - z**2*z**t"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"formula=Sm('x^2(x+y)^t-xyy^t+xyz^t+xy(x+y)^t+xy^tz-xzzt-xz(x+y)^t+y^2z^t-2yzz^t+z^2z^t')\n",
"expand(formula.subs(S('[y,z],[z,y]'),simultaneous=True)-formula)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def point(formula):\n",
" fs=list(formula.free_symbols)\n",
" il=1\n",
" for s in fs[1:]:\n",
" il*=s\n",
" fr=formula.subs(fs[0],1/il)\n",
" print(fr)\n",
" return(extrema(fr))\n",
"#point(Sm('(a^2+b^2+c^2+d^2)-a*(b+c+d)'))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Substitute $a\\to 11a/8$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $b\\to 4b/5$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $c\\to 4c/5$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $d\\to 4d/5$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"numerator: $3025a^2-1760ab-1760ac-1760ad+1024b^2+1024c^2+1024d^2$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"denominator: $1600$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"status: 0"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"From weighted AM-GM inequality:"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$1760ab \\le 880a^2+880b^2$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$1760ac \\le 880a^2+880c^2$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$1760ad \\le 880a^2+880d^2$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$ 0 \\le 385a^2+144b^2+144c^2+144d^2 $$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"The sum of all inequalities gives us a proof of the inequality."
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prove(Sm('(a^2+b^2+c^2+d^2)-a*(b+c+d)'),'11/8,4/5,4/5,4/5')"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.06133333333333324"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"216/125-5/3"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle - \\frac{2 \\sqrt{2} \\left(a - b\\right)}{c} + \\frac{\\left(a - b\\right)^{2}}{c^{2}} - \\frac{2 \\sqrt{2} \\left(- a + c\\right)}{b} + \\frac{\\left(- a + c\\right)^{2}}{b^{2}} - \\frac{2 \\sqrt{2} \\left(b - c\\right)}{a} + \\frac{\\left(b - c\\right)^{2}}{a^{2}}$"
],
"text/plain": [
"-2*sqrt(2)*(a - b)/c + (a - b)**2/c**2 - 2*sqrt(2)*(-a + c)/b + (-a + c)**2/b**2 - 2*sqrt(2)*(b - c)/a + (b - c)**2/a**2"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $a\\to a+b$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $b\\to b+c$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimization terminated successfully.\n",
" Current function value: 0.000000\n",
" Iterations: 51\n",
" Function evaluations: 97\n",
"[1.17092486 1.170961 1.17093843]\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{a^{4} b^{2} + 2 a^{4} b c + 2 a^{4} c^{2} + 2 a^{3} b^{3} - 2 \\sqrt{2} a^{3} b^{2} c + 6 a^{3} b^{2} c - 2 \\sqrt{2} a^{3} b c^{2} + 10 a^{3} b c^{2} + 4 a^{3} c^{3} + a^{2} b^{4} - 4 \\sqrt{2} a^{2} b^{3} c + 4 a^{2} b^{3} c - 6 \\sqrt{2} a^{2} b^{2} c^{2} + 12 a^{2} b^{2} c^{2} - 2 \\sqrt{2} a^{2} b c^{3} + 10 a^{2} b c^{3} + 2 a^{2} c^{4} - 2 \\sqrt{2} a b^{4} c - 4 \\sqrt{2} a b^{3} c^{2} + 4 a b^{3} c^{2} - 2 \\sqrt{2} a b^{2} c^{3} + 6 a b^{2} c^{3} + 2 a b c^{4} + 2 b^{4} c^{2} + 4 b^{3} c^{3} + 2 b^{2} c^{4}}{c^{2} \\left(a^{2} b^{2} + 2 a^{2} b c + a^{2} c^{2} + 2 a b^{3} + 6 a b^{2} c + 6 a b c^{2} + 2 a c^{3} + b^{4} + 4 b^{3} c + 6 b^{2} c^{2} + 4 b c^{3} + c^{4}\\right)}$"
],
"text/plain": [
"(a**4*b**2 + 2*a**4*b*c + 2*a**4*c**2 + 2*a**3*b**3 - 2*sqrt(2)*a**3*b**2*c + 6*a**3*b**2*c - 2*sqrt(2)*a**3*b*c**2 + 10*a**3*b*c**2 + 4*a**3*c**3 + a**2*b**4 - 4*sqrt(2)*a**2*b**3*c + 4*a**2*b**3*c - 6*sqrt(2)*a**2*b**2*c**2 + 12*a**2*b**2*c**2 - 2*sqrt(2)*a**2*b*c**3 + 10*a**2*b*c**3 + 2*a**2*c**4 - 2*sqrt(2)*a*b**4*c - 4*sqrt(2)*a*b**3*c**2 + 4*a*b**3*c**2 - 2*sqrt(2)*a*b**2*c**3 + 6*a*b**2*c**3 + 2*a*b*c**4 + 2*b**4*c**2 + 4*b**3*c**3 + 2*b**2*c**4)/(c**2*(a**2*b**2 + 2*a**2*b*c + a**2*c**2 + 2*a*b**3 + 6*a*b**2*c + 6*a*b*c**2 + 2*a*c**3 + b**4 + 4*b**3*c + 6*b**2*c**2 + 4*b*c**3 + c**4))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"formula=cyclize(Sm('((a-b)/c)^2-8**(1/2)*(a-b)/c'))\n",
"display(formula)\n",
"from scipy.optimize import fmin\n",
"import numpy as np\n",
"def f(x):\n",
" num,den=fraction(cancel(formula))\n",
" fs=sorted(newformula.free_symbols,key=str)\n",
" return num.subs(zip(fs,x))\n",
"newformula=(makesubs(formula,'[b,oo],[c,oo]'))\n",
"print(fmin(f,np.array([2,1,1])))\n",
"display(simplify(newformula))\n",
"#prove(newformula)\n",
"#prove(makesubs(formula,'[b,oo],[a,oo]',variables='c,b,a'))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Warning: Maximum number of function evaluations has been exceeded.\n"
]
},
{
"data": {
"text/plain": [
"array([-1.62831265e+41, -1.93382892e+41])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def g(x):\n",
" return x[0]+x[1]\n",
"fmin(g,[0,0])"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{a^{2} + b^{2} + c^{2} + d^{2}}{a \\left(b + c + d\\right)}$"
],
"text/plain": [
"(a**2 + b**2 + c**2 + d**2)/(a*(b + c + d))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimization terminated successfully.\n",
" Current function value: 1.154701\n",
" Iterations: 80\n",
" Function evaluations: 144\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle \\left( \\frac{11}{8}, \\ \\frac{4}{5}, \\ \\frac{4}{5}, \\ \\frac{4}{5}\\right)$"
],
"text/plain": [
"(11/8, 4/5, 4/5, 4/5)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"formula=Sm('(a^2+b^2+c^2+d^2)/(a*(b+c+d))')\n",
"display(formula)\n",
"def f(x):\n",
" fs=sorted(formula.free_symbols,key=str)\n",
" return formula.subs(zip(fs,x))\n",
"nsimplify(tuple(fmin(f,(1,1,1,1))),tolerance=0.1,rational=True)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{55}{32}$"
],
"text/plain": [
"55/32"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"_[0]/_[1]"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Substitute $a\\to a+b$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $b\\to b+c$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle a^{8} b^{4} + 2 a^{8} b^{2} c^{2} + 2 a^{8} c^{4} + 2 a^{6} b^{6} - 2 \\sqrt{2} a^{6} b^{4} c^{2} + 6 a^{6} b^{4} c^{2} - 2 \\sqrt{2} a^{6} b^{2} c^{4} + 10 a^{6} b^{2} c^{4} + 4 a^{6} c^{6} + a^{4} b^{8} - 4 \\sqrt{2} a^{4} b^{6} c^{2} + 4 a^{4} b^{6} c^{2} - 6 \\sqrt{2} a^{4} b^{4} c^{4} + 12 a^{4} b^{4} c^{4} - 2 \\sqrt{2} a^{4} b^{2} c^{6} + 10 a^{4} b^{2} c^{6} + 2 a^{4} c^{8} - 2 \\sqrt{2} a^{2} b^{8} c^{2} - 4 \\sqrt{2} a^{2} b^{6} c^{4} + 4 a^{2} b^{6} c^{4} - 2 \\sqrt{2} a^{2} b^{4} c^{6} + 6 a^{2} b^{4} c^{6} + 2 a^{2} b^{2} c^{8} + 2 b^{8} c^{4} + 4 b^{6} c^{6} + 2 b^{4} c^{8}$"
],
"text/plain": [
"a**8*b**4 + 2*a**8*b**2*c**2 + 2*a**8*c**4 + 2*a**6*b**6 - 2*sqrt(2)*a**6*b**4*c**2 + 6*a**6*b**4*c**2 - 2*sqrt(2)*a**6*b**2*c**4 + 10*a**6*b**2*c**4 + 4*a**6*c**6 + a**4*b**8 - 4*sqrt(2)*a**4*b**6*c**2 + 4*a**4*b**6*c**2 - 6*sqrt(2)*a**4*b**4*c**4 + 12*a**4*b**4*c**4 - 2*sqrt(2)*a**4*b**2*c**6 + 10*a**4*b**2*c**6 + 2*a**4*c**8 - 2*sqrt(2)*a**2*b**8*c**2 - 4*sqrt(2)*a**2*b**6*c**4 + 4*a**2*b**6*c**4 - 2*sqrt(2)*a**2*b**4*c**6 + 6*a**2*b**4*c**6 + 2*a**2*b**2*c**8 + 2*b**8*c**4 + 4*b**6*c**6 + 2*b**4*c**8"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{a: 2, b: 2, c: 2}\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{19 \\sqrt{2}}{12}$"
],
"text/plain": [
"19*sqrt(2)/12"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"x\n",
"Optimization terminated successfully.\n",
" Current function value: 1.000000\n",
" Iterations: 147\n",
" Function evaluations: 267\n"
]
},
{
"data": {
"text/plain": [
"(4.4110810020775736e-12, 28.52020821072961, 3.1191098416324057e-12)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle \\left( 0, \\ \\frac{57}{2}, \\ 0\\right)$"
],
"text/plain": [
"(0, 57/2, 0)"
]
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"formula=cyclize(Sm('((a-b)/c)^2-8**(1/2)*(a-b)/c'))\n",
"formula=(makesubs(formula,'[b,oo],[c,oo]'))\n",
"num,den=fraction(cancel(formula))\n",
"num=num.subs(zip(fs,list(map(lambda x:x**2,fs))))\n",
"display(num)\n",
"numm=0\n",
"nump=0\n",
"for addend in num.as_ordered_terms():\n",
" coef,facts=addend.as_coeff_mul()\n",
" if coef<0:\n",
" numm-=addend\n",
" else:\n",
" nump+=addend\n",
"num=nump/numm\n",
"fs=sorted(num.free_symbols,key=str)\n",
"numm,nump=Poly(numm),Poly(nump)\n",
"def f(x):\n",
" return nump.eval(dict(zip(fs,x)))/numm.eval(dict(zip(fs,x))) \n",
"print(dict(zip(fs,(2,2,2))))\n",
"display(f((2,2,2)))\n",
"print('x')\n",
"tup=tuple(fmin(f,(2,2,2)))\n",
"display(tuple([x*x for x in tup]))\n",
"nsimplify(tuple([x*x for x in tup]),tolerance=0.1,rational=True)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left( 2, \\ \\frac{31}{3}, \\ \\frac{7}{4}\\right)$"
],
"text/plain": [
"(2, 31/3, 7/4)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nsimplify((2.1002573656763053, 10.340431462974655, 1.7661001788212371),tolerance=0.3,rational=True)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.414211498165453"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(2.1002573656763053/1.7661001788212371)**2"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Case $a\\ge c\\ge b$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $a\\to a+c$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $c\\to b+c$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"numerator: $2a^4b^2+2a^4bc+a^4c^2+4a^3b^3+2\\sqrt{2}a^3b^2c+10a^3b^2c+2\\sqrt{2}a^3bc^2+6a^3bc^2+2a^3c^3+2a^2b^4+2\\sqrt{2}a^2b^3c+10a^2b^3c+6\\sqrt{2}a^2b^2c^2+12a^2b^2c^2+4a^2bc^3+4\\sqrt{2}a^2bc^3+a^2c^4+2ab^4c+2\\sqrt{2}ab^3c^2+6ab^3c^2+4ab^2c^3+4\\sqrt{2}ab^2c^3+2\\sqrt{2}abc^4+2b^4c^2+4b^3c^3+2b^2c^4$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"denominator: $a^2b^4+2a^2b^3c+a^2b^2c^2+2ab^5+6ab^4c+6ab^3c^2+2ab^2c^3+b^6+4b^5c+6b^4c^2+4b^3c^3+b^2c^4$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"status: 0"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$ 0 \\le 2a^4b^2+2a^4bc+a^4c^2+4a^3b^3+2\\sqrt{2}a^3b^2c+10a^3b^2c+2\\sqrt{2}a^3bc^2+6a^3bc^2+2a^3c^3+2a^2b^4+2\\sqrt{2}a^2b^3c+10a^2b^3c+6\\sqrt{2}a^2b^2c^2+12a^2b^2c^2+4\\sqrt{2}a^2bc^3+4a^2bc^3+a^2c^4+2ab^4c+2\\sqrt{2}ab^3c^2+6ab^3c^2+4\\sqrt{2}ab^2c^3+4ab^2c^3+2\\sqrt{2}abc^4+2b^4c^2+4b^3c^3+2b^2c^4 $$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"The sum of all inequalities gives us a proof of the inequality."
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Case $a\\ge b\\ge c$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $a\\to a+b$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $b\\to b+c$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $a\\to \\sqrt{2}a$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"numerator: $16a^4b^2+32a^4bc+32a^4c^2+16\\sqrt{2}a^3b^3-32a^3b^2c+48\\sqrt{2}a^3b^2c-32a^3bc^2+80\\sqrt{2}a^3bc^2+32\\sqrt{2}a^3c^3+8a^2b^4-32\\sqrt{2}a^2b^3c+32a^2b^3c-48\\sqrt{2}a^2b^2c^2+96a^2b^2c^2-16\\sqrt{2}a^2bc^3+80a^2bc^3+16a^2c^4-16ab^4c-32ab^3c^2+16\\sqrt{2}ab^3c^2-16ab^2c^3+24\\sqrt{2}ab^2c^3+8\\sqrt{2}abc^4+8b^4c^2+16b^3c^3+8b^2c^4$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"denominator: $2a^2b^2c^2+4a^2bc^3+2a^2c^4+2\\sqrt{2}ab^3c^2+6\\sqrt{2}ab^2c^3+6\\sqrt{2}abc^4+2\\sqrt{2}ac^5+b^4c^2+4b^3c^3+6b^2c^4+4bc^5+c^6$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"status: 0"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"From weighted AM-GM inequality:"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$32a^3b^2c \\le 16a^4b^2+16a^2b^2c^2$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$32\\sqrt{2}a^2b^3c \\le 16\\sqrt{2}a^3b^3+16\\sqrt{2}ab^3c^2$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$48\\sqrt{2}a^2b^2c^2 \\le 24\\sqrt{2}a^3b^2c+24\\sqrt{2}ab^2c^3$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$16\\sqrt{2}a^2bc^3 \\le 8\\sqrt{2}a^3bc^2+8\\sqrt{2}abc^4$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$16ab^4c \\le 8a^2b^4+8b^4c^2$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$32ab^3c^2 \\le 16a^2b^3c+16b^3c^3$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$32a^3bc^2 \\le 16a^4bc+16a^2bc^3$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$16ab^2c^3 \\le 8a^2b^2c^2+8b^2c^4$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$ 0 \\le 16a^4bc+32a^4c^2+24\\sqrt{2}a^3b^2c+72\\sqrt{2}a^3bc^2+32\\sqrt{2}a^3c^3+16a^2b^3c+72a^2b^2c^2+64a^2bc^3+16a^2c^4 $$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"The sum of all inequalities gives us a proof of the inequality."
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"formula=cyclize(Sm('((a-b)/c)^2-8**(1/2)*(a-b)/c'))\n",
"display(Latex('Case $a\\ge c\\ge b$'))\n",
"formula1=makesubs(formula,'[c,oo],[b,oo]',variables='a,c,b')\n",
"prove(formula1)\n",
"display(Latex('Case $a\\ge b\\ge c$'))\n",
"formula2=makesubs(formula,'[b,oo],[c,oo]')\n",
"prove(formula2*4,values='2**(1/2),1,1')"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Substitute $a\\to a+b$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $b\\to b+c$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $a\\to\\sqrt{2}a$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"numerator: $4a^4b^2+8a^4bc+8a^4c^2+4\\sqrt{2}a^3b^3-8a^3b^2c+12\\sqrt{2}a^3b^2c-8a^3bc^2+20\\sqrt{2}a^3bc^2+8\\sqrt{2}a^3c^3+2a^2b^4-8\\sqrt{2}a^2b^3c+8a^2b^3c-12\\sqrt{2}a^2b^2c^2+24a^2b^2c^2-4\\sqrt{2}a^2bc^3+20a^2bc^3+4a^2c^4-4ab^4c-8ab^3c^2+4\\sqrt{2}ab^3c^2-4ab^2c^3+6\\sqrt{2}ab^2c^3+2\\sqrt{2}abc^4+2b^4c^2+4b^3c^3+2b^2c^4$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"denominator: $1$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"status: 0"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"From weighted AM-GM inequality:"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$8a^3b^2c \\le 4a^4b^2+4a^2b^2c^2$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$8\\sqrt{2}a^2b^3c \\le 4\\sqrt{2}a^3b^3+4\\sqrt{2}ab^3c^2$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$12\\sqrt{2}a^2b^2c^2 \\le 6\\sqrt{2}a^3b^2c+6\\sqrt{2}ab^2c^3$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$4\\sqrt{2}a^2bc^3 \\le 2\\sqrt{2}a^3bc^2+2\\sqrt{2}abc^4$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$4ab^4c \\le 2a^2b^4+2b^4c^2$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$8ab^3c^2 \\le 4a^2b^3c+4b^3c^3$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$8a^3bc^2 \\le 4a^4bc+4a^2bc^3$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$4ab^2c^3 \\le 2a^2b^2c^2+2b^2c^4$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$ 0 \\le 4a^4bc+8a^4c^2+6\\sqrt{2}a^3b^2c+18\\sqrt{2}a^3bc^2+8\\sqrt{2}a^3c^3+4a^2b^3c+18a^2b^2c^2+16a^2bc^3+4a^2c^4 $$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"The sum of all inequalities gives us a proof of the inequality."
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"formula=cyclize(Sm('((a-b)/c)^2-8**(1/2)*(a-b)/c'))\n",
"formula=(makesubs(formula,'[b,oo],[c,oo]'))\n",
"formula,_=fraction(cancel(formula))\n",
"formula=formula.subs('a','2**(1/2)*a')\n",
"sVars.display(r'Substitute $a\\to\\sqrt{2}a$')\n",
"prove(formula)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, (x, y, sqrt(2)))"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"S('2**(1/2)*x*y').as_coeff_mul()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, (x, y, sqrt(2)))"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"S('sqrt(2)*x*y').as_coeff_mul()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 1, 1), (0, 0, 0)]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly('sqrt(2)*x*y+8').monoms()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 58]"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly('sqrt(2)*x*y+58').coeffs()"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(x, y, sqrt(2), sqrt(3), sqrt(6))"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly('sqrt(2)*x*y+sqrt(3)+sqrt(2)*sqrt(3)').gens"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([0], [(0, 0)], [1, 7], [(2, 0), (0, 1)], (x, y))"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def _formula2list(formula):\n",
" neg=pos=0\n",
" for addend in formula.as_ordered_terms():\n",
" coef,facts=addend.as_coeff_mul()\n",
" if coef<0:\n",
" neg-=addend\n",
" else:\n",
" pos+=addend\n",
" neg=Poly(neg,gens=Poly(formula).gens)\n",
" pos=Poly(pos,gens=Poly(formula).gens)\n",
" return neg.coeffs(),neg.monoms(),pos.coeffs(),pos.monoms(),Poly(formula).gens\n",
"_formula2list(S('x^2+7*y'))"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"numerator: $\\sqrt{2}x^2-2\\sqrt{2}xy+\\sqrt{2}y^2$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"denominator: $1$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"status: 0"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"From weighted AM-GM inequality:"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$2\\sqrt{2}xy \\le \\sqrt{2}x^2+\\sqrt{2}y^2$$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$$ 0 \\le 0 $$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"The sum of all inequalities gives us a proof of the inequality."
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prove(S('sqrt(2)*x^2-sqrt(8)*x*y+sqrt(2)*y^2'))"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[(1, 0), (0, 2), (0, 1), (0, 0)], [1, 1, 1, 1]]"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[Poly('1+2**(1/3)+4**(1/3)+x').monoms(),Poly('1+2**(1/3)+4**(1/3)+x').coeffs()]"
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\operatorname{Poly}{\\left( x + \\sqrt{x}, x, \\sqrt{x}, domain=\\mathbb{Z} \\right)}$"
],
"text/plain": [
"Poly(x + (sqrt(x)), x, sqrt(x), domain='ZZ')"
]
},
"execution_count": 168,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=Symbol('x', positive=True)\n",
"Poly(x+sqrt(x))"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"prove?"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\operatorname{Poly}{\\left( x^{2} + 1, x, domain=\\mathbb{Z} \\right)}$"
],
"text/plain": [
"Poly(x**2 + 1, x, domain='ZZ')"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly('x^2-1').abs()"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\operatorname{Poly}{\\left( x\\frac{1}{x + y}, x, \\frac{1}{x + y}, domain=\\mathbb{Z} \\right)}$"
],
"text/plain": [
"Poly(x*(1/(x + y)), x, 1/(x + y), domain='ZZ')"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly(Poly('x')/Poly('x+y'))"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\operatorname{Poly}{\\left( x + \\sqrt{x} + x^{\\frac{2}{3}} + \\sqrt[3]{x} + \\sqrt[4]{x} + \\sqrt{2}, x, \\sqrt{x}, \\sqrt[3]{x}, \\sqrt[4]{x}, domain=QQ<sqrt(2)> \\right)}$"
],
"text/plain": [
"Poly(x + (sqrt(x)) + (x**(1/3))**2 + (x**(1/3)) + (x**(1/4)) + sqrt(2), x, sqrt(x), x**(1/3), x**(1/4), domain='QQ<sqrt(2)>')"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly('sqrt(2)+x+sqrt(x)+x**(1/4)+x**(1/3)+x**(2/3)',extension=1)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, sqrt(6)]"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly('x+sqrt(6)',S('x'),S('sqrt(2)'),S('sqrt(3)')).coeffs()"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, x]"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly('x+sqrt(x)',S('sqrt(x)')).coeffs()"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"In file: /home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/sympy/polys/polytools.py\n",
"class Poly(Expr):\n",
" \"\"\"\n",
" Generic class for representing and operating on polynomial expressions.\n",
" Subclasses Expr class.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" Create a univariate polynomial:\n",
"\n",
" >>> Poly(x*(x**2 + x - 1)**2)\n",
" Poly(x**5 + 2*x**4 - x**3 - 2*x**2 + x, x, domain='ZZ')\n",
"\n",
" Create a univariate polynomial with specific domain:\n",
"\n",
" >>> from sympy import sqrt\n",
" >>> Poly(x**2 + 2*x + sqrt(3), domain='R')\n",
" Poly(1.0*x**2 + 2.0*x + 1.73205080756888, x, domain='RR')\n",
"\n",
" Create a multivariate polynomial:\n",
"\n",
" >>> Poly(y*x**2 + x*y + 1)\n",
" Poly(x**2*y + x*y + 1, x, y, domain='ZZ')\n",
"\n",
" Create a univariate polynomial, where y is a constant:\n",
"\n",
" >>> Poly(y*x**2 + x*y + 1,x)\n",
" Poly(y*x**2 + y*x + 1, x, domain='ZZ[y]')\n",
"\n",
" You can evaluate the above polynomial as a function of y:\n",
"\n",
" >>> Poly(y*x**2 + x*y + 1,x).eval(2)\n",
" 6*y + 1\n",
"\n",
" See Also\n",
" ========\n",
"\n",
" sympy.core.expr.Expr\n",
"\n",
" \"\"\"\n",
"\n",
" __slots__ = ['rep', 'gens']\n",
"\n",
" is_commutative = True\n",
" is_Poly = True\n",
" _op_priority = 10.001\n",
"\n",
" def __new__(cls, rep, *gens, **args):\n",
" \"\"\"Create a new polynomial instance out of something useful. \"\"\"\n",
" opt = options.build_options(gens, args)\n",
"\n",
" if 'order' in opt:\n",
" raise NotImplementedError(\"'order' keyword is not implemented yet\")\n",
"\n",
" if iterable(rep, exclude=str):\n",
" if isinstance(rep, dict):\n",
" return cls._from_dict(rep, opt)\n",
" else:\n",
" return cls._from_list(list(rep), opt)\n",
" else:\n",
" rep = sympify(rep)\n",
"\n",
" if rep.is_Poly:\n",
" return cls._from_poly(rep, opt)\n",
" else:\n",
" return cls._from_expr(rep, opt)\n",
"\n",
" @classmethod\n",
" def new(cls, rep, *gens):\n",
" \"\"\"Construct :class:`Poly` instance from raw representation. \"\"\"\n",
" if not isinstance(rep, DMP):\n",
" raise PolynomialError(\n",
" \"invalid polynomial representation: %s\" % rep)\n",
" elif rep.lev != len(gens) - 1:\n",
" raise PolynomialError(\"invalid arguments: %s, %s\" % (rep, gens))\n",
"\n",
" obj = Basic.__new__(cls)\n",
"\n",
" obj.rep = rep\n",
" obj.gens = gens\n",
"\n",
" return obj\n",
"\n",
" @classmethod\n",
" def from_dict(cls, rep, *gens, **args):\n",
" \"\"\"Construct a polynomial from a ``dict``. \"\"\"\n",
" opt = options.build_options(gens, args)\n",
" return cls._from_dict(rep, opt)\n",
"\n",
" @classmethod\n",
" def from_list(cls, rep, *gens, **args):\n",
" \"\"\"Construct a polynomial from a ``list``. \"\"\"\n",
" opt = options.build_options(gens, args)\n",
" return cls._from_list(rep, opt)\n",
"\n",
" @classmethod\n",
" def from_poly(cls, rep, *gens, **args):\n",
" \"\"\"Construct a polynomial from a polynomial. \"\"\"\n",
" opt = options.build_options(gens, args)\n",
" return cls._from_poly(rep, opt)\n",
"\n",
" @classmethod\n",
" def from_expr(cls, rep, *gens, **args):\n",
" \"\"\"Construct a polynomial from an expression. \"\"\"\n",
" opt = options.build_options(gens, args)\n",
" return cls._from_expr(rep, opt)\n",
"\n",
" @classmethod\n",
" def _from_dict(cls, rep, opt):\n",
" \"\"\"Construct a polynomial from a ``dict``. \"\"\"\n",
" gens = opt.gens\n",
"\n",
" if not gens:\n",
" raise GeneratorsNeeded(\n",
" \"can't initialize from 'dict' without generators\")\n",
"\n",
" level = len(gens) - 1\n",
" domain = opt.domain\n",
"\n",
" if domain is None:\n",
" domain, rep = construct_domain(rep, opt=opt)\n",
" else:\n",
" for monom, coeff in rep.items():\n",
" rep[monom] = domain.convert(coeff)\n",
"\n",
" return cls.new(DMP.from_dict(rep, level, domain), *gens)\n",
"\n",
" @classmethod\n",
" def _from_list(cls, rep, opt):\n",
" \"\"\"Construct a polynomial from a ``list``. \"\"\"\n",
" gens = opt.gens\n",
"\n",
" if not gens:\n",
" raise GeneratorsNeeded(\n",
" \"can't initialize from 'list' without generators\")\n",
" elif len(gens) != 1:\n",
" raise MultivariatePolynomialError(\n",
" \"'list' representation not supported\")\n",
"\n",
" level = len(gens) - 1\n",
" domain = opt.domain\n",
"\n",
" if domain is None:\n",
" domain, rep = construct_domain(rep, opt=opt)\n",
" else:\n",
" rep = list(map(domain.convert, rep))\n",
"\n",
" return cls.new(DMP.from_list(rep, level, domain), *gens)\n",
"\n",
" @classmethod\n",
" def _from_poly(cls, rep, opt):\n",
" \"\"\"Construct a polynomial from a polynomial. \"\"\"\n",
" if cls != rep.__class__:\n",
" rep = cls.new(rep.rep, *rep.gens)\n",
"\n",
" gens = opt.gens\n",
" field = opt.field\n",
" domain = opt.domain\n",
"\n",
" if gens and rep.gens != gens:\n",
" if set(rep.gens) != set(gens):\n",
" return cls._from_expr(rep.as_expr(), opt)\n",
" else:\n",
" rep = rep.reorder(*gens)\n",
"\n",
" if 'domain' in opt and domain:\n",
" rep = rep.set_domain(domain)\n",
" elif field is True:\n",
" rep = rep.to_field()\n",
"\n",
" return rep\n",
"\n",
" @classmethod\n",
" def _from_expr(cls, rep, opt):\n",
" \"\"\"Construct a polynomial from an expression. \"\"\"\n",
" rep, opt = _dict_from_expr(rep, opt)\n",
" return cls._from_dict(rep, opt)\n",
"\n",
" def _hashable_content(self):\n",
" \"\"\"Allow SymPy to hash Poly instances. \"\"\"\n",
" return (self.rep, self.gens)\n",
"\n",
" def __hash__(self):\n",
" return super(Poly, self).__hash__()\n",
"\n",
" @property\n",
" def free_symbols(self):\n",
" \"\"\"\n",
" Free symbols of a polynomial expression.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y, z\n",
"\n",
" >>> Poly(x**2 + 1).free_symbols\n",
" {x}\n",
" >>> Poly(x**2 + y).free_symbols\n",
" {x, y}\n",
" >>> Poly(x**2 + y, x).free_symbols\n",
" {x, y}\n",
" >>> Poly(x**2 + y, x, z).free_symbols\n",
" {x, y}\n",
"\n",
" \"\"\"\n",
" symbols = set()\n",
" gens = self.gens\n",
" for i in range(len(gens)):\n",
" for monom in self.monoms():\n",
" if monom[i]:\n",
" symbols |= gens[i].free_symbols\n",
" break\n",
"\n",
" return symbols | self.free_symbols_in_domain\n",
"\n",
" @property\n",
" def free_symbols_in_domain(self):\n",
" \"\"\"\n",
" Free symbols of the domain of ``self``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + 1).free_symbols_in_domain\n",
" set()\n",
" >>> Poly(x**2 + y).free_symbols_in_domain\n",
" set()\n",
" >>> Poly(x**2 + y, x).free_symbols_in_domain\n",
" {y}\n",
"\n",
" \"\"\"\n",
" domain, symbols = self.rep.dom, set()\n",
"\n",
" if domain.is_Composite:\n",
" for gen in domain.symbols:\n",
" symbols |= gen.free_symbols\n",
" elif domain.is_EX:\n",
" for coeff in self.coeffs():\n",
" symbols |= coeff.free_symbols\n",
"\n",
" return symbols\n",
"\n",
" @property\n",
" def args(self):\n",
" \"\"\"\n",
" Don't mess up with the core.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).args\n",
" (x**2 + 1,)\n",
"\n",
" \"\"\"\n",
" return (self.as_expr(),)\n",
"\n",
" @property\n",
" def gen(self):\n",
" \"\"\"\n",
" Return the principal generator.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).gen\n",
" x\n",
"\n",
" \"\"\"\n",
" return self.gens[0]\n",
"\n",
" @property\n",
" def domain(self):\n",
" \"\"\"Get the ground domain of ``self``. \"\"\"\n",
" return self.get_domain()\n",
"\n",
" @property\n",
" def zero(self):\n",
" \"\"\"Return zero polynomial with ``self``'s properties. \"\"\"\n",
" return self.new(self.rep.zero(self.rep.lev, self.rep.dom), *self.gens)\n",
"\n",
" @property\n",
" def one(self):\n",
" \"\"\"Return one polynomial with ``self``'s properties. \"\"\"\n",
" return self.new(self.rep.one(self.rep.lev, self.rep.dom), *self.gens)\n",
"\n",
" @property\n",
" def unit(self):\n",
" \"\"\"Return unit polynomial with ``self``'s properties. \"\"\"\n",
" return self.new(self.rep.unit(self.rep.lev, self.rep.dom), *self.gens)\n",
"\n",
" def unify(f, g):\n",
" \"\"\"\n",
" Make ``f`` and ``g`` belong to the same domain.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f, g = Poly(x/2 + 1), Poly(2*x + 1)\n",
"\n",
" >>> f\n",
" Poly(1/2*x + 1, x, domain='QQ')\n",
" >>> g\n",
" Poly(2*x + 1, x, domain='ZZ')\n",
"\n",
" >>> F, G = f.unify(g)\n",
"\n",
" >>> F\n",
" Poly(1/2*x + 1, x, domain='QQ')\n",
" >>> G\n",
" Poly(2*x + 1, x, domain='QQ')\n",
"\n",
" \"\"\"\n",
" _, per, F, G = f._unify(g)\n",
" return per(F), per(G)\n",
"\n",
" def _unify(f, g):\n",
" g = sympify(g)\n",
"\n",
" if not g.is_Poly:\n",
" try:\n",
" return f.rep.dom, f.per, f.rep, f.rep.per(f.rep.dom.from_sympy(g))\n",
" except CoercionFailed:\n",
" raise UnificationFailed(\"can't unify %s with %s\" % (f, g))\n",
"\n",
" if isinstance(f.rep, DMP) and isinstance(g.rep, DMP):\n",
" gens = _unify_gens(f.gens, g.gens)\n",
"\n",
" dom, lev = f.rep.dom.unify(g.rep.dom, gens), len(gens) - 1\n",
"\n",
" if f.gens != gens:\n",
" f_monoms, f_coeffs = _dict_reorder(\n",
" f.rep.to_dict(), f.gens, gens)\n",
"\n",
" if f.rep.dom != dom:\n",
" f_coeffs = [dom.convert(c, f.rep.dom) for c in f_coeffs]\n",
"\n",
" F = DMP(dict(list(zip(f_monoms, f_coeffs))), dom, lev)\n",
" else:\n",
" F = f.rep.convert(dom)\n",
"\n",
" if g.gens != gens:\n",
" g_monoms, g_coeffs = _dict_reorder(\n",
" g.rep.to_dict(), g.gens, gens)\n",
"\n",
" if g.rep.dom != dom:\n",
" g_coeffs = [dom.convert(c, g.rep.dom) for c in g_coeffs]\n",
"\n",
" G = DMP(dict(list(zip(g_monoms, g_coeffs))), dom, lev)\n",
" else:\n",
" G = g.rep.convert(dom)\n",
" else:\n",
" raise UnificationFailed(\"can't unify %s with %s\" % (f, g))\n",
"\n",
" cls = f.__class__\n",
"\n",
" def per(rep, dom=dom, gens=gens, remove=None):\n",
" if remove is not None:\n",
" gens = gens[:remove] + gens[remove + 1:]\n",
"\n",
" if not gens:\n",
" return dom.to_sympy(rep)\n",
"\n",
" return cls.new(rep, *gens)\n",
"\n",
" return dom, per, F, G\n",
"\n",
" def per(f, rep, gens=None, remove=None):\n",
" \"\"\"\n",
" Create a Poly out of the given representation.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, ZZ\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> from sympy.polys.polyclasses import DMP\n",
"\n",
" >>> a = Poly(x**2 + 1)\n",
"\n",
" >>> a.per(DMP([ZZ(1), ZZ(1)], ZZ), gens=[y])\n",
" Poly(y + 1, y, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if gens is None:\n",
" gens = f.gens\n",
"\n",
" if remove is not None:\n",
" gens = gens[:remove] + gens[remove + 1:]\n",
"\n",
" if not gens:\n",
" return f.rep.dom.to_sympy(rep)\n",
"\n",
" return f.__class__.new(rep, *gens)\n",
"\n",
" def set_domain(f, domain):\n",
" \"\"\"Set the ground domain of ``f``. \"\"\"\n",
" opt = options.build_options(f.gens, {'domain': domain})\n",
" return f.per(f.rep.convert(opt.domain))\n",
"\n",
" def get_domain(f):\n",
" \"\"\"Get the ground domain of ``f``. \"\"\"\n",
" return f.rep.dom\n",
"\n",
" def set_modulus(f, modulus):\n",
" \"\"\"\n",
" Set the modulus of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(5*x**2 + 2*x - 1, x).set_modulus(2)\n",
" Poly(x**2 + 1, x, modulus=2)\n",
"\n",
" \"\"\"\n",
" modulus = options.Modulus.preprocess(modulus)\n",
" return f.set_domain(FF(modulus))\n",
"\n",
" def get_modulus(f):\n",
" \"\"\"\n",
" Get the modulus of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, modulus=2).get_modulus()\n",
" 2\n",
"\n",
" \"\"\"\n",
" domain = f.get_domain()\n",
"\n",
" if domain.is_FiniteField:\n",
" return Integer(domain.characteristic())\n",
" else:\n",
" raise PolynomialError(\"not a polynomial over a Galois field\")\n",
"\n",
" def _eval_subs(f, old, new):\n",
" \"\"\"Internal implementation of :func:`subs`. \"\"\"\n",
" if old in f.gens:\n",
" if new.is_number:\n",
" return f.eval(old, new)\n",
" else:\n",
" try:\n",
" return f.replace(old, new)\n",
" except PolynomialError:\n",
" pass\n",
"\n",
" return f.as_expr().subs(old, new)\n",
"\n",
" def exclude(f):\n",
" \"\"\"\n",
" Remove unnecessary generators from ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import a, b, c, d, x\n",
"\n",
" >>> Poly(a + x, a, b, c, d, x).exclude()\n",
" Poly(a + x, a, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" J, new = f.rep.exclude()\n",
" gens = []\n",
"\n",
" for j in range(len(f.gens)):\n",
" if j not in J:\n",
" gens.append(f.gens[j])\n",
"\n",
" return f.per(new, gens=gens)\n",
"\n",
" def replace(f, x, y=None, *_ignore):\n",
" # XXX this does not match Basic's signature\n",
" \"\"\"\n",
" Replace ``x`` with ``y`` in generators list.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + 1, x).replace(x, y)\n",
" Poly(y**2 + 1, y, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if y is None:\n",
" if f.is_univariate:\n",
" x, y = f.gen, x\n",
" else:\n",
" raise PolynomialError(\n",
" \"syntax supported only in univariate case\")\n",
"\n",
" if x == y or x not in f.gens:\n",
" return f\n",
"\n",
" if x in f.gens and y not in f.gens:\n",
" dom = f.get_domain()\n",
"\n",
" if not dom.is_Composite or y not in dom.symbols:\n",
" gens = list(f.gens)\n",
" gens[gens.index(x)] = y\n",
" return f.per(f.rep, gens=gens)\n",
"\n",
" raise PolynomialError(\"can't replace %s with %s in %s\" % (x, y, f))\n",
"\n",
" def reorder(f, *gens, **args):\n",
" \"\"\"\n",
" Efficiently apply new order of generators.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + x*y**2, x, y).reorder(y, x)\n",
" Poly(y**2*x + x**2, y, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" opt = options.Options((), args)\n",
"\n",
" if not gens:\n",
" gens = _sort_gens(f.gens, opt=opt)\n",
" elif set(f.gens) != set(gens):\n",
" raise PolynomialError(\n",
" \"generators list can differ only up to order of elements\")\n",
"\n",
" rep = dict(list(zip(*_dict_reorder(f.rep.to_dict(), f.gens, gens))))\n",
"\n",
" return f.per(DMP(rep, f.rep.dom, len(gens) - 1), gens=gens)\n",
"\n",
" def ltrim(f, gen):\n",
" \"\"\"\n",
" Remove dummy generators from ``f`` that are to the left of\n",
" specified ``gen`` in the generators as ordered. When ``gen``\n",
" is an integer, it refers to the generator located at that\n",
" position within the tuple of generators of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y, z\n",
"\n",
" >>> Poly(y**2 + y*z**2, x, y, z).ltrim(y)\n",
" Poly(y**2 + y*z**2, y, z, domain='ZZ')\n",
" >>> Poly(z, x, y, z).ltrim(-1)\n",
" Poly(z, z, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" rep = f.as_dict(native=True)\n",
" j = f._gen_to_level(gen)\n",
"\n",
" terms = {}\n",
"\n",
" for monom, coeff in rep.items():\n",
"\n",
" if any(i for i in monom[:j]):\n",
" # some generator is used in the portion to be trimmed\n",
" raise PolynomialError(\"can't left trim %s\" % f)\n",
"\n",
" terms[monom[j:]] = coeff\n",
"\n",
" gens = f.gens[j:]\n",
"\n",
" return f.new(DMP.from_dict(terms, len(gens) - 1, f.rep.dom), *gens)\n",
"\n",
" def has_only_gens(f, *gens):\n",
" \"\"\"\n",
" Return ``True`` if ``Poly(f, *gens)`` retains ground domain.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y, z\n",
"\n",
" >>> Poly(x*y + 1, x, y, z).has_only_gens(x, y)\n",
" True\n",
" >>> Poly(x*y + z, x, y, z).has_only_gens(x, y)\n",
" False\n",
"\n",
" \"\"\"\n",
" indices = set()\n",
"\n",
" for gen in gens:\n",
" try:\n",
" index = f.gens.index(gen)\n",
" except ValueError:\n",
" raise GeneratorsError(\n",
" \"%s doesn't have %s as generator\" % (f, gen))\n",
" else:\n",
" indices.add(index)\n",
"\n",
" for monom in f.monoms():\n",
" for i, elt in enumerate(monom):\n",
" if i not in indices and elt:\n",
" return False\n",
"\n",
" return True\n",
"\n",
" def to_ring(f):\n",
" \"\"\"\n",
" Make the ground domain a ring.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, QQ\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, domain=QQ).to_ring()\n",
" Poly(x**2 + 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'to_ring'):\n",
" result = f.rep.to_ring()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'to_ring')\n",
"\n",
" return f.per(result)\n",
"\n",
" def to_field(f):\n",
" \"\"\"\n",
" Make the ground domain a field.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, ZZ\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x, domain=ZZ).to_field()\n",
" Poly(x**2 + 1, x, domain='QQ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'to_field'):\n",
" result = f.rep.to_field()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'to_field')\n",
"\n",
" return f.per(result)\n",
"\n",
" def to_exact(f):\n",
" \"\"\"\n",
" Make the ground domain exact.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, RR\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1.0, x, domain=RR).to_exact()\n",
" Poly(x**2 + 1, x, domain='QQ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'to_exact'):\n",
" result = f.rep.to_exact()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'to_exact')\n",
"\n",
" return f.per(result)\n",
"\n",
" def retract(f, field=None):\n",
" \"\"\"\n",
" Recalculate the ground domain of a polynomial.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f = Poly(x**2 + 1, x, domain='QQ[y]')\n",
" >>> f\n",
" Poly(x**2 + 1, x, domain='QQ[y]')\n",
"\n",
" >>> f.retract()\n",
" Poly(x**2 + 1, x, domain='ZZ')\n",
" >>> f.retract(field=True)\n",
" Poly(x**2 + 1, x, domain='QQ')\n",
"\n",
" \"\"\"\n",
" dom, rep = construct_domain(f.as_dict(zero=True),\n",
" field=field, composite=f.domain.is_Composite or None)\n",
" return f.from_dict(rep, f.gens, domain=dom)\n",
"\n",
" def slice(f, x, m, n=None):\n",
" \"\"\"Take a continuous subsequence of terms of ``f``. \"\"\"\n",
" if n is None:\n",
" j, m, n = 0, x, m\n",
" else:\n",
" j = f._gen_to_level(x)\n",
"\n",
" m, n = int(m), int(n)\n",
"\n",
" if hasattr(f.rep, 'slice'):\n",
" result = f.rep.slice(m, n, j)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'slice')\n",
"\n",
" return f.per(result)\n",
"\n",
" def coeffs(f, order=None):\n",
" \"\"\"\n",
" Returns all non-zero coefficients from ``f`` in lex order.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**3 + 2*x + 3, x).coeffs()\n",
" [1, 2, 3]\n",
"\n",
" See Also\n",
" ========\n",
" all_coeffs\n",
" coeff_monomial\n",
" nth\n",
"\n",
" \"\"\"\n",
" return [f.rep.dom.to_sympy(c) for c in f.rep.coeffs(order=order)]\n",
"\n",
" def monoms(f, order=None):\n",
" \"\"\"\n",
" Returns all non-zero monomials from ``f`` in lex order.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).monoms()\n",
" [(2, 0), (1, 2), (1, 1), (0, 1)]\n",
"\n",
" See Also\n",
" ========\n",
" all_monoms\n",
"\n",
" \"\"\"\n",
" return f.rep.monoms(order=order)\n",
"\n",
" def terms(f, order=None):\n",
" \"\"\"\n",
" Returns all non-zero terms from ``f`` in lex order.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).terms()\n",
" [((2, 0), 1), ((1, 2), 2), ((1, 1), 1), ((0, 1), 3)]\n",
"\n",
" See Also\n",
" ========\n",
" all_terms\n",
"\n",
" \"\"\"\n",
" return [(m, f.rep.dom.to_sympy(c)) for m, c in f.rep.terms(order=order)]\n",
"\n",
" def all_coeffs(f):\n",
" \"\"\"\n",
" Returns all coefficients from a univariate polynomial ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**3 + 2*x - 1, x).all_coeffs()\n",
" [1, 0, 2, -1]\n",
"\n",
" \"\"\"\n",
" return [f.rep.dom.to_sympy(c) for c in f.rep.all_coeffs()]\n",
"\n",
" def all_monoms(f):\n",
" \"\"\"\n",
" Returns all monomials from a univariate polynomial ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**3 + 2*x - 1, x).all_monoms()\n",
" [(3,), (2,), (1,), (0,)]\n",
"\n",
" See Also\n",
" ========\n",
" all_terms\n",
"\n",
" \"\"\"\n",
" return f.rep.all_monoms()\n",
"\n",
" def all_terms(f):\n",
" \"\"\"\n",
" Returns all terms from a univariate polynomial ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**3 + 2*x - 1, x).all_terms()\n",
" [((3,), 1), ((2,), 0), ((1,), 2), ((0,), -1)]\n",
"\n",
" \"\"\"\n",
" return [(m, f.rep.dom.to_sympy(c)) for m, c in f.rep.all_terms()]\n",
"\n",
" def termwise(f, func, *gens, **args):\n",
" \"\"\"\n",
" Apply a function to all terms of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> def func(k, coeff):\n",
" ... k = k[0]\n",
" ... return coeff//10**(2-k)\n",
"\n",
" >>> Poly(x**2 + 20*x + 400).termwise(func)\n",
" Poly(x**2 + 2*x + 4, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" terms = {}\n",
"\n",
" for monom, coeff in f.terms():\n",
" result = func(monom, coeff)\n",
"\n",
" if isinstance(result, tuple):\n",
" monom, coeff = result\n",
" else:\n",
" coeff = result\n",
"\n",
" if coeff:\n",
" if monom not in terms:\n",
" terms[monom] = coeff\n",
" else:\n",
" raise PolynomialError(\n",
" \"%s monomial was generated twice\" % monom)\n",
"\n",
" return f.from_dict(terms, *(gens or f.gens), **args)\n",
"\n",
" def length(f):\n",
" \"\"\"\n",
" Returns the number of non-zero terms in ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 2*x - 1).length()\n",
" 3\n",
"\n",
" \"\"\"\n",
" return len(f.as_dict())\n",
"\n",
" def as_dict(f, native=False, zero=False):\n",
" \"\"\"\n",
" Switch to a ``dict`` representation.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + 2*x*y**2 - y, x, y).as_dict()\n",
" {(0, 1): -1, (1, 2): 2, (2, 0): 1}\n",
"\n",
" \"\"\"\n",
" if native:\n",
" return f.rep.to_dict(zero=zero)\n",
" else:\n",
" return f.rep.to_sympy_dict(zero=zero)\n",
"\n",
" def as_list(f, native=False):\n",
" \"\"\"Switch to a ``list`` representation. \"\"\"\n",
" if native:\n",
" return f.rep.to_list()\n",
" else:\n",
" return f.rep.to_sympy_list()\n",
"\n",
" def as_expr(f, *gens):\n",
" \"\"\"\n",
" Convert a Poly instance to an Expr instance.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> f = Poly(x**2 + 2*x*y**2 - y, x, y)\n",
"\n",
" >>> f.as_expr()\n",
" x**2 + 2*x*y**2 - y\n",
" >>> f.as_expr({x: 5})\n",
" 10*y**2 - y + 25\n",
" >>> f.as_expr(5, 6)\n",
" 379\n",
"\n",
" \"\"\"\n",
" if not gens:\n",
" gens = f.gens\n",
" elif len(gens) == 1 and isinstance(gens[0], dict):\n",
" mapping = gens[0]\n",
" gens = list(f.gens)\n",
"\n",
" for gen, value in mapping.items():\n",
" try:\n",
" index = gens.index(gen)\n",
" except ValueError:\n",
" raise GeneratorsError(\n",
" \"%s doesn't have %s as generator\" % (f, gen))\n",
" else:\n",
" gens[index] = value\n",
"\n",
" return basic_from_dict(f.rep.to_sympy_dict(), *gens)\n",
"\n",
" def lift(f):\n",
" \"\"\"\n",
" Convert algebraic coefficients to rationals.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, I\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + I*x + 1, x, extension=I).lift()\n",
" Poly(x**4 + 3*x**2 + 1, x, domain='QQ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'lift'):\n",
" result = f.rep.lift()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'lift')\n",
"\n",
" return f.per(result)\n",
"\n",
" def deflate(f):\n",
" \"\"\"\n",
" Reduce degree of ``f`` by mapping ``x_i**m`` to ``y_i``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**6*y**2 + x**3 + 1, x, y).deflate()\n",
" ((3, 2), Poly(x**2*y + x + 1, x, y, domain='ZZ'))\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'deflate'):\n",
" J, result = f.rep.deflate()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'deflate')\n",
"\n",
" return J, f.per(result)\n",
"\n",
" def inject(f, front=False):\n",
" \"\"\"\n",
" Inject ground domain generators into ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> f = Poly(x**2*y + x*y**3 + x*y + 1, x)\n",
"\n",
" >>> f.inject()\n",
" Poly(x**2*y + x*y**3 + x*y + 1, x, y, domain='ZZ')\n",
" >>> f.inject(front=True)\n",
" Poly(y**3*x + y*x**2 + y*x + 1, y, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" dom = f.rep.dom\n",
"\n",
" if dom.is_Numerical:\n",
" return f\n",
" elif not dom.is_Poly:\n",
" raise DomainError(\"can't inject generators over %s\" % dom)\n",
"\n",
" if hasattr(f.rep, 'inject'):\n",
" result = f.rep.inject(front=front)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'inject')\n",
"\n",
" if front:\n",
" gens = dom.symbols + f.gens\n",
" else:\n",
" gens = f.gens + dom.symbols\n",
"\n",
" return f.new(result, *gens)\n",
"\n",
" def eject(f, *gens):\n",
" \"\"\"\n",
" Eject selected generators into the ground domain.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> f = Poly(x**2*y + x*y**3 + x*y + 1, x, y)\n",
"\n",
" >>> f.eject(x)\n",
" Poly(x*y**3 + (x**2 + x)*y + 1, y, domain='ZZ[x]')\n",
" >>> f.eject(y)\n",
" Poly(y*x**2 + (y**3 + y)*x + 1, x, domain='ZZ[y]')\n",
"\n",
" \"\"\"\n",
" dom = f.rep.dom\n",
"\n",
" if not dom.is_Numerical:\n",
" raise DomainError(\"can't eject generators over %s\" % dom)\n",
"\n",
" k = len(gens)\n",
"\n",
" if f.gens[:k] == gens:\n",
" _gens, front = f.gens[k:], True\n",
" elif f.gens[-k:] == gens:\n",
" _gens, front = f.gens[:-k], False\n",
" else:\n",
" raise NotImplementedError(\n",
" \"can only eject front or back generators\")\n",
"\n",
" dom = dom.inject(*gens)\n",
"\n",
" if hasattr(f.rep, 'eject'):\n",
" result = f.rep.eject(dom, front=front)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'eject')\n",
"\n",
" return f.new(result, *_gens)\n",
"\n",
" def terms_gcd(f):\n",
" \"\"\"\n",
" Remove GCD of terms from the polynomial ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**6*y**2 + x**3*y, x, y).terms_gcd()\n",
" ((3, 1), Poly(x**3*y + 1, x, y, domain='ZZ'))\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'terms_gcd'):\n",
" J, result = f.rep.terms_gcd()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'terms_gcd')\n",
"\n",
" return J, f.per(result)\n",
"\n",
" def add_ground(f, coeff):\n",
" \"\"\"\n",
" Add an element of the ground domain to ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x + 1).add_ground(2)\n",
" Poly(x + 3, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'add_ground'):\n",
" result = f.rep.add_ground(coeff)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'add_ground')\n",
"\n",
" return f.per(result)\n",
"\n",
" def sub_ground(f, coeff):\n",
" \"\"\"\n",
" Subtract an element of the ground domain from ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x + 1).sub_ground(2)\n",
" Poly(x - 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'sub_ground'):\n",
" result = f.rep.sub_ground(coeff)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'sub_ground')\n",
"\n",
" return f.per(result)\n",
"\n",
" def mul_ground(f, coeff):\n",
" \"\"\"\n",
" Multiply ``f`` by a an element of the ground domain.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x + 1).mul_ground(2)\n",
" Poly(2*x + 2, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'mul_ground'):\n",
" result = f.rep.mul_ground(coeff)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'mul_ground')\n",
"\n",
" return f.per(result)\n",
"\n",
" def quo_ground(f, coeff):\n",
" \"\"\"\n",
" Quotient of ``f`` by a an element of the ground domain.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(2*x + 4).quo_ground(2)\n",
" Poly(x + 2, x, domain='ZZ')\n",
"\n",
" >>> Poly(2*x + 3).quo_ground(2)\n",
" Poly(x + 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'quo_ground'):\n",
" result = f.rep.quo_ground(coeff)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'quo_ground')\n",
"\n",
" return f.per(result)\n",
"\n",
" def exquo_ground(f, coeff):\n",
" \"\"\"\n",
" Exact quotient of ``f`` by a an element of the ground domain.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(2*x + 4).exquo_ground(2)\n",
" Poly(x + 2, x, domain='ZZ')\n",
"\n",
" >>> Poly(2*x + 3).exquo_ground(2)\n",
" Traceback (most recent call last):\n",
" ...\n",
" ExactQuotientFailed: 2 does not divide 3 in ZZ\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'exquo_ground'):\n",
" result = f.rep.exquo_ground(coeff)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'exquo_ground')\n",
"\n",
" return f.per(result)\n",
"\n",
" def abs(f):\n",
" \"\"\"\n",
" Make all coefficients in ``f`` positive.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 1, x).abs()\n",
" Poly(x**2 + 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'abs'):\n",
" result = f.rep.abs()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'abs')\n",
"\n",
" return f.per(result)\n",
"\n",
" def neg(f):\n",
" \"\"\"\n",
" Negate all coefficients in ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 1, x).neg()\n",
" Poly(-x**2 + 1, x, domain='ZZ')\n",
"\n",
" >>> -Poly(x**2 - 1, x)\n",
" Poly(-x**2 + 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'neg'):\n",
" result = f.rep.neg()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'neg')\n",
"\n",
" return f.per(result)\n",
"\n",
" def add(f, g):\n",
" \"\"\"\n",
" Add two polynomials ``f`` and ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).add(Poly(x - 2, x))\n",
" Poly(x**2 + x - 1, x, domain='ZZ')\n",
"\n",
" >>> Poly(x**2 + 1, x) + Poly(x - 2, x)\n",
" Poly(x**2 + x - 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" g = sympify(g)\n",
"\n",
" if not g.is_Poly:\n",
" return f.add_ground(g)\n",
"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'add'):\n",
" result = F.add(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'add')\n",
"\n",
" return per(result)\n",
"\n",
" def sub(f, g):\n",
" \"\"\"\n",
" Subtract two polynomials ``f`` and ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).sub(Poly(x - 2, x))\n",
" Poly(x**2 - x + 3, x, domain='ZZ')\n",
"\n",
" >>> Poly(x**2 + 1, x) - Poly(x - 2, x)\n",
" Poly(x**2 - x + 3, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" g = sympify(g)\n",
"\n",
" if not g.is_Poly:\n",
" return f.sub_ground(g)\n",
"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'sub'):\n",
" result = F.sub(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'sub')\n",
"\n",
" return per(result)\n",
"\n",
" def mul(f, g):\n",
" \"\"\"\n",
" Multiply two polynomials ``f`` and ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).mul(Poly(x - 2, x))\n",
" Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ')\n",
"\n",
" >>> Poly(x**2 + 1, x)*Poly(x - 2, x)\n",
" Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" g = sympify(g)\n",
"\n",
" if not g.is_Poly:\n",
" return f.mul_ground(g)\n",
"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'mul'):\n",
" result = F.mul(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'mul')\n",
"\n",
" return per(result)\n",
"\n",
" def sqr(f):\n",
" \"\"\"\n",
" Square a polynomial ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x - 2, x).sqr()\n",
" Poly(x**2 - 4*x + 4, x, domain='ZZ')\n",
"\n",
" >>> Poly(x - 2, x)**2\n",
" Poly(x**2 - 4*x + 4, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'sqr'):\n",
" result = f.rep.sqr()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'sqr')\n",
"\n",
" return f.per(result)\n",
"\n",
" def pow(f, n):\n",
" \"\"\"\n",
" Raise ``f`` to a non-negative power ``n``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x - 2, x).pow(3)\n",
" Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ')\n",
"\n",
" >>> Poly(x - 2, x)**3\n",
" Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" n = int(n)\n",
"\n",
" if hasattr(f.rep, 'pow'):\n",
" result = f.rep.pow(n)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'pow')\n",
"\n",
" return f.per(result)\n",
"\n",
" def pdiv(f, g):\n",
" \"\"\"\n",
" Polynomial pseudo-division of ``f`` by ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).pdiv(Poly(2*x - 4, x))\n",
" (Poly(2*x + 4, x, domain='ZZ'), Poly(20, x, domain='ZZ'))\n",
"\n",
" \"\"\"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'pdiv'):\n",
" q, r = F.pdiv(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'pdiv')\n",
"\n",
" return per(q), per(r)\n",
"\n",
" def prem(f, g):\n",
" \"\"\"\n",
" Polynomial pseudo-remainder of ``f`` by ``g``.\n",
"\n",
" Caveat: The function prem(f, g, x) can be safely used to compute\n",
" in Z[x] _only_ subresultant polynomial remainder sequences (prs's).\n",
"\n",
" To safely compute Euclidean and Sturmian prs's in Z[x]\n",
" employ anyone of the corresponding functions found in\n",
" the module sympy.polys.subresultants_qq_zz. The functions\n",
" in the module with suffix _pg compute prs's in Z[x] employing\n",
" rem(f, g, x), whereas the functions with suffix _amv\n",
" compute prs's in Z[x] employing rem_z(f, g, x).\n",
"\n",
" The function rem_z(f, g, x) differs from prem(f, g, x) in that\n",
" to compute the remainder polynomials in Z[x] it premultiplies\n",
" the divident times the absolute value of the leading coefficient\n",
" of the divisor raised to the power degree(f, x) - degree(g, x) + 1.\n",
"\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).prem(Poly(2*x - 4, x))\n",
" Poly(20, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'prem'):\n",
" result = F.prem(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'prem')\n",
"\n",
" return per(result)\n",
"\n",
" def pquo(f, g):\n",
" \"\"\"\n",
" Polynomial pseudo-quotient of ``f`` by ``g``.\n",
"\n",
" See the Caveat note in the function prem(f, g).\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).pquo(Poly(2*x - 4, x))\n",
" Poly(2*x + 4, x, domain='ZZ')\n",
"\n",
" >>> Poly(x**2 - 1, x).pquo(Poly(2*x - 2, x))\n",
" Poly(2*x + 2, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'pquo'):\n",
" result = F.pquo(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'pquo')\n",
"\n",
" return per(result)\n",
"\n",
" def pexquo(f, g):\n",
" \"\"\"\n",
" Polynomial exact pseudo-quotient of ``f`` by ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 1, x).pexquo(Poly(2*x - 2, x))\n",
" Poly(2*x + 2, x, domain='ZZ')\n",
"\n",
" >>> Poly(x**2 + 1, x).pexquo(Poly(2*x - 4, x))\n",
" Traceback (most recent call last):\n",
" ...\n",
" ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1\n",
"\n",
" \"\"\"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'pexquo'):\n",
" try:\n",
" result = F.pexquo(G)\n",
" except ExactQuotientFailed as exc:\n",
" raise exc.new(f.as_expr(), g.as_expr())\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'pexquo')\n",
"\n",
" return per(result)\n",
"\n",
" def div(f, g, auto=True):\n",
" \"\"\"\n",
" Polynomial division with remainder of ``f`` by ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).div(Poly(2*x - 4, x))\n",
" (Poly(1/2*x + 1, x, domain='QQ'), Poly(5, x, domain='QQ'))\n",
"\n",
" >>> Poly(x**2 + 1, x).div(Poly(2*x - 4, x), auto=False)\n",
" (Poly(0, x, domain='ZZ'), Poly(x**2 + 1, x, domain='ZZ'))\n",
"\n",
" \"\"\"\n",
" dom, per, F, G = f._unify(g)\n",
" retract = False\n",
"\n",
" if auto and dom.is_Ring and not dom.is_Field:\n",
" F, G = F.to_field(), G.to_field()\n",
" retract = True\n",
"\n",
" if hasattr(f.rep, 'div'):\n",
" q, r = F.div(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'div')\n",
"\n",
" if retract:\n",
" try:\n",
" Q, R = q.to_ring(), r.to_ring()\n",
" except CoercionFailed:\n",
" pass\n",
" else:\n",
" q, r = Q, R\n",
"\n",
" return per(q), per(r)\n",
"\n",
" def rem(f, g, auto=True):\n",
" \"\"\"\n",
" Computes the polynomial remainder of ``f`` by ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x))\n",
" Poly(5, x, domain='ZZ')\n",
"\n",
" >>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x), auto=False)\n",
" Poly(x**2 + 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" dom, per, F, G = f._unify(g)\n",
" retract = False\n",
"\n",
" if auto and dom.is_Ring and not dom.is_Field:\n",
" F, G = F.to_field(), G.to_field()\n",
" retract = True\n",
"\n",
" if hasattr(f.rep, 'rem'):\n",
" r = F.rem(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'rem')\n",
"\n",
" if retract:\n",
" try:\n",
" r = r.to_ring()\n",
" except CoercionFailed:\n",
" pass\n",
"\n",
" return per(r)\n",
"\n",
" def quo(f, g, auto=True):\n",
" \"\"\"\n",
" Computes polynomial quotient of ``f`` by ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).quo(Poly(2*x - 4, x))\n",
" Poly(1/2*x + 1, x, domain='QQ')\n",
"\n",
" >>> Poly(x**2 - 1, x).quo(Poly(x - 1, x))\n",
" Poly(x + 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" dom, per, F, G = f._unify(g)\n",
" retract = False\n",
"\n",
" if auto and dom.is_Ring and not dom.is_Field:\n",
" F, G = F.to_field(), G.to_field()\n",
" retract = True\n",
"\n",
" if hasattr(f.rep, 'quo'):\n",
" q = F.quo(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'quo')\n",
"\n",
" if retract:\n",
" try:\n",
" q = q.to_ring()\n",
" except CoercionFailed:\n",
" pass\n",
"\n",
" return per(q)\n",
"\n",
" def exquo(f, g, auto=True):\n",
" \"\"\"\n",
" Computes polynomial exact quotient of ``f`` by ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 1, x).exquo(Poly(x - 1, x))\n",
" Poly(x + 1, x, domain='ZZ')\n",
"\n",
" >>> Poly(x**2 + 1, x).exquo(Poly(2*x - 4, x))\n",
" Traceback (most recent call last):\n",
" ...\n",
" ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1\n",
"\n",
" \"\"\"\n",
" dom, per, F, G = f._unify(g)\n",
" retract = False\n",
"\n",
" if auto and dom.is_Ring and not dom.is_Field:\n",
" F, G = F.to_field(), G.to_field()\n",
" retract = True\n",
"\n",
" if hasattr(f.rep, 'exquo'):\n",
" try:\n",
" q = F.exquo(G)\n",
" except ExactQuotientFailed as exc:\n",
" raise exc.new(f.as_expr(), g.as_expr())\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'exquo')\n",
"\n",
" if retract:\n",
" try:\n",
" q = q.to_ring()\n",
" except CoercionFailed:\n",
" pass\n",
"\n",
" return per(q)\n",
"\n",
" def _gen_to_level(f, gen):\n",
" \"\"\"Returns level associated with the given generator. \"\"\"\n",
" if isinstance(gen, int):\n",
" length = len(f.gens)\n",
"\n",
" if -length <= gen < length:\n",
" if gen < 0:\n",
" return length + gen\n",
" else:\n",
" return gen\n",
" else:\n",
" raise PolynomialError(\"-%s <= gen < %s expected, got %s\" %\n",
" (length, length, gen))\n",
" else:\n",
" try:\n",
" return f.gens.index(sympify(gen))\n",
" except ValueError:\n",
" raise PolynomialError(\n",
" \"a valid generator expected, got %s\" % gen)\n",
"\n",
" def degree(f, gen=0):\n",
" \"\"\"\n",
" Returns degree of ``f`` in ``x_j``.\n",
"\n",
" The degree of 0 is negative infinity.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + y*x + 1, x, y).degree()\n",
" 2\n",
" >>> Poly(x**2 + y*x + y, x, y).degree(y)\n",
" 1\n",
" >>> Poly(0, x).degree()\n",
" -oo\n",
"\n",
" \"\"\"\n",
" j = f._gen_to_level(gen)\n",
"\n",
" if hasattr(f.rep, 'degree'):\n",
" return f.rep.degree(j)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'degree')\n",
"\n",
" def degree_list(f):\n",
" \"\"\"\n",
" Returns a list of degrees of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + y*x + 1, x, y).degree_list()\n",
" (2, 1)\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'degree_list'):\n",
" return f.rep.degree_list()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'degree_list')\n",
"\n",
" def total_degree(f):\n",
" \"\"\"\n",
" Returns the total degree of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + y*x + 1, x, y).total_degree()\n",
" 2\n",
" >>> Poly(x + y**5, x, y).total_degree()\n",
" 5\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'total_degree'):\n",
" return f.rep.total_degree()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'total_degree')\n",
"\n",
" def homogenize(f, s):\n",
" \"\"\"\n",
" Returns the homogeneous polynomial of ``f``.\n",
"\n",
" A homogeneous polynomial is a polynomial whose all monomials with\n",
" non-zero coefficients have the same total degree. If you only\n",
" want to check if a polynomial is homogeneous, then use\n",
" :func:`Poly.is_homogeneous`. If you want not only to check if a\n",
" polynomial is homogeneous but also compute its homogeneous order,\n",
" then use :func:`Poly.homogeneous_order`.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y, z\n",
"\n",
" >>> f = Poly(x**5 + 2*x**2*y**2 + 9*x*y**3)\n",
" >>> f.homogenize(z)\n",
" Poly(x**5 + 2*x**2*y**2*z + 9*x*y**3*z, x, y, z, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if not isinstance(s, Symbol):\n",
" raise TypeError(\"``Symbol`` expected, got %s\" % type(s))\n",
" if s in f.gens:\n",
" i = f.gens.index(s)\n",
" gens = f.gens\n",
" else:\n",
" i = len(f.gens)\n",
" gens = f.gens + (s,)\n",
" if hasattr(f.rep, 'homogenize'):\n",
" return f.per(f.rep.homogenize(i), gens=gens)\n",
" raise OperationNotSupported(f, 'homogeneous_order')\n",
"\n",
" def homogeneous_order(f):\n",
" \"\"\"\n",
" Returns the homogeneous order of ``f``.\n",
"\n",
" A homogeneous polynomial is a polynomial whose all monomials with\n",
" non-zero coefficients have the same total degree. This degree is\n",
" the homogeneous order of ``f``. If you only want to check if a\n",
" polynomial is homogeneous, then use :func:`Poly.is_homogeneous`.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> f = Poly(x**5 + 2*x**3*y**2 + 9*x*y**4)\n",
" >>> f.homogeneous_order()\n",
" 5\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'homogeneous_order'):\n",
" return f.rep.homogeneous_order()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'homogeneous_order')\n",
"\n",
" def LC(f, order=None):\n",
" \"\"\"\n",
" Returns the leading coefficient of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(4*x**3 + 2*x**2 + 3*x, x).LC()\n",
" 4\n",
"\n",
" \"\"\"\n",
" if order is not None:\n",
" return f.coeffs(order)[0]\n",
"\n",
" if hasattr(f.rep, 'LC'):\n",
" result = f.rep.LC()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'LC')\n",
"\n",
" return f.rep.dom.to_sympy(result)\n",
"\n",
" def TC(f):\n",
" \"\"\"\n",
" Returns the trailing coefficient of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**3 + 2*x**2 + 3*x, x).TC()\n",
" 0\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'TC'):\n",
" result = f.rep.TC()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'TC')\n",
"\n",
" return f.rep.dom.to_sympy(result)\n",
"\n",
" def EC(f, order=None):\n",
" \"\"\"\n",
" Returns the last non-zero coefficient of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**3 + 2*x**2 + 3*x, x).EC()\n",
" 3\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'coeffs'):\n",
" return f.coeffs(order)[-1]\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'EC')\n",
"\n",
" def coeff_monomial(f, monom):\n",
" \"\"\"\n",
" Returns the coefficient of ``monom`` in ``f`` if there, else None.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, exp\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> p = Poly(24*x*y*exp(8) + 23*x, x, y)\n",
"\n",
" >>> p.coeff_monomial(x)\n",
" 23\n",
" >>> p.coeff_monomial(y)\n",
" 0\n",
" >>> p.coeff_monomial(x*y)\n",
" 24*exp(8)\n",
"\n",
" Note that ``Expr.coeff()`` behaves differently, collecting terms\n",
" if possible; the Poly must be converted to an Expr to use that\n",
" method, however:\n",
"\n",
" >>> p.as_expr().coeff(x)\n",
" 24*y*exp(8) + 23\n",
" >>> p.as_expr().coeff(y)\n",
" 24*x*exp(8)\n",
" >>> p.as_expr().coeff(x*y)\n",
" 24*exp(8)\n",
"\n",
" See Also\n",
" ========\n",
" nth: more efficient query using exponents of the monomial's generators\n",
"\n",
" \"\"\"\n",
" return f.nth(*Monomial(monom, f.gens).exponents)\n",
"\n",
" def nth(f, *N):\n",
" \"\"\"\n",
" Returns the ``n``-th coefficient of ``f`` where ``N`` are the\n",
" exponents of the generators in the term of interest.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, sqrt\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**3 + 2*x**2 + 3*x, x).nth(2)\n",
" 2\n",
" >>> Poly(x**3 + 2*x*y**2 + y**2, x, y).nth(1, 2)\n",
" 2\n",
" >>> Poly(4*sqrt(x)*y)\n",
" Poly(4*y*(sqrt(x)), y, sqrt(x), domain='ZZ')\n",
" >>> _.nth(1, 1)\n",
" 4\n",
"\n",
" See Also\n",
" ========\n",
" coeff_monomial\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'nth'):\n",
" if len(N) != len(f.gens):\n",
" raise ValueError('exponent of each generator must be specified')\n",
" result = f.rep.nth(*list(map(int, N)))\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'nth')\n",
"\n",
" return f.rep.dom.to_sympy(result)\n",
"\n",
" def coeff(f, x, n=1, right=False):\n",
" # the semantics of coeff_monomial and Expr.coeff are different;\n",
" # if someone is working with a Poly, they should be aware of the\n",
" # differences and chose the method best suited for the query.\n",
" # Alternatively, a pure-polys method could be written here but\n",
" # at this time the ``right`` keyword would be ignored because Poly\n",
" # doesn't work with non-commutatives.\n",
" raise NotImplementedError(\n",
" 'Either convert to Expr with `as_expr` method '\n",
" 'to use Expr\\'s coeff method or else use the '\n",
" '`coeff_monomial` method of Polys.')\n",
"\n",
" def LM(f, order=None):\n",
" \"\"\"\n",
" Returns the leading monomial of ``f``.\n",
"\n",
" The Leading monomial signifies the monomial having\n",
" the highest power of the principal generator in the\n",
" expression f.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LM()\n",
" x**2*y**0\n",
"\n",
" \"\"\"\n",
" return Monomial(f.monoms(order)[0], f.gens)\n",
"\n",
" def EM(f, order=None):\n",
" \"\"\"\n",
" Returns the last non-zero monomial of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).EM()\n",
" x**0*y**1\n",
"\n",
" \"\"\"\n",
" return Monomial(f.monoms(order)[-1], f.gens)\n",
"\n",
" def LT(f, order=None):\n",
" \"\"\"\n",
" Returns the leading term of ``f``.\n",
"\n",
" The Leading term signifies the term having\n",
" the highest power of the principal generator in the\n",
" expression f along with its coefficient.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LT()\n",
" (x**2*y**0, 4)\n",
"\n",
" \"\"\"\n",
" monom, coeff = f.terms(order)[0]\n",
" return Monomial(monom, f.gens), coeff\n",
"\n",
" def ET(f, order=None):\n",
" \"\"\"\n",
" Returns the last non-zero term of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).ET()\n",
" (x**0*y**1, 3)\n",
"\n",
" \"\"\"\n",
" monom, coeff = f.terms(order)[-1]\n",
" return Monomial(monom, f.gens), coeff\n",
"\n",
" def max_norm(f):\n",
" \"\"\"\n",
" Returns maximum norm of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(-x**2 + 2*x - 3, x).max_norm()\n",
" 3\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'max_norm'):\n",
" result = f.rep.max_norm()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'max_norm')\n",
"\n",
" return f.rep.dom.to_sympy(result)\n",
"\n",
" def l1_norm(f):\n",
" \"\"\"\n",
" Returns l1 norm of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(-x**2 + 2*x - 3, x).l1_norm()\n",
" 6\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'l1_norm'):\n",
" result = f.rep.l1_norm()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'l1_norm')\n",
"\n",
" return f.rep.dom.to_sympy(result)\n",
"\n",
" def clear_denoms(self, convert=False):\n",
" \"\"\"\n",
" Clear denominators, but keep the ground domain.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, S, QQ\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f = Poly(x/2 + S(1)/3, x, domain=QQ)\n",
"\n",
" >>> f.clear_denoms()\n",
" (6, Poly(3*x + 2, x, domain='QQ'))\n",
" >>> f.clear_denoms(convert=True)\n",
" (6, Poly(3*x + 2, x, domain='ZZ'))\n",
"\n",
" \"\"\"\n",
" f = self\n",
"\n",
" if not f.rep.dom.is_Field:\n",
" return S.One, f\n",
"\n",
" dom = f.get_domain()\n",
" if dom.has_assoc_Ring:\n",
" dom = f.rep.dom.get_ring()\n",
"\n",
" if hasattr(f.rep, 'clear_denoms'):\n",
" coeff, result = f.rep.clear_denoms()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'clear_denoms')\n",
"\n",
" coeff, f = dom.to_sympy(coeff), f.per(result)\n",
"\n",
" if not convert or not dom.has_assoc_Ring:\n",
" return coeff, f\n",
" else:\n",
" return coeff, f.to_ring()\n",
"\n",
" def rat_clear_denoms(self, g):\n",
" \"\"\"\n",
" Clear denominators in a rational function ``f/g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> f = Poly(x**2/y + 1, x)\n",
" >>> g = Poly(x**3 + y, x)\n",
"\n",
" >>> p, q = f.rat_clear_denoms(g)\n",
"\n",
" >>> p\n",
" Poly(x**2 + y, x, domain='ZZ[y]')\n",
" >>> q\n",
" Poly(y*x**3 + y**2, x, domain='ZZ[y]')\n",
"\n",
" \"\"\"\n",
" f = self\n",
"\n",
" dom, per, f, g = f._unify(g)\n",
"\n",
" f = per(f)\n",
" g = per(g)\n",
"\n",
" if not (dom.is_Field and dom.has_assoc_Ring):\n",
" return f, g\n",
"\n",
" a, f = f.clear_denoms(convert=True)\n",
" b, g = g.clear_denoms(convert=True)\n",
"\n",
" f = f.mul_ground(b)\n",
" g = g.mul_ground(a)\n",
"\n",
" return f, g\n",
"\n",
" def integrate(self, *specs, **args):\n",
" \"\"\"\n",
" Computes indefinite integral of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + 2*x + 1, x).integrate()\n",
" Poly(1/3*x**3 + x**2 + x, x, domain='QQ')\n",
"\n",
" >>> Poly(x*y**2 + x, x, y).integrate((0, 1), (1, 0))\n",
" Poly(1/2*x**2*y**2 + 1/2*x**2, x, y, domain='QQ')\n",
"\n",
" \"\"\"\n",
" f = self\n",
"\n",
" if args.get('auto', True) and f.rep.dom.is_Ring:\n",
" f = f.to_field()\n",
"\n",
" if hasattr(f.rep, 'integrate'):\n",
" if not specs:\n",
" return f.per(f.rep.integrate(m=1))\n",
"\n",
" rep = f.rep\n",
"\n",
" for spec in specs:\n",
" if type(spec) is tuple:\n",
" gen, m = spec\n",
" else:\n",
" gen, m = spec, 1\n",
"\n",
" rep = rep.integrate(int(m), f._gen_to_level(gen))\n",
"\n",
" return f.per(rep)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'integrate')\n",
"\n",
" def diff(f, *specs, **kwargs):\n",
" \"\"\"\n",
" Computes partial derivative of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + 2*x + 1, x).diff()\n",
" Poly(2*x + 2, x, domain='ZZ')\n",
"\n",
" >>> Poly(x*y**2 + x, x, y).diff((0, 0), (1, 1))\n",
" Poly(2*x*y, x, y, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if not kwargs.get('evaluate', True):\n",
" return Derivative(f, *specs, **kwargs)\n",
"\n",
" if hasattr(f.rep, 'diff'):\n",
" if not specs:\n",
" return f.per(f.rep.diff(m=1))\n",
"\n",
" rep = f.rep\n",
"\n",
" for spec in specs:\n",
" if type(spec) is tuple:\n",
" gen, m = spec\n",
" else:\n",
" gen, m = spec, 1\n",
"\n",
" rep = rep.diff(int(m), f._gen_to_level(gen))\n",
"\n",
" return f.per(rep)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'diff')\n",
"\n",
" _eval_derivative = diff\n",
"\n",
" def eval(self, x, a=None, auto=True):\n",
" \"\"\"\n",
" Evaluate ``f`` at ``a`` in the given variable.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y, z\n",
"\n",
" >>> Poly(x**2 + 2*x + 3, x).eval(2)\n",
" 11\n",
"\n",
" >>> Poly(2*x*y + 3*x + y + 2, x, y).eval(x, 2)\n",
" Poly(5*y + 8, y, domain='ZZ')\n",
"\n",
" >>> f = Poly(2*x*y + 3*x + y + 2*z, x, y, z)\n",
"\n",
" >>> f.eval({x: 2})\n",
" Poly(5*y + 2*z + 6, y, z, domain='ZZ')\n",
" >>> f.eval({x: 2, y: 5})\n",
" Poly(2*z + 31, z, domain='ZZ')\n",
" >>> f.eval({x: 2, y: 5, z: 7})\n",
" 45\n",
"\n",
" >>> f.eval((2, 5))\n",
" Poly(2*z + 31, z, domain='ZZ')\n",
" >>> f(2, 5)\n",
" Poly(2*z + 31, z, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" f = self\n",
"\n",
" if a is None:\n",
" if isinstance(x, dict):\n",
" mapping = x\n",
"\n",
" for gen, value in mapping.items():\n",
" f = f.eval(gen, value)\n",
"\n",
" return f\n",
" elif isinstance(x, (tuple, list)):\n",
" values = x\n",
"\n",
" if len(values) > len(f.gens):\n",
" raise ValueError(\"too many values provided\")\n",
"\n",
" for gen, value in zip(f.gens, values):\n",
" f = f.eval(gen, value)\n",
"\n",
" return f\n",
" else:\n",
" j, a = 0, x\n",
" else:\n",
" j = f._gen_to_level(x)\n",
"\n",
" if not hasattr(f.rep, 'eval'): # pragma: no cover\n",
" raise OperationNotSupported(f, 'eval')\n",
"\n",
" try:\n",
" result = f.rep.eval(a, j)\n",
" except CoercionFailed:\n",
" if not auto:\n",
" raise DomainError(\"can't evaluate at %s in %s\" % (a, f.rep.dom))\n",
" else:\n",
" a_domain, [a] = construct_domain([a])\n",
" new_domain = f.get_domain().unify_with_symbols(a_domain, f.gens)\n",
"\n",
" f = f.set_domain(new_domain)\n",
" a = new_domain.convert(a, a_domain)\n",
"\n",
" result = f.rep.eval(a, j)\n",
"\n",
" return f.per(result, remove=j)\n",
"\n",
" def __call__(f, *values):\n",
" \"\"\"\n",
" Evaluate ``f`` at the give values.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y, z\n",
"\n",
" >>> f = Poly(2*x*y + 3*x + y + 2*z, x, y, z)\n",
"\n",
" >>> f(2)\n",
" Poly(5*y + 2*z + 6, y, z, domain='ZZ')\n",
" >>> f(2, 5)\n",
" Poly(2*z + 31, z, domain='ZZ')\n",
" >>> f(2, 5, 7)\n",
" 45\n",
"\n",
" \"\"\"\n",
" return f.eval(values)\n",
"\n",
" def half_gcdex(f, g, auto=True):\n",
" \"\"\"\n",
" Half extended Euclidean algorithm of ``f`` and ``g``.\n",
"\n",
" Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15\n",
" >>> g = x**3 + x**2 - 4*x - 4\n",
"\n",
" >>> Poly(f).half_gcdex(Poly(g))\n",
" (Poly(-1/5*x + 3/5, x, domain='QQ'), Poly(x + 1, x, domain='QQ'))\n",
"\n",
" \"\"\"\n",
" dom, per, F, G = f._unify(g)\n",
"\n",
" if auto and dom.is_Ring:\n",
" F, G = F.to_field(), G.to_field()\n",
"\n",
" if hasattr(f.rep, 'half_gcdex'):\n",
" s, h = F.half_gcdex(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'half_gcdex')\n",
"\n",
" return per(s), per(h)\n",
"\n",
" def gcdex(f, g, auto=True):\n",
" \"\"\"\n",
" Extended Euclidean algorithm of ``f`` and ``g``.\n",
"\n",
" Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15\n",
" >>> g = x**3 + x**2 - 4*x - 4\n",
"\n",
" >>> Poly(f).gcdex(Poly(g))\n",
" (Poly(-1/5*x + 3/5, x, domain='QQ'),\n",
" Poly(1/5*x**2 - 6/5*x + 2, x, domain='QQ'),\n",
" Poly(x + 1, x, domain='QQ'))\n",
"\n",
" \"\"\"\n",
" dom, per, F, G = f._unify(g)\n",
"\n",
" if auto and dom.is_Ring:\n",
" F, G = F.to_field(), G.to_field()\n",
"\n",
" if hasattr(f.rep, 'gcdex'):\n",
" s, t, h = F.gcdex(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'gcdex')\n",
"\n",
" return per(s), per(t), per(h)\n",
"\n",
" def invert(f, g, auto=True):\n",
" \"\"\"\n",
" Invert ``f`` modulo ``g`` when possible.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 1, x).invert(Poly(2*x - 1, x))\n",
" Poly(-4/3, x, domain='QQ')\n",
"\n",
" >>> Poly(x**2 - 1, x).invert(Poly(x - 1, x))\n",
" Traceback (most recent call last):\n",
" ...\n",
" NotInvertible: zero divisor\n",
"\n",
" \"\"\"\n",
" dom, per, F, G = f._unify(g)\n",
"\n",
" if auto and dom.is_Ring:\n",
" F, G = F.to_field(), G.to_field()\n",
"\n",
" if hasattr(f.rep, 'invert'):\n",
" result = F.invert(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'invert')\n",
"\n",
" return per(result)\n",
"\n",
" def revert(f, n):\n",
" \"\"\"\n",
" Compute ``f**(-1)`` mod ``x**n``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(1, x).revert(2)\n",
" Poly(1, x, domain='ZZ')\n",
"\n",
" >>> Poly(1 + x, x).revert(1)\n",
" Poly(1, x, domain='ZZ')\n",
"\n",
" >>> Poly(x**2 - 1, x).revert(1)\n",
" Traceback (most recent call last):\n",
" ...\n",
" NotReversible: only unity is reversible in a ring\n",
"\n",
" >>> Poly(1/x, x).revert(1)\n",
" Traceback (most recent call last):\n",
" ...\n",
" PolynomialError: 1/x contains an element of the generators set\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'revert'):\n",
" result = f.rep.revert(int(n))\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'revert')\n",
"\n",
" return f.per(result)\n",
"\n",
" def subresultants(f, g):\n",
" \"\"\"\n",
" Computes the subresultant PRS of ``f`` and ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 1, x).subresultants(Poly(x**2 - 1, x))\n",
" [Poly(x**2 + 1, x, domain='ZZ'),\n",
" Poly(x**2 - 1, x, domain='ZZ'),\n",
" Poly(-2, x, domain='ZZ')]\n",
"\n",
" \"\"\"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'subresultants'):\n",
" result = F.subresultants(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'subresultants')\n",
"\n",
" return list(map(per, result))\n",
"\n",
" def resultant(f, g, includePRS=False):\n",
" \"\"\"\n",
" Computes the resultant of ``f`` and ``g`` via PRS.\n",
"\n",
" If includePRS=True, it includes the subresultant PRS in the result.\n",
" Because the PRS is used to calculate the resultant, this is more\n",
" efficient than calling :func:`subresultants` separately.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f = Poly(x**2 + 1, x)\n",
"\n",
" >>> f.resultant(Poly(x**2 - 1, x))\n",
" 4\n",
" >>> f.resultant(Poly(x**2 - 1, x), includePRS=True)\n",
" (4, [Poly(x**2 + 1, x, domain='ZZ'), Poly(x**2 - 1, x, domain='ZZ'),\n",
" Poly(-2, x, domain='ZZ')])\n",
"\n",
" \"\"\"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'resultant'):\n",
" if includePRS:\n",
" result, R = F.resultant(G, includePRS=includePRS)\n",
" else:\n",
" result = F.resultant(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'resultant')\n",
"\n",
" if includePRS:\n",
" return (per(result, remove=0), list(map(per, R)))\n",
" return per(result, remove=0)\n",
"\n",
" def discriminant(f):\n",
" \"\"\"\n",
" Computes the discriminant of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + 2*x + 3, x).discriminant()\n",
" -8\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'discriminant'):\n",
" result = f.rep.discriminant()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'discriminant')\n",
"\n",
" return f.per(result, remove=0)\n",
"\n",
" def dispersionset(f, g=None):\n",
" r\"\"\"Compute the *dispersion set* of two polynomials.\n",
"\n",
" For two polynomials `f(x)` and `g(x)` with `\\deg f > 0`\n",
" and `\\deg g > 0` the dispersion set `\\operatorname{J}(f, g)` is defined as:\n",
"\n",
" .. math::\n",
" \\operatorname{J}(f, g)\n",
" & := \\{a \\in \\mathbb{N}_0 | \\gcd(f(x), g(x+a)) \\neq 1\\} \\\\\n",
" & = \\{a \\in \\mathbb{N}_0 | \\deg \\gcd(f(x), g(x+a)) \\geq 1\\}\n",
"\n",
" For a single polynomial one defines `\\operatorname{J}(f) := \\operatorname{J}(f, f)`.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import poly\n",
" >>> from sympy.polys.dispersion import dispersion, dispersionset\n",
" >>> from sympy.abc import x\n",
"\n",
" Dispersion set and dispersion of a simple polynomial:\n",
"\n",
" >>> fp = poly((x - 3)*(x + 3), x)\n",
" >>> sorted(dispersionset(fp))\n",
" [0, 6]\n",
" >>> dispersion(fp)\n",
" 6\n",
"\n",
" Note that the definition of the dispersion is not symmetric:\n",
"\n",
" >>> fp = poly(x**4 - 3*x**2 + 1, x)\n",
" >>> gp = fp.shift(-3)\n",
" >>> sorted(dispersionset(fp, gp))\n",
" [2, 3, 4]\n",
" >>> dispersion(fp, gp)\n",
" 4\n",
" >>> sorted(dispersionset(gp, fp))\n",
" []\n",
" >>> dispersion(gp, fp)\n",
" -oo\n",
"\n",
" Computing the dispersion also works over field extensions:\n",
"\n",
" >>> from sympy import sqrt\n",
" >>> fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>')\n",
" >>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>')\n",
" >>> sorted(dispersionset(fp, gp))\n",
" [2]\n",
" >>> sorted(dispersionset(gp, fp))\n",
" [1, 4]\n",
"\n",
" We can even perform the computations for polynomials\n",
" having symbolic coefficients:\n",
"\n",
" >>> from sympy.abc import a\n",
" >>> fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x)\n",
" >>> sorted(dispersionset(fp))\n",
" [0, 1]\n",
"\n",
" See Also\n",
" ========\n",
"\n",
" dispersion\n",
"\n",
" References\n",
" ==========\n",
"\n",
" 1. [ManWright94]_\n",
" 2. [Koepf98]_\n",
" 3. [Abramov71]_\n",
" 4. [Man93]_\n",
" \"\"\"\n",
" from sympy.polys.dispersion import dispersionset\n",
" return dispersionset(f, g)\n",
"\n",
" def dispersion(f, g=None):\n",
" r\"\"\"Compute the *dispersion* of polynomials.\n",
"\n",
" For two polynomials `f(x)` and `g(x)` with `\\deg f > 0`\n",
" and `\\deg g > 0` the dispersion `\\operatorname{dis}(f, g)` is defined as:\n",
"\n",
" .. math::\n",
" \\operatorname{dis}(f, g)\n",
" & := \\max\\{ J(f,g) \\cup \\{0\\} \\} \\\\\n",
" & = \\max\\{ \\{a \\in \\mathbb{N} | \\gcd(f(x), g(x+a)) \\neq 1\\} \\cup \\{0\\} \\}\n",
"\n",
" and for a single polynomial `\\operatorname{dis}(f) := \\operatorname{dis}(f, f)`.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import poly\n",
" >>> from sympy.polys.dispersion import dispersion, dispersionset\n",
" >>> from sympy.abc import x\n",
"\n",
" Dispersion set and dispersion of a simple polynomial:\n",
"\n",
" >>> fp = poly((x - 3)*(x + 3), x)\n",
" >>> sorted(dispersionset(fp))\n",
" [0, 6]\n",
" >>> dispersion(fp)\n",
" 6\n",
"\n",
" Note that the definition of the dispersion is not symmetric:\n",
"\n",
" >>> fp = poly(x**4 - 3*x**2 + 1, x)\n",
" >>> gp = fp.shift(-3)\n",
" >>> sorted(dispersionset(fp, gp))\n",
" [2, 3, 4]\n",
" >>> dispersion(fp, gp)\n",
" 4\n",
" >>> sorted(dispersionset(gp, fp))\n",
" []\n",
" >>> dispersion(gp, fp)\n",
" -oo\n",
"\n",
" Computing the dispersion also works over field extensions:\n",
"\n",
" >>> from sympy import sqrt\n",
" >>> fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>')\n",
" >>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>')\n",
" >>> sorted(dispersionset(fp, gp))\n",
" [2]\n",
" >>> sorted(dispersionset(gp, fp))\n",
" [1, 4]\n",
"\n",
" We can even perform the computations for polynomials\n",
" having symbolic coefficients:\n",
"\n",
" >>> from sympy.abc import a\n",
" >>> fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x)\n",
" >>> sorted(dispersionset(fp))\n",
" [0, 1]\n",
"\n",
" See Also\n",
" ========\n",
"\n",
" dispersionset\n",
"\n",
" References\n",
" ==========\n",
"\n",
" 1. [ManWright94]_\n",
" 2. [Koepf98]_\n",
" 3. [Abramov71]_\n",
" 4. [Man93]_\n",
" \"\"\"\n",
" from sympy.polys.dispersion import dispersion\n",
" return dispersion(f, g)\n",
"\n",
" def cofactors(f, g):\n",
" \"\"\"\n",
" Returns the GCD of ``f`` and ``g`` and their cofactors.\n",
"\n",
" Returns polynomials ``(h, cff, cfg)`` such that ``h = gcd(f, g)``, and\n",
" ``cff = quo(f, h)`` and ``cfg = quo(g, h)`` are, so called, cofactors\n",
" of ``f`` and ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 1, x).cofactors(Poly(x**2 - 3*x + 2, x))\n",
" (Poly(x - 1, x, domain='ZZ'),\n",
" Poly(x + 1, x, domain='ZZ'),\n",
" Poly(x - 2, x, domain='ZZ'))\n",
"\n",
" \"\"\"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'cofactors'):\n",
" h, cff, cfg = F.cofactors(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'cofactors')\n",
"\n",
" return per(h), per(cff), per(cfg)\n",
"\n",
" def gcd(f, g):\n",
" \"\"\"\n",
" Returns the polynomial GCD of ``f`` and ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 1, x).gcd(Poly(x**2 - 3*x + 2, x))\n",
" Poly(x - 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'gcd'):\n",
" result = F.gcd(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'gcd')\n",
"\n",
" return per(result)\n",
"\n",
" def lcm(f, g):\n",
" \"\"\"\n",
" Returns polynomial LCM of ``f`` and ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 1, x).lcm(Poly(x**2 - 3*x + 2, x))\n",
" Poly(x**3 - 2*x**2 - x + 2, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'lcm'):\n",
" result = F.lcm(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'lcm')\n",
"\n",
" return per(result)\n",
"\n",
" def trunc(f, p):\n",
" \"\"\"\n",
" Reduce ``f`` modulo a constant ``p``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(2*x**3 + 3*x**2 + 5*x + 7, x).trunc(3)\n",
" Poly(-x**3 - x + 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" p = f.rep.dom.convert(p)\n",
"\n",
" if hasattr(f.rep, 'trunc'):\n",
" result = f.rep.trunc(p)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'trunc')\n",
"\n",
" return f.per(result)\n",
"\n",
" def monic(self, auto=True):\n",
" \"\"\"\n",
" Divides all coefficients by ``LC(f)``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, ZZ\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(3*x**2 + 6*x + 9, x, domain=ZZ).monic()\n",
" Poly(x**2 + 2*x + 3, x, domain='QQ')\n",
"\n",
" >>> Poly(3*x**2 + 4*x + 2, x, domain=ZZ).monic()\n",
" Poly(x**2 + 4/3*x + 2/3, x, domain='QQ')\n",
"\n",
" \"\"\"\n",
" f = self\n",
"\n",
" if auto and f.rep.dom.is_Ring:\n",
" f = f.to_field()\n",
"\n",
" if hasattr(f.rep, 'monic'):\n",
" result = f.rep.monic()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'monic')\n",
"\n",
" return f.per(result)\n",
"\n",
" def content(f):\n",
" \"\"\"\n",
" Returns the GCD of polynomial coefficients.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(6*x**2 + 8*x + 12, x).content()\n",
" 2\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'content'):\n",
" result = f.rep.content()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'content')\n",
"\n",
" return f.rep.dom.to_sympy(result)\n",
"\n",
" def primitive(f):\n",
" \"\"\"\n",
" Returns the content and a primitive form of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(2*x**2 + 8*x + 12, x).primitive()\n",
" (2, Poly(x**2 + 4*x + 6, x, domain='ZZ'))\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'primitive'):\n",
" cont, result = f.rep.primitive()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'primitive')\n",
"\n",
" return f.rep.dom.to_sympy(cont), f.per(result)\n",
"\n",
" def compose(f, g):\n",
" \"\"\"\n",
" Computes the functional composition of ``f`` and ``g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + x, x).compose(Poly(x - 1, x))\n",
" Poly(x**2 - x, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" _, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(f.rep, 'compose'):\n",
" result = F.compose(G)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'compose')\n",
"\n",
" return per(result)\n",
"\n",
" def decompose(f):\n",
" \"\"\"\n",
" Computes a functional decomposition of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**4 + 2*x**3 - x - 1, x, domain='ZZ').decompose()\n",
" [Poly(x**2 - x - 1, x, domain='ZZ'), Poly(x**2 + x, x, domain='ZZ')]\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'decompose'):\n",
" result = f.rep.decompose()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'decompose')\n",
"\n",
" return list(map(f.per, result))\n",
"\n",
" def shift(f, a):\n",
" \"\"\"\n",
" Efficiently compute Taylor shift ``f(x + a)``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 2*x + 1, x).shift(2)\n",
" Poly(x**2 + 2*x + 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'shift'):\n",
" result = f.rep.shift(a)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'shift')\n",
"\n",
" return f.per(result)\n",
"\n",
" def transform(f, p, q):\n",
" \"\"\"\n",
" Efficiently evaluate the functional transformation ``q**n * f(p/q)``.\n",
"\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 2*x + 1, x).transform(Poly(x + 1, x), Poly(x - 1, x))\n",
" Poly(4, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" P, Q = p.unify(q)\n",
" F, P = f.unify(P)\n",
" F, Q = F.unify(Q)\n",
"\n",
" if hasattr(F.rep, 'transform'):\n",
" result = F.rep.transform(P.rep, Q.rep)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(F, 'transform')\n",
"\n",
" return F.per(result)\n",
"\n",
" def sturm(self, auto=True):\n",
" \"\"\"\n",
" Computes the Sturm sequence of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**3 - 2*x**2 + x - 3, x).sturm()\n",
" [Poly(x**3 - 2*x**2 + x - 3, x, domain='QQ'),\n",
" Poly(3*x**2 - 4*x + 1, x, domain='QQ'),\n",
" Poly(2/9*x + 25/9, x, domain='QQ'),\n",
" Poly(-2079/4, x, domain='QQ')]\n",
"\n",
" \"\"\"\n",
" f = self\n",
"\n",
" if auto and f.rep.dom.is_Ring:\n",
" f = f.to_field()\n",
"\n",
" if hasattr(f.rep, 'sturm'):\n",
" result = f.rep.sturm()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'sturm')\n",
"\n",
" return list(map(f.per, result))\n",
"\n",
" def gff_list(f):\n",
" \"\"\"\n",
" Computes greatest factorial factorization of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f = x**5 + 2*x**4 - x**3 - 2*x**2\n",
"\n",
" >>> Poly(f).gff_list()\n",
" [(Poly(x, x, domain='ZZ'), 1), (Poly(x + 2, x, domain='ZZ'), 4)]\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'gff_list'):\n",
" result = f.rep.gff_list()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'gff_list')\n",
"\n",
" return [(f.per(g), k) for g, k in result]\n",
"\n",
" def norm(f):\n",
" \"\"\"\n",
" Computes the product, ``Norm(f)``, of the conjugates of\n",
" a polynomial ``f`` defined over a number field ``K``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, sqrt\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> a, b = sqrt(2), sqrt(3)\n",
"\n",
" A polynomial over a quadratic extension.\n",
" Two conjugates x - a and x + a.\n",
"\n",
" >>> f = Poly(x - a, x, extension=a)\n",
" >>> f.norm()\n",
" Poly(x**2 - 2, x, domain='QQ')\n",
"\n",
" A polynomial over a quartic extension.\n",
" Four conjugates x - a, x - a, x + a and x + a.\n",
"\n",
" >>> f = Poly(x - a, x, extension=(a, b))\n",
" >>> f.norm()\n",
" Poly(x**4 - 4*x**2 + 4, x, domain='QQ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'norm'):\n",
" r = f.rep.norm()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'norm')\n",
"\n",
" return f.per(r)\n",
"\n",
" def sqf_norm(f):\n",
" \"\"\"\n",
" Computes square-free norm of ``f``.\n",
"\n",
" Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and\n",
" ``r(x) = Norm(g(x))`` is a square-free polynomial over ``K``,\n",
" where ``a`` is the algebraic extension of the ground domain.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, sqrt\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> s, f, r = Poly(x**2 + 1, x, extension=[sqrt(3)]).sqf_norm()\n",
"\n",
" >>> s\n",
" 1\n",
" >>> f\n",
" Poly(x**2 - 2*sqrt(3)*x + 4, x, domain='QQ<sqrt(3)>')\n",
" >>> r\n",
" Poly(x**4 - 4*x**2 + 16, x, domain='QQ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'sqf_norm'):\n",
" s, g, r = f.rep.sqf_norm()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'sqf_norm')\n",
"\n",
" return s, f.per(g), f.per(r)\n",
"\n",
" def sqf_part(f):\n",
" \"\"\"\n",
" Computes square-free part of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**3 - 3*x - 2, x).sqf_part()\n",
" Poly(x**2 - x - 2, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'sqf_part'):\n",
" result = f.rep.sqf_part()\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'sqf_part')\n",
"\n",
" return f.per(result)\n",
"\n",
" def sqf_list(f, all=False):\n",
" \"\"\"\n",
" Returns a list of square-free factors of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16\n",
"\n",
" >>> Poly(f).sqf_list()\n",
" (2, [(Poly(x + 1, x, domain='ZZ'), 2),\n",
" (Poly(x + 2, x, domain='ZZ'), 3)])\n",
"\n",
" >>> Poly(f).sqf_list(all=True)\n",
" (2, [(Poly(1, x, domain='ZZ'), 1),\n",
" (Poly(x + 1, x, domain='ZZ'), 2),\n",
" (Poly(x + 2, x, domain='ZZ'), 3)])\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'sqf_list'):\n",
" coeff, factors = f.rep.sqf_list(all)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'sqf_list')\n",
"\n",
" return f.rep.dom.to_sympy(coeff), [(f.per(g), k) for g, k in factors]\n",
"\n",
" def sqf_list_include(f, all=False):\n",
" \"\"\"\n",
" Returns a list of square-free factors of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, expand\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f = expand(2*(x + 1)**3*x**4)\n",
" >>> f\n",
" 2*x**7 + 6*x**6 + 6*x**5 + 2*x**4\n",
"\n",
" >>> Poly(f).sqf_list_include()\n",
" [(Poly(2, x, domain='ZZ'), 1),\n",
" (Poly(x + 1, x, domain='ZZ'), 3),\n",
" (Poly(x, x, domain='ZZ'), 4)]\n",
"\n",
" >>> Poly(f).sqf_list_include(all=True)\n",
" [(Poly(2, x, domain='ZZ'), 1),\n",
" (Poly(1, x, domain='ZZ'), 2),\n",
" (Poly(x + 1, x, domain='ZZ'), 3),\n",
" (Poly(x, x, domain='ZZ'), 4)]\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'sqf_list_include'):\n",
" factors = f.rep.sqf_list_include(all)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'sqf_list_include')\n",
"\n",
" return [(f.per(g), k) for g, k in factors]\n",
"\n",
" def factor_list(f):\n",
" \"\"\"\n",
" Returns a list of irreducible factors of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y\n",
"\n",
" >>> Poly(f).factor_list()\n",
" (2, [(Poly(x + y, x, y, domain='ZZ'), 1),\n",
" (Poly(x**2 + 1, x, y, domain='ZZ'), 2)])\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'factor_list'):\n",
" try:\n",
" coeff, factors = f.rep.factor_list()\n",
" except DomainError:\n",
" return S.One, [(f, 1)]\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'factor_list')\n",
"\n",
" return f.rep.dom.to_sympy(coeff), [(f.per(g), k) for g, k in factors]\n",
"\n",
" def factor_list_include(f):\n",
" \"\"\"\n",
" Returns a list of irreducible factors of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y\n",
"\n",
" >>> Poly(f).factor_list_include()\n",
" [(Poly(2*x + 2*y, x, y, domain='ZZ'), 1),\n",
" (Poly(x**2 + 1, x, y, domain='ZZ'), 2)]\n",
"\n",
" \"\"\"\n",
" if hasattr(f.rep, 'factor_list_include'):\n",
" try:\n",
" factors = f.rep.factor_list_include()\n",
" except DomainError:\n",
" return [(f, 1)]\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'factor_list_include')\n",
"\n",
" return [(f.per(g), k) for g, k in factors]\n",
"\n",
" def intervals(f, all=False, eps=None, inf=None, sup=None, fast=False, sqf=False):\n",
" \"\"\"\n",
" Compute isolating intervals for roots of ``f``.\n",
"\n",
" For real roots the Vincent-Akritas-Strzebonski (VAS) continued fractions method is used.\n",
"\n",
" References\n",
" ==========\n",
" .. [#] Alkiviadis G. Akritas and Adam W. Strzebonski: A Comparative Study of Two Real Root\n",
" Isolation Methods . Nonlinear Analysis: Modelling and Control, Vol. 10, No. 4, 297-304, 2005.\n",
" .. [#] Alkiviadis G. Akritas, Adam W. Strzebonski and Panagiotis S. Vigklas: Improving the\n",
" Performance of the Continued Fractions Method Using new Bounds of Positive Roots. Nonlinear\n",
" Analysis: Modelling and Control, Vol. 13, No. 3, 265-279, 2008.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 3, x).intervals()\n",
" [((-2, -1), 1), ((1, 2), 1)]\n",
" >>> Poly(x**2 - 3, x).intervals(eps=1e-2)\n",
" [((-26/15, -19/11), 1), ((19/11, 26/15), 1)]\n",
"\n",
" \"\"\"\n",
" if eps is not None:\n",
" eps = QQ.convert(eps)\n",
"\n",
" if eps <= 0:\n",
" raise ValueError(\"'eps' must be a positive rational\")\n",
"\n",
" if inf is not None:\n",
" inf = QQ.convert(inf)\n",
" if sup is not None:\n",
" sup = QQ.convert(sup)\n",
"\n",
" if hasattr(f.rep, 'intervals'):\n",
" result = f.rep.intervals(\n",
" all=all, eps=eps, inf=inf, sup=sup, fast=fast, sqf=sqf)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'intervals')\n",
"\n",
" if sqf:\n",
" def _real(interval):\n",
" s, t = interval\n",
" return (QQ.to_sympy(s), QQ.to_sympy(t))\n",
"\n",
" if not all:\n",
" return list(map(_real, result))\n",
"\n",
" def _complex(rectangle):\n",
" (u, v), (s, t) = rectangle\n",
" return (QQ.to_sympy(u) + I*QQ.to_sympy(v),\n",
" QQ.to_sympy(s) + I*QQ.to_sympy(t))\n",
"\n",
" real_part, complex_part = result\n",
"\n",
" return list(map(_real, real_part)), list(map(_complex, complex_part))\n",
" else:\n",
" def _real(interval):\n",
" (s, t), k = interval\n",
" return ((QQ.to_sympy(s), QQ.to_sympy(t)), k)\n",
"\n",
" if not all:\n",
" return list(map(_real, result))\n",
"\n",
" def _complex(rectangle):\n",
" ((u, v), (s, t)), k = rectangle\n",
" return ((QQ.to_sympy(u) + I*QQ.to_sympy(v),\n",
" QQ.to_sympy(s) + I*QQ.to_sympy(t)), k)\n",
"\n",
" real_part, complex_part = result\n",
"\n",
" return list(map(_real, real_part)), list(map(_complex, complex_part))\n",
"\n",
" def refine_root(f, s, t, eps=None, steps=None, fast=False, check_sqf=False):\n",
" \"\"\"\n",
" Refine an isolating interval of a root to the given precision.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 3, x).refine_root(1, 2, eps=1e-2)\n",
" (19/11, 26/15)\n",
"\n",
" \"\"\"\n",
" if check_sqf and not f.is_sqf:\n",
" raise PolynomialError(\"only square-free polynomials supported\")\n",
"\n",
" s, t = QQ.convert(s), QQ.convert(t)\n",
"\n",
" if eps is not None:\n",
" eps = QQ.convert(eps)\n",
"\n",
" if eps <= 0:\n",
" raise ValueError(\"'eps' must be a positive rational\")\n",
"\n",
" if steps is not None:\n",
" steps = int(steps)\n",
" elif eps is None:\n",
" steps = 1\n",
"\n",
" if hasattr(f.rep, 'refine_root'):\n",
" S, T = f.rep.refine_root(s, t, eps=eps, steps=steps, fast=fast)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'refine_root')\n",
"\n",
" return QQ.to_sympy(S), QQ.to_sympy(T)\n",
"\n",
" def count_roots(f, inf=None, sup=None):\n",
" \"\"\"\n",
" Return the number of roots of ``f`` in ``[inf, sup]`` interval.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly, I\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**4 - 4, x).count_roots(-3, 3)\n",
" 2\n",
" >>> Poly(x**4 - 4, x).count_roots(0, 1 + 3*I)\n",
" 1\n",
"\n",
" \"\"\"\n",
" inf_real, sup_real = True, True\n",
"\n",
" if inf is not None:\n",
" inf = sympify(inf)\n",
"\n",
" if inf is S.NegativeInfinity:\n",
" inf = None\n",
" else:\n",
" re, im = inf.as_real_imag()\n",
"\n",
" if not im:\n",
" inf = QQ.convert(inf)\n",
" else:\n",
" inf, inf_real = list(map(QQ.convert, (re, im))), False\n",
"\n",
" if sup is not None:\n",
" sup = sympify(sup)\n",
"\n",
" if sup is S.Infinity:\n",
" sup = None\n",
" else:\n",
" re, im = sup.as_real_imag()\n",
"\n",
" if not im:\n",
" sup = QQ.convert(sup)\n",
" else:\n",
" sup, sup_real = list(map(QQ.convert, (re, im))), False\n",
"\n",
" if inf_real and sup_real:\n",
" if hasattr(f.rep, 'count_real_roots'):\n",
" count = f.rep.count_real_roots(inf=inf, sup=sup)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'count_real_roots')\n",
" else:\n",
" if inf_real and inf is not None:\n",
" inf = (inf, QQ.zero)\n",
"\n",
" if sup_real and sup is not None:\n",
" sup = (sup, QQ.zero)\n",
"\n",
" if hasattr(f.rep, 'count_complex_roots'):\n",
" count = f.rep.count_complex_roots(inf=inf, sup=sup)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'count_complex_roots')\n",
"\n",
" return Integer(count)\n",
"\n",
" def root(f, index, radicals=True):\n",
" \"\"\"\n",
" Get an indexed root of a polynomial.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f = Poly(2*x**3 - 7*x**2 + 4*x + 4)\n",
"\n",
" >>> f.root(0)\n",
" -1/2\n",
" >>> f.root(1)\n",
" 2\n",
" >>> f.root(2)\n",
" 2\n",
" >>> f.root(3)\n",
" Traceback (most recent call last):\n",
" ...\n",
" IndexError: root index out of [-3, 2] range, got 3\n",
"\n",
" >>> Poly(x**5 + x + 1).root(0)\n",
" CRootOf(x**3 - x**2 + 1, 0)\n",
"\n",
" \"\"\"\n",
" return sympy.polys.rootoftools.rootof(f, index, radicals=radicals)\n",
"\n",
" def real_roots(f, multiple=True, radicals=True):\n",
" \"\"\"\n",
" Return a list of real roots with multiplicities.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(2*x**3 - 7*x**2 + 4*x + 4).real_roots()\n",
" [-1/2, 2, 2]\n",
" >>> Poly(x**3 + x + 1).real_roots()\n",
" [CRootOf(x**3 + x + 1, 0)]\n",
"\n",
" \"\"\"\n",
" reals = sympy.polys.rootoftools.CRootOf.real_roots(f, radicals=radicals)\n",
"\n",
" if multiple:\n",
" return reals\n",
" else:\n",
" return group(reals, multiple=False)\n",
"\n",
" def all_roots(f, multiple=True, radicals=True):\n",
" \"\"\"\n",
" Return a list of real and complex roots with multiplicities.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(2*x**3 - 7*x**2 + 4*x + 4).all_roots()\n",
" [-1/2, 2, 2]\n",
" >>> Poly(x**3 + x + 1).all_roots()\n",
" [CRootOf(x**3 + x + 1, 0),\n",
" CRootOf(x**3 + x + 1, 1),\n",
" CRootOf(x**3 + x + 1, 2)]\n",
"\n",
" \"\"\"\n",
" roots = sympy.polys.rootoftools.CRootOf.all_roots(f, radicals=radicals)\n",
"\n",
" if multiple:\n",
" return roots\n",
" else:\n",
" return group(roots, multiple=False)\n",
"\n",
" def nroots(f, n=15, maxsteps=50, cleanup=True):\n",
" \"\"\"\n",
" Compute numerical approximations of roots of ``f``.\n",
"\n",
" Parameters\n",
" ==========\n",
"\n",
" n ... the number of digits to calculate\n",
" maxsteps ... the maximum number of iterations to do\n",
"\n",
" If the accuracy `n` cannot be reached in `maxsteps`, it will raise an\n",
" exception. You need to rerun with higher maxsteps.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 3).nroots(n=15)\n",
" [-1.73205080756888, 1.73205080756888]\n",
" >>> Poly(x**2 - 3).nroots(n=30)\n",
" [-1.73205080756887729352744634151, 1.73205080756887729352744634151]\n",
"\n",
" \"\"\"\n",
" from sympy.functions.elementary.complexes import sign\n",
" if f.is_multivariate:\n",
" raise MultivariatePolynomialError(\n",
" \"can't compute numerical roots of %s\" % f)\n",
"\n",
" if f.degree() <= 0:\n",
" return []\n",
"\n",
" # For integer and rational coefficients, convert them to integers only\n",
" # (for accuracy). Otherwise just try to convert the coefficients to\n",
" # mpmath.mpc and raise an exception if the conversion fails.\n",
" if f.rep.dom is ZZ:\n",
" coeffs = [int(coeff) for coeff in f.all_coeffs()]\n",
" elif f.rep.dom is QQ:\n",
" denoms = [coeff.q for coeff in f.all_coeffs()]\n",
" from sympy.core.numbers import ilcm\n",
" fac = ilcm(*denoms)\n",
" coeffs = [int(coeff*fac) for coeff in f.all_coeffs()]\n",
" else:\n",
" coeffs = [coeff.evalf(n=n).as_real_imag()\n",
" for coeff in f.all_coeffs()]\n",
" try:\n",
" coeffs = [mpmath.mpc(*coeff) for coeff in coeffs]\n",
" except TypeError:\n",
" raise DomainError(\"Numerical domain expected, got %s\" % \\\n",
" f.rep.dom)\n",
"\n",
" dps = mpmath.mp.dps\n",
" mpmath.mp.dps = n\n",
"\n",
" try:\n",
" # We need to add extra precision to guard against losing accuracy.\n",
" # 10 times the degree of the polynomial seems to work well.\n",
" roots = mpmath.polyroots(coeffs, maxsteps=maxsteps,\n",
" cleanup=cleanup, error=False, extraprec=f.degree()*10)\n",
"\n",
" # Mpmath puts real roots first, then complex ones (as does all_roots)\n",
" # so we make sure this convention holds here, too.\n",
" roots = list(map(sympify,\n",
" sorted(roots, key=lambda r: (1 if r.imag else 0, r.real, abs(r.imag), sign(r.imag)))))\n",
" except NoConvergence:\n",
" raise NoConvergence(\n",
" 'convergence to root failed; try n < %s or maxsteps > %s' % (\n",
" n, maxsteps))\n",
" finally:\n",
" mpmath.mp.dps = dps\n",
"\n",
" return roots\n",
"\n",
" def ground_roots(f):\n",
" \"\"\"\n",
" Compute roots of ``f`` by factorization in the ground domain.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**6 - 4*x**4 + 4*x**3 - x**2).ground_roots()\n",
" {0: 2, 1: 2}\n",
"\n",
" \"\"\"\n",
" if f.is_multivariate:\n",
" raise MultivariatePolynomialError(\n",
" \"can't compute ground roots of %s\" % f)\n",
"\n",
" roots = {}\n",
"\n",
" for factor, k in f.factor_list()[1]:\n",
" if factor.is_linear:\n",
" a, b = factor.all_coeffs()\n",
" roots[-b/a] = k\n",
"\n",
" return roots\n",
"\n",
" def nth_power_roots_poly(f, n):\n",
" \"\"\"\n",
" Construct a polynomial with n-th powers of roots of ``f``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f = Poly(x**4 - x**2 + 1)\n",
"\n",
" >>> f.nth_power_roots_poly(2)\n",
" Poly(x**4 - 2*x**3 + 3*x**2 - 2*x + 1, x, domain='ZZ')\n",
" >>> f.nth_power_roots_poly(3)\n",
" Poly(x**4 + 2*x**2 + 1, x, domain='ZZ')\n",
" >>> f.nth_power_roots_poly(4)\n",
" Poly(x**4 + 2*x**3 + 3*x**2 + 2*x + 1, x, domain='ZZ')\n",
" >>> f.nth_power_roots_poly(12)\n",
" Poly(x**4 - 4*x**3 + 6*x**2 - 4*x + 1, x, domain='ZZ')\n",
"\n",
" \"\"\"\n",
" if f.is_multivariate:\n",
" raise MultivariatePolynomialError(\n",
" \"must be a univariate polynomial\")\n",
"\n",
" N = sympify(n)\n",
"\n",
" if N.is_Integer and N >= 1:\n",
" n = int(N)\n",
" else:\n",
" raise ValueError(\"'n' must an integer and n >= 1, got %s\" % n)\n",
"\n",
" x = f.gen\n",
" t = Dummy('t')\n",
"\n",
" r = f.resultant(f.__class__.from_expr(x**n - t, x, t))\n",
"\n",
" return r.replace(t, x)\n",
"\n",
" def cancel(f, g, include=False):\n",
" \"\"\"\n",
" Cancel common factors in a rational function ``f/g``.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x))\n",
" (1, Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ'))\n",
"\n",
" >>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x), include=True)\n",
" (Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ'))\n",
"\n",
" \"\"\"\n",
" dom, per, F, G = f._unify(g)\n",
"\n",
" if hasattr(F, 'cancel'):\n",
" result = F.cancel(G, include=include)\n",
" else: # pragma: no cover\n",
" raise OperationNotSupported(f, 'cancel')\n",
"\n",
" if not include:\n",
" if dom.has_assoc_Ring:\n",
" dom = dom.get_ring()\n",
"\n",
" cp, cq, p, q = result\n",
"\n",
" cp = dom.to_sympy(cp)\n",
" cq = dom.to_sympy(cq)\n",
"\n",
" return cp/cq, per(p), per(q)\n",
" else:\n",
" return tuple(map(per, result))\n",
"\n",
" @property\n",
" def is_zero(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` is a zero polynomial.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(0, x).is_zero\n",
" True\n",
" >>> Poly(1, x).is_zero\n",
" False\n",
"\n",
" \"\"\"\n",
" return f.rep.is_zero\n",
"\n",
" @property\n",
" def is_one(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` is a unit polynomial.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(0, x).is_one\n",
" False\n",
" >>> Poly(1, x).is_one\n",
" True\n",
"\n",
" \"\"\"\n",
" return f.rep.is_one\n",
"\n",
" @property\n",
" def is_sqf(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` is a square-free polynomial.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 - 2*x + 1, x).is_sqf\n",
" False\n",
" >>> Poly(x**2 - 1, x).is_sqf\n",
" True\n",
"\n",
" \"\"\"\n",
" return f.rep.is_sqf\n",
"\n",
" @property\n",
" def is_monic(f):\n",
" \"\"\"\n",
" Returns ``True`` if the leading coefficient of ``f`` is one.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x + 2, x).is_monic\n",
" True\n",
" >>> Poly(2*x + 2, x).is_monic\n",
" False\n",
"\n",
" \"\"\"\n",
" return f.rep.is_monic\n",
"\n",
" @property\n",
" def is_primitive(f):\n",
" \"\"\"\n",
" Returns ``True`` if GCD of the coefficients of ``f`` is one.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(2*x**2 + 6*x + 12, x).is_primitive\n",
" False\n",
" >>> Poly(x**2 + 3*x + 6, x).is_primitive\n",
" True\n",
"\n",
" \"\"\"\n",
" return f.rep.is_primitive\n",
"\n",
" @property\n",
" def is_ground(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` is an element of the ground domain.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x, x).is_ground\n",
" False\n",
" >>> Poly(2, x).is_ground\n",
" True\n",
" >>> Poly(y, x).is_ground\n",
" True\n",
"\n",
" \"\"\"\n",
" return f.rep.is_ground\n",
"\n",
" @property\n",
" def is_linear(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` is linear in all its variables.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x + y + 2, x, y).is_linear\n",
" True\n",
" >>> Poly(x*y + 2, x, y).is_linear\n",
" False\n",
"\n",
" \"\"\"\n",
" return f.rep.is_linear\n",
"\n",
" @property\n",
" def is_quadratic(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` is quadratic in all its variables.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x*y + 2, x, y).is_quadratic\n",
" True\n",
" >>> Poly(x*y**2 + 2, x, y).is_quadratic\n",
" False\n",
"\n",
" \"\"\"\n",
" return f.rep.is_quadratic\n",
"\n",
" @property\n",
" def is_monomial(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` is zero or has only one term.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(3*x**2, x).is_monomial\n",
" True\n",
" >>> Poly(3*x**2 + 1, x).is_monomial\n",
" False\n",
"\n",
" \"\"\"\n",
" return f.rep.is_monomial\n",
"\n",
" @property\n",
" def is_homogeneous(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` is a homogeneous polynomial.\n",
"\n",
" A homogeneous polynomial is a polynomial whose all monomials with\n",
" non-zero coefficients have the same total degree. If you want not\n",
" only to check if a polynomial is homogeneous but also compute its\n",
" homogeneous order, then use :func:`Poly.homogeneous_order`.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + x*y, x, y).is_homogeneous\n",
" True\n",
" >>> Poly(x**3 + x*y, x, y).is_homogeneous\n",
" False\n",
"\n",
" \"\"\"\n",
" return f.rep.is_homogeneous\n",
"\n",
" @property\n",
" def is_irreducible(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` has no factors over its domain.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> Poly(x**2 + x + 1, x, modulus=2).is_irreducible\n",
" True\n",
" >>> Poly(x**2 + 1, x, modulus=2).is_irreducible\n",
" False\n",
"\n",
" \"\"\"\n",
" return f.rep.is_irreducible\n",
"\n",
" @property\n",
" def is_univariate(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` is a univariate polynomial.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + x + 1, x).is_univariate\n",
" True\n",
" >>> Poly(x*y**2 + x*y + 1, x, y).is_univariate\n",
" False\n",
" >>> Poly(x*y**2 + x*y + 1, x).is_univariate\n",
" True\n",
" >>> Poly(x**2 + x + 1, x, y).is_univariate\n",
" False\n",
"\n",
" \"\"\"\n",
" return len(f.gens) == 1\n",
"\n",
" @property\n",
" def is_multivariate(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` is a multivariate polynomial.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x, y\n",
"\n",
" >>> Poly(x**2 + x + 1, x).is_multivariate\n",
" False\n",
" >>> Poly(x*y**2 + x*y + 1, x, y).is_multivariate\n",
" True\n",
" >>> Poly(x*y**2 + x*y + 1, x).is_multivariate\n",
" False\n",
" >>> Poly(x**2 + x + 1, x, y).is_multivariate\n",
" True\n",
"\n",
" \"\"\"\n",
" return len(f.gens) != 1\n",
"\n",
" @property\n",
" def is_cyclotomic(f):\n",
" \"\"\"\n",
" Returns ``True`` if ``f`` is a cyclotomic polnomial.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> from sympy import Poly\n",
" >>> from sympy.abc import x\n",
"\n",
" >>> f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1\n",
"\n",
" >>> Poly(f).is_cyclotomic\n",
" False\n",
"\n",
" >>> g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1\n",
"\n",
" >>> Poly(g).is_cyclotomic\n",
" True\n",
"\n",
" \"\"\"\n",
" return f.rep.is_cyclotomic\n",
"\n",
" def __abs__(f):\n",
" return f.abs()\n",
"\n",
" def __neg__(f):\n",
" return f.neg()\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __add__(f, g):\n",
" if not g.is_Poly:\n",
" try:\n",
" g = f.__class__(g, *f.gens)\n",
" except PolynomialError:\n",
" return f.as_expr() + g\n",
"\n",
" return f.add(g)\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __radd__(f, g):\n",
" if not g.is_Poly:\n",
" try:\n",
" g = f.__class__(g, *f.gens)\n",
" except PolynomialError:\n",
" return g + f.as_expr()\n",
"\n",
" return g.add(f)\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __sub__(f, g):\n",
" if not g.is_Poly:\n",
" try:\n",
" g = f.__class__(g, *f.gens)\n",
" except PolynomialError:\n",
" return f.as_expr() - g\n",
"\n",
" return f.sub(g)\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __rsub__(f, g):\n",
" if not g.is_Poly:\n",
" try:\n",
" g = f.__class__(g, *f.gens)\n",
" except PolynomialError:\n",
" return g - f.as_expr()\n",
"\n",
" return g.sub(f)\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __mul__(f, g):\n",
" if not g.is_Poly:\n",
" try:\n",
" g = f.__class__(g, *f.gens)\n",
" except PolynomialError:\n",
" return f.as_expr()*g\n",
"\n",
" return f.mul(g)\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __rmul__(f, g):\n",
" if not g.is_Poly:\n",
" try:\n",
" g = f.__class__(g, *f.gens)\n",
" except PolynomialError:\n",
" return g*f.as_expr()\n",
"\n",
" return g.mul(f)\n",
"\n",
" @_sympifyit('n', NotImplemented)\n",
" def __pow__(f, n):\n",
" if n.is_Integer and n >= 0:\n",
" return f.pow(n)\n",
" else:\n",
" return f.as_expr()**n\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __divmod__(f, g):\n",
" if not g.is_Poly:\n",
" g = f.__class__(g, *f.gens)\n",
"\n",
" return f.div(g)\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __rdivmod__(f, g):\n",
" if not g.is_Poly:\n",
" g = f.__class__(g, *f.gens)\n",
"\n",
" return g.div(f)\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __mod__(f, g):\n",
" if not g.is_Poly:\n",
" g = f.__class__(g, *f.gens)\n",
"\n",
" return f.rem(g)\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __rmod__(f, g):\n",
" if not g.is_Poly:\n",
" g = f.__class__(g, *f.gens)\n",
"\n",
" return g.rem(f)\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __floordiv__(f, g):\n",
" if not g.is_Poly:\n",
" g = f.__class__(g, *f.gens)\n",
"\n",
" return f.quo(g)\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __rfloordiv__(f, g):\n",
" if not g.is_Poly:\n",
" g = f.__class__(g, *f.gens)\n",
"\n",
" return g.quo(f)\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __div__(f, g):\n",
" return f.as_expr()/g.as_expr()\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __rdiv__(f, g):\n",
" return g.as_expr()/f.as_expr()\n",
"\n",
" __truediv__ = __div__\n",
" __rtruediv__ = __rdiv__\n",
"\n",
" @_sympifyit('other', NotImplemented)\n",
" def __eq__(self, other):\n",
" f, g = self, other\n",
"\n",
" if not g.is_Poly:\n",
" try:\n",
" g = f.__class__(g, f.gens, domain=f.get_domain())\n",
" except (PolynomialError, DomainError, CoercionFailed):\n",
" return False\n",
"\n",
" if f.gens != g.gens:\n",
" return False\n",
"\n",
" if f.rep.dom != g.rep.dom:\n",
" try:\n",
" dom = f.rep.dom.unify(g.rep.dom, f.gens)\n",
" except UnificationFailed:\n",
" return False\n",
"\n",
" f = f.set_domain(dom)\n",
" g = g.set_domain(dom)\n",
"\n",
" return f.rep == g.rep\n",
"\n",
" @_sympifyit('g', NotImplemented)\n",
" def __ne__(f, g):\n",
" return not f == g\n",
"\n",
" def __nonzero__(f):\n",
" return not f.is_zero\n",
"\n",
" __bool__ = __nonzero__\n",
"\n",
" def eq(f, g, strict=False):\n",
" if not strict:\n",
" return f == g\n",
" else:\n",
" return f._strict_eq(sympify(g))\n",
"\n",
" def ne(f, g, strict=False):\n",
" return not f.eq(g, strict=strict)\n",
"\n",
" def _strict_eq(f, g):\n",
" return isinstance(g, f.__class__) and f.gens == g.gens and f.rep.eq(g.rep, strict=True)\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/sympy/core/decorators.py:38: SymPyDeprecationWarning: \n",
"\n",
"source has been deprecated since SymPy 1.3. Use ?? in IPython/Jupyter\n",
"or inspect.getsource instead. See\n",
"https://github.com/sympy/sympy/issues/14905 for more info.\n",
"\n",
" _warn_deprecation(wrapped, 3)\n"
]
}
],
"source": [
"source(Poly)"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\operatorname{Poly}{\\left( x + \\sqrt{x}, \\sqrt{x}, domain=\\mathbb{Z} \\right)}$"
],
"text/plain": [
"Poly((sqrt(x))**2 + (sqrt(x)), sqrt(x), domain='ZZ')"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly({1:1,2:1},gens=S('sqrt(x)'))"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle - x + \\sqrt{x^{2}}$"
],
"text/plain": [
"-x + sqrt(x**2)"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=symbols('x', positive=True)\n",
"S('sqrt(x^2)-x')"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Poly((sqrt(x))**4 + (sqrt(x))**2 + (sqrt(x)), sqrt(x), domain='ZZ')\""
]
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str(Poly({1:1,2:1,4:1},gens=S('sqrt(x)')))"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Poly(x**2 + x + (sqrt(x)), x, sqrt(x), domain='ZZ')\""
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str(Poly('x^2+x+sqrt(x)'))"
]
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Substitute $a\\to a+b$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $b\\to b+c$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimization terminated successfully.\n",
" Current function value: 1.000000\n",
" Iterations: 137\n",
" Function evaluations: 249\n"
]
},
{
"data": {
"text/plain": [
"(2.5326984818340415e-10, 7.129450063690368, 1.7908873553542452e-10)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"1.4142142855953455"
]
},
"execution_count": 164,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def findvalues(formula,values=None,variables=None):\n",
" num,den=fractioncancel(formula)\n",
" if variables==None:\n",
" variables=sorted(num.free_symbols,key=str)\n",
" num=num.subs(zip(variables,list(map(lambda x:x**2,fs))))\n",
" num=Poly(num)\n",
" newformula=S((num.abs()+num)/(num.abs()-num))\n",
" f=lambdify(variables,newformula)\n",
" f2=lambda x:f(*x)\n",
" if values==None:\n",
" values=[1.0]*len(variables)\n",
" tup=tuple(fmin(f2,values))\n",
" return tuple([x*x for x in tup])\n",
"formula=cyclize(Sm('((a-b)/c)^2-8**(1/2)*(a-b)/c'))\n",
"formula=(makesubs(formula,'[b,oo],[c,oo]'))\n",
"values=findvalues(formula)\n",
"display(values)\n",
"nsimplify(values,tolerance=0.1,rational=True)\n",
"values[0]/values[2]"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"Substitute $a\\to a+b$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"Substitute $b\\to b+c$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"ok\n",
"100 loops, best of 5: 9.06 ms per loop\n"
]
},
{
"ename": "TypeError",
"evalue": "int() argument must be a string, a bytes-like object or a number, not 'dict'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-156-45f6965e4d63>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'ok'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'timeit evaluate((random(),random(),random()))'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'timeit evaluate2((random(),random(),random()))'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 19\u001b[0m \u001b[0mPoly\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'x^2+sqrt(x)'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meval\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py\u001b[0m in \u001b[0;36mmagic\u001b[0;34m(self, arg_s)\u001b[0m\n\u001b[1;32m 2158\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marg_s\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpartition\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m' '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2159\u001b[0m \u001b[0mmagic_name\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlstrip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprefilter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mESC_MAGIC\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2160\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_line_magic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmagic_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2161\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2162\u001b[0m \u001b[0;31m#-------------------------------------------------------------------------\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py\u001b[0m in \u001b[0;36mrun_line_magic\u001b[0;34m(self, magic_name, line)\u001b[0m\n\u001b[1;32m 2079\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'local_ns'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getframe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstack_depth\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf_locals\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2080\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuiltin_trap\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2081\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2082\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2083\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m</home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/decorator.py:decorator-gen-59>\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, line, cell)\u001b[0m\n",
"\u001b[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/IPython/core/magic.py\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(f, *a, **k)\u001b[0m\n\u001b[1;32m 186\u001b[0m \u001b[0;31m# but it's overkill for just that one bit of state.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 187\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmagic_deco\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 188\u001b[0;31m \u001b[0mcall\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 189\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 190\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/IPython/core/magics/execution.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, line, cell)\u001b[0m\n\u001b[1;32m 1055\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1056\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0m_\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1057\u001b[0;31m \u001b[0mtime_number\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtimer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1058\u001b[0m \u001b[0mworst_tuning\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mworst_tuning\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtime_number\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1059\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtime_number\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0;36m0.2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/IPython/core/magics/execution.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, number)\u001b[0m\n\u001b[1;32m 137\u001b[0m \u001b[0mgc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 139\u001b[0;31m \u001b[0mtiming\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minner\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 140\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 141\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mgcold\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<magic-timeit>\u001b[0m in \u001b[0;36minner\u001b[0;34m(_it, _timer)\u001b[0m\n",
"\u001b[0;32m<ipython-input-156-45f6965e4d63>\u001b[0m in \u001b[0;36mevaluate2\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mnewformula\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mevalf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msubs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mevaluate2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mnum1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mevalf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mnum2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mevalf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 15\u001b[0m \u001b[0;31m#display(num2.eval(dict(zip(fs,(1,1,1)))))\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'ok'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/sympy/core/evalf.py\u001b[0m in \u001b[0;36mevalf\u001b[0;34m(self, n, subs, maxn, chop, strict, quad, verbose)\u001b[0m\n\u001b[1;32m 1432\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mevalf_table\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1433\u001b[0m \u001b[0m_create_evalf_table\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1434\u001b[0;31m \u001b[0mprec\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdps_to_prec\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1435\u001b[0m options = {'maxprec': max(prec, int(maxn*LG10)), 'chop': chop,\n\u001b[1;32m 1436\u001b[0m 'strict': strict, 'verbose': verbose}\n",
"\u001b[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/mpmath/libmp/libmpf.py\u001b[0m in \u001b[0;36mdps_to_prec\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 65\u001b[0m \"\"\"Return the number of bits required to represent n decimals\n\u001b[1;32m 66\u001b[0m accurately.\"\"\"\n\u001b[0;32m---> 67\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mround\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m3.3219280948873626\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 68\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mrepr_dps\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: int() argument must be a string, a bytes-like object or a number, not 'dict'"
]
}
],
"source": [
"from random import random\n",
"formula=cyclize(Sm('((a-b)/c)^2-8**(1/2)*(a-b)/c'))\n",
"formula=(makesubs(formula,'[b,oo],[c,oo]'))\n",
"num,den=fractioncancel(formula)\n",
"fs=sorted(num.free_symbols,key=str)\n",
"num=num.subs(zip(fs,list(map(lambda x:x**2,fs))))\n",
"num=Poly(num,domain='RR')\n",
"num1=(num.abs()+num)\n",
"num2=(num.abs()-num)\n",
"newformula=S(num1/num2)\n",
"def evaluate(x):\n",
" return newformula.evalf(subs=dict(zip(fs,(1,1,1))))\n",
"def evaluate2(x):\n",
" return num1.evalf(dict(zip(fs,x)))/num2.eval(dict(zip(fs,x)))\n",
"#display(num2.eval(dict(zip(fs,(1,1,1)))))\n",
"print('ok')\n",
"%timeit evaluate((random(),random(),random()))\n",
"%timeit evaluate2((random(),random(),random()))\n",
"Poly('x^2+sqrt(x)').eval\n"
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100000 loops, best of 5: 6.56 µs per loop\n",
"100000 loops, best of 5: 6.39 µs per loop\n"
]
}
],
"source": [
"evaluate4=lambdify(fs,newformula)\n",
"evaluate5=lambda x:evaluate4(*x)\n",
"%timeit evaluate4(*(random(),random(),random()))\n",
"%timeit evaluate5((random(),random(),random()))"
]
},
{
"cell_type": "code",
"execution_count": 172,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"first attempt: Poly(x**2 + x + (sqrt(x)), x, sqrt(x), domain='ZZ')\n",
"second attempt: Poly(x**2 + x + (sqrt(x)), x, sqrt(x), domain='ZZ')\n",
"third attempt: Poly((sqrt(x))**4 + (sqrt(x))**2 + (sqrt(x)), sqrt(x), domain='ZZ')\n"
]
}
],
"source": [
"from sympy import *\n",
"print('first attempt:',Poly('x^2+x+sqrt(x)'))\n",
"x=Symbol('x', positive=True)\n",
"print('second attempt:',Poly(x**2+x+sqrt(x)))\n",
"print('third attempt:',str(Poly({1:1,2:1,4:1},gens=S('sqrt(x)'))))"
]
},
{
"cell_type": "code",
"execution_count": 176,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\sqrt{x} + x$"
],
"text/plain": [
"sqrt(x) + x"
]
},
"execution_count": 176,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(Poly({1:1,2:1,4:1},gens=S('sqrt(x)'))-Poly('x^2')).as_expr()"
]
},
{
"cell_type": "code",
"execution_count": 178,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(x*(1 - x), [])"
]
},
"execution_count": 178,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sympy.solvers.solvers import unrad\n",
"unrad('sqrt(x)+x')"
]
},
{
"cell_type": "code",
"execution_count": 206,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 206,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"formula=('x^(2/3)+x^(3/4)+sqrt(x+y)')\n",
"type((Poly(formula).gens[1]).args[0])==Symbol"
]
},
{
"cell_type": "code",
"execution_count": 223,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(x, 1/2)"
]
},
"execution_count": 223,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"S('sqrt(x)').args"
]
},
{
"cell_type": "code",
"execution_count": 222,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\operatorname{Poly}{\\left( x^{2} + y^{2} + \\sqrt{x + y} + \\sqrt{x} + \\sqrt[3]{x_{12}}, x, y, \\sqrt{x + y}, \\sqrt{x}, \\sqrt[3]{x_{12}}, domain=\\mathbb{Z} \\right)}$"
],
"text/plain": [
"Poly(x**2 + y**2 + (sqrt(x + y)) + (sqrt(x)) + (x12**(1/3)), x, y, sqrt(x + y), sqrt(x), x12**(1/3), domain='ZZ')"
]
},
"execution_count": 222,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def _powr(formula):\n",
"\tif formula.func==Pow:\n",
"\t\treturn formula.args\n",
"\telse:\n",
"\t\treturn [formula,S('1')]\n",
"pol=Poly('x**2+y**2+sqrt(x+y)+x**(1/2)+x**(1/3)')\n",
"genes=pol.gens\n",
"newgens={}\n",
"for gen in genes:\n",
" gen=_powr(gen):\n",
" if gen[0] in newgens:\n",
" newgens[gen[0]]=\n",
" else:\n",
" newgens[gen[0]]=gen[1]"
]
},
{
"cell_type": "code",
"execution_count": 227,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"sympy.core.add.Add"
]
},
"execution_count": 227,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"S('x+y*6').func=="
]
},
{
"cell_type": "code",
"execution_count": 229,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 229,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"6 in {3:4,5:6}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}