{ "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": 18, "metadata": {}, "outputs": [], "source": [ "from shiroindev import *\n", "from sympy import *\n", "from itertools import permutations, combinations\n", "shiro.seed=1\n", "from IPython.display import Latex\n", "shiro.display=lambda x:display(Latex(x))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "__________________________" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Latex(r'__________________________')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Substitute $a\\to s-\\frac{s}{d+1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $b\\to s-\\frac{s}{e+1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $c\\to s-\\frac{s}{f+1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "numerator: $defs^2+s^2$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "denominator: $def+de+df+d+ef+e+f+1$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "status: 0" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$ 0 \\le defs^2+s^2 $$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "The sum of all inequalities gives us a proof of the inequality." ], "text/plain": [ "" ] }, "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 $x\\to g+y$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$g^{2}$$" ], "text/plain": [ "" ] }, "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 $y\\to h+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "4 $$h^{2} \\left(h + x\\right)^{t} - h x x^{t} + h x z^{t} + h x \\left(h + x\\right)^{t} + h x^{t} z - h z z^{t} - h z \\left(h + x\\right)^{t} + x^{2} z^{t} - 2 x z z^{t} + z^{2} z^{t}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $z\\to i+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "4 $$i^{2} \\left(i + x\\right)^{t} - i x x^{t} + i x y^{t} + i x \\left(i + x\\right)^{t} + i x^{t} y - i y y^{t} - i y \\left(i + x\\right)^{t} + x^{2} y^{t} - 2 x y y^{t} + y^{2} y^{t}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $z\\to j+y$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "4 $$j^{2} \\left(j + y\\right)^{t} - j x x^{t} + j x y^{t} - j x \\left(j + y\\right)^{t} + j x^{t} y - j y y^{t} + j y \\left(j + y\\right)^{t} + x^{2} x^{t} - 2 x x^{t} y + x^{t} y^{2}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $y\\to k+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $z\\to l+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "4 $$k^{2} \\left(k + x\\right)^{t} + k l x^{t} - k l \\left(k + x\\right)^{t} - k l \\left(l + x\\right)^{t} + l^{2} \\left(l + x\\right)^{t}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $l\\to k+m$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "3 $$k^{2} x^{t} + k m x^{t} - k m \\left(k + x\\right)^{t} + k m \\left(k + m + x\\right)^{t} + m^{2} \\left(k + m + x\\right)^{t}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $z\\to n+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $y\\to o+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "4 $$n^{2} \\left(n + x\\right)^{t} + n o x^{t} - n o \\left(n + x\\right)^{t} - n o \\left(o + x\\right)^{t} + o^{2} \\left(o + x\\right)^{t}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $o\\to n+p$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "3 $$n^{2} x^{t} + n p x^{t} - n p \\left(n + x\\right)^{t} + n p \\left(n + p + x\\right)^{t} + p^{2} \\left(n + p + x\\right)^{t}$$" ], "text/plain": [ "" ] }, "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 q+y$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\displaystyle q^{2} \\left(q + y\\right)^{t} - q y y^{t} + q y z^{t} + q y \\left(q + y\\right)^{t} + q y^{t} z - q z z^{t} - q z \\left(q + y\\right)^{t} + y^{2} z^{t} - 2 y z z^{t} + z^{2} z^{t}$" ], "text/plain": [ "q**2*(q + y)**t - q*y*y**t + q*y*z**t + q*y*(q + y)**t + q*y**t*z - q*z*z**t - q*z*(q + 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 r+y$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\displaystyle r^{2} \\left(r + y\\right)^{t} - r y y^{t} + r y z^{t} + r y \\left(r + y\\right)^{t} + r y^{t} z - r z z^{t} - r z \\left(r + y\\right)^{t} + y^{2} z^{t} - 2 y z z^{t} + z^{2} z^{t}$" ], "text/plain": [ "r**2*(r + y)**t - r*y*y**t + r*y*z**t + r*y*(r + y)**t + r*y**t*z - r*z*z**t - r*z*(r + 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 $y\\to u+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "4 $$u^{2} \\left(u + x\\right)^{t} - u x x^{t} + u x z^{t} + u x \\left(u + x\\right)^{t} + u x^{t} z - u z z^{t} - u z \\left(u + x\\right)^{t} + x^{2} z^{t} - 2 x z z^{t} + z^{2} z^{t}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $z\\to v+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "4 $$v^{2} \\left(v + x\\right)^{t} - v x x^{t} + v x y^{t} + v x \\left(v + x\\right)^{t} + v x^{t} y - v y y^{t} - v y \\left(v + x\\right)^{t} + x^{2} y^{t} - 2 x y y^{t} + y^{2} y^{t}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $z\\to w+y$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "4 $$w^{2} \\left(w + y\\right)^{t} - w x x^{t} + w x y^{t} - w x \\left(w + y\\right)^{t} + w x^{t} y - w y y^{t} + w y \\left(w + y\\right)^{t} + x^{2} x^{t} - 2 x x^{t} y + x^{t} y^{2}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $y\\to a_{1}+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $z\\to b_{1}+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "4 $$a_{1}^{2} \\left(a_{1} + x\\right)^{t} + a_{1} b_{1} x^{t} - a_{1} b_{1} \\left(a_{1} + x\\right)^{t} - a_{1} b_{1} \\left(b_{1} + x\\right)^{t} + b_{1}^{2} \\left(b_{1} + x\\right)^{t}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $b_{1}\\to a_{1}+c_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "3 $$a_{1}^{2} x^{t} + a_{1} c_{1} x^{t} - a_{1} c_{1} \\left(a_{1} + x\\right)^{t} + a_{1} c_{1} \\left(a_{1} + c_{1} + x\\right)^{t} + c_{1}^{2} \\left(a_{1} + c_{1} + x\\right)^{t}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $z\\to d_{1}+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $y\\to e_{1}+x$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "4 $$d_{1}^{2} \\left(d_{1} + x\\right)^{t} + d_{1} e_{1} x^{t} - d_{1} e_{1} \\left(d_{1} + x\\right)^{t} - d_{1} e_{1} \\left(e_{1} + x\\right)^{t} + e_{1}^{2} \\left(e_{1} + x\\right)^{t}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $e_{1}\\to d_{1}+f_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "3 $$d_{1}^{2} x^{t} + d_{1} f_{1} x^{t} - d_{1} f_{1} \\left(d_{1} + x\\right)^{t} + d_{1} f_{1} \\left(d_{1} + f_{1} + x\\right)^{t} + f_{1}^{2} \\left(d_{1} + f_{1} + x\\right)^{t}$$" ], "text/plain": [ "" ] }, "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 11g_{1}/8$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $b\\to 4h_{1}/5$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $c\\to 4i_{1}/5$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $d\\to 4j_{1}/5$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "numerator: $3025g_{1}^2-1760g_{1}h_{1}-1760g_{1}i_{1}-1760g_{1}j_{1}+1024h_{1}^2+1024i_{1}^2+1024j_{1}^2$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "denominator: $1600$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "status: 0" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "From weighted AM-GM inequality:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$1760g_{1}h_{1} \\le 880g_{1}^2+880h_{1}^2$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$1760g_{1}i_{1} \\le 880g_{1}^2+880i_{1}^2$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$1760g_{1}j_{1} \\le 880g_{1}^2+880j_{1}^2$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$ 0 \\le 385g_{1}^2+144h_{1}^2+144i_{1}^2+144j_{1}^2 $$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "The sum of all inequalities gives us a proof of the inequality." ], "text/plain": [ "" ] }, "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/plain": [ "\"formula=cyclize(Sm('((a-b)/c)^2-8**(1/2)*(a-b)/c'))\\ndisplay(formula)\\nfrom scipy.optimize import fmin\\nimport numpy as np\\ndef f(x):\\n num,den=fraction(cancel(newformula))\\n fs=sorted(newformula.free_symbols,key=str)\\n return float(num.subs(list(zip(fs,x))))\\nnewformula=(makesubs(formula,'[b,oo],[c,oo]'))\\nprint(f([2,1,1]))\\nfmin(f,[2,1,1])\\nprint(fmin(f,np.array([2,1,1])))\\ndisplay(simplify(newformula))\"" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "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(newformula))\n", " fs=sorted(newformula.free_symbols,key=str)\n", " return float(num.subs(list(zip(fs,x))))\n", "newformula=(makesubs(formula,'[b,oo],[c,oo]'))\n", "print(f([2,1,1]))\n", "fmin(f,[2,1,1])\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": 24, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Substitute $a\\to b+k_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $b\\to c+l_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "ename": "NameError", "evalue": "name 'fs' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mformula\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmakesubs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mformula\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'[b,oo],[c,oo]'\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 3\u001b[0m \u001b[0mnum\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mden\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfraction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcancel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mformula\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----> 4\u001b[0;31m \u001b[0mnum\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msubs\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[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mfs\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[0m\u001b[1;32m 5\u001b[0m \u001b[0mdisplay\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mnumm\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'fs' is not defined" ] } ], "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": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.414211498165453" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2.1002573656763053/1.7661001788212371)**2" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Case $a\\ge c\\ge b$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $a\\to c+r_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $c\\to b+s_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "numerator: $2b^4r_{1}^2+2b^4r_{1}s_{1}+2b^4s_{1}^2+4b^3r_{1}^3+2\\sqrt{2}b^3r_{1}^2s_{1}+10b^3r_{1}^2s_{1}+2\\sqrt{2}b^3r_{1}s_{1}^2+6b^3r_{1}s_{1}^2+4b^3s_{1}^3+2b^2r_{1}^4+2\\sqrt{2}b^2r_{1}^3s_{1}+10b^2r_{1}^3s_{1}+6\\sqrt{2}b^2r_{1}^2s_{1}^2+12b^2r_{1}^2s_{1}^2+4b^2r_{1}s_{1}^3+4\\sqrt{2}b^2r_{1}s_{1}^3+2b^2s_{1}^4+2br_{1}^4s_{1}+2\\sqrt{2}br_{1}^3s_{1}^2+6br_{1}^3s_{1}^2+4br_{1}^2s_{1}^3+4\\sqrt{2}br_{1}^2s_{1}^3+2\\sqrt{2}br_{1}s_{1}^4+r_{1}^4s_{1}^2+2r_{1}^3s_{1}^3+r_{1}^2s_{1}^4$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "denominator: $b^6+2b^5r_{1}+4b^5s_{1}+b^4r_{1}^2+6b^4r_{1}s_{1}+6b^4s_{1}^2+2b^3r_{1}^2s_{1}+6b^3r_{1}s_{1}^2+4b^3s_{1}^3+b^2r_{1}^2s_{1}^2+2b^2r_{1}s_{1}^3+b^2s_{1}^4$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "status: 0" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$ 0 \\le r_{1}^4s_{1}^2+2br_{1}^4s_{1}+2b^2r_{1}^4+2r_{1}^3s_{1}^3+2\\sqrt{2}br_{1}^3s_{1}^2+6br_{1}^3s_{1}^2+2\\sqrt{2}b^2r_{1}^3s_{1}+10b^2r_{1}^3s_{1}+4b^3r_{1}^3+r_{1}^2s_{1}^4+4\\sqrt{2}br_{1}^2s_{1}^3+4br_{1}^2s_{1}^3+6\\sqrt{2}b^2r_{1}^2s_{1}^2+12b^2r_{1}^2s_{1}^2+2\\sqrt{2}b^3r_{1}^2s_{1}+10b^3r_{1}^2s_{1}+2b^4r_{1}^2+2\\sqrt{2}br_{1}s_{1}^4+4\\sqrt{2}b^2r_{1}s_{1}^3+4b^2r_{1}s_{1}^3+2\\sqrt{2}b^3r_{1}s_{1}^2+6b^3r_{1}s_{1}^2+2b^4r_{1}s_{1}+2b^2s_{1}^4+4b^3s_{1}^3+2b^4s_{1}^2 $$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "The sum of all inequalities gives us a proof of the inequality." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Case $a\\ge b\\ge c$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $a\\to b+t_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $b\\to c+u_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $t_{1}\\to \\sqrt{2}v_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "numerator: $8c^4u_{1}^2+8\\sqrt{2}c^4u_{1}v_{1}+16c^4v_{1}^2+16c^3u_{1}^3-16c^3u_{1}^2v_{1}+24\\sqrt{2}c^3u_{1}^2v_{1}-16\\sqrt{2}c^3u_{1}v_{1}^2+80c^3u_{1}v_{1}^2+32\\sqrt{2}c^3v_{1}^3+8c^2u_{1}^4-32c^2u_{1}^3v_{1}+16\\sqrt{2}c^2u_{1}^3v_{1}-48\\sqrt{2}c^2u_{1}^2v_{1}^2+96c^2u_{1}^2v_{1}^2-32c^2u_{1}v_{1}^3+80\\sqrt{2}c^2u_{1}v_{1}^3+32c^2v_{1}^4-16cu_{1}^4v_{1}-32\\sqrt{2}cu_{1}^3v_{1}^2+32cu_{1}^3v_{1}^2-32cu_{1}^2v_{1}^3+48\\sqrt{2}cu_{1}^2v_{1}^3+32cu_{1}v_{1}^4+8u_{1}^4v_{1}^2+16\\sqrt{2}u_{1}^3v_{1}^3+16u_{1}^2v_{1}^4$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "denominator: $c^6+4c^5u_{1}+2\\sqrt{2}c^5v_{1}+6c^4u_{1}^2+6\\sqrt{2}c^4u_{1}v_{1}+2c^4v_{1}^2+4c^3u_{1}^3+6\\sqrt{2}c^3u_{1}^2v_{1}+4c^3u_{1}v_{1}^2+c^2u_{1}^4+2\\sqrt{2}c^2u_{1}^3v_{1}+2c^2u_{1}^2v_{1}^2$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "status: 0" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "From weighted AM-GM inequality:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$16cu_{1}^4v_{1} \\le 8u_{1}^4v_{1}^2+8c^2u_{1}^4$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$32\\sqrt{2}cu_{1}^3v_{1}^2 \\le 16\\sqrt{2}u_{1}^3v_{1}^3+16\\sqrt{2}c^2u_{1}^3v_{1}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$32c^2u_{1}^3v_{1} \\le 16cu_{1}^3v_{1}^2+16c^3u_{1}^3$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$32cu_{1}^2v_{1}^3 \\le 16cu_{1}^3v_{1}^2+16cu_{1}v_{1}^4$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$48\\sqrt{2}c^2u_{1}^2v_{1}^2 \\le 24\\sqrt{2}cu_{1}^2v_{1}^3+24\\sqrt{2}c^3u_{1}^2v_{1}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$16c^3u_{1}^2v_{1} \\le 8c^2u_{1}^2v_{1}^2+8c^4u_{1}^2$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$32c^2u_{1}v_{1}^3 \\le 16u_{1}^2v_{1}^4+16c^4v_{1}^2$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$16\\sqrt{2}c^3u_{1}v_{1}^2 \\le 8\\sqrt{2}c^2u_{1}v_{1}^3+8\\sqrt{2}c^4u_{1}v_{1}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$ 0 \\le 24\\sqrt{2}cu_{1}^2v_{1}^3+88c^2u_{1}^2v_{1}^2+16cu_{1}v_{1}^4+72\\sqrt{2}c^2u_{1}v_{1}^3+80c^3u_{1}v_{1}^2+32c^2v_{1}^4+32\\sqrt{2}c^3v_{1}^3 $$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "The sum of all inequalities gives us a proof of the inequality." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "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" ] }, "execution_count": 28, "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='1,sqrt(2),1')\n", "formula" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Substitute $a\\to b+w_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $b\\to c+x_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $a\\to\\sqrt{2}a$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "numerator: $2c^4w_{1}^2+2c^4w_{1}x_{1}+2c^4x_{1}^2+4c^3w_{1}^3-2\\sqrt{2}c^3w_{1}^2x_{1}+10c^3w_{1}^2x_{1}-2\\sqrt{2}c^3w_{1}x_{1}^2+6c^3w_{1}x_{1}^2+4c^3x_{1}^3+2c^2w_{1}^4-2\\sqrt{2}c^2w_{1}^3x_{1}+10c^2w_{1}^3x_{1}-6\\sqrt{2}c^2w_{1}^2x_{1}^2+12c^2w_{1}^2x_{1}^2-4\\sqrt{2}c^2w_{1}x_{1}^3+4c^2w_{1}x_{1}^3+2c^2x_{1}^4+2cw_{1}^4x_{1}-2\\sqrt{2}cw_{1}^3x_{1}^2+6cw_{1}^3x_{1}^2-4\\sqrt{2}cw_{1}^2x_{1}^3+4cw_{1}^2x_{1}^3-2\\sqrt{2}cw_{1}x_{1}^4+w_{1}^4x_{1}^2+2w_{1}^3x_{1}^3+w_{1}^2x_{1}^4$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "denominator: $1$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "status: 2" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Program couldn't find any proof." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$ 2\\sqrt{2}cw_{1}x_{1}^4+4\\sqrt{2}cw_{1}^2x_{1}^3+4\\sqrt{2}c^2w_{1}x_{1}^3+2\\sqrt{2}cw_{1}^3x_{1}^2+6\\sqrt{2}c^2w_{1}^2x_{1}^2+2\\sqrt{2}c^3w_{1}x_{1}^2+2\\sqrt{2}c^2w_{1}^3x_{1}+2\\sqrt{2}c^3w_{1}^2x_{1} \\le w_{1}^2x_{1}^4+2c^2x_{1}^4+2w_{1}^3x_{1}^3+4cw_{1}^2x_{1}^3+4c^2w_{1}x_{1}^3+4c^3x_{1}^3+w_{1}^4x_{1}^2+6cw_{1}^3x_{1}^2+12c^2w_{1}^2x_{1}^2+6c^3w_{1}x_{1}^2+2c^4x_{1}^2+2cw_{1}^4x_{1}+10c^2w_{1}^3x_{1}+10c^3w_{1}^2x_{1}+2c^4w_{1}x_{1}+2c^2w_{1}^4+4c^3w_{1}^3+2c^4w_{1}^2 $$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "2" ] }, "execution_count": 29, "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": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, (x, y, sqrt(2)))" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S('2**(1/2)*x*y').as_coeff_mul()" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, (x, y, sqrt(2)))" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S('sqrt(2)*x*y').as_coeff_mul()" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 1, 1), (0, 0, 0)]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly('sqrt(2)*x*y+8').monoms()" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 58]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly('sqrt(2)*x*y+58').coeffs()" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(x, y, sqrt(2), sqrt(3), sqrt(6))" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly('sqrt(2)*x*y+sqrt(3)+sqrt(2)*sqrt(3)').gens" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([0], [(0, 0)], [1, 7], [(2, 0), (0, 1)], (x, y))" ] }, "execution_count": 35, "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": 36, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "numerator: $\\sqrt{2}x^2-2\\sqrt{2}xy+\\sqrt{2}y^2$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "denominator: $1$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "status: 0" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "From weighted AM-GM inequality:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$2\\sqrt{2}xy \\le \\sqrt{2}x^2+\\sqrt{2}y^2$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$ 0 \\le 0 $$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "The sum of all inequalities gives us a proof of the inequality." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "0" ] }, "execution_count": 36, "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": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[(1, 0), (0, 2), (0, 1), (0, 0)], [1, 1, 1, 1]]" ] }, "execution_count": 37, "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": 38, "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": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x=Symbol('x', positive=True)\n", "Poly(x+sqrt(x))" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "prove?" ] }, { "cell_type": "code", "execution_count": 40, "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": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly('x^2-1').abs()" ] }, { "cell_type": "code", "execution_count": 41, "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": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly(Poly('x')/Poly('x+y'))" ] }, { "cell_type": "code", "execution_count": 42, "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 \\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')" ] }, "execution_count": 42, "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": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, sqrt(6)]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly('x+sqrt(6)',S('x'),S('sqrt(2)'),S('sqrt(3)')).coeffs()" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, x]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly('x+sqrt(x)',S('sqrt(x)')).coeffs()" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "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" ] }, { "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')\n", " >>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ')\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')\n", " >>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ')\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')\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" ] } ], "source": [ "source(Poly)" ] }, { "cell_type": "code", "execution_count": 46, "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": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly({1:1,2:1},gens=S('sqrt(x)'))" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle - x + \\sqrt{x^{2}}$" ], "text/plain": [ "-x + sqrt(x**2)" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x=symbols('x', positive=True)\n", "S('sqrt(x^2)-x')" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Poly((sqrt(x))**4 + (sqrt(x))**2 + (sqrt(x)), sqrt(x), domain='ZZ')\"" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(Poly({1:1,2:1,4:1},gens=S('sqrt(x)')))" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Poly(x**2 + x + (sqrt(x)), x, sqrt(x), domain='ZZ')\"" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(Poly('x^2+x+sqrt(x)'))" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Substitute $a\\to b+y_{1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $b\\to c+z_{1}$" ], "text/plain": [ "" ] }, "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": [ "(1.7908873553542452e-10, 2.5326984818340415e-10, 7.129450063690368)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "2.5119572187973787e-11" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def findvalues(formula,values=None,variables=None):\n", " formula=S(formula)\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,variables))))\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": 54, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Substitute $a\\to b+e_{2}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $b\\to c+f_{2}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "ok\n", "100 loops, best of 5: 8.66 ms per loop\n", "1000 loops, best of 5: 1.31 ms per loop\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "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.eval(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": null, "metadata": {}, "outputs": [], "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": null, "metadata": {}, "outputs": [], "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": 52, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\sqrt{x} + x$" ], "text/plain": [ "sqrt(x) + x" ] }, "execution_count": 52, "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": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(x*(1 - x), [])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sympy.solvers.solvers import unrad\n", "unrad('sqrt(x)+x')" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 56, "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": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(x, 1/2)" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S('sqrt(x)').args" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 12)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m12\u001b[0m\n\u001b[0;31m newgens[gen[0]]=\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "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", "newgens={}\n", "for gen in pol.gens:\n", " base,pw=_powr(gen)\n", " num,den=fraction(pw)\n", " if (gen[0],num) in newgens:\n", " newgens[gen[0]]=\n", " else:\n", " newgens[gen[0]]=gen[1]" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(S('1'),Rational)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6 in {3:4,5:6}" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 1$" ], "text/plain": [ "1" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import *\n", "from sympy import *\n", "gcd(S('a/b'),S('c/d'))" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\operatorname{Poly}{\\left( x^{2 \\pi}, x^{\\pi}, domain=\\mathbb{Z} \\right)}$" ], "text/plain": [ "Poly((x**pi)**2, x**pi, domain='ZZ')" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly('x^(2*pi)')" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{1}{8}$" ], "text/plain": [ "1/8" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gcd(S('1/8'),S('1/4'))" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, (x, sqrt(2)))" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S('sqrt(2)*3*x').as_coeff_mul()" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Poly((sqrt(x))**4 + (sqrt(x))**2 + (sqrt(x)), sqrt(x), domain='ZZ')\n", "Poly((x**(1/6))**12 + (x**(1/6))**3 + (x**(1/6))**2 + y**2, x**(1/6), y, domain='ZZ')\n", "Poly((sqrt(x)) + (sqrt(x*z)) + (sqrt(z)), sqrt(x), sqrt(x*z), sqrt(z), domain='ZZ')\n", "Poly((sqrt(x))*(sqrt(z)) + (sqrt(x)) + (sqrt(z)), sqrt(x), sqrt(z), domain='ZZ')\n", "Poly((sqrt(t + z)) + (sqrt(t*x + t*y + x*z + y*z)) + (sqrt(x + y)), sqrt(t + z), sqrt(t*x + t*y + x*z + y*z), sqrt(x + y), domain='ZZ')\n", "Poly((sqrt(2)) + (sqrt(3)) + (sqrt(6)), sqrt(2), sqrt(3), sqrt(6), domain='ZZ')\n" ] } ], "source": [ "def _powr(formula):\n", "\tif formula.func==Pow:\n", "\t\treturn formula.args\n", "\telse:\n", "\t\treturn [formula,S('1')]\n", "def reducegens(formula):\n", "\tpol=Poly(formula)\n", "\tnewgens={}\n", "\tind={}\n", "\tfor gen in pol.gens:\n", "\t\tbase,pw=_powr(gen)\n", "\t\tcoef,_=pw.as_coeff_mul()\n", "\t\tml=pw/coef\n", "\t\tif base**ml in newgens:\n", "\t\t\tnewgens[base**ml]=gcd(newgens[base**ml],coef)\n", "\t\telse:\n", "\t\t\tnewgens[base**ml]=coef\n", "\t\t\tind[base**ml]=S('tmp'+str(len(ind)))\n", "\tfor gen in pol.gens:\n", "\t\tbase,pw=_powr(gen)\n", "\t\tcoef,_=pw.as_coeff_mul()\n", "\t\tml=pw/coef\n", "\t\tpol=pol.replace(gen,ind[base**ml]**(coef/newgens[base**ml]))\n", "\tnewpol=Poly(pol.as_expr())\n", "\tfor gen in newgens:\n", "\t\tnewpol=newpol.replace(ind[gen],gen**newgens[gen])\n", "\treturn newpol\n", "print(reducegens(S('x^2+x+sqrt(x)')))\n", "print(reducegens(S('x**2+y**2+x**(1/2)+x**(1/3)')))\n", "print(reducegens(S('sqrt(x)+sqrt(z)+sqrt(x*z)')))\n", "print(reducegens(S('sqrt(x)+sqrt(z)+sqrt(x)*sqrt(z)')))\n", "print(reducegens(S('sqrt(x+y)+sqrt(z+t)+sqrt((x+y)*(z+t))')))\n", "print(reducegens(S('sqrt(2)+sqrt(3)+sqrt(6)')))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "S('-2').as_coeff_mul()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x=Symbol('x', positive=True)\n", "y=Symbol('y', positive=True)\n", "Poly(sqrt(x)+sqrt(y)+sqrt(x*y))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sqrt(x*y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "simplify(S('sqrt(x*y)-sqrt(x)*sqrt(y)'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x=symbols('x',positive=True)\n", "ask(Q.positive(x))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Poly('x^2+x')+Poly('-x+7')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(reducegens(S('sqrt(8)')))\n", "sqrt(-1*-1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Poly('sqrt(2*x)+sqrt(3*x)+sqrt(6*x)')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Poly('sqrt(2*pi)+sqrt(3*pi)')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "solve(S('sqrt(6*x)')-S('sqrt(2*y)'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Poly('sqrt(6)+sqrt(10)+sqrt(14)+sqrt(15)+sqrt(21)+sqrt(35)+sqrt()')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from shiroindev import *\n", "formula=S('(a+b-c)^2')\n", "prove(makesubs(formula,'[c,oo]'))\n", "prove(makesubs(formula,'[a,oo]',variables='c'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "formula=S('(a+b-d)^2+(a+b-c)^2')\n", "prove(makesubs(formula,'[a+b,oo],[a+b,oo]',variables='c,d'))\n", "prove(makesubs(formula,'[0,a+b],[a+b,oo]',variables='c,d'))\n", "prove(makesubs(formula,'[0,a+b],[0,a+b]',variables='c,d'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "findvalues(formula)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "display(cyclize('((a-b)/c)^2'))\n", "display(S('sqrt(8)')*cyclize('(a-b)/c'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "formula=cyclize('((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": null, "metadata": {}, "outputs": [], "source": [ "formula=cyclize('((a-b)/c)^2-8**(1/2)*(a-b)/c')\n", "formula2=makesubs(formula,'[c,oo],[c,oo]')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "findvalues(formula2,values=[5,2,7])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x,y=symbols('x,y')\n", "ask(Q.positive(x**2+1),Q.real(x))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "formula=Sm('(a^2+b^2+c^2)^2- 3(a^3b+b^3c+c^3a)')\n", "formula1=makesubs(formula,'[b,oo],[c,oo]',variables='a,b')\n", "formula2=makesubs(formula,'[c,oo],[b,oo]',variables='a,c')\n", "prove(formula2)\n", "print(findvalues(formula1))\n", "#prove(formula1,values='4*b*(1-b)-1,4*c*(1-c)-1,1')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "makesubs(Sm('a^2-2ab+bc-c^2+ca'),'[c,oo],[a,oo]',variables='b,c')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle - \\sqrt{x} \\sqrt{y} + \\sqrt{x y}$" ], "text/plain": [ "-sqrt(x)*sqrt(y) + sqrt(x*y)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\displaystyle 0$" ], "text/plain": [ "0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\n", "formula=S('sqrt(x*y)-sqrt(x)*sqrt(y)')\n", "display(formula)\n", "def assumeall(formula,**kwargs):\n", " formula=S(formula)\n", " fs=formula.free_symbols\n", " for x in fs:\n", " y=Symbol(str(x),**kwargs)\n", " formula=formula.subs(x,y)\n", " return formula\n", "display(assumeall(formula,positive=True))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Poly(assumeall('x+sqrt(x)',positive=True))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str(type(S('f1(x,y,z)')))=='f1'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ReprPrinter?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "srepr(S('x^2+f(x)+f(x,y)+f()+sqrt(y)'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sympy import Function,srepr,S\n", "import re\n", "f=Function('f')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def allsymbols(formula):\n", " formula=S(formula)\n", " funcsymbols=[x[10:-2] for x in re.findall(r\"Function\\(\\'.*?\\'\\)\",srepr(formula))]\n", " return set(funcsymbols)|set(map(str,formula.free_symbols))\n", "def vargen(n):\n", " x='abcdefghijklmnopqrstuvwxyz'[n%26]\n", " if n>=26:\n", " x+=str(n//26)\n", " return x\n", "allsymbols(S('f(5,7)+f(5)+g(8)+sqrt(x*y)'))\n", "vargen(36)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sympy import *\n", "from sympy.printing.repr import *\n", "ReprPrinter??" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "{5,7} | {8,7}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[x[10:-2] for x in re.findall(r\"Function\\(\\'.*?\\'\\)\",srepr(S('x^2')))]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "S('f(5,7)+f(5)+g(8)+sqrt(x*y)')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "srepr(S('f(5,7)+f(5)+g(8)+sqrt(x*y)'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "display(Latex('$'+str(S('z12345'))+'$'))" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: ''", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;34m'xyz'\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m2\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[0;32m----> 2\u001b[0;31m \u001b[0mint\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[0;31mValueError\u001b[0m: invalid literal for int() with base 10: ''" ] } ], "source": [ "'xyz'[-2::]" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "def sortkey(x):\n", " x=str(x)\n", " i=len(x)-1\n", " while i>=0:\n", " if x[i] not in '0123456789':\n", " break\n", " i-=1\n", " if i==len(x)-1:\n", " return (-1,x)\n", " return (int(x[i+1:]),x[:i+1])" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[x, y, x0, qq1, z1, t2]" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(S('x,y,z1,t2,qq1,x0'),key=sortkey)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Optimization terminated successfully.\n", " Current function value: 1.154701\n", " Iterations: 85\n", " Function evaluations: 152\n" ] }, { "data": { "text/plain": [ "[1.0,\n", " 3.000209557356011,\n", " 3.9998018341331227,\n", " 2.9997236704279775,\n", " 0.999903278712487]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S('x_1^2+x_2^2+x_3^2+x_4^2+x_5^2')\n", "formula=S('x_1^2+x_2^2+x_3^2+x_4^2+x_5^2- (x_1*x_2+x_2*x_3+x_3*x_4+x_4*x_5)')\n", "values=findvalues(formula)\n", "values=[value/values[0] for value in values]\n", "[value**2 for value in values]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{2 \\sqrt{3}}{3}$" ], "text/plain": [ "2*sqrt(3)/3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formula1=S('(x_1^2+x_2^2+x_3^2+x_4^2+x_5^2)/(x_1*x_2+x_2*x_3+x_3*x_4+x_4*x_5)')\n", "formula1.subs(S('[[x_1,1],[x_2,sqrt(3)],[x_3,2],[x_4,sqrt(3)],[x_5,1]]'))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "formula2=S('x_1^2+x_2^2+x_3^2+x_4^2+x_5^2- (2/sqrt(3))*(x_1*x_2+x_2*x_3+x_3*x_4+x_4*x_5)')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Substitute $x_{2}\\to \\sqrt{3} g$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $x_{3}\\to 2 h$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $x_{4}\\to \\sqrt{3} i$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "numerator: $3 g^{2} - 4 g h - 2 g x_{1} + 4 h^{2} - 4 h i + 3 i^{2} - 2 i x_{5} + x_{1}^{2} + x_{5}^{2}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "denominator: $1$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "status: 0" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "From weighted AM-GM inequality:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$4 g h \\le 2 g^{2}+2 h^{2}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$2 g x_{1} \\le g^{2}+x_{1}^{2}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$4 h i \\le 2 h^{2}+2 i^{2}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$2 i x_{5} \\le i^{2}+x_{5}^{2}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$ 0 \\le 0 $$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "The sum of all inequalities gives us a proof of the inequality." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prove(formula2,values='1,sqrt(3),2,sqrt(3),1')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\operatorname{Poly}{\\left( a^{k}a^{n}, a^{k}, a^{n}, domain=\\mathbb{Z} \\right)}$" ], "text/plain": [ "Poly((a**k)*(a**n), a**k, a**n, domain='ZZ')" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly('a^(n+k)')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'FiniteSet' object has no attribute 'as_coeff_Mul'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mS\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'a^{n+k}/{b^n}+{b^{n+k}}/{c^n}+{c^{n+k}}/{a^n}-( a^k+b^k+c^k)'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/sympy/core/sympify.py\u001b[0m in \u001b[0;36msympify\u001b[0;34m(a, locals, convert_xor, strict, rational, evaluate)\u001b[0m\n\u001b[1;32m 382\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[1;32m 383\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'\\n'\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[0;32m--> 384\u001b[0;31m \u001b[0mexpr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mparse_expr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlocal_dict\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlocals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtransformations\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtransformations\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mevaluate\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mevaluate\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 385\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mTokenError\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mSyntaxError\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mexc\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 386\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mSympifyError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'could not parse %r'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexc\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/parsing/sympy_parser.py\u001b[0m in \u001b[0;36mparse_expr\u001b[0;34m(s, local_dict, transformations, global_dict, evaluate)\u001b[0m\n\u001b[1;32m 964\u001b[0m \u001b[0mcode\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcompile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mevaluateFalse\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcode\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'eval'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 965\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 966\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0meval_expr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlocal_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mglobal_dict\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 967\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 968\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/sympy/parsing/sympy_parser.py\u001b[0m in \u001b[0;36meval_expr\u001b[0;34m(code, local_dict, global_dict)\u001b[0m\n\u001b[1;32m 877\u001b[0m \"\"\"\n\u001b[1;32m 878\u001b[0m expr = eval(\n\u001b[0;32m--> 879\u001b[0;31m code, global_dict, local_dict) # take local objects in preference\n\u001b[0m\u001b[1;32m 880\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 881\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mexpr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n", "\u001b[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/sympy/core/expr.py\u001b[0m in \u001b[0;36m__pow__\u001b[0;34m(self, other, mod)\u001b[0m\n\u001b[1;32m 163\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__pow__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\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 164\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mmod\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 165\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_pow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\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 166\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[1;32m 167\u001b[0m \u001b[0m_self\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmod\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mas_int\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mas_int\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mas_int\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmod\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/decorators.py\u001b[0m in \u001b[0;36m__sympifyit_wrapper\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 89\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'_op_priority'\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 90\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msympify\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstrict\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 91\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\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 92\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mSympifyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 93\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mretval\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/decorators.py\u001b[0m in \u001b[0;36mbinary_op_wrapper\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 127\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mf\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 128\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 129\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\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 130\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mbinary_op_wrapper\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 131\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mpriority_decorator\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/expr.py\u001b[0m in \u001b[0;36m_pow\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 159\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mcall_highest_priority\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'__rpow__'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 160\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_pow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\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--> 161\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mPow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\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 162\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 163\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__pow__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\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/sympy/core/cache.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 92\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mwrapper\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 93\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---> 94\u001b[0;31m \u001b[0mretval\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcfunc\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 95\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 96\u001b[0m \u001b[0mretval\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfunc\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[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/sympy/core/power.py\u001b[0m in \u001b[0;36m__new__\u001b[0;34m(cls, b, e, evaluate)\u001b[0m\n\u001b[1;32m 274\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_Atom\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mb\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mS\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mExp1\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexp_polar\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 275\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0msympy\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdenom\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msign\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mim\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfactor_terms\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 276\u001b[0;31m \u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mex\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfactor_terms\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msign\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_coeff_Mul\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 277\u001b[0m \u001b[0mden\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdenom\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mex\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 278\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mden\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlog\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mden\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: 'FiniteSet' object has no attribute 'as_coeff_Mul'" ] } ], "source": [ "S('a^{n+k}/{b^n}+{b^(n+k)/(c^n)+c^(n+k)/{a^n}-( a^k+b^k+c^k)')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'767980716'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import re\n", "s=r\"\"\"99-my-name-is-John-Smith-6376827-%^-1-2-767980716\"\"\"\n", "re.compile(r\"^(.*?)-\").search(str(s[::-1])).group(1)[::-1]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ANTLR runtime and generated code versions disagree: 4.8!=4.7.1\n", "ANTLR runtime and generated code versions disagree: 4.8!=4.7.1\n" ] }, { "data": { "text/latex": [ "numerator: $a^{k} a^{2 n} c^{n} - a^{k} a^{n} b^{n} c^{n} + a^{n} b^{k} b^{2 n} - a^{n} b^{k} b^{n} c^{n} - a^{n} b^{n} c^{k} c^{n} + b^{n} c^{k} c^{2 n}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "denominator: $1$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "status: 2" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Program couldn't find any proof." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$ a^{k} a^{n} b^{n} c^{n}+a^{n} b^{k} b^{n} c^{n}+a^{n} b^{n} c^{k} c^{n} \\le a^{k} a^{2 n} c^{n}+a^{n} b^{k} b^{2 n}+b^{n} c^{k} c^{2 n} $$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $n\\to d + k$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "numerator: $a^{2 d} a^{3 k} c^{d} c^{k} - a^{d} a^{2 k} b^{d} b^{k} c^{d} c^{k} + a^{d} a^{k} b^{2 d} b^{3 k} - a^{d} a^{k} b^{d} b^{2 k} c^{d} c^{k} - a^{d} a^{k} b^{d} b^{k} c^{d} c^{2 k} + b^{d} b^{k} c^{2 d} c^{3 k}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "denominator: $1$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "status: 2" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Program couldn't find any proof." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$ a^{d} a^{2 k} b^{d} b^{k} c^{d} c^{k}+a^{d} a^{k} b^{d} b^{2 k} c^{d} c^{k}+a^{d} a^{k} b^{d} b^{k} c^{d} c^{2 k} \\le a^{2 d} a^{3 k} c^{d} c^{k}+a^{d} a^{k} b^{2 d} b^{3 k}+b^{d} b^{k} c^{2 d} c^{3 k} $$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "2" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sympy import *\n", "from sympy.parsing.latex import parse_latex\n", "from shiroindev import *\n", "shiro.seed=1\n", "from IPython.display import Latex\n", "shiro.display=lambda x:display(Latex(x))\n", "formula=expand(parse_latex(r'(\\frac{a^{n+k}}{b^n}+\\frac{b^{n+k}}{c^n}+\\frac{c^{n+k}}{a^n}-( a^k+b^k+c^k))*a^nb^nc^n'))\n", "newproof()\n", "prove(formula)\n", "prove(makesubs(formula,['k',oo],variables='n'))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\operatorname{Poly}{\\left( 6 x^{3}y^{9} + 5 x^{2}y^{7} + 4 xy^{8}, x, y, domain=\\mathbb{Z} \\right)}$" ], "text/plain": [ "Poly(6*x**3*y**9 + 5*x**2*y**7 + 4*x*y**8, x, y, domain='ZZ')" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly(dict(zip([(1,8),(2,7),(3,9)],[4,5,6])),gens=S('x,y'))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\sqrt{x} + x^{2} + x$" ], "text/plain": [ "sqrt(x) + x**2 + x" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly({1:1,2:1,4:1},gens=S('sqrt(x)')).as_expr()" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "def _writ2(coef,fun,variables):\n", "\treturn latex(Poly(dict(zip(fun,coef)),gens=variables).as_expr())" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'8 x^{3} y^{4} + 7 x y^{2}'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "_writ2([7,8],[(1,2),(3,4)],S('x,y'))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ANTLR runtime and generated code versions disagree: 4.8!=4.7.1\n", "ANTLR runtime and generated code versions disagree: 4.8!=4.7.1\n" ] }, { "data": { "text/latex": [ "$\\displaystyle 2 a b$" ], "text/plain": [ "2*(a*b)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "parse_latex(r' a^2+b^2\\geq 2ab').rhs" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ANTLR runtime and generated code versions disagree: 4.8!=4.7.1\n", "ANTLR runtime and generated code versions disagree: 4.8!=4.7.1\n" ] }, { "data": { "text/latex": [ "$\\displaystyle a^{2} + b^{2}$" ], "text/plain": [ "a**2 + b**2" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "parse_latex(r'2ab \\geq a^2+b^2').rhs" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "numerator: $a d + a f + b c + b e + c f + d e - 2 \\sqrt{a b} \\sqrt{c d} - 2 \\sqrt{a b} \\sqrt{e f} - 2 \\sqrt{c d} \\sqrt{e f}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "denominator: $1$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Poly((sqrt(a))**2*(sqrt(d))**2 + (sqrt(a))**2*(sqrt(f))**2 - 2*(sqrt(a))*(sqrt(b))*(sqrt(c))*(sqrt(d)) - 2*(sqrt(a))*(sqrt(b))*(sqrt(e))*(sqrt(f)) + (sqrt(b))**2*(sqrt(c))**2 + (sqrt(b))**2*(sqrt(e))**2 + (sqrt(c))**2*(sqrt(f))**2 - 2*(sqrt(c))*(sqrt(d))*(sqrt(e))*(sqrt(f)) + (sqrt(d))**2*(sqrt(e))**2, sqrt(a), sqrt(b), sqrt(c), sqrt(d), sqrt(e), sqrt(f), domain='ZZ')\n", "Poly((sqrt(a))**2*(sqrt(d))**2 + (sqrt(a))**2*(sqrt(f))**2 + (sqrt(b))**2*(sqrt(c))**2 + (sqrt(b))**2*(sqrt(e))**2 + (sqrt(c))**2*(sqrt(f))**2 + (sqrt(d))**2*(sqrt(e))**2, sqrt(a), sqrt(b), sqrt(c), sqrt(d), sqrt(e), sqrt(f), domain='QQ')\n", "Poly(2*(sqrt(a))*(sqrt(b))*(sqrt(c))*(sqrt(d)) + 2*(sqrt(a))*(sqrt(b))*(sqrt(e))*(sqrt(f)) + 2*(sqrt(c))*(sqrt(d))*(sqrt(e))*(sqrt(f)), sqrt(a), sqrt(b), sqrt(c), sqrt(d), sqrt(e), sqrt(f), domain='QQ')\n" ] }, { "data": { "text/latex": [ "status: 0" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "From weighted AM-GM inequality:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$2 \\sqrt{a} \\sqrt{b} \\sqrt{c} \\sqrt{d} \\le a d+b c$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$2 \\sqrt{a} \\sqrt{b} \\sqrt{e} \\sqrt{f} \\le a f+b e$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$2 \\sqrt{c} \\sqrt{d} \\sqrt{e} \\sqrt{f} \\le c f+d e$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$ 0 \\le 0 $$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "The sum of all inequalities gives us a proof of the inequality." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "0" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fr=parse_latex(r'\\sqrt{ab}+\\sqrt{cd}+\\sqrt{ef}\\leq\\sqrt{(a+c+e)(b+d+f)}')\n", "prove(fr.rhs**2-fr.lhs**2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 0$" ], "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "assumeall(S('sqrt(x*y)-sqrt(x)*sqrt(y)'),positive=True)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "ename": "GeneratorsError", "evalue": "duplicated generators: [sqrt(x), sqrt(x)]", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mGeneratorsError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mp\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mreducegens\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'x+sqrt(x)'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mS\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'x+sqrt(x)'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mPoly\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mgens\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mS\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'sqrt(x)'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mS\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'sqrt(x)'\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[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mPoly\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mgens\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mS\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'sqrt(x)'\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[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/sympy/polys/polytools.py\u001b[0m in \u001b[0;36m__new__\u001b[0;34m(cls, rep, *gens, **args)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__new__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcls\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0mgens\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[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 108\u001b[0m \u001b[0;34m\"\"\"Create a new polynomial instance out of something useful. \"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 109\u001b[0;31m \u001b[0mopt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0moptions\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuild_options\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgens\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0margs\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 110\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 111\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;34m'order'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mopt\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/polys/polyoptions.py\u001b[0m in \u001b[0;36mbuild_options\u001b[0;34m(gens, args)\u001b[0m\n\u001b[1;32m 729\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 730\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;34m'opt'\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0margs\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mgens\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 731\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mOptions\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgens\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0margs\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 732\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'opt'\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/polys/polyoptions.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, gens, args, flags, strict)\u001b[0m\n\u001b[1;32m 152\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0moption\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpreprocess\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 154\u001b[0;31m \u001b[0mpreprocess_options\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\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 155\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 156\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdefaults\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\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[0;32m/home/grzegorz/Pobrane/SageMath/local/lib/python3.7/site-packages/sympy/polys/polyoptions.py\u001b[0m in \u001b[0;36mpreprocess_options\u001b[0;34m(args)\u001b[0m\n\u001b[1;32m 150\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 151\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 152\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0moption\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpreprocess\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\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 153\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 154\u001b[0m \u001b[0mpreprocess_options\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\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/polys/polyoptions.py\u001b[0m in \u001b[0;36mpreprocess\u001b[0;34m(cls, gens)\u001b[0m\n\u001b[1;32m 289\u001b[0m \u001b[0mgens\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 290\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mhas_dups\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgens\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--> 291\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mGeneratorsError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"duplicated generators: %s\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgens\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 292\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0many\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgen\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_commutative\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mFalse\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mgen\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mgens\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 293\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mGeneratorsError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"non-commutative generators: %s\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgens\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;31mGeneratorsError\u001b[0m: duplicated generators: [sqrt(x), sqrt(x)]" ] } ], "source": [ "p=reducegens('x+sqrt(x)')\n", "q=S('x+sqrt(x)')\n", "print(Poly(p,gens=[S('sqrt(x)'),S('sqrt(x)')]))\n", "print(Poly(q,gens=S('sqrt(x)')))" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "from importlib import reload\n", "from sympy import *\n", "import shiroindev\n", "reload(shiroindev)\n", "from shiroindev import *\n", "from sympy.parsing.latex import parse_latex\n", "from itertools import permutations, combinations\n", "shiro.seed=1\n", "from IPython.display import Latex\n", "shiro.display=lambda x:display(Latex(x))" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\operatorname{Poly}{\\left( x, \\sqrt{x}, domain=\\mathbb{Z} \\right)}$" ], "text/plain": [ "Poly((sqrt(x))**2, sqrt(x), domain='ZZ')" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly('sqrt(x)')*Poly('sqrt(x)')" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\operatorname{Poly}{\\left( 0, x, domain=\\mathbb{Z} \\right)}$" ], "text/plain": [ "Poly(0, x, domain='ZZ')" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Poly('x')-Poly('x').abs()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\operatorname{Poly}{\\left( x, x, y, domain=\\mathbb{Q} \\right)}$" ], "text/plain": [ "Poly(x, x, y, domain='QQ')" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fr=Poly('x-y')\n", "(fr+fr.abs())*(S('1/2'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from itertools import permutations \n", "t5=[]\n", "for ineq in ineqs2:\n", " u=0\n", " for vars in permutations(ineq.free_symbols):\n", " ineqp=makesubs(ineq,list(zip(vars[1:],[oo]*len(vars[1:]))),variables=vars[:-1])\n", " u=max(u,prove(ineqp))\n", " print(u,end=',')\n", " t5+=[u]\n", "Counter(t5)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pvalue 4.43444926375551e-06\n", "statistic 59.0\n" ] } ], "source": [ "from statsmodels.stats.contingency_tables import mcnemar\n", "print(mcnemar([[101,121],[59,33]]))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "nsimplify?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "shiro.display=lambda x:display(Latex(x))\n", "newproof()\n", "fr=makesubs(ineqs2[15],'[c,oo],[a,oo]',variables='b,c')\n", "display(fr)\n", "fr2=fractioncancel(fr.subs(S('[[e,(sqrt(5)-1)/2*f]]')))[0]\n", "display(fr2)\n", "prove(makesubs(fr2,'11/5,9/4',variables=[S('sqrt(5)')]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def makesubs(formula,intervals,values=None,variables=None,numden=False):\n", "#This function generates a new formula which satisfies this condition:\n", "#for all positive variables new formula is nonnegative iff\n", "#for all variables in corresponding intervals old formula is nonnegative\n", "\tformula=S(formula)\n", "\taddsymbols(formula)\n", "\tintervals=_smakeiterable2(intervals)\n", "\tif variables: variables=_smakeiterable(variables)\n", "\telse: variables=sorted(formula.free_symbols,key=str)\n", "\tif values!=None:\n", "\t\tvalues=_smakeiterable(values)\n", "\t\tequations=[var-value for var,value in zip(variables,values)]\n", "\telse:\n", "\t\tequations=[]\n", "\tnewvars=[]\n", "\tusedvars=set()\n", "\tfor var,interval in zip(variables,intervals):\n", "\t\tend1,end2=interval\n", "\t\tz=newvar()\n", "\t\tnewvars+=[z]\n", "\t\tusedvars|={z}\n", "\t\tif (end1.free_symbols|end2.free_symbols)&usedvars:\n", "\t\t\tshiro.warning(shiro.translation[\n", "\t\t\t'Warning: intervals contain backwards dependencies. Consider changing order of variables'])\n", "\t\tif end1 in {S('-oo'),S('oo')}:\n", "\t\t\tend1,end2=end2,end1\n", "\t\tif {end1,end2}=={S('-oo'),S('oo')}:\n", "\t\t\tsub1=sub2=(z-1/z)\n", "\t\telif end2==S('oo'):\n", "\t\t\tsub1=sub2=(end1+z)\n", "\t\telif end2==S('-oo'):\n", "\t\t\tsub1=sub2=end1-z\n", "\t\telse:\n", "\t\t\tsub1=end2+(end1-end2)/z\n", "\t\t\tsub2=end2+(end1-end2)/(1+z)\n", "\t\tformula=formula.subs(var,sub1)\n", "\t\tshiro.display(shiro.translation['Substitute']+\" $\"+latex(var)+'\\\\to '+latex(sub2)+'$')\n", "\t\tequations=[equation.subs(var,sub1) for equation in equations]\n", "\tnum,den=fractioncancel(formula)\n", "\tfor var,interval in zip(newvars,intervals):\n", "\t\tif {interval[0],interval[1]} & {S('oo'),S('-oo')}==set():\n", "\t\t\tnum=num.subs(var,var+1)\n", "\t\t\tden=den.subs(var,var+1)\n", "\t\t\tequations=[equation.subs(var,var+1) for equation in equations]\n", "\tif values:\n", "\t\tvalues=ssolve(equations,newvars)\n", "\t\tif len(values):\n", "\t\t\tvalues=values[0]\n", "\tnum,den=expand(num),expand(den)\n", "\t#shiro.display(shiro.translation[\"Formula after substitution:\"],\"$$\",latex(num/den),'$$')\n", "\tif values and numden:\n", "\t\treturn num,den,values\n", "\telif values:\n", "\t\treturn num/den,values\n", "\telif numden:\n", "\t\treturn num,den\n", "\telse:\n", "\t\treturn num/den" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{3, 5, 7, 8}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x={5,7,8}\n", "x|={3}\n", "x" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{x}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from shiroindev import *\n", "S('x+7').free_symbols" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(set())" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Substitute $x\\to - y + 1 + \\frac{y - 1}{a + 1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $y\\to 1 - \\frac{1}{b + 1}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "Substitute $b\\to \\frac{c}{2}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "numerator: $a^{4} c^{2} + a^{3} c^{2} - 2 a^{3} c - 4 a^{2} c + 4 a^{2} + a c^{2} - 2 a c + c^{2}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "denominator: $a^{3} c^{2} + 2 a^{3} c + 2 a^{2} c^{2} + 4 a^{2} c + a c^{2} + 2 a c$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "status: 0" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "From weighted AM-GM inequality:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$2 a^{3} c \\le a^{4} c^{2}+a^{2}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$4 a^{2} c \\le a^{3} c^{2}+2 a^{2}+a c^{2}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$2 a c \\le a^{2}+c^{2}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$ 0 \\le 0 $$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "The sum of all inequalities gives us a proof of the inequality." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from importlib import reload\n", "newproof()\n", "import shiroindev\n", "reload(shiroindev)\n", "from shiroindev import *\n", "shiro.display=lambda x:display(Latex(x))\n", "formula=Sm('xy/z+yz/x+zx/y-1').subs('z',S('1-x-y'))\n", "newformula,values=makesubs(formula,'[0,1-y],[0,1]','1/3,1/3')\n", "prove(newformula,values)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "def findvalues(formula,values=None,variables=None,**kwargs):\n", "\t\"\"\"finds a candidate for parameter \"values\" in \"prove\" function\n", "\tblabla\"\"\"\n", "\tformula=S(formula)\n", "\taddsymbols(formula)\n", "\tnum,den=fractioncancel(formula)\n", "\tif variables==None:\n", "\t\tvariables=sorted(formula.free_symbols,key=str)\n", "\tnum=num.subs(zip(variables,list(map(lambda x:x**2,variables))))\n", "\tnum=Poly(num)\n", "\tnewformula=S((num.abs()+num)/(num.abs()-num))\n", "\tf=lambdify(variables,newformula)\n", "\tf2=lambda x:f(*x)\n", "\tif values==None:\n", "\t\tvalues=[1.0]*len(variables)\n", "\telse:\n", "\t\tvalues=S(values)\n", "\ttup=tuple(fmin(f2,values,**kwargs))\n", "\treturn tuple([x*x for x in tup])" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Optimization terminated successfully.\n", " Current function value: 1.000000\n", " Iterations: 51\n", " Function evaluations: 109\n" ] }, { "data": { "text/plain": [ "(1.9999999989778676, 0.6689493176765962)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "findvalues(Sm('(x^2-4x+4)/y'))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "findvalues?" ] }, { "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 }