diff --git a/Defective_matrix_eigen_vectors.ipynb b/Defective_matrix_eigen_vectors.ipynb new file mode 100644 index 0000000..c811904 --- /dev/null +++ b/Defective_matrix_eigen_vectors.ipynb @@ -0,0 +1,166 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "6a2895e6", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np \n", + "import matplotlib.pylab as plt\n", + "from tqdm.auto import tqdm\n", + "from pint import UnitRegistry\n", + "%config InlineBackend.figure_format = 'retina'" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "72978ada", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def shrinkList(lst):\n", + " tup_list = []\n", + " i, index = 0, 0\n", + " while(index < len(lst)):\n", + " element_count = 0\n", + " while(i < len(lst) and lst[i] == lst[index]):\n", + " element_count += 1\n", + " i += 1\n", + " tup_list.append((lst[index], element_count))\n", + " index += element_count\n", + " \n", + " return tup_list\n", + "def generalized_eig(A,eigen_val,multi):\n", + " from scipy.linalg import null_space\n", + " coef=A-eigen_val*np.eye(np.shape(A)[0])\n", + " ns=null_space(np.linalg.matrix_power(coef,multi))\n", + " M=np.zeros((np.shape(A)[0],multi))\n", + " M[:,0]=ns[:,0].T\n", + " for i in range(1,multi):\n", + " eig=M[:,i-1]\n", + " eig2=np.matmul(coef,eig)\n", + " M[:,i]=eig2\n", + " return M\n", + "def defective_eigenvec(A):\n", + " omega, M = np.linalg.eig(A)\n", + " shrink_eig=shrinkList(omega)\n", + " eig_vec=np.zeros((np.shape(A)[0],1))\n", + " for i in range(1,np.shape(shrink_eig)[0]+1):\n", + " temp=generalized_eig(A,shrink_eig[i-1][0],shrink_eig[i-1][1])\n", + " eig_vec=np.concatenate((eig_vec,temp),axis=1)\n", + " eig_vec_trim=eig_vec[:,1:]\n", + " return eig_vec_trim\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "c943cab4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1.00000000e+00 -1.00000000e+00 1.00000000e+00 -9.39618477e-01]\n", + " [ 0.00000000e+00 1.11022302e-15 -1.11022302e-15 2.68462422e-01]\n", + " [ 0.00000000e+00 0.00000000e+00 6.16297582e-31 -2.01346817e-01]\n", + " [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 6.71156055e-02]]\n" + ] + }, + { + "data": { + "text/plain": [ + "array([[ 1.00000000e+00, 9.00719925e+14, -1.97032484e+15,\n", + " -9.00719925e+15],\n", + " [ 0.00000000e+00, 9.00719925e+14, 1.62259277e+30,\n", + " 4.86777830e+30],\n", + " [ 0.00000000e+00, 0.00000000e+00, 1.62259277e+30,\n", + " 4.86777830e+30],\n", + " [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", + " 1.48996644e+01]])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "AA=[[5,1,-2,4],[0,5,2,2],[0,0,5,3],[0,0,0,4]]\n", + "omega, M = np.linalg.eig(AA)\n", + "print(M)\n", + "np.linalg.inv(M)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "7aa72b9e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 0. -2. 2. 0.93961848]\n", + " [ 0. 2. 0. -0.26846242]\n", + " [ 1. 0. 0. 0.20134682]\n", + " [ 0. 0. 0. -0.06711561]]\n" + ] + }, + { + "data": { + "text/plain": [ + "array([[ 0. , 0. , 1. , 3. ],\n", + " [ 0. , 0.5 , 0. , -2. ],\n", + " [ 0.5 , 0.5 , 0. , 5. ],\n", + " [ -0. , -0. , -0. , -14.89966443]])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "test=defective_eigenvec(AA)\n", + "print(test)\n", + "np.linalg.inv(test)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18e0ca79", + "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.8.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}