Created
November 16, 2019 17:01
-
-
Save ntuaha/d7272baf534379be9d9f48333758ace9 to your computer and use it in GitHub Desktop.
神奇計算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import scipy as sci" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"N = 3\n", | |
"A = np.random.random((N,N))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0.37075245, 0.6739229 , 0.20091591],\n", | |
" [0.39703536, 0.0495583 , 0.49734277],\n", | |
" [0.58168862, 0.67693551, 0.8502318 ]])" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"A" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"A1 = A+A.T" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0.7415049 , 1.07095825, 0.78260453],\n", | |
" [1.07095825, 0.0991166 , 1.17427829],\n", | |
" [0.78260453, 1.17427829, 1.70046361]])" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"A1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"w,v = np.linalg.eig(A1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([ 2.98360474, 0.38211522, -0.82463486])" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"w" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[-0.48415423, -0.74705032, 0.4555332 ],\n", | |
" [-0.47808952, -0.21016968, -0.85279489],\n", | |
" [-0.73281996, 0.6306699 , 0.25540239]])" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"v" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"U = np.zeros((N,N))\n", | |
"for i in range(N):\n", | |
" lambda_D = w.copy()\n", | |
" lambda_D[i] = np.nan\n", | |
" lambda_D = lambda_D[~np.isnan(lambda_D)]\n", | |
" denominator = np.prod(w[i]-lambda_D)\n", | |
" for j in range(N):\n", | |
" M = A1.copy()\n", | |
" M[:,j] = np.nan\n", | |
" M[j,:] = np.nan\n", | |
" M = M[~np.isnan(M)]\n", | |
" M = M.reshape(N-1,N-1)\n", | |
" w2,v2 = np.linalg.eig(M)\n", | |
" fraction = np.prod(w[i]-w2)\n", | |
" U[j,i] = fraction/denominator" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1.63404690107061e-15" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.linalg.norm(U-v*v)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0.23440532, 0.55808418, 0.2075105 ],\n", | |
" [0.22856958, 0.04417129, 0.72725912],\n", | |
" [0.53702509, 0.39774453, 0.06523038]])" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"U" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0.23440532, 0.55808418, 0.2075105 ],\n", | |
" [0.22856958, 0.04417129, 0.72725912],\n", | |
" [0.53702509, 0.39774453, 0.06523038]])" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"v*v" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
要移除指定的 index
可以使用 np.deleteU = np.zeros((N,N)) for i in range(N): lambda_D = np.delete(w, i) denominator = np.prod(w[i]-lambda_D) for j in range(N): M = np.delete(A1, j, 0) M = np.delete(M, j, 1) w2, v2 = np.linalg.eig(M) fraction = np.prod(w[i]-w2) U[j,i] = fraction/denominator
感謝!這個更加簡潔好讀
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
要移除指定的 index
可以使用 np.delete