diff options
Diffstat (limited to 'Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER6.ipynb')
-rw-r--r-- | Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER6.ipynb | 373 |
1 files changed, 373 insertions, 0 deletions
diff --git a/Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER6.ipynb b/Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER6.ipynb new file mode 100644 index 00000000..bb0b912e --- /dev/null +++ b/Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER6.ipynb @@ -0,0 +1,373 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 6 Positive Definite Matrices" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:6.1.1 Pg:313" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "f(x,y)=x**2-10*x*y+y**2\n", + "f(1,1)= -8\n", + "The conditions a>0 and c>0 ensure that f(x,y) is positive on the x and y axes. But this function is negative on the line x=y,because b=-10 overwhelms a and c. \n" + ] + } + ], + "source": [ + "print 'f(x,y)=x**2-10*x*y+y**2'\n", + "a=1\n", + "c=1\n", + "def f(x,y):\n", + " ff=x**2-10*x*y+y**2\n", + " return ff\n", + "print 'f(1,1)=',f(1,1)\n", + "print 'The conditions a>0 and c>0 ensure that f(x,y) is positive on the x and y axes. But this function is negative on the line x=y,because b=-10 overwhelms a and c. '" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:6.1.3 Pg:315" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "f(x,y)=2*x**2+4*x*y+y**2\n", + "ac= 1\n", + "b**2= 4\n", + "Saddle point,as ac<b**2\n" + ] + } + ], + "source": [ + "print 'f(x,y)=2*x**2+4*x*y+y**2'\n", + "A=[[2, 2],[2, 1]]\n", + "a=1\n", + "c=1\n", + "b=2\n", + "print 'ac=',a*c\n", + "print 'b**2=',b**2\n", + "print 'Saddle point,as ac<b**2'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:6.1.4 Pg:315" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "f(x,y)=2*x**2+4*x*y+y**2\n", + "ac= 0\n", + "b**2= 1\n", + "Saddle point,as ac<b**2\n" + ] + } + ], + "source": [ + "print 'f(x,y)=2*x**2+4*x*y+y**2'\n", + "A=[[2 ,2],[2, 1]]\n", + "a=0\n", + "c=0\n", + "b=1\n", + "print 'ac=',a*c\n", + "print 'b**2=',b**2\n", + "print 'Saddle point,as ac<b**2'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:6.2.2 Pg:313" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "f(x,y)=x**2+4*x*y+y**2\n", + "f(0,0)= 0\n", + "Here 2b=4 it still does not ensure a minimum ,the sign of b is of no importance.Neither F nor f has a minimum at(0,0) because f(1,-1)=-1.\n" + ] + } + ], + "source": [ + "print 'f(x,y)=x**2+4*x*y+y**2'\n", + "a=1\n", + "c=1\n", + "def f(x,y):\n", + " ff=x**2+4*x*y+y**2\n", + " return ff\n", + "print 'f(0,0)=',f(0,0)\n", + "print 'Here 2b=4 it still does not ensure a minimum ,the sign of b is of no importance.Neither F nor f has a minimum at(0,0) because f(1,-1)=-1.'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:6.3.1 Pg:332" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A= [[-1]\n", + " [ 2]\n", + " [ 2]]\n", + "U= [[-0.33333333 0.66666667 0.66666667]\n", + " [ 0.66666667 0.66666667 -0.33333333]\n", + " [ 0.66666667 -0.33333333 0.66666667]]\n", + "diagnol= [ 3.]\n", + "V'= [[ 1.]]\n", + "A=U*diagnol*V'=\n", + "[[-1. 2. 2.]\n", + " [ 2. 2. -1.]\n", + " [ 2. -1. 2.]]\n" + ] + } + ], + "source": [ + "from scipy.linalg import svd\n", + "from numpy import array,transpose\n", + "A=transpose(array([[-1, 2, 2]]))\n", + "print 'A=',A\n", + "ans=svd(A)\n", + "U=ans[0]\n", + "diagnol=ans[1]\n", + "V=ans[2] \n", + "print 'U=',U\n", + "print 'diagnol=',diagnol\n", + "print \"V'=\",V\n", + "print \"A=U*diagnol*V'=\\n\",U*diagnol*transpose(V)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:6.3.2 Pg:332" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A=\n", + "[[-1 1 0]\n", + " [ 0 -1 1]]\n", + "U=\n", + "[[-0.70710678 0.70710678]\n", + " [ 0.70710678 0.70710678]]\n", + "Diagonal=\n", + "[[ 1.73205081 0. 0. ]\n", + " [ 0. 1. 0. ]]\n", + "V'= [[ 4.08248290e-01 -7.07106781e-01 5.77350269e-01]\n", + " [ -8.16496581e-01 -2.77555756e-16 5.77350269e-01]\n", + " [ 4.08248290e-01 7.07106781e-01 5.77350269e-01]]\n", + "[[-1.22474487 0.70710678 0. ]\n", + " [ 1.22474487 0.70710678 0. ]]\n", + "A=U*diagonal*V'=\n", + "[[-1.07735027 0.8660254 -0.29885849]\n", + " [-0.07735027 -0.8660254 1.11535507]]\n" + ] + } + ], + "source": [ + "from scipy.linalg import svd\n", + "from numpy import mat,array,transpose as tp, zeros\n", + "A=mat([[-1, 1, 0],[0, -1, 1]])\n", + "print 'A=\\n',A\n", + "U,diagnl1,V=svd(A)\n", + "diagnl=zeros([2,3])\n", + "diagnl[0,0]=diagnl1[0]\n", + "diagnl[1,1]=diagnl1[1]\n", + "U=mat(U)\n", + "diagnl=mat(diagnl)\n", + "V=mat(V)\n", + "print 'U=\\n',U\n", + "print 'Diagonal=\\n',diagnl\n", + "print \"V'=\",tp(V)\n", + "print (U*diagnl)\n", + "print \"A=U*diagonal*V'=\\n\",((U*diagnl)*tp(V))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:6.3.3 Pg:332" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A=\n", + "[[ 1 -2]\n", + " [ 3 -1]]\n", + "Q=\n", + "[[ 1.11022302e-16 -1.00000000e+00]\n", + " [ 1.00000000e+00 1.11022302e-16]]\n", + "S=\n", + "[[ 3. -1.]\n", + " [-1. 2.]]\n", + "A=SQ=\n", + "[[ 1. -2.]\n", + " [ 3. -1.]]\n" + ] + } + ], + "source": [ + "from numpy import mat, transpose as tp \n", + "from scipy.linalg import svd\n", + "A=mat([[1, -2],[3, -1]])\n", + "print 'A=\\n',A\n", + "U,S,V=svd(A)\n", + "Q=U*tp(mat(V))\n", + "S=V*S*tp(mat(V))\n", + "print 'Q=\\n',Q\n", + "print 'S=\\n',S\n", + "print 'A=SQ=\\n',(Q*S)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:6.3.4 Pg:332" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A=\n", + "[[ 1 -2]\n", + " [ 3 -1]]\n", + "Q=\n", + "[[ 1.11022302e-16 -1.00000000e+00]\n", + " [ 1.00000000e+00 1.11022302e-16]]\n", + "S=\n", + "[[2 1]\n", + " [1 3]]\n", + "A=S'Q=\n", + "[[-1. 2.]\n", + " [-3. 1.]]\n" + ] + } + ], + "source": [ + "from __future__ import division\n", + "from numpy import mat, transpose as tp \n", + "from scipy.linalg import svd\n", + "\n", + "A=mat([[1, -2],[3, -1]])\n", + "print 'A=\\n',A\n", + "U,diag1,V=svd(A)\n", + "Q=U*tp(mat(V))\n", + "S=mat([[2, 1],[1, 3]])\n", + "print 'Q=\\n',Q\n", + "print 'S=\\n',S\n", + "print \"A=S'Q=\\n\",(S*tp(mat(Q)))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.9" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} |