{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-2 & 0 & 0 & -1 & 1\\\\2 & 2 & 1 & 1 & -1\\\\-4 & -4 & -2 & -3 & 2\\\\0 & 0 & 0 & 0 & 0\\\\-2 & 0 & 0 & -1 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[-2,  0,  0, -1,  1],\n",
       "[ 2,  2,  1,  1, -1],\n",
       "[-4, -4, -2, -3,  2],\n",
       "[ 0,  0,  0,  0,  0],\n",
       "[-2,  0,  0, -1,  1]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = sp.Matrix([[-2, 0, 0, -1, 1], [2, 2, 1, 1, -1], [-4, -4, -2, -3, 2], [0, 0, 0, 0, 0], [-2, 0, 0, -1, 1]])\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x^{5} + x^{4}$"
      ],
      "text/plain": [
       "x**5 + x**4"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#bestimme charakteristisches Polynom\n",
    "x = sp.symbols('x')\n",
    "(x*sp.eye(5) - A).det()\n",
    "#am besten mit Laplace Entwicklung nach der 4. Zeile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1\\\\-1\\\\2\\\\0\\\\1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 1],\n",
       "[-1],\n",
       "[ 2],\n",
       "[ 0],\n",
       "[ 1]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Es gibt also 2 Eigenwerte: -1 mit algebraischer Vielfachheit 1\n",
    "# und 0 mit Vielfachheit 4. \n",
    "#Für die -1 ist nicht viel zu machen: wir sehen schon algebraische = geometrische Vielfachheit und brauchen \n",
    "#nur einen Eigenvektor dazu.\n",
    "s5 = sp.Matrix((-sp.eye(5) - A).nullspace())\n",
    "s5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1\\\\0\\\\0\\\\0\\\\2\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1],\n",
       "[0],\n",
       "[0],\n",
       "[0],\n",
       "[2]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Die folgenden beiden Eigenvektoren zum EW 0 sieht man der Matrix direkt an:\n",
    "s1 = sp.Matrix([1, 0, 0, 0, 2])\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0\\\\1\\\\-2\\\\0\\\\0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 0],\n",
       "[ 1],\n",
       "[-2],\n",
       "[ 0],\n",
       "[ 0]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = sp.Matrix([0, 1, -2, 0, 0])\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2 & 0 & 0 & 1 & -1\\\\-2 & 0 & 0 & -2 & 1\\\\4 & 0 & 0 & 4 & -2\\\\0 & 0 & 0 & 0 & 0\\\\2 & 0 & 0 & 1 & -1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 2, 0, 0,  1, -1],\n",
       "[-2, 0, 0, -2,  1],\n",
       "[ 4, 0, 0,  4, -2],\n",
       "[ 0, 0, 0,  0,  0],\n",
       "[ 2, 0, 0,  1, -1]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Die beiden sind offenbar linear unabhängig und damit wissen wir schon, dass sich die vier 0en auf der\n",
    "#Diagonalen der JNF von A auf mindestens 2 Jordankästchen verteilen müssen. Schauen wir uns die Potenzen von -A an:\n",
    "(-A)**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2 & 0 & 0 & 1 & -1\\\\-2 & 0 & 0 & -1 & 1\\\\4 & 0 & 0 & 2 & -2\\\\0 & 0 & 0 & 0 & 0\\\\2 & 0 & 0 & 1 & -1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 2, 0, 0,  1, -1],\n",
       "[-2, 0, 0, -1,  1],\n",
       "[ 4, 0, 0,  2, -2],\n",
       "[ 0, 0, 0,  0,  0],\n",
       "[ 2, 0, 0,  1, -1]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(-A)**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-1 & 0 & 0 & 0 & 0\\\\0 & 0 & 1 & 0 & 0\\\\0 & 0 & 0 & 1 & 0\\\\0 & 0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[-1, 0, 0, 0, 0],\n",
       "[ 0, 0, 1, 0, 0],\n",
       "[ 0, 0, 0, 1, 0],\n",
       "[ 0, 0, 0, 0, 0],\n",
       "[ 0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Wir sehen also, dass der Rang von A^3 gleich 1, der von A^2 aber noch nicht. Wir folgern, \n",
    "#dass das größte Jordankästchen zum Eigenwert 0 Größe haben muss. Damit ist klar, die JNF von A aussieht:\n",
    "S, J = A.jordan_form()\n",
    "J"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1\\\\0\\\\0\\\\0\\\\0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1],\n",
       "[0],\n",
       "[0],\n",
       "[0],\n",
       "[0]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Nun bestimmen wir die Matrix S. Wir brauchen zunächst irgendeinen Vektor, der nicht im Kern von -A^3 liegt, z.B.\n",
    "s = sp.Matrix([1, 0, 0, 0, 0])\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2 & 2 & 1\\\\-2 & -2 & 0\\\\4 & 4 & 0\\\\0 & 0 & 0\\\\2 & 2 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 2,  2, 1],\n",
       "[-2, -2, 0],\n",
       "[ 4,  4, 0],\n",
       "[ 0,  0, 0],\n",
       "[ 2,  2, 0]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Der Teil von S, der zu dem Jordankästchen der Größe 3 korrespondiert is nun direkt gegeben als\n",
    "S = ((-A)*(-A)*s).row_join((-A)*s).row_join(s)\n",
    "S"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2\\\\-2\\\\4\\\\0\\\\2\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 2],\n",
       "[-2],\n",
       "[ 4],\n",
       "[ 0],\n",
       "[ 2]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A*A*s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-2\\\\2\\\\-4\\\\0\\\\-2\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[-2],\n",
       "[ 2],\n",
       "[-4],\n",
       "[ 0],\n",
       "[-2]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A*s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-2 & 0 & 0 & -1 & 1\\\\2 & 0 & 0 & 1 & -1\\\\-4 & 0 & 0 & -2 & 2\\\\0 & 0 & 0 & 0 & 0\\\\-2 & 0 & 0 & -1 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[-2, 0, 0, -1,  1],\n",
       "[ 2, 0, 0,  1, -1],\n",
       "[-4, 0, 0, -2,  2],\n",
       "[ 0, 0, 0,  0,  0],\n",
       "[-2, 0, 0, -1,  1]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2 & 0 & 0 & 1 & -1\\\\-2 & 0 & 0 & -1 & 1\\\\4 & 0 & 0 & 2 & -2\\\\0 & 0 & 0 & 0 & 0\\\\2 & 0 & 0 & 1 & -1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 2, 0, 0,  1, -1],\n",
       "[-2, 0, 0, -1,  1],\n",
       "[ 4, 0, 0,  2, -2],\n",
       "[ 0, 0, 0,  0,  0],\n",
       "[ 2, 0, 0,  1, -1]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A**4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
