{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python3/dist-packages/sage/misc/remote_file.py:46: DeprecationWarning: ssl.SSLContext() without protocol argument is deprecated.\n",
      "  content = urlopen(req, timeout=1, context=SSLContext())\n",
      "/usr/lib/python3/dist-packages/sage/misc/remote_file.py:46: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated\n",
      "  content = urlopen(req, timeout=1, context=SSLContext())\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\begin{array}{l}\n",
       "\\begin{array}{lcrcrcrcrcl}\n",
       " \\max \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 10 x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 57 x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 9 x_{3} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 24 x_{4} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{11}{2} x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{5}{2} x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 9 x_{4} \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 0 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{3}{2} x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{4} \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 0 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} x_{1} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 1 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2}, x_{3}, x_{4} \\geq 0\n",
       "\\end{array}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\begin{array}{l}\n",
       "\\begin{array}{lcrcrcrcrcl}\n",
       " \\max \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 10 x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 57 x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 9 x_{3} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 24 x_{4} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{11}{2} x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{5}{2} x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 9 x_{4} \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 0 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{3}{2} x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{4} \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 0 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} x_{1} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 1 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2}, x_{3}, x_{4} \\geq 0\n",
       "\\end{array}$$"
      ],
      "text/plain": [
       "LP problem (use 'view(...)' or '%display typeset' for details)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}1</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}1$$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(1,\\,0,\\,1,\\,0\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(1,\\,0,\\,1,\\,0\\right)$$"
      ],
      "text/plain": [
       "(1, 0, 1, 0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{}$$"
      ],
      "text/plain": [
       "salto"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & \\frac{1}{2} & -\\frac{11}{2} & -\\frac{5}{2} & 9 & 1 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 10 & -57 & -9 & -24 & 0 & 0 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & \\frac{1}{2} & -\\frac{11}{2} & -\\frac{5}{2} & 9 & 1 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 10 & -57 & -9 & -24 & 0 & 0 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[    B|   x1    x2    x3    x4    s1    s2    s3|  rhs]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[   s1|  1/2 -11/2  -5/2     9     1     0     0|    0]\n",
       "[   s2|  1/2  -3/2  -1/2     1     0     1     0|    0]\n",
       "[   s3|    1     0     0     0     0     0     1|    1]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[    Z|   10   -57    -9   -24     0     0     0|    0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tipo='max'     #   Problema de tipo max o min\n",
    "\n",
    "n=4 #  número de variables x's\n",
    "m=3 #  número de restricciones (distintas de las de signo)\n",
    "\n",
    "c=matrix(QQ,1,n,[10,-57,-9,-24] )  # vector de costos\n",
    "\n",
    "A=matrix(QQ,m,n,[   # coeficientes de las variables x's\n",
    "[1/2,-11/2,-5/2,9],\n",
    "[1/2,-3/2,-1/2,1],\n",
    "[1,0,0,0]\n",
    "])\n",
    "\n",
    "b=matrix(QQ,m,1,[0,0,1])  # vector de la derecha\n",
    "\n",
    "\n",
    "h=matrix(ZZ,1,m,[1,1,1])  # variables de holgura: \n",
    "                            # Pon 1 si va sumando, \n",
    "                            # -1 si va restando y \n",
    "                            # 0 si no hay variable de holgura.\n",
    "                            \n",
    "t=matrix(ZZ,1,m,[0,0,0])   # variables artificiales: \n",
    "                             # pon 1 si utilizas variable artificial en la fila i-ésima\n",
    "                             #     0 si no utilizas variable artificial en la fila i-ésima\n",
    "\n",
    "colId=(5,6,7) # posición de la matriz identidad\n",
    "\n",
    "\n",
    "    \n",
    "    \n",
    "####################################################################\n",
    "############  HASTA AQUÍ LOS DATOS  ################################\n",
    "####################################################################\n",
    "\n",
    "\n",
    "#load('https://sage.unex.es/501708/simplex2023Auto.sage')\n",
    "\n",
    "load('https://sage.unex.es/501708/simplex2023CompletamenteManual.sage')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & -11 & -5 & 18 & 2 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & 0 & 4 & 2 & -8 & -1 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 11 & 5 & -18 & -2 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 53 & 41 & -204 & -20 & 0 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & -11 & -5 & 18 & 2 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & 0 & 4 & 2 & -8 & -1 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 11 & 5 & -18 & -2 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 53 & 41 & -204 & -20 & 0 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   s1   s2   s3| rhs]\n",
       "[----+----------------------------------+----]\n",
       "[  x1|   1  -11   -5   18    2    0    0|   0]\n",
       "[  s2|   0    4    2   -8   -1    1    0|   0]\n",
       "[  s3|   0   11    5  -18   -2    0    1|   1]\n",
       "[----+----------------------------------+----]\n",
       "[   Z|   0   53   41 -204  -20    0    0|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{1}{2} & -4 & -\\frac{3}{4} & \\frac{11}{4} & 0 & 0 \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{2} & -2 & -\\frac{1}{4} & \\frac{1}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 0 & -\\frac{1}{2} & 4 & \\frac{3}{4} & -\\frac{11}{4} & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & \\frac{29}{2} & -98 & -\\frac{27}{4} & -\\frac{53}{4} & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{1}{2} & -4 & -\\frac{3}{4} & \\frac{11}{4} & 0 & 0 \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{2} & -2 & -\\frac{1}{4} & \\frac{1}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 0 & -\\frac{1}{2} & 4 & \\frac{3}{4} & -\\frac{11}{4} & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & \\frac{29}{2} & -98 & -\\frac{27}{4} & -\\frac{53}{4} & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[    B|   x1    x2    x3    x4    s1    s2    s3|  rhs]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[   x1|    1     0   1/2    -4  -3/4  11/4     0|    0]\n",
       "[   x2|    0     1   1/2    -2  -1/4   1/4     0|    0]\n",
       "[   s3|    0     0  -1/2     4   3/4 -11/4     1|    1]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[    Z|    0     0  29/2   -98 -27/4 -53/4     0|    0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & 2 & 0 & 1 & -8 & -\\frac{3}{2} & \\frac{11}{2} & 0 & 0 \\\\\n",
       "{x}_{2} & -1 & 1 & 0 & 2 & \\frac{1}{2} & -\\frac{5}{2} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -29 & 0 & 0 & 18 & 15 & -93 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & 2 & 0 & 1 & -8 & -\\frac{3}{2} & \\frac{11}{2} & 0 & 0 \\\\\n",
       "{x}_{2} & -1 & 1 & 0 & 2 & \\frac{1}{2} & -\\frac{5}{2} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -29 & 0 & 0 & 18 & 15 & -93 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   s1   s2   s3| rhs]\n",
       "[----+----------------------------------+----]\n",
       "[  x3|   2    0    1   -8 -3/2 11/2    0|   0]\n",
       "[  x2|  -1    1    0    2  1/2 -5/2    0|   0]\n",
       "[  s3|   1    0    0    0    0    0    1|   1]\n",
       "[----+----------------------------------+----]\n",
       "[   Z| -29    0    0   18   15  -93    0|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & -2 & 4 & 1 & 0 & \\frac{1}{2} & -\\frac{9}{2} & 0 & 0 \\\\\n",
       "{x}_{4} & -\\frac{1}{2} & \\frac{1}{2} & 0 & 1 & \\frac{1}{4} & -\\frac{5}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -20 & -9 & 0 & 0 & \\frac{21}{2} & -\\frac{141}{2} & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & -2 & 4 & 1 & 0 & \\frac{1}{2} & -\\frac{9}{2} & 0 & 0 \\\\\n",
       "{x}_{4} & -\\frac{1}{2} & \\frac{1}{2} & 0 & 1 & \\frac{1}{4} & -\\frac{5}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -20 & -9 & 0 & 0 & \\frac{21}{2} & -\\frac{141}{2} & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[     B|    x1     x2     x3     x4     s1     s2     s3|   rhs]\n",
       "[------+------------------------------------------------+------]\n",
       "[    x3|    -2      4      1      0    1/2   -9/2      0|     0]\n",
       "[    x4|  -1/2    1/2      0      1    1/4   -5/4      0|     0]\n",
       "[    s3|     1      0      0      0      0      0      1|     1]\n",
       "[------+------------------------------------------------+------]\n",
       "[     Z|   -20     -9      0      0   21/2 -141/2      0|     0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(2,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & -4 & 8 & 2 & 0 & 1 & -9 & 0 & 0 \\\\\n",
       "{x}_{4} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 22 & -93 & -21 & 0 & 0 & 24 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & -4 & 8 & 2 & 0 & 1 & -9 & 0 & 0 \\\\\n",
       "{x}_{4} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 22 & -93 & -21 & 0 & 0 & 24 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   s1   s2   s3| rhs]\n",
       "[----+----------------------------------+----]\n",
       "[  s1|  -4    8    2    0    1   -9    0|   0]\n",
       "[  x4| 1/2 -3/2 -1/2    1    0    1    0|   0]\n",
       "[  s3|   1    0    0    0    0    0    1|   1]\n",
       "[----+----------------------------------+----]\n",
       "[   Z|  22  -93  -21    0    0   24    0|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & \\frac{1}{2} & -\\frac{11}{2} & -\\frac{5}{2} & 9 & 1 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 10 & -57 & -9 & -24 & 0 & 0 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & \\frac{1}{2} & -\\frac{11}{2} & -\\frac{5}{2} & 9 & 1 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 10 & -57 & -9 & -24 & 0 & 0 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[    B|   x1    x2    x3    x4    s1    s2    s3|  rhs]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[   s1|  1/2 -11/2  -5/2     9     1     0     0|    0]\n",
       "[   s2|  1/2  -3/2  -1/2     1     0     1     0|    0]\n",
       "[   s3|    1     0     0     0     0     0     1|    1]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[    Z|   10   -57    -9   -24     0     0     0|    0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(2,6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hemos vuelto a la tabla inicial! Apliquemos ahora las reglas de R.Bland. Suponemos que el orden establecido es el de los índices, primero las x's y luego las s's."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & -11 & -5 & 18 & 2 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & 0 & 4 & 2 & -8 & -1 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 11 & 5 & -18 & -2 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 53 & 41 & -204 & -20 & 0 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & -11 & -5 & 18 & 2 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & 0 & 4 & 2 & -8 & -1 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 11 & 5 & -18 & -2 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 53 & 41 & -204 & -20 & 0 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   s1   s2   s3| rhs]\n",
       "[----+----------------------------------+----]\n",
       "[  x1|   1  -11   -5   18    2    0    0|   0]\n",
       "[  s2|   0    4    2   -8   -1    1    0|   0]\n",
       "[  s3|   0   11    5  -18   -2    0    1|   1]\n",
       "[----+----------------------------------+----]\n",
       "[   Z|   0   53   41 -204  -20    0    0|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{1}{2} & -4 & -\\frac{3}{4} & \\frac{11}{4} & 0 & 0 \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{2} & -2 & -\\frac{1}{4} & \\frac{1}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 0 & -\\frac{1}{2} & 4 & \\frac{3}{4} & -\\frac{11}{4} & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & \\frac{29}{2} & -98 & -\\frac{27}{4} & -\\frac{53}{4} & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{1}{2} & -4 & -\\frac{3}{4} & \\frac{11}{4} & 0 & 0 \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{2} & -2 & -\\frac{1}{4} & \\frac{1}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 0 & -\\frac{1}{2} & 4 & \\frac{3}{4} & -\\frac{11}{4} & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & \\frac{29}{2} & -98 & -\\frac{27}{4} & -\\frac{53}{4} & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[    B|   x1    x2    x3    x4    s1    s2    s3|  rhs]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[   x1|    1     0   1/2    -4  -3/4  11/4     0|    0]\n",
       "[   x2|    0     1   1/2    -2  -1/4   1/4     0|    0]\n",
       "[   s3|    0     0  -1/2     4   3/4 -11/4     1|    1]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[    Z|    0     0  29/2   -98 -27/4 -53/4     0|    0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & 2 & 0 & 1 & -8 & -\\frac{3}{2} & \\frac{11}{2} & 0 & 0 \\\\\n",
       "{x}_{2} & -1 & 1 & 0 & 2 & \\frac{1}{2} & -\\frac{5}{2} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -29 & 0 & 0 & 18 & 15 & -93 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & 2 & 0 & 1 & -8 & -\\frac{3}{2} & \\frac{11}{2} & 0 & 0 \\\\\n",
       "{x}_{2} & -1 & 1 & 0 & 2 & \\frac{1}{2} & -\\frac{5}{2} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -29 & 0 & 0 & 18 & 15 & -93 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   s1   s2   s3| rhs]\n",
       "[----+----------------------------------+----]\n",
       "[  x3|   2    0    1   -8 -3/2 11/2    0|   0]\n",
       "[  x2|  -1    1    0    2  1/2 -5/2    0|   0]\n",
       "[  s3|   1    0    0    0    0    0    1|   1]\n",
       "[----+----------------------------------+----]\n",
       "[   Z| -29    0    0   18   15  -93    0|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & -2 & 4 & 1 & 0 & \\frac{1}{2} & -\\frac{9}{2} & 0 & 0 \\\\\n",
       "{x}_{4} & -\\frac{1}{2} & \\frac{1}{2} & 0 & 1 & \\frac{1}{4} & -\\frac{5}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -20 & -9 & 0 & 0 & \\frac{21}{2} & -\\frac{141}{2} & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & -2 & 4 & 1 & 0 & \\frac{1}{2} & -\\frac{9}{2} & 0 & 0 \\\\\n",
       "{x}_{4} & -\\frac{1}{2} & \\frac{1}{2} & 0 & 1 & \\frac{1}{4} & -\\frac{5}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -20 & -9 & 0 & 0 & \\frac{21}{2} & -\\frac{141}{2} & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[     B|    x1     x2     x3     x4     s1     s2     s3|   rhs]\n",
       "[------+------------------------------------------------+------]\n",
       "[    x3|    -2      4      1      0    1/2   -9/2      0|     0]\n",
       "[    x4|  -1/2    1/2      0      1    1/4   -5/4      0|     0]\n",
       "[    s3|     1      0      0      0      0      0      1|     1]\n",
       "[------+------------------------------------------------+------]\n",
       "[     Z|   -20     -9      0      0   21/2 -141/2      0|     0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(2,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & -4 & 8 & 2 & 0 & 1 & -9 & 0 & 0 \\\\\n",
       "{x}_{4} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 22 & -93 & -21 & 0 & 0 & 24 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & -4 & 8 & 2 & 0 & 1 & -9 & 0 & 0 \\\\\n",
       "{x}_{4} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 22 & -93 & -21 & 0 & 0 & 24 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   s1   s2   s3| rhs]\n",
       "[----+----------------------------------+----]\n",
       "[  s1|  -4    8    2    0    1   -9    0|   0]\n",
       "[  x4| 1/2 -3/2 -1/2    1    0    1    0|   0]\n",
       "[  s3|   1    0    0    0    0    0    1|   1]\n",
       "[----+----------------------------------+----]\n",
       "[   Z|  22  -93  -21    0    0   24    0|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & 0 & -4 & -2 & 8 & 1 & -1 & 0 & 0 \\\\\n",
       "{x}_{1} & 1 & -3 & -1 & 2 & 0 & 2 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 3 & 1 & -2 & 0 & -2 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & -27 & 1 & -44 & 0 & -20 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & 0 & -4 & -2 & 8 & 1 & -1 & 0 & 0 \\\\\n",
       "{x}_{1} & 1 & -3 & -1 & 2 & 0 & 2 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 3 & 1 & -2 & 0 & -2 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & -27 & 1 & -44 & 0 & -20 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  x4  s1  s2  s3|rhs]\n",
       "[---+---------------------------+---]\n",
       "[ s1|  0  -4  -2   8   1  -1   0|  0]\n",
       "[ x1|  1  -3  -1   2   0   2   0|  0]\n",
       "[ s3|  0   3   1  -2   0  -2   1|  1]\n",
       "[---+---------------------------+---]\n",
       "[  Z|  0 -27   1 -44   0 -20   0|  0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(2,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & 0 & 2 & 0 & 4 & 1 & -5 & 2 & 2 \\\\\n",
       "{x}_{1} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "{x}_{3} & 0 & 3 & 1 & -2 & 0 & -2 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & -30 & 0 & -42 & 0 & -18 & -1 & 1\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & 0 & 2 & 0 & 4 & 1 & -5 & 2 & 2 \\\\\n",
       "{x}_{1} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "{x}_{3} & 0 & 3 & 1 & -2 & 0 & -2 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & -30 & 0 & -42 & 0 & -18 & -1 & 1\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  x4  s1  s2  s3|rhs]\n",
       "[---+---------------------------+---]\n",
       "[ s1|  0   2   0   4   1  -5   2|  2]\n",
       "[ x1|  1   0   0   0   0   0   1|  1]\n",
       "[ x3|  0   3   1  -2   0  -2   1|  1]\n",
       "[---+---------------------------+---]\n",
       "[  Z|  0 -30   0 -42   0 -18  -1|  1]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python3/dist-packages/sage/misc/remote_file.py:46: DeprecationWarning: ssl.SSLContext() without protocol argument is deprecated.\n",
      "  content = urlopen(req, timeout=1, context=SSLContext())\n",
      "/usr/lib/python3/dist-packages/sage/misc/remote_file.py:46: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated\n",
      "  content = urlopen(req, timeout=1, context=SSLContext())\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\begin{array}{l}\n",
       "\\begin{array}{lcrcrcrcrcl}\n",
       " \\max \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 10 x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 57 x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 9 x_{3} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 24 x_{4} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{11}{2} x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{5}{2} x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 9 x_{4} \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 0 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{3}{2} x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{4} \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 0 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} x_{1} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 1 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2}, x_{3}, x_{4} \\geq 0\n",
       "\\end{array}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\begin{array}{l}\n",
       "\\begin{array}{lcrcrcrcrcl}\n",
       " \\max \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 10 x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 57 x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 9 x_{3} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 24 x_{4} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{11}{2} x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{5}{2} x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 9 x_{4} \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 0 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{3}{2} x_{2} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} \\frac{1}{2} x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{4} \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 0 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} x_{1} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 1 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2}, x_{3}, x_{4} \\geq 0\n",
       "\\end{array}$$"
      ],
      "text/plain": [
       "LP problem (use 'view(...)' or '%display typeset' for details)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}1</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}1$$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(1,\\,0,\\,1,\\,0\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(1,\\,0,\\,1,\\,0\\right)$$"
      ],
      "text/plain": [
       "(1, 0, 1, 0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{}$$"
      ],
      "text/plain": [
       "salto"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & \\frac{1}{2} & -\\frac{11}{2} & -\\frac{5}{2} & 9 & 1 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 10 & -57 & -9 & -24 & 0 & 0 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & \\frac{1}{2} & -\\frac{11}{2} & -\\frac{5}{2} & 9 & 1 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 10 & -57 & -9 & -24 & 0 & 0 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[    B|   x1    x2    x3    x4    s1    s2    s3|  rhs]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[   s1|  1/2 -11/2  -5/2     9     1     0     0|    0]\n",
       "[   s2|  1/2  -3/2  -1/2     1     0     1     0|    0]\n",
       "[   s3|    1     0     0     0     0     0     1|    1]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[    Z|   10   -57    -9   -24     0     0     0|    0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tipo='max'     #   Problema de tipo max o min\n",
    "\n",
    "n=4 #  número de variables x's\n",
    "m=3 #  número de restricciones (distintas de las de signo)\n",
    "\n",
    "c=matrix(QQ,1,n,[10,-57,-9,-24] )  # vector de costos\n",
    "\n",
    "A=matrix(QQ,m,n,[   # coeficientes de las variables x's\n",
    "[1/2,-11/2,-5/2,9],\n",
    "[1/2,-3/2,-1/2,1],\n",
    "[1,0,0,0]\n",
    "])\n",
    "\n",
    "b=matrix(QQ,m,1,[0,0,1])  # vector de la derecha\n",
    "\n",
    "\n",
    "h=matrix(ZZ,1,m,[1,1,1])  # variables de holgura: \n",
    "                            # Pon 1 si va sumando, \n",
    "                            # -1 si va restando y \n",
    "                            # 0 si no hay variable de holgura.\n",
    "                            \n",
    "t=matrix(ZZ,1,m,[0,0,0])   # variables artificiales: \n",
    "                             # pon 1 si utilizas variable artificial en la fila i-ésima\n",
    "                             #     0 si no utilizas variable artificial en la fila i-ésima\n",
    "\n",
    "colId=(5,6,7) # posición de la matriz identidad\n",
    "\n",
    "\n",
    "    \n",
    "    \n",
    "####################################################################\n",
    "############  HASTA AQUÍ LOS DATOS  ################################\n",
    "####################################################################\n",
    "\n",
    "\n",
    "#load('https://sage.unex.es/501708/simplex2023Auto.sage')\n",
    "\n",
    "load('https://sage.unex.es/501708/simplex2023CompletamenteManual.sage')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & -11 & -5 & 18 & 2 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & 0 & 4 & 2 & -8 & -1 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 11 & 5 & -18 & -2 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 53 & 41 & -204 & -20 & 0 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & -11 & -5 & 18 & 2 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & 0 & 4 & 2 & -8 & -1 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 11 & 5 & -18 & -2 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 53 & 41 & -204 & -20 & 0 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   s1   s2   s3| rhs]\n",
       "[----+----------------------------------+----]\n",
       "[  x1|   1  -11   -5   18    2    0    0|   0]\n",
       "[  s2|   0    4    2   -8   -1    1    0|   0]\n",
       "[  s3|   0   11    5  -18   -2    0    1|   1]\n",
       "[----+----------------------------------+----]\n",
       "[   Z|   0   53   41 -204  -20    0    0|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{1}{2} & -4 & -\\frac{3}{4} & \\frac{11}{4} & 0 & 0 \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{2} & -2 & -\\frac{1}{4} & \\frac{1}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 0 & -\\frac{1}{2} & 4 & \\frac{3}{4} & -\\frac{11}{4} & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & \\frac{29}{2} & -98 & -\\frac{27}{4} & -\\frac{53}{4} & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{1}{2} & -4 & -\\frac{3}{4} & \\frac{11}{4} & 0 & 0 \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{2} & -2 & -\\frac{1}{4} & \\frac{1}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 0 & 0 & -\\frac{1}{2} & 4 & \\frac{3}{4} & -\\frac{11}{4} & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & \\frac{29}{2} & -98 & -\\frac{27}{4} & -\\frac{53}{4} & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[    B|   x1    x2    x3    x4    s1    s2    s3|  rhs]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[   x1|    1     0   1/2    -4  -3/4  11/4     0|    0]\n",
       "[   x2|    0     1   1/2    -2  -1/4   1/4     0|    0]\n",
       "[   s3|    0     0  -1/2     4   3/4 -11/4     1|    1]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[    Z|    0     0  29/2   -98 -27/4 -53/4     0|    0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & 2 & 0 & 1 & -8 & -\\frac{3}{2} & \\frac{11}{2} & 0 & 0 \\\\\n",
       "{x}_{2} & -1 & 1 & 0 & 2 & \\frac{1}{2} & -\\frac{5}{2} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -29 & 0 & 0 & 18 & 15 & -93 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & 2 & 0 & 1 & -8 & -\\frac{3}{2} & \\frac{11}{2} & 0 & 0 \\\\\n",
       "{x}_{2} & -1 & 1 & 0 & 2 & \\frac{1}{2} & -\\frac{5}{2} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -29 & 0 & 0 & 18 & 15 & -93 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   s1   s2   s3| rhs]\n",
       "[----+----------------------------------+----]\n",
       "[  x3|   2    0    1   -8 -3/2 11/2    0|   0]\n",
       "[  x2|  -1    1    0    2  1/2 -5/2    0|   0]\n",
       "[  s3|   1    0    0    0    0    0    1|   1]\n",
       "[----+----------------------------------+----]\n",
       "[   Z| -29    0    0   18   15  -93    0|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & -2 & 4 & 1 & 0 & \\frac{1}{2} & -\\frac{9}{2} & 0 & 0 \\\\\n",
       "{x}_{4} & -\\frac{1}{2} & \\frac{1}{2} & 0 & 1 & \\frac{1}{4} & -\\frac{5}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -20 & -9 & 0 & 0 & \\frac{21}{2} & -\\frac{141}{2} & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & -2 & 4 & 1 & 0 & \\frac{1}{2} & -\\frac{9}{2} & 0 & 0 \\\\\n",
       "{x}_{4} & -\\frac{1}{2} & \\frac{1}{2} & 0 & 1 & \\frac{1}{4} & -\\frac{5}{4} & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -20 & -9 & 0 & 0 & \\frac{21}{2} & -\\frac{141}{2} & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[     B|    x1     x2     x3     x4     s1     s2     s3|   rhs]\n",
       "[------+------------------------------------------------+------]\n",
       "[    x3|    -2      4      1      0    1/2   -9/2      0|     0]\n",
       "[    x4|  -1/2    1/2      0      1    1/4   -5/4      0|     0]\n",
       "[    s3|     1      0      0      0      0      0      1|     1]\n",
       "[------+------------------------------------------------+------]\n",
       "[     Z|   -20     -9      0      0   21/2 -141/2      0|     0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & -4 & 8 & 2 & 0 & 1 & -9 & 0 & 0 \\\\\n",
       "{x}_{4} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 22 & -93 & -21 & 0 & 0 & 24 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & -4 & 8 & 2 & 0 & 1 & -9 & 0 & 0 \\\\\n",
       "{x}_{4} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 22 & -93 & -21 & 0 & 0 & 24 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   s1   s2   s3| rhs]\n",
       "[----+----------------------------------+----]\n",
       "[  s1|  -4    8    2    0    1   -9    0|   0]\n",
       "[  x4| 1/2 -3/2 -1/2    1    0    1    0|   0]\n",
       "[  s3|   1    0    0    0    0    0    1|   1]\n",
       "[----+----------------------------------+----]\n",
       "[   Z|  22  -93  -21    0    0   24    0|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & \\frac{1}{2} & -\\frac{11}{2} & -\\frac{5}{2} & 9 & 1 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 10 & -57 & -9 & -24 & 0 & 0 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & \\frac{1}{2} & -\\frac{11}{2} & -\\frac{5}{2} & 9 & 1 & 0 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{2} & \\frac{1}{2} & -\\frac{3}{2} & -\\frac{1}{2} & 1 & 0 & 1 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 10 & -57 & -9 & -24 & 0 & 0 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[    B|   x1    x2    x3    x4    s1    s2    s3|  rhs]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[   s1|  1/2 -11/2  -5/2     9     1     0     0|    0]\n",
       "[   s2|  1/2  -3/2  -1/2     1     0     1     0|    0]\n",
       "[   s3|    1     0     0     0     0     0     1|    1]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[    Z|   10   -57    -9   -24     0     0     0|    0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,1);cambio(2,2);cambio(1,3);cambio(2,4);cambio(1,5);cambio(2,6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hagamos cambios para sacar los 0's del vector de la derecha (intento a la desesperada)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & -2 & 2 & 0 & 4 & 1 & -5 & 0 & 0 \\\\\n",
       "{x}_{3} & -1 & 3 & 1 & -2 & 0 & -2 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 1 & -30 & 0 & -42 & 0 & -18 & 0 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & -2 & 2 & 0 & 4 & 1 & -5 & 0 & 0 \\\\\n",
       "{x}_{3} & -1 & 3 & 1 & -2 & 0 & -2 & 0 & 0 \\\\\n",
       "{\\color{green}{s}}_{3} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 1 & -30 & 0 & -42 & 0 & -18 & 0 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  x4  s1  s2  s3|rhs]\n",
       "[---+---------------------------+---]\n",
       "[ s1| -2   2   0   4   1  -5   0|  0]\n",
       "[ x3| -1   3   1  -2   0  -2   0|  0]\n",
       "[ s3|  1   0   0   0   0   0   1|  1]\n",
       "[---+---------------------------+---]\n",
       "[  Z|  1 -30   0 -42   0 -18   0|  0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & 0 & 2 & 0 & 4 & 1 & -5 & 2 & 2 \\\\\n",
       "{x}_{3} & 0 & 3 & 1 & -2 & 0 & -2 & 1 & 1 \\\\\n",
       "{x}_{1} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & -30 & 0 & -42 & 0 & -18 & -1 & 1\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & 0 & 2 & 0 & 4 & 1 & -5 & 2 & 2 \\\\\n",
       "{x}_{3} & 0 & 3 & 1 & -2 & 0 & -2 & 1 & 1 \\\\\n",
       "{x}_{1} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & -30 & 0 & -42 & 0 & -18 & -1 & 1\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  x4  s1  s2  s3|rhs]\n",
       "[---+---------------------------+---]\n",
       "[ s1|  0   2   0   4   1  -5   2|  2]\n",
       "[ x3|  0   3   1  -2   0  -2   1|  1]\n",
       "[ x1|  1   0   0   0   0   0   1|  1]\n",
       "[---+---------------------------+---]\n",
       "[  Z|  0 -30   0 -42   0 -18  -1|  1]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & 0 & 0 & -\\frac{2}{3} & \\frac{16}{3} & 1 & -\\frac{11}{3} & \\frac{4}{3} & \\frac{4}{3} \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{3} & -\\frac{2}{3} & 0 & -\\frac{2}{3} & \\frac{1}{3} & \\frac{1}{3} \\\\\n",
       "{x}_{1} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & 10 & -62 & 0 & -38 & 9 & -9\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & 0 & 0 & -\\frac{2}{3} & \\frac{16}{3} & 1 & -\\frac{11}{3} & \\frac{4}{3} & \\frac{4}{3} \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{3} & -\\frac{2}{3} & 0 & -\\frac{2}{3} & \\frac{1}{3} & \\frac{1}{3} \\\\\n",
       "{x}_{1} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & 10 & -62 & 0 & -38 & 9 & -9\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[    B|   x1    x2    x3    x4    s1    s2    s3|  rhs]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[   s1|    0     0  -2/3  16/3     1 -11/3   4/3|  4/3]\n",
       "[   x2|    0     1   1/3  -2/3     0  -2/3   1/3|  1/3]\n",
       "[   x1|    1     0     0     0     0     0     1|    1]\n",
       "[-----+-----------------------------------------+-----]\n",
       "[    Z|    0     0    10   -62     0   -38     9|   -9]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & 0 & 2 & 0 & 4 & 1 & -5 & 2 & 2 \\\\\n",
       "{x}_{3} & 0 & 3 & 1 & -2 & 0 & -2 & 1 & 1 \\\\\n",
       "{x}_{1} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & -30 & 0 & -42 & 0 & -18 & -1 & 1\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{1} & 0 & 2 & 0 & 4 & 1 & -5 & 2 & 2 \\\\\n",
       "{x}_{3} & 0 & 3 & 1 & -2 & 0 & -2 & 1 & 1 \\\\\n",
       "{x}_{1} & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & -30 & 0 & -42 & 0 & -18 & -1 & 1\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  x4  s1  s2  s3|rhs]\n",
       "[---+---------------------------+---]\n",
       "[ s1|  0   2   0   4   1  -5   2|  2]\n",
       "[ x3|  0   3   1  -2   0  -2   1|  1]\n",
       "[ x1|  1   0   0   0   0   0   1|  1]\n",
       "[---+---------------------------+---]\n",
       "[  Z|  0 -30   0 -42   0 -18  -1|  1]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(2,3);cambio(3,1);cambio(2,2);cambio(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 9.5",
   "language": "sage",
   "name": "sagemath"
  },
  "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.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
