diff --git a/notebooks/todo.ipynb b/notebooks/todo.ipynb index 70ed6be..f3c0727 100644 --- a/notebooks/todo.ipynb +++ b/notebooks/todo.ipynb @@ -177,7 +177,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -327,7 +327,42 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# Funciones añadidas" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + " def get_multidegree(self, p):\n", + " a3 = p.parent()\n", + " n = len(a3.gens())/2\n", + " multidegrees = {}\n", + " for k in p.dict().keys():\n", + " multideg = vector(QQ,n*[0])\n", + " for i in range(n):\n", + " multideg[i] = k[i]+k[i+n]\n", + " multidegrees[k] = multideg\n", + " return multidegrees\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -343,7 +378,7 @@ "0" ] }, - "execution_count": 7, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -366,7 +401,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -377,7 +412,7 @@ " (1, 0, 0, 0, 0, 1, 1, 0): 1}" ] }, - "execution_count": 50, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -389,54 +424,14 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ], - "text/latex": [ - "\\begin{math}\n", - "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{rrrrr}\n", - "1 & 0 & 0 & -2 & -\\frac{1}{2} \\\\\n", - "0 & 1 & 0 & -2 & -\\frac{1}{2} \\\\\n", - "0 & 0 & 1 & -1 & 0 \\\\\n", - "0 & 0 & 0 & 0 & 0 \\\\\n", - "0 & 0 & 0 & 0 & 0 \\\\\n", - "0 & 0 & 0 & 0 & 0\n", - "\\end{array}\\right)\n", - "\\end{math}" - ], - "text/plain": [ - "[ 1 0 0 -2 -1/2]\n", - "[ 0 1 0 -2 -1/2]\n", - "[ 0 0 1 -1 0]\n", - "[ 0 0 0 0 0]\n", - "[ 0 0 0 0 0]\n", - "[ 0 0 0 0 0]" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "M = Matrix(QQ,6,5,[[-2,2,0,0,0],[-1,0,2,0,1/2],[-1,0,0,2,1/2],[0,-1,2,0,1/2],[0,-1,0,2,1/2],[0,0,-1,1,0]])\n", - "show(M.echelon_form())" - ] + "outputs": [], + "source": [] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -452,7 +447,7 @@ "True" ] }, - "execution_count": 17, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -475,7 +470,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -484,7 +479,7 @@ "[t2^2*y0*y1, t1*y0*y2, t0*y1*y2]" ] }, - "execution_count": 57, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -496,7 +491,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -505,7 +500,7 @@ "5*t2^2*y0*y1 + 5*y0*y1" ] }, - "execution_count": 67, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -515,6 +510,29 @@ "p" ] }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{(0, 0, 2, 0, 1, 1, 0, 0): 5,\n", + " (0, 1, 0, 0, 1, 0, 1, 0): -1,\n", + " (1, 0, 0, 0, 0, 1, 1, 0): 1}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "p = 5*t2^2*y0*y1-t1*y0*y2+t0*y1*y2\n", + "p.dict()" + ] + }, { "cell_type": "code", "execution_count": null, @@ -522,6 +540,322 @@ "outputs": [], "source": [] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def get_term(coef, exp, gens):\n", + " return coef*prod(list(map(lambda exp, gen: gen^exp, exp, gens)))\n", + "\n", + "def get_terms(p):\n", + " a = p.parent()\n", + " n = len(a.gens())/2\n", + " ts_only_gens = a.gens()[:n]+tuple(n*[1])\n", + " ys_only_gens = tuple(n*[1])+a.gens()[n:]\n", + " terms = {}\n", + " pol = p.dict()\n", + " for exp in pol.keys():\n", + " key = get_term(1, exp,ys_only_gens)\n", + " if key not in terms.keys():\n", + " coef = get_term(pol[exp],exp,ts_only_gens)\n", + " terms[key] = coef\n", + " else:\n", + " coef = get_term(pol[exp],exp,ts_only_gens)\n", + " terms[key] += coef \n", + " return terms\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5*t2^2*y0*y1 - t1*y0*y2 + t0*y1*y2 + 3*y0*y1\n" + ] + }, + { + "data": { + "text/plain": [ + "{y0*y1: 5*t2^2 + 3, y0*y2: -t1, y1*y2: t0}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "p = 5*t2^2*y0*y1-t1*y0*y2+t0*y1*y2+3*y0*y1\n", + "print(p)\n", + "terms = get_terms(p)\n", + "terms" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# comprobamos que la suma de los términos nos da el polinomio original\n", + "p2 = sum([terms[key]*key for key in terms.keys()])\n", + "p2==p" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{y1: 5*t0*t2^2 + 3*t0}" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lead_term = get_leading_term(p)\n", + "symbolic_partial_integral(lead_term, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": {}, + "outputs": [], + "source": [ + "def get_leading_pol(pol):\n", + " terms = get_terms(pol)\n", + " keys = list(terms.keys())\n", + " keys.sort(reverse=True)\n", + " return terms[keys[0]]*keys[0]\n", + "\n", + "def get_integration_idx(exp):\n", + " # gets the idx 'i' of the first generator 'y_i' that appears in exp\n", + " n = len(exp)/2\n", + " filter_ts = n*[0]+n*[1]\n", + " diff_forms = list(map(lambda v, f: v*f, exp, filter_ts))\n", + " return next((i for i, x in enumerate(list(diff_forms)) if x), None)-n\n", + "\n", + "def symbolic_partial_monomial_integral(monomial, idx):\n", + " exp = list(monomial.dict().keys())[0]\n", + " coef = monomial.dict()[exp]\n", + " n = len(exp)/2\n", + " if (exp[n+idx]==0):\n", + " raise IndexError(\"El término no se puede integrar respecto a esa variable\")\n", + " integral = list(copy.deepcopy(exp))\n", + " integral[n+idx] = 0\n", + " integral[idx] += 1\n", + " new_coef = coef/integral[idx]\n", + " a = monomial.parent()\n", + " return get_term(new_coef, integral, a.gens())\n", + " \n", + "def symbolic_partial_integral(polynomial):\n", + " # necessary condition: all monomials have apl.gens[n+integration_idx] factor\n", + " exp = list(polynomial.dict().keys())[0]\n", + " integration_idx = get_integration_idx(exp)\n", + " aGens = polynomial.parent().gens()\n", + " integral = 0\n", + " for exp in polynomial.dict().keys():\n", + " coef = polynomial.dict()[exp]\n", + " monomial = get_term(coef, exp, aGens)\n", + " integral += symbolic_partial_monomial_integral(monomial, integration_idx)\n", + " terms = get_terms(integral)\n", + " result = sum([terms[key]*key for key in terms.keys()])\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5*t0*t2^2*y1 + 3*t0*y1" + ] + }, + "execution_count": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "symbolic_partial_integral(terms[y0*y1]*y0*y1)" + ] + }, + { + "cell_type": "code", + "execution_count": 143, + "metadata": {}, + "outputs": [], + "source": [ + "def find_preimage_diff(pol):\n", + " if pol.differential()!=0 :\n", + " raise ValueError(\"This polynomial is not in the kernel of the differential\")\n", + " lead_pol = get_leading_pol(pol)\n", + " w = symbolic_partial_integral(lead_pol)\n", + " theta = pol - w.differential()\n", + " if (theta == 0):\n", + " return w\n", + " else:\n", + " return w+find_preimage_diff(theta)" + ] + }, + { + "cell_type": "code", + "execution_count": 144, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "t0*t1" + ] + }, + "execution_count": 144, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Ejemplo 1\n", + "q = t0*y1+t1*y0\n", + "find_preimage_diff(q)" + ] + }, + { + "cell_type": "code", + "execution_count": 149, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 149, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Ejemplo 2\n", + "q1 = (-1/2)*(t0*t1^2*t2*t3*y0)\n", + "q2 = (-1/2)*(t0^2*t1*t2*t3*y1)\n", + "q = q1+q2\n", + "dq = q.differential()\n", + "w = find_preimage_diff(dq)\n", + "w.differential() == dq" + ] + }, + { + "cell_type": "code", + "execution_count": 157, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\begin{math}\n", + "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\verb|-1/2*t0*t1^2*t2*t3*y0|\\phantom{\\verb!x!}\\verb|-|\\phantom{\\verb!x!}\\verb|1/2*t0^2*t1*t2*t3*y1|\n", + "\\end{math}" + ], + "text/plain": [ + "-1/2*t0*t1^2*t2*t3*y0 - 1/2*t0^2*t1*t2*t3*y1" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\begin{math}\n", + "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\verb|1/4*t0^2*t1^2*t3*y2|\\phantom{\\verb!x!}\\verb|+|\\phantom{\\verb!x!}\\verb|1/4*t0^2*t1^2*t2*y3|\n", + "\\end{math}" + ], + "text/plain": [ + "1/4*t0^2*t1^2*t3*y2 + 1/4*t0^2*t1^2*t2*y3" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "show(q)\n", + "show(w)" + ] + }, { "cell_type": "markdown", "metadata": {},