{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "3cb66c1b-bfe9-4660-a69d-858c19afb39f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e03d62e5-d90a-47e6-b2da-74c88f30bf30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.5,  0. ,  0. ],\n",
       "       [ 0. ,  0.5, -0.5],\n",
       "       [ 0. , -0.5,  0.5]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = 0.5*np.array([[1,0,0],[0,1,-1],[0,-1,1]])\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "65dfea39-e9c1-4557-9be2-a3407086b17a",
   "metadata": {},
   "outputs": [],
   "source": [
    "eigenvalues, eigenvectors = np.linalg.eig(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3796c3df-99d9-41cc-a14e-9dd71fea787b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.00000000e+00, 1.11022302e-16, 5.00000000e-01])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigenvalues"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f54e0593-72ff-4cde-b1de-32f4d24de4a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        ,  0.        ,  1.        ],\n",
       "       [-0.70710678,  0.70710678,  0.        ],\n",
       "       [ 0.70710678,  0.70710678,  0.        ]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigenvectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "437b9ebb-2cf2-4a9f-8a64-8783334e7fe1",
   "metadata": {},
   "outputs": [],
   "source": [
    "D = np.diag(eigenvalues)\n",
    "T = eigenvectors\n",
    "T_inv = np.linalg.inv(T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7690cc82-1733-4e4d-a360-8c7affa88565",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
       "       [0.00000000e+00, 1.11022302e-16, 0.00000000e+00],\n",
       "       [0.00000000e+00, 0.00000000e+00, 5.00000000e-01]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1e06aafa-1d56-4718-ad55-91999a98bad2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        ,  0.        ,  1.        ],\n",
       "       [-0.70710678,  0.70710678,  0.        ],\n",
       "       [ 0.70710678,  0.70710678,  0.        ]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9d6fd391-ae28-40ab-a116-43ff04c8a4cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        , -0.70710678,  0.70710678],\n",
       "       [ 0.        ,  0.70710678,  0.70710678],\n",
       "       [ 1.        ,  0.        ,  0.        ]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T_inv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f855da4d-6185-40d6-8e7f-e9ed6a0a6a9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.5,  0. ,  0. ],\n",
       "       [ 0. ,  0.5, -0.5],\n",
       "       [ 0. , -0.5,  0.5]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(T, np.matmul(D, T_inv))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d0b0d2a-813a-44af-bc03-43ef8566bd1d",
   "metadata": {},
   "source": [
    "**Please do not use element wise multiplication T\\*D\\*T_inv**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7c0d5925-7aec-4631-bde7-901e44ca6a5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.00000000e+00, -0.00000000e+00,  0.00000000e+00],\n",
       "       [-0.00000000e+00,  5.55111512e-17,  0.00000000e+00],\n",
       "       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T*D*T_inv"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "529e2c1d-157f-44b2-b6cc-e292fcd78abb",
   "metadata": {},
   "source": [
    "**We see that the columns in T are already set to length one**  \n",
    "**Start with index 0**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "be682781-4811-4748-8aa1-445ce12d603c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.9999999999999999)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.norm(T[:,0])\n",
    "# np.linalg.norm(T[:,1])\n",
    "# np.linalg.norm(T[:,2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b250e9f8-0953-45d2-8b9c-21be0e486255",
   "metadata": {},
   "source": [
    "**We see that T_inv is the same as transposed of T**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c82027a9-d0e3-4711-8da5-167bfd4920b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        , -0.70710678,  0.70710678],\n",
       "       [ 0.        ,  0.70710678,  0.70710678],\n",
       "       [ 1.        ,  0.        ,  0.        ]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c8457d4f-2501-45e4-bdd0-b13d7ea5fb63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.00097656,  0.        ,  0.        ],\n",
       "       [ 0.        ,  0.5       , -0.5       ],\n",
       "       [ 0.        , -0.5       ,  0.5       ]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.matrix_power(A,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "35d44971-986b-4d1d-8e94-83a52303c44a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.0000000e+000, 0.0000000e+000, 0.0000000e+000],\n",
       "       [0.0000000e+000, 2.8451312e-160, 0.0000000e+000],\n",
       "       [0.0000000e+000, 0.0000000e+000, 9.7656250e-004]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D_new = np.diag(eigenvalues**10)\n",
    "D_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "82311a63-7236-4805-baa5-66f15dbfc445",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.00097656,  0.        ,  0.        ],\n",
       "       [ 0.        ,  0.5       , -0.5       ],\n",
       "       [ 0.        , -0.5       ,  0.5       ]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(np.matmul(T,D_new), np.transpose(T))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "469c8cc9-adca-448e-852b-17bf96711dcb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0. ,  0. ,  0. ],\n",
       "       [ 0. ,  0.5, -0.5],\n",
       "       [ 0. , -0.5,  0.5]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# eigenvalues**infty\n",
    "D_limit = np.diag(np.array([1,0,0]))\n",
    "np.matmul(np.matmul(T,D_limit), np.transpose(T))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my_python",
   "language": "python",
   "name": "my_python"
  },
  "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
