Skip to content

Instantly share code, notes, and snippets.

@Asmilex
Last active January 9, 2021 11:42
Show Gist options
  • Save Asmilex/c631446304cfbc029055d0951a8967cc to your computer and use it in GitHub Desktop.
Save Asmilex/c631446304cfbc029055d0951a8967cc to your computer and use it in GitHub Desktop.
Calculadora para ecuaciones cúbicas y cuárticas
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [],
"source": [
"import sympy as sym\n",
"x = sym.Symbol('x')"
]
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [],
"source": [
"a = 1\n",
"b = 6\n",
"c = 9\n",
"d = 4\n",
"\n",
"n = 3\n",
"beta = b/n"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [],
"source": [
"def f(x):\n",
" return a* x**3 + b* x**2 + c*x + d"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"def red_f(x):\n",
" return f(x-beta).simplify()"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Poly(1.0*x**3 - 3.0*x + 2.0, x, domain='RR')"
],
"text/latex": "$\\displaystyle \\operatorname{Poly}{\\left( 1.0 x^{3} - 3.0 x + 2.0, x, domain=\\mathbb{R} \\right)}$"
},
"metadata": {},
"execution_count": 167
}
],
"source": [
"polinomio_reducido = (sym.poly_from_expr(red_f(x), x))[0]\n",
"polinomio_reducido"
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"p = -3.00000000000000 ; q = 2.00000000000000\n"
]
}
],
"source": [
"p = polinomio_reducido.nth(1)\n",
"q = polinomio_reducido.nth(0)\n",
"\n",
"if (polinomio_reducido.nth(1) == 0):\n",
" print(\"\\n\\n\\t\\t\\tIGNORA EL RESTO DE LA SALIDA. Solo esto es lo importante\\n\\n\\n Has tenido potra y te ha salido muy fácil, no hace falta aplicar Cardano. Las soluciones beta de la reducida vienen dadas por \\n\\tBeta_1 = \", sym.rootof(polinomio_reducido, 0), \"\\n\\tBeta_2 = \", sym.rootof(polinomio_reducido, 0), \"* e^(i * 2Pi/3)\\n\\tBeta_3 = \", sym.rootof(polinomio_reducido, 0), \"* e^(i * 4Pi/3)\\nY las de la cúbica son \\n\\tAlpha_i = Beta_i - \", b/n)\n",
"else: \n",
" print(\"p = \", p, \"; q = \", q)"
]
},
{
"cell_type": "code",
"execution_count": 169,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"x**2 + 2.0*x + 1.0"
],
"text/latex": "$\\displaystyle x^{2} + 2.0 x + 1.0$"
},
"metadata": {},
"execution_count": 169
}
],
"source": [
"def resol_f(x):\n",
" return x**2 +q*x - (p**3)/27\n",
"resol_f(x)"
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Queremos soluciones de la forma x = y + z tal que \n",
"\tyz = 1.00000000000000\n",
"\n",
"Hay que resolver el sistema \n",
"\ty^3 + z^3 = -2.00000000000000 \n",
"\ty^3 * z^3 = 1.00000000000000\n",
"\n",
"Para ello, tenemos que resolver el polinomio\n",
"\t x**2 + 2.0*x + 1.0\n"
]
}
],
"source": [
"print(\"Queremos soluciones de la forma x = y + z tal que \\n\\tyz = \", -p/3)\n",
"print(\"\\nHay que resolver el sistema \\n\\ty^3 + z^3 = \", -q, \"\\n\\ty^3 * z^3 = \", - (p*p*p)/27)\n",
"print(\"\\nPara ello, tenemos que resolver el polinomio\\n\\t\", resol_f(x))"
]
},
{
"cell_type": "code",
"execution_count": 181,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Las raíces de esta ecuación son -1 -1\n\nPor tanto, nuestras y_i vienen dadas por\n\ty_1 = 0.5 + 0.866025403784439*I \n\ty_2 = (0.5 + 0.866025403784439*I)*exp(2*I*pi/3) \n\ty_3 = (0.5 + 0.866025403784439*I)*exp(-2*I*pi/3)\n\n\tz_1 = 0.5 + 0.866025403784439*I \n\tz_2 = (0.5 + 0.866025403784439*I)*exp(2*I*pi/3) \n\tz_3 = (0.5 + 0.866025403784439*I)*exp(-2*I*pi/3)\nNota: recomiendo hacer esto a mano, para operar con complejos de forma más cómoda. Usa esto solo como comprobación\n"
]
}
],
"source": [
"\n",
"raiz_y = (sym.poly_from_expr(resol_f(x), x))[0].all_roots()[0]\n",
"raiz_z = (sym.poly_from_expr(resol_f(x), x))[0].all_roots()[1]\n",
"\n",
"print(\"Las raíces de esta ecuación son\", raiz_y, raiz_z)\n",
"print(\"\\nPor tanto, nuestras y_i vienen dadas por\")\n",
"\n",
"w = sym.exp(2 * sym.pi * sym.I / 3)\n",
"\n",
"y = [(raiz_y**(1/3)), (w*(raiz_y**(1/3))), (w*w*(raiz_y**(1/3)))]\n",
"z = [(raiz_z**(1/3)), (w*(raiz_z**(1/3))), (w*w*(raiz_z**(1/3)))]\n",
"\n",
"print(\"\\ty_1 = \", y[0] , \"\\n\\ty_2 = \", y[1], \"\\n\\ty_3 = \", y[2])\n",
"print(\"\\n\\tz_1 = \", z[0], \"\\n\\tz_2 = \", z[1], \"\\n\\tz_3 = \", z[2])\n",
"\n",
"print(\"Nota: recomiendo hacer esto a mano, para operar con complejos de forma más cómoda. Usa esto solo como comprobación\")"
]
},
{
"cell_type": "code",
"execution_count": 187,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Nota: a partir de este paso es posible que se rompa, porque está multiplicando con complejos. ¡Ten cuidado!\nHan de emparejarse de forma que y_i * z_j = -p/3 = 1.00000000000000 \n\nLas únicas que dan esto son\nAntes de proseguir, comprobar que las raíces que nos salen están en Q. Si no estuvieran, comprobar que hay 2 raíces complejas no reales exatamente => G(f/Q) isomorfo a S3\n\n"
]
},
{
"output_type": "error",
"ename": "IndexError",
"evalue": "list index out of range",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-187-fda7c1705d02>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[0mraíces_iniciales\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mbeta\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0msoluciones_reducida\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"\\nLas soluciones de la reducida son \\n\\t\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msoluciones_reducida\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"\\n\\t\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msoluciones_reducida\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"\\n\\t\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msoluciones_reducida\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"\\n\\nPor lo que las raíces de la ecuación inicial son \\n\\t\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mraíces_iniciales\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"\\n\\t\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mraíces_iniciales\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"\\n\\t\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mraíces_iniciales\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"print(\"Nota: a partir de este paso es posible que se rompa, porque está multiplicando con complejos. ¡Ten cuidado!\")\n",
"print(\"Han de emparejarse de forma que y_i * z_j = -p/3 = \", -p/3, \"\\n\\nLas únicas que dan esto son\")\n",
"\n",
"soluciones_reducida = []\n",
"for i in range(0, 3):\n",
" for j in range (0, 3):\n",
" if ((y[i] * z[j]).simplify() == -p/3):\n",
" print(\"\\ty_\", i+1, \" * z_\", j+1, \" = \", y[i] * z[j])\n",
" soluciones_reducida.append((y[i] + z[j]))\n",
"\n",
"print(\"Antes de proseguir, comprobar que las raíces que nos salen están en Q. Si no estuvieran, comprobar que hay 2 raíces complejas no reales exatamente => G(f/Q) isomorfo a S3\\n\")\n",
"raíces_iniciales = [i - beta for i in soluciones_reducida]\n",
"\n",
"print(\"\\nLas soluciones de la reducida son \\n\\t\", soluciones_reducida[0], \"\\n\\t\", soluciones_reducida[1], \"\\n\\t\", soluciones_reducida[2], \"\\n\\nPor lo que las raíces de la ecuación inicial son \\n\\t\", raíces_iniciales[0], \"\\n\\t\", raíces_iniciales[1], \"\\n\\t\", raíces_iniciales[2])\n"
]
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3.7.0 32-bit",
"metadata": {
"interpreter": {
"hash": "ca88bab9ce8d9e4c3717e0c654e4f59b1c36aef4cbef335c62f6a9b2c686e4f2"
}
}
},
"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.0-final"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment