{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "id": "eXcJc7ELc2Bq" }, "outputs": [], "source": [ "import numpy as np\n", "from numpy import linalg as la" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "COF5AdaIsIvU" }, "outputs": [], "source": [ "# Let's start with a symmetric matrix.\n", "S = [[1, 2], [2, 1]]\n", "\n", "# Let's use NumPy to do the eigenvalue decomposition of S.\n", "l, U = la.eig(S)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Ryi1YnVvsVTz", "outputId": "60fc0128-d890-48a6-ac7f-301fd7bb64f5" }, "outputs": [ { "data": { "text/plain": [ "(array([ 3., -1.]),\n", " array([[ 0.70710678, -0.70710678],\n", " [ 0.70710678, 0.70710678]]))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l, U" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([-1., 3.]),\n", " array([[-0.70710678, 0.70710678],\n", " [ 0.70710678, 0.70710678]]))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# For Hermitian (symetric) matrices, Numpy has a more efficient version called 'eigh'\n", "# which also returns the eigenvalues in sorted order.\n", "l, U = la.eigh(S)\n", "l, U" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DsFFSR7OsWoL", "outputId": "6293dee7-c3a1-4b70-bee9-f5c5b4ebd2c6" }, "outputs": [ { "data": { "text/plain": [ "array([ 3., -1.])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show the vector of eigenvalues. We can see that S is not positive semi-definite, as it has a negative eigenvalue.\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "J8IcZ9zUsXjr", "outputId": "8a961922-667f-4014-bd4a-9c9b79bb6351" }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.70710678, -0.70710678],\n", " [ 0.70710678, 0.70710678]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show the matrix of eigenvectors, one per column.\n", "U" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aV0_ZyWwsYWr", "outputId": "0c761880-fae4-429e-cfb2-96e66f2315e3" }, "outputs": [ { "data": { "text/plain": [ "array([2.12132034, 2.12132034])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Compute S x u1, in order to show that S x u1 = lambda1 x u1.\n", "S @ U[:,0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jnBquBh8smEq", "outputId": "af665c68-5622-42a6-a734-68b8f2db1f81" }, "outputs": [ { "data": { "text/plain": [ "array([2.12132034, 2.12132034])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Compute lambda1 x u1, it should be equal to S x u1 above.\n", "l[0] * U[:,0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "knZ-8I8ksqg6", "outputId": "d7807066-720c-4242-e69d-5147db21874b" }, "outputs": [ { "data": { "text/plain": [ "0.9999999999999999" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show that the norm of u1 is equal to 1.\n", "U[:,0].T @ U[:,0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3o3LjwBIs0qC", "outputId": "0191eec7-c3d0-4baf-ae12-539b12d4f2de" }, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show that u1 is orthogonal to u2.\n", "U[:,0].T @ U[:,1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3QAd_5tds4TC", "outputId": "f2610bef-e463-4c02-97ee-0ec3f5733352" }, "outputs": [ { "data": { "text/plain": [ "array([[ 2.12132034, 0.70710678],\n", " [ 2.12132034, -0.70710678]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Compute S x U, to check that S x U = Lambda x U.\n", "S @ U" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Oe8SwZSvtbjB", "outputId": "479e9c16-bf74-4f18-9484-a38b9b68aaf0" }, "outputs": [ { "data": { "text/plain": [ "array([[ 2.12132034, 0.70710678],\n", " [ 2.12132034, -0.70710678]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Compute U x Lambda, it should be equal with S x U above.\n", "U @ np.diag(l)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "z182bIPYtgDB", "outputId": "9d10b3d2-3fb5-4ec4-851b-002e7e556e8e" }, "outputs": [ { "data": { "text/plain": [ "array([[ 3., 0.],\n", " [ 0., -1.]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create the Lambda matrix containing the eigenvalues on the diagonal.\n", "np.diag(l)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cGuGMCLYtnPJ", "outputId": "1da6b48c-c2c1-480a-85ca-cf24c33aadfe" }, "outputs": [ { "data": { "text/plain": [ "array([[1., 0.],\n", " [0., 1.]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This shows that the eigenvectors are orthonormal.\n", "U.T @ U" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "h8S3PsJvtwFQ" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "provenance": [] }, "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.12" } }, "nbformat": 4, "nbformat_minor": 1 }