{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4ab1086b",
   "metadata": {},
   "source": [
    "# Eksempler på matrixalgebra i SymPy\n",
    "\n",
    "Denne notebook viser klassiske matrixoperationer med **SymPy** (symbolsk matematik)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a31f251",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "sp.init_printing()\n",
    "\n",
    "# Definér to 2x2-matricer\n",
    "A = sp.Matrix([[1, 2],\n",
    "               [3, 4]])\n",
    "\n",
    "B = sp.Matrix([[5, 6],\n",
    "               [7, 8]])\n",
    "\n",
    "A, B"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b84a8e5",
   "metadata": {},
   "source": [
    "## 1) Matrixaddition og -subtraktion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4677862e",
   "metadata": {},
   "outputs": [],
   "source": [
    "C = A + B\n",
    "D = A - B\n",
    "C, D"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0253def6",
   "metadata": {},
   "source": [
    "## 2) Skalarmultiplikation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7852a17b",
   "metadata": {},
   "outputs": [],
   "source": [
    "E = 3 * A\n",
    "E"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f9265a2",
   "metadata": {},
   "source": [
    "## 3) Matrixmultiplikation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "730ea51c",
   "metadata": {},
   "outputs": [],
   "source": [
    "F = A * B          # eller A @ B i nyere Python, men SymPy bruger '*'\n",
    "F"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81b78fe6",
   "metadata": {},
   "source": [
    "## 4) Transponering"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4db386f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "A_T = A.T\n",
    "A_T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92f8d6f9",
   "metadata": {},
   "source": [
    "## 5) Invers matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "867ddc1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "A_inv = A.inv()\n",
    "A_inv"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76108938",
   "metadata": {},
   "source": [
    "## 6) Determinant"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da164bf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "det_A = A.det()\n",
    "det_A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6db79dd",
   "metadata": {},
   "source": [
    "## 7) Rang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27b3e5ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "rank_A = A.rank()\n",
    "rank_A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76c09ec6",
   "metadata": {},
   "source": [
    "## 8) Egenværdier og egenvektorer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd25014d",
   "metadata": {},
   "outputs": [],
   "source": [
    "eigenvals = A.eigenvals()     # dict: {egenværdi: algebraisk multiplicitet}\n",
    "eigenvects = A.eigenvects()   # liste af (egenværdi, geom. multiplicitet, [egenvectors])\n",
    "eigenvals, eigenvects"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb58e66f",
   "metadata": {},
   "source": [
    "## 9) Løsning af lineære ligningssystemer $Ax=b$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3aeb8e0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = sp.Matrix([1, 2])\n",
    "x = sp.linsolve((A,b))   # numerisk stabil i SymPy; finder eksakt løsning som rationale tal\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1f7591c",
   "metadata": {},
   "source": [
    "## 10) Karakteristisk polynomium"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6752edc",
   "metadata": {},
   "outputs": [],
   "source": [
    "lambda_symbol = sp.symbols('λ')  # bare til visning\n",
    "charpoly = A.charpoly(lambda_symbol)\n",
    "charpoly.as_expr()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9418e502",
   "metadata": {},
   "source": [
    "## 11) Diagonalisering (hvis mulig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "795dd554",
   "metadata": {},
   "outputs": [],
   "source": [
    "P, D = A.diagonalize()  # giver fejl hvis den ikke er diagonaliserbar\n",
    "P, D"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "formats": "ipynb,md:myst",
   "text_representation": {
    "extension": ".md",
    "format_name": "myst",
    "format_version": 0.13,
    "jupytext_version": "1.16.0"
   }
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "source_map": [
   13,
   19,
   31,
   35,
   39,
   43,
   46,
   50,
   53,
   57,
   60,
   64,
   67,
   71,
   74,
   78,
   81,
   85,
   89,
   93,
   97,
   101,
   105,
   109
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 5
}