Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## The SVD decomposition of a symmetric matrix ##"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let S be a symmetric matrix:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "import numpy as np"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [],
- "source": [
- "S=np.array([[2, -1, 3], [-1, 2, -1], [3, -1, 2]])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "D, P=np.linalg.eigh(S)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([-1. , 1.43844719, 5.56155281])"
- ]
- },
- "execution_count": 4,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "D"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[ 7.07106781e-01, 2.60956474e-01, -6.57192300e-01],\n",
- " [ 2.08159786e-16, 9.29410263e-01, 3.69048184e-01],\n",
- " [-7.07106781e-01, 2.60956474e-01, -6.57192300e-01]])"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "P"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "$PDP^T$:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[ 2., -1., 3.],\n",
- " [-1., 2., -1.],\n",
- " [ 3., -1., 2.]])"
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.dot(P, np.dot(np.diag(D), P.T))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Singular decomposition of the same matrix:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [],
- "source": [
- "U, Sigma, Vt =np.linalg.svd(S)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([5.56155281, 1.43844719, 1. ])"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "Sigma"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Comparing $\\Sigma$ to D we notice that $\\Sigma=\\pi (|D|)$, where $\\pi$ is the permutation $\\pi(0)=2, \\pi(1)=1, \\pi(2)=0$,\n",
- "i.e. $\\pi(|\\lambda_0|)=\\sigma_2$, etc"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[-6.57192300e-01, 2.60956474e-01, 7.07106781e-01],\n",
- " [ 3.69048184e-01, 9.29410263e-01, -1.59154867e-16],\n",
- " [-6.57192300e-01, 2.60956474e-01, -7.07106781e-01]])"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "U"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The columns in U are the column in P permuted by $\\pi$."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[-6.57192300e-01, 3.69048184e-01, -6.57192300e-01],\n",
- " [ 2.60956474e-01, 9.29410263e-01, 2.60956474e-01],\n",
- " [-7.07106781e-01, -3.96348516e-16, 7.07106781e-01]])"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "Vt"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "`Vt=U^T`"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[-6.57192300e-01, 3.69048184e-01, -6.57192300e-01],\n",
- " [ 2.60956474e-01, 9.29410263e-01, 2.60956474e-01],\n",
- " [ 7.07106781e-01, -1.59154867e-16, -7.07106781e-01]])"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "U.T"
- ]
- },
- {
- "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.6.4"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment