-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: Add spectral decomposition demo and documentation
- Loading branch information
Showing
3 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 35, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"from numpy import linalg as LA" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 36, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[[1 0]\n", | ||
" [0 1]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"A = np.array([[1, 0], [0, 1]])\n", | ||
"print(A)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 37, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"eigenvalues\n", | ||
" [1. 1.]\n", | ||
"eigenvectors\n", | ||
" [[1. 0.]\n", | ||
" [0. 1.]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"eigenvalues, eigenvectors = LA.eig(A)\n", | ||
"print(\"eigenvalues\\n\", eigenvalues)\n", | ||
"print(\"eigenvectors\\n\", eigenvectors)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 38, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"P\n", | ||
" [[1. 0.]\n", | ||
" [0. 1.]]\n", | ||
"D\n", | ||
" [[1. 0.]\n", | ||
" [0. 1.]]\n", | ||
"P_inv\n", | ||
" [[1. 0.]\n", | ||
" [0. 1.]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# P -> composed of eigenvectors\n", | ||
"# D -> composed of eigenvalues\n", | ||
"P = eigenvectors\n", | ||
"D = np.diag(eigenvalues)\n", | ||
"P_inv = np.linalg.inv(P)\n", | ||
"print(\"P\\n\", P)\n", | ||
"print(\"D\\n\", D)\n", | ||
"print(\"P_inv\\n\", P_inv)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 39, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([[1., 0.],\n", | ||
" [0., 1.]])" | ||
] | ||
}, | ||
"execution_count": 39, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"P @ D @ P_inv" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## From chapter 25" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 40, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[[-3 5]\n", | ||
" [ 4 -2]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"A = np.array([[-3, 5], [4, -2]])\n", | ||
"print(A)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 41, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"eigenvalues\n", | ||
" [-7. 2.]\n", | ||
"eigenvectors\n", | ||
" [[-0.78086881 -0.70710678]\n", | ||
" [ 0.62469505 -0.70710678]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"eigenvalues, eigenvectors = LA.eig(A)\n", | ||
"print(\"eigenvalues\\n\", eigenvalues)\n", | ||
"print(\"eigenvectors\\n\", eigenvectors)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 42, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"P\n", | ||
" [[-0.78086881 -0.70710678]\n", | ||
" [ 0.62469505 -0.70710678]]\n", | ||
"D\n", | ||
" [[-7. 0.]\n", | ||
" [ 0. 2.]]\n", | ||
"P_inv\n", | ||
" [[-0.71145825 0.71145825]\n", | ||
" [-0.62853936 -0.7856742 ]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# P -> composed of eigenvectors\n", | ||
"# D -> composed of eigenvalues\n", | ||
"P = eigenvectors\n", | ||
"D = np.diag(eigenvalues)\n", | ||
"P_inv = np.linalg.inv(P)\n", | ||
"print(\"P\\n\", P)\n", | ||
"print(\"D\\n\", D)\n", | ||
"print(\"P_inv\\n\", P_inv)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 43, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([[-3., 5.],\n", | ||
" [ 4., -2.]])" | ||
] | ||
}, | ||
"execution_count": 43, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"P @ D @ P_inv" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "venv", | ||
"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.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Spectral Decomposition | ||
|
||
## General | ||
* given matrix A find matrix P and D such that `A = PDP^-1` | ||
* many use-cases, like PCA, signal processing, etc. | ||
|
||
## References | ||
* [some write up](https://zief0002.github.io/matrix-algebra/spectral-decompostion.html) | ||
* [stackexchange](https://math.stackexchange.com/questions/2568305/find-the-spectral-decomposition-of-a) |