{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "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}{lcrcrcrcrcrcl}\n",
       " \\min \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 3 x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{3} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 2 x_{4} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 2 x_{5} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 3 x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 4 x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{4} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} x_{5} \\mspace{-6mu}&\\mspace{-6mu} = \\mspace{-6mu}&\\mspace{-6mu} 2 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2}, x_{3}, x_{4}, x_{5} \\geq 0\n",
       "\\end{array}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\begin{array}{l}\n",
       "\\begin{array}{lcrcrcrcrcrcl}\n",
       " \\min \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 3 x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{3} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 2 x_{4} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 2 x_{5} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 3 x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 4 x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{4} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} x_{5} \\mspace{-6mu}&\\mspace{-6mu} = \\mspace{-6mu}&\\mspace{-6mu} 2 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2}, x_{3}, x_{4}, x_{5} \\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}}-\\infty</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}-\\infty$$"
      ],
      "text/plain": [
       "-Infinity"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\mathrm{None}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\mathrm{None}$$"
      ],
      "text/plain": [
       "None"
      ]
     },
     "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|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {x}_{5} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 3 & 4 & 1 & -1 & 2 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & -7 & -11 & -5 & 1 & 6\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {x}_{5} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 3 & 4 & 1 & -1 & 2 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & -7 & -11 & -5 & 1 & 6\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  x4  x5|rhs]\n",
       "[---+-------------------+---]\n",
       "[ x1|  1   3   4   1  -1|  2]\n",
       "[---+-------------------+---]\n",
       "[  Z|  0  -7 -11  -5   1|  6]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tipo='min'     #   Problema de tipo max o min\n",
    "\n",
    "n=5 #  número de variables x's\n",
    "m=1 #  número de restricciones (distintas de las de signo)\n",
    "\n",
    "c=matrix(QQ,1,n,[3,2,1,-2,-2] )  # vector de costos\n",
    "\n",
    "A=matrix(QQ,m,n,[   # coeficientes de las variables x's\n",
    "[1,3,4,1,-1]\n",
    "])\n",
    "\n",
    "b=matrix(QQ,m,1,[2])  # vector de la derecha\n",
    "\n",
    "\n",
    "h=matrix(ZZ,1,m,[0])  # 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])   # 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=(1,1) # posición de la matriz identidad\n",
    "\n",
    "\n",
    "    \n",
    "    \n",
    "####################################################################\n",
    "############  HASTA AQUÍ LOS DATOS  ################################\n",
    "####################################################################\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "load('https://sage.unex.es/501708/simplex2023CompletamenteManual.sage')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {x}_{5} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{2} & \\frac{1}{3} & 1 & \\frac{4}{3} & \\frac{1}{3} & -\\frac{1}{3} & \\frac{2}{3} \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & \\frac{7}{3} & 0 & -\\frac{5}{3} & -\\frac{8}{3} & -\\frac{4}{3} & \\frac{4}{3}\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {x}_{5} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{2} & \\frac{1}{3} & 1 & \\frac{4}{3} & \\frac{1}{3} & -\\frac{1}{3} & \\frac{2}{3} \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & \\frac{7}{3} & 0 & -\\frac{5}{3} & -\\frac{8}{3} & -\\frac{4}{3} & \\frac{4}{3}\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   x5| rhs]\n",
       "[----+------------------------+----]\n",
       "[  x2| 1/3    1  4/3  1/3 -1/3| 2/3]\n",
       "[----+------------------------+----]\n",
       "[   Z| 7/3    0 -5/3 -8/3 -4/3| 4/3]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,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|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {x}_{5} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & \\frac{1}{4} & \\frac{3}{4} & 1 & \\frac{1}{4} & -\\frac{1}{4} & \\frac{1}{2} \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & \\frac{11}{4} & \\frac{5}{4} & 0 & -\\frac{9}{4} & -\\frac{7}{4} & \\frac{1}{2}\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {x}_{5} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & \\frac{1}{4} & \\frac{3}{4} & 1 & \\frac{1}{4} & -\\frac{1}{4} & \\frac{1}{2} \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & \\frac{11}{4} & \\frac{5}{4} & 0 & -\\frac{9}{4} & -\\frac{7}{4} & \\frac{1}{2}\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   x5| rhs]\n",
       "[----+------------------------+----]\n",
       "[  x3| 1/4  3/4    1  1/4 -1/4| 1/2]\n",
       "[----+------------------------+----]\n",
       "[   Z|11/4  5/4    0 -9/4 -7/4| 1/2]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "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|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {x}_{5} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{4} & 1 & 3 & 4 & 1 & -1 & 2 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 5 & 8 & 9 & 0 & -4 & -4\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {x}_{5} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{4} & 1 & 3 & 4 & 1 & -1 & 2 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 5 & 8 & 9 & 0 & -4 & -4\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  x4  x5|rhs]\n",
       "[---+-------------------+---]\n",
       "[ x4|  1   3   4   1  -1|  2]\n",
       "[---+-------------------+---]\n",
       "[  Z|  5   8   9   0  -4| -4]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,4)"
   ]
  }
 ],
 "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
}
