Merge remote-tracking branch 'origin/koen' into main

Choose Koen's version he handed in.
This commit is contained in:
2022-03-08 16:51:05 +01:00
3 changed files with 15264 additions and 55 deletions

View File

@ -39,7 +39,7 @@
"cell_type": "raw", "cell_type": "raw",
"metadata": {}, "metadata": {},
"source": [ "source": [
"team_members = \"Koen Vendrig, Kees van Kempen\"" "team_members = \"Kees van Kempen, Koen Vendrig\""
] ]
}, },
{ {
@ -66,7 +66,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"metadata": { "metadata": {
"deletable": false, "deletable": false,
"nbgrader": { "nbgrader": {
@ -84,7 +84,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"import numpy as np\n", "import numpy as np\n",
"import numpy.linalg as linalg" "import math as m"
] ]
}, },
{ {
@ -137,7 +137,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": null,
"metadata": { "metadata": {
"deletable": false, "deletable": false,
"nbgrader": { "nbgrader": {
@ -180,8 +180,8 @@
" assert len(A.shape) == 2 and A.shape[0] == A.shape[1]\n", " assert len(A.shape) == 2 and A.shape[0] == A.shape[1]\n",
" \n", " \n",
" # Setup some initial values.\n", " # Setup some initial values.\n",
" #B = linalg.inv(A - sigma*np.ones(n))\n", " #B = np.linalg.inv(A - sigma*np.ones(n))\n",
" B = linalg.inv(A - sigma*np.eye(n))\n", " B = np.linalg.inv(A - sigma*np.eye(n))\n",
" #B = 1/(A - sigma*np.ones(n))\n", " #B = 1/(A - sigma*np.ones(n))\n",
" #B = 1/(A - sigma*np.eye(n))\n", " #B = 1/(A - sigma*np.eye(n))\n",
" b = np.ones(n)\n", " b = np.ones(n)\n",
@ -193,7 +193,7 @@
" k += 1\n", " k += 1\n",
" \n", " \n",
" b = B @ b\n", " b = B @ b\n",
" b /= np.sqrt(b @ b) # although b = linalg.norm(b) could be used\n", " b /= np.sqrt(b @ b) # although b = np.linalg.norm(b) could be used\n",
" e = np.sqrt(np.sum( (np.abs(b_prev) - np.abs(b))**2) )\n", " e = np.sqrt(np.sum( (np.abs(b_prev) - np.abs(b))**2) )\n",
" \n", " \n",
" return b, k" " return b, k"
@ -201,7 +201,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": null,
"metadata": { "metadata": {
"deletable": false, "deletable": false,
"nbgrader": { "nbgrader": {
@ -216,30 +216,7 @@
"task": false "task": false
} }
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"For sigma = 0.03\n",
"b = [ 0.45440139 -0.76618454 0.45440139]\n",
"lam = [0.62771919 0.62771831 0.62771919]\n",
"\n",
"\n",
"For sigma = 6.0\n",
"b = [0.54177432 0.64262054 0.54177432]\n",
"lam = [6.37228128 6.37228139 6.37228128]\n",
"\n",
"\n",
"For sigma = 1.99999989999999\n",
"b = [-7.07106781e-01 -4.21799612e-14 7.07106781e-01]\n",
"lam = [ 2. -0. 2.]\n",
"\n",
"\n",
"[6.37228132 2. 0.62771868]\n"
]
}
],
"source": [ "source": [
"# Use this cell for your own testing ...\n", "# Use this cell for your own testing ...\n",
"\n", "\n",
@ -257,7 +234,7 @@
" print()\n", " print()\n",
" print()\n", " print()\n",
"\n", "\n",
"print(linalg.eig(A)[0])" "print(np.linalg.eig(A)[0])"
] ]
}, },
{ {
@ -281,8 +258,6 @@
"source": [ "source": [
"def test_inversePower():\n", "def test_inversePower():\n",
" A = np.array([[3, 2, 1], [2, 3, 2], [1, 2, 3]])\n", " A = np.array([[3, 2, 1], [2, 3, 2], [1, 2, 3]])\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()\n",
" \n", " \n",
"test_inversePower()" "test_inversePower()"
] ]
@ -348,8 +323,26 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def tridiagonalize(A):\n", "def tridiagonalize(A):\n",
" # YOUR CODE HERE\n", " \"\"\"\n",
" raise NotImplementedError()" " Tridiagonalizes a given matrix following the Householder method.\n",
" Args:\n",
" A: NxN matrix\n",
" Returns:\n",
" The matrix A, but now tridiagonalized\n",
" \"\"\"\n",
" assert A.shape[0] == A.shape[1] and len(A.shape) == 2\n",
" n = len(A)\n",
" for k in range(n-1):\n",
" q = m.sqrt(np.sum(A[k+1:n,k]**2))\n",
" alpha = -1*np.sign(A[k+1,k])*q\n",
" r = m.sqrt((alpha**2 - A[k+1,k]*alpha)/2)\n",
" v = np.zeros(n)\n",
" v[k+1] = (A[k+1,k]-alpha)/(2*r)\n",
" print(np.shape(v),np.shape(A),np.shape(r),np.shape(A[k+2:n]/(2*r)))\n",
" v[k+2:n] = A[k+2:n,k]/(2*r)\n",
" P = np.identity(n)-(2*np.outer(v,v))\n",
" A = np.dot(np.dot(P,A),P)\n",
" return A"
] ]
}, },
{ {
@ -371,10 +364,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# Apply your routine here ...\n", "B = np.array([[3,2,1],[2,3,2],[1,2,3]])\n",
"\n", "\n",
"# YOUR CODE HERE\n", "tridiagonalize(B)"
"raise NotImplementedError()"
] ]
}, },
{ {
@ -431,8 +423,22 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def QREig(T, eps):\n", "def QREig(T, eps):\n",
" # YOUR CODE HERE\n", " \"\"\"\n",
" raise NotImplementedError()" " Follows the method of the QR decomposition based diagonalization routine for tridiagonalized matrices. The matrix T is\n",
" diagonalized, resulting in all diagonal elements being an eigenvalue.\n",
" Args:\n",
" T: a already tridiagonalized matrix\n",
" eps: the desired accuracy\n",
" Returns:\n",
" A one dimensional array with the eigenvalues of the matrix T\n",
" \"\"\"\n",
" e = eps + 1\n",
" while e > eps:\n",
" Q,R = np.linalg.qr(T)\n",
" T = np.matmul(R,Q)\n",
" e = np.sum(np.absolute(np.diag(T,k=1)))\n",
" #print(np.diag(T))\n",
" return np.diag(T)"
] ]
}, },
{ {
@ -454,10 +460,8 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# Use this cell for your own testing ...\n", "A_tridiag = tridiagonalize(A)\n",
"\n", "print(A_tridiag)"
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
] ]
}, },
{ {
@ -480,10 +484,14 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def test_QREig():\n", "def test_QREig():\n",
" # YOUR CODE HERE\n", " \"\"\"\n",
" raise NotImplementedError()\n", " Tests the QREig function for the matrix A_tridiag (defined in task 2) and eps = 0.001\n",
" \n", " \"\"\"\n",
"test_QREig()" " eps = 0.001\n",
" x = QREig(A_tridiag,eps)\n",
" print(x)\n",
"\n",
"test_QREig()\n"
] ]
}, },
{ {
@ -529,8 +537,15 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# YOUR CODE HERE\n", "\"\"\"\n",
"raise NotImplementedError()" "Eigenvalues and -vectors calculated numerically for matrix A using functions from previous tasks.\n",
"\"\"\"\n",
"eps = 0.001\n",
"T = QREig(A_tridiag,eps)\n",
"for i in range(len(T)):\n",
" sigma = T[i] + np.random.normal(0,0.01,1)\n",
" x = inversePower(A_tridiag,sigma,eps)\n",
" print(x)"
] ]
}, },
{ {
@ -574,14 +589,41 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# YOUR CODE HERE\n", "\"\"\"\n",
"raise NotImplementedError()" "Completely 'random' and totally not self-made matrices C and D are being tridiagonalized and its eigenvalues and -vectors are\n",
"calculated.\n",
"\"\"\"\n",
"C = np.array([[5,4,3,2,1]\n",
" ,[3,4,5,1,2]\n",
" ,[5,1,4,2,3]\n",
" ,[3,2,1,5,1]\n",
" ,[5,4,3,2,1]])\n",
"#print(C)\n",
"C_tridiag = tridiagonalize(C)\n",
"T = QREig(C_tridiag,eps)\n",
"for i in range(len(T)):\n",
" sigma = T[i] + np.random.normal(0,0.01,1)\n",
" x = inversePower(C_tridiag,sigma,eps)\n",
" print(x)\n",
"\n",
"D = np.array([[6,4,3,2,1]\n",
" ,[3,3,5,1,2]\n",
" ,[5,1,4,2,2]\n",
" ,[3,1,1,5,6]\n",
" ,[1,4,7,2,3]])\n",
"#print(C)\n",
"C_tridiag = tridiagonalize(C)\n",
"T = QREig(C_tridiag,eps)\n",
"for i in range(len(T)):\n",
" sigma = T[i] + np.random.normal(0,0.01,1)\n",
" x = inversePower(C_tridiag,sigma,eps)\n",
" print(x)"
] ]
} }
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "Python 3 (ipykernel)",
"language": "python", "language": "python",
"name": "python3" "name": "python3"
}, },

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff