notes

uni notes
git clone git://popovic.xyz/notes.git
Log | Files | Refs

prb6_p.ipynb (4255B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "code",
      5    "execution_count": 1,
      6    "id": "9518bc9b",
      7    "metadata": {},
      8    "outputs": [],
      9    "source": [
     10     "import numpy as np\n",
     11     "from matplotlib import pyplot as plt"
     12    ]
     13   },
     14   {
     15    "cell_type": "code",
     16    "execution_count": 17,
     17    "id": "1f94b10c",
     18    "metadata": {},
     19    "outputs": [
     20     {
     21      "name": "stdout",
     22      "output_type": "stream",
     23      "text": [
     24       "[0.70678805 0.70742537]\t2.000899273263614\n"
     25      ]
     26     }
     27    ],
     28    "source": [
     29     "def power_iter(A: np.ndarray, x_0 :np.array, N :int=10):\n",
     30     "    x_k = x_0\n",
     31     "    for i in range(N):\n",
     32     "        x_k1 = A@x_k\n",
     33     "        r_k = (x_k1 @ x_k)/(x_k @ x_k)\n",
     34     "        x_k = x_k1\n",
     35     "    return x_k, r_k\n",
     36     "A = np.array([[1, 1], [2, 0]])\n",
     37     "x_0 = np.random.randint(11, size=len(A))\n",
     38     "x_k, r_k = power_iter(A, x_0)\n",
     39     "print(x_k/np.linalg.norm(x_k), r_k, sep='\\t')"
     40    ]
     41   },
     42   {
     43    "cell_type": "code",
     44    "execution_count": 22,
     45    "id": "c7f73c0c",
     46    "metadata": {},
     47    "outputs": [
     48     {
     49      "data": {
     50       "text/plain": [
     51        "array([10,  6])"
     52       ]
     53      },
     54      "execution_count": 22,
     55      "metadata": {},
     56      "output_type": "execute_result"
     57     }
     58    ],
     59    "source": [
     60     "A@x_0"
     61    ]
     62   },
     63   {
     64    "cell_type": "code",
     65    "execution_count": null,
     66    "id": "ff0193ca",
     67    "metadata": {},
     68    "outputs": [],
     69    "source": []
     70   },
     71   {
     72    "cell_type": "code",
     73    "execution_count": 20,
     74    "id": "742e8c5e",
     75    "metadata": {},
     76    "outputs": [
     77     {
     78      "name": "stdout",
     79      "output_type": "stream",
     80      "text": [
     81       "[1.8852459  0.26229508]\t[ 1.26229508 -0.8852459 ]\n"
     82      ]
     83     }
     84    ],
     85    "source": [
     86     "def QR_alg(A:np.ndarray, N:int=10):\n",
     87     "    A_k = A\n",
     88     "    for i in range(N):\n",
     89     "        Q, R = np.linalg.qr(A_k)\n",
     90     "        A_k = R@Q\n",
     91     "    return A_k, Q, R\n",
     92     "    \n",
     93     "A_k, Q, R = QR_alg(A, N=3)\n",
     94     "print(A_k[:,0], A_k[:,1], sep='\\t')"
     95    ]
     96   },
     97   {
     98    "cell_type": "code",
     99    "execution_count": 188,
    100    "id": "04812e53",
    101    "metadata": {},
    102    "outputs": [
    103     {
    104      "data": {
    105       "text/plain": [
    106        "array([[ 37375.,  25025.],\n",
    107        "       [  5200., -17550.]])"
    108       ]
    109      },
    110      "execution_count": 188,
    111      "metadata": {},
    112      "output_type": "execute_result"
    113     }
    114    ],
    115    "source": [
    116     "A_k*np.sqrt(793)*5*5*np.sqrt(793)"
    117    ]
    118   },
    119   {
    120    "cell_type": "code",
    121    "execution_count": 154,
    122    "id": "233539a6",
    123    "metadata": {},
    124    "outputs": [],
    125    "source": [
    126     "a1 = 1/5*np.array([7, 4])\n",
    127     "e1 = a1/np.linalg.norm(a1)\n",
    128     "a2 = 1/5 * np.array([9, -2])"
    129    ]
    130   },
    131   {
    132    "cell_type": "code",
    133    "execution_count": 177,
    134    "id": "3e998ed0",
    135    "metadata": {},
    136    "outputs": [
    137     {
    138      "data": {
    139       "text/plain": [
    140        "array([[135., -25.],\n",
    141        "       [ 40., -70.]])"
    142       ]
    143      },
    144      "execution_count": 177,
    145      "metadata": {},
    146      "output_type": "execute_result"
    147     }
    148    ],
    149    "source": [
    150     "A_k*np.sqrt(65)*np.sqrt(65)"
    151    ]
    152   },
    153   {
    154    "cell_type": "code",
    155    "execution_count": 179,
    156    "id": "26fc5985",
    157    "metadata": {},
    158    "outputs": [
    159     {
    160      "data": {
    161       "text/plain": [
    162        "140.80127840328723"
    163       ]
    164      },
    165      "execution_count": 179,
    166      "metadata": {},
    167      "output_type": "execute_result"
    168     }
    169    ],
    170    "source": [
    171     "135**2 + 40**2"
    172    ]
    173   },
    174   {
    175    "cell_type": "code",
    176    "execution_count": 169,
    177    "id": "4cd82a72",
    178    "metadata": {},
    179    "outputs": [
    180     {
    181      "data": {
    182       "text/plain": [
    183        "55"
    184       ]
    185      },
    186      "execution_count": 169,
    187      "metadata": {},
    188      "output_type": "execute_result"
    189     }
    190    ],
    191    "source": [
    192     "9*7-4*2"
    193    ]
    194   },
    195   {
    196    "cell_type": "code",
    197    "execution_count": null,
    198    "id": "4c09f8ff",
    199    "metadata": {},
    200    "outputs": [],
    201    "source": []
    202   }
    203  ],
    204  "metadata": {
    205   "kernelspec": {
    206    "display_name": "Python 3",
    207    "language": "python",
    208    "name": "python3"
    209   },
    210   "language_info": {
    211    "codemirror_mode": {
    212     "name": "ipython",
    213     "version": 3
    214    },
    215    "file_extension": ".py",
    216    "mimetype": "text/x-python",
    217    "name": "python",
    218    "nbconvert_exporter": "python",
    219    "pygments_lexer": "ipython3",
    220    "version": "3.10.8"
    221   }
    222  },
    223  "nbformat": 4,
    224  "nbformat_minor": 5
    225 }