📄 matrix.ipynb
/home/palash/git/misc/maths/matrix.ipynb
Language: ipynb • Lines: 277
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b1335fc0-8ea5-4686-ac3b-9d0e79a8dac4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import math\n",
    "from numpy import linalg as la\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2242fbb7-0167-4442-8306-d75c5435f70c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 2],\n",
       "       [6, 4]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2], [3,4]])\n",
    "b = np.array([2,1])\n",
    "\n",
    "a * b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c38b710c-e065-477e-b82b-bc4431fa7186",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4, 10])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6dc9b454-4b59-4178-b877-93e58d4dec8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.arange(9).reshape((3,3))\n",
    "va, ve = la.eig(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e33d691b-3a22-4f00-81a8-67696c9f74d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "diag = np.diag(va)\n",
    "inv = la.inv(ve)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9a970377-f1b0-4dcf-bb7f-4c835018e33c",
   "metadata": {},
   "outputs": [],
   "source": [
    "c=np.matmul(diag, inv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "be910829-995f-46ff-a1bb-b65e941402c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "c=np.matmul(ve, c).astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "68e08103-e769-4e54-9e05-380f616439b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True],\n",
       "       [ True,  True,  True],\n",
       "       [ True,  True,  True]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b ==c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d73c6258-f240-4b59-acf9-4201605c574b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True],\n",
       "       [ True,  True,  True],\n",
       "       [ True,  True,  True]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = np.matmul(ve, diag)\n",
    "d = np.matmul(d, inv).astype(int)\n",
    "d==b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "af25c833-9dc1-4b73-ac04-fd3958e7d469",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True],\n",
       "       [ True,  True,  True],\n",
       "       [ True,  True,  True]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d == c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7271d9dd-6c9e-418e-ab59-00367a640734",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0],\n",
       "       [0, 1, 0],\n",
       "       [0, 0, 1]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(ve, inv).astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4a9524a3-8cbb-4d67-af38-bc41b96429ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[13,  0,  0],\n",
       "       [ 0, -1,  0],\n",
       "       [ 0,  0,  0]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diag.astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b69465ea-c038-4513-873f-818e02cc4673",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[13,  0,  0],\n",
       "       [ 0, -1,  0],\n",
       "       [ 0,  0,  0]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "e = np.matmul(ve, inv)\n",
    "e = np.matmul(e, diag).astype(int)\n",
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc078bb4-582c-41e7-aedf-49bd643b086f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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": 5
}