{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 8 - Inner product spaces" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 271 Example 8.1" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n = 4\n", "a = [[ 1. 5. 4. 1.]]\n", "b = [[ 8. 6. 6. 8.]]\n", "Then, (a|b) = \n", "\n", "[[ 8. 40. 32. 8.]\n", " [ 6. 30. 24. 6.]\n", " [ 6. 30. 24. 6.]\n", " [ 8. 40. 32. 8.]]\n" ] } ], "source": [ "from numpy import array,random,transpose\n", "n=random.randint(2,9)\n", "a=random.rand(1,n)\n", "b=random.rand(1,n)\n", "for i in range(0,n):\n", " a[0,i]=round(a[0,i]*10)\n", " b[0,i]=round(b[0,i]*10)\n", "print 'n = ',n\n", "print 'a = ',a\n", "print 'b = ',b\n", "print 'Then, (a|b) = \\n\\n',a*transpose(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 271 Example 8.2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = [[ 9. 4.]]\n", "b = [[ 1. 9.]]\n", "Then, a|b = 68.0\n" ] } ], "source": [ "from numpy import array,random,transpose\n", "a=random.rand(1,2)\n", "b=random.rand(1,2)\n", "for i in range(0,2):\n", " a[0,i]=round(a[0,i]*10)\n", " b[0,i]=round(b[0,i]*10)\n", "print 'a = ',a\n", "print 'b = ',b\n", "x1 = a[0,0]#\n", "x2 = a[0,1]#\n", "y1 = b[0,0]#\n", "y2 = b[0,1]#\n", "t = x1*y1 - x2*y1 - x1*y2 + 4*x2*y2#\n", "print 'Then, a|b = ',t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 307 Example 8.28" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x1 and x2 are two real nos. i.e., x1**2 + x2**2 = 1\n", "x1 = 0.248003219206\n", "x2 = 0.968759208092\n", "B = \n", "[[ 0.24800322 0.96875921 0. ]\n", " [ 0. 1. 0. ]\n", " [ 0. 0. 1. ]]\n", "Applying Gram-Schmidt process to B:\n", "a1 = [ 0.24800322 0.96875921 0. ]\n", "a2 = [-0.2402554 0.0615056 0. ]\n", "a3 = [0 0 1]\n", "U = \n", "[[[ 0.24800322 0.96875921 0. ]]\n", "\n", " [[-0.96875921 0.24800322 0. ]]\n", "\n", " [[ 0. 0. 1. ]]]\n", "M = \n", "[[ 1. 0. 0. ]\n", " [-3.90623642 4.03220572 0. ]\n", " [ 0. 0. 1. ]]\n", "inverse(M) * U = [[[ 0.24800322 -0. -0. ]\n", " [ 0.2402554 0.2402554 0. ]\n", " [ 0. 0. 0. ]]\n", "\n", " [[-0.96875921 -0. -0. ]\n", " [-0.9384944 0.0615056 0. ]\n", " [-0. 0. 0. ]]\n", "\n", " [[ 0. -0. -0. ]\n", " [ 0. 0. 0. ]\n", " [ 0. 0. 1. ]]]\n", "So, B = inverse(M) * U\n" ] } ], "source": [ "from numpy import array,random,transpose,linalg,sqrt\n", "print 'x1 and x2 are two real nos. i.e., x1**2 + x2**2 = 1'\n", "x1 = random.rand()\n", "x2 = sqrt(1 - x1**2)\n", "print 'x1 = ',x1\n", "print 'x2 = ',x2\n", "B = array([[x1, x2, 0],[0, 1, 0],[0, 0, 1]])\n", "print 'B = \\n',B\n", "print 'Applying Gram-Schmidt process to B:'\n", "a1 = array([x1, x2, 0])\n", "a2 = array([0 ,1 ,0]) - x2 * array([x1 ,x2 ,0])\n", "a3 = array([0, 0, 1])\n", "print 'a1 = ',a1\n", "print 'a2 = ',a2\n", "print 'a3 = ',a3\n", "U = array([[a1],[a2/x1],[a3]])\n", "print 'U = \\n',U\n", "M = array([[1, 0, 0],[-x2/x1, 1/x1, 0],[0, 0, 1]])\n", "print 'M = \\n',M\n", "print 'inverse(M) * U = ',linalg.inv(M) * U\n", "print 'So, B = inverse(M) * U'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 278 Example 8.9" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(x,y) = [[ 7. 4.]]\n", "(-y,x) = [-4.0, 7.0]\n", "Inner product of these vectors is:\n", "(x,y)|(-y,x) = 0.0\n", "So, these are orthogonal.\n", "------------------------------------------\n", "If inner product is defined as:\n", "(x1,x2)|(y1,y2) = x1y1- x2y1 - x1y2 + 4x2y2\n", "Then, (x,y)|(-y,x) = -x*y+y**2-x**2+4*x*y = 0 if,\n", "y = 1/2(-3 + sqrt(13))*x\n", "or\n", "y = 1/2(-3 - sqrt(13))*x\n", "Hence,\n", "[[ 7. 4.]]\n", "is orthogonal to\n", "[-4.0, 7.0]\n" ] } ], "source": [ "from numpy import array,random,transpose,linalg,sqrt\n", "#a = round(rand(1,2) * 10)#\n", "a=random.rand(1,2)\n", "for j in [0,1]:\n", " a[0,j]=round(a[0,j]*10)\n", "\n", "x = a[0,0]\n", "y = a[0,1]\n", "b = [-y, x]#\n", "print '(x,y) = ',a\n", "print '(-y,x) = ',b\n", "print 'Inner product of these vectors is:'\n", "t = -x*y + y*x#\n", "print '(x,y)|(-y,x) = ',t\n", "\n", "print 'So, these are orthogonal.'\n", "print '------------------------------------------'\n", "print 'If inner product is defined as:'\n", "print '(x1,x2)|(y1,y2) = x1y1- x2y1 - x1y2 + 4x2y2'\n", "print 'Then, (x,y)|(-y,x) = -x*y+y**2-x**2+4*x*y = 0 if,'\n", "print 'y = 1/2(-3 + sqrt(13))*x'\n", "print 'or'\n", "print 'y = 1/2(-3 - sqrt(13))*x'\n", "print 'Hence,'\n", "if y == (1./2*(-3 + sqrt(13))*x) or (1./2*(-3 - sqrt(13))*x):\n", " print a\n", " print 'is orthogonal to'\n", " print b\n", "else:\n", " print a\n", " print 'is not orthogonal to'\n", " print b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 282 Example 8.12" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b1 = [3 0 4]\n", "b2 = [-1 0 7]\n", "b3 = [ 2 9 11]\n", "Applying the Gram-Schmidt process to b1,b2,b3:\n", "a1 = [3 0 4]\n", "a2 = [2 0 3]\n", "a3 = [2 9 4]\n", "{a1,a2,a3} are mutually orthogonal and hence forms orthogonal basis for R**3\n", "Any arbitrary vector {x1,x2,x3} in R**3 can be expressed as:\n", "y = {x1,x2,x3} = (3*x1 + 4*x3)/25*a1 + (-4*x1 + 3*x3)/25*a2 + x2/9*a3\n", "x1 = 1\n", "x2 = 2\n", "x3 = 3\n", "y = [0 0 0]\n", "i.e. y = [x1 x2 x3], according to above equation.\n", "Hence, we get the orthonormal basis as:\n", ", [ 0.6 0. 0.8]\n", ", [ 0.4 0. 0.6]\n", "[ 0.22222222 1. 0.44444444]\n" ] } ], "source": [ "from numpy import array,random,transpose,linalg,sqrt\n", "b1 = array([3, 0, 4])\n", "b2 = array([-1 ,0 ,7])\n", "b3 = array([2 ,9 ,11])\n", "print 'b1 = ',b1\n", "print 'b2 = ',b2\n", "print 'b3 = ',b3\n", "print 'Applying the Gram-Schmidt process to b1,b2,b3:'\n", "a1 = b1\n", "print 'a1 = ',a1\n", "a2 = b2-(transpose((b2*transpose(b1)))/25*b1)\n", "print 'a2 = ',a2\n", "a3 = b3-(transpose(b3*transpose(b1))/25*b1) - (transpose(b3*transpose(a2))/25*a2)\n", "print 'a3 = ',a3\n", "print '{a1,a2,a3} are mutually orthogonal and hence forms orthogonal basis for R**3'\n", "print 'Any arbitrary vector {x1,x2,x3} in R**3 can be expressed as:'\n", "print 'y = {x1,x2,x3} = (3*x1 + 4*x3)/25*a1 + (-4*x1 + 3*x3)/25*a2 + x2/9*a3'\n", "x1 = 1#\n", "x2 = 2#\n", "x3 = 3#\n", "y = (3*x1 + 4*x3)/25*a1 + (-4*x1 + 3*x3)/25*a2 + x2/9*a3#\n", "print 'x1 = ',x1\n", "print 'x2 = ',x2\n", "print 'x3 = ',x3\n", "print 'y = ',y\n", "print 'i.e. y = [x1 x2 x3], according to above equation.'\n", "print 'Hence, we get the orthonormal basis as:'\n", "\n", "print ',',1./5*a1\n", "print ',',1./5*a2\n", "print 1/9.*a3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 283 Example 8.13" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A = [[ 1.83351494 1.26265003]\n", " [ 0.46651205 0.76790774]]\n", "b1 = [ 1.83351494 1.26265003]\n", "b2 = [ 0.46651205 0.76790774]\n", "Applying the orthogonalization process to b1,b2:\n", "[1.8335149394280341, 1.2626500316837608] a1 = \n", "[] a2 = \n", "a2 is not equal to zero if and only if b1 and b2 are linearly independent.\n", "That is, if determinant of A is non-zero.\n" ] } ], "source": [ "from numpy import array,random,transpose,linalg,sqrt\n", "A = random.rand(2,2)\n", "A[0,:] = A[0,:] + 1# #so b1 is not equal to zero\n", "a = A[0,0]\n", "b = A[0,1]\n", "c = A[1,0]\n", "d = A[1,1]\n", "b1 = A[0,:]\n", "b2 = A[1,:]\n", "print 'A = ',A\n", "print 'b1 = ',b1\n", "print 'b2 = ',b2\n", "print 'Applying the orthogonalization process to b1,b2:'\n", "\n", "a1 = [a, b]\n", "a2 = (linalg.det(A)/(a**2 + b**2))*[-transpose(b), transpose(a)]\n", "print a1,'a1 = '\n", "print a2,'a2 = '\n", "print 'a2 is not equal to zero if and only if b1 and b2 are linearly independent.'\n", "print 'That is, if determinant of A is non-zero.'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 286 Example 8.14" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "v = [-10 2 8]\n", "u = [ 3 12 -1]\n", "Orthogonal projection of v1 on subspace W spanned by v2 is given by:\n", "[-3 0 1]\n", "Orthogonal projection of R**3 on W is the linear transformation E given by:\n", "(x1,x2,x3) -> (3*x1 + 12*x2 - x3)/%d * (3 12 -1) 154\n", "Rank(E) = 1\n", "Nullity(E) = 2\n" ] } ], "source": [ "from numpy import array,random,transpose,linalg,sqrt\n", "v = array([-10 ,2 ,8])\n", "u = array([3, 12, -1])\n", "print 'v = ',v\n", "print 'u = ',u\n", "print 'Orthogonal projection of v1 on subspace W spanned by v2 is given by:'\n", "a = (transpose(u*transpose(v)))/(u[0]**2 + u[1]**2 + u[2]**2) * u\n", "print a\n", "print 'Orthogonal projection of R**3 on W is the linear transformation E given by:'\n", "print '(x1,x2,x3) -> (3*x1 + 12*x2 - x3)/%d * (3 12 -1)',(u[0]**2 + u[1]**2 + u[2]**2)\n", "print 'Rank(E) = 1'\n", "print 'Nullity(E) = 2'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 288 Example 8.15" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f = (sqrt(2)*cos(2*pi*t) + sqrt(2)*sin(4*pi*t))**2\n", "Integration (f dt) in limits 0 to 1 = 2.0\n" ] } ], "source": [ "from mpmath import quad,cos,sin,pi,sqrt\n", "\n", "#part c\n", "print 'f = (sqrt(2)*cos(2*pi*t) + sqrt(2)*sin(4*pi*t))**2'\n", "print 'Integration (f dt) in limits 0 to 1 = ',\n", "x0 = 0#\n", "x1 = 1#\n", "X = quad(lambda t:(sqrt(2)*cos(2*pi*t) + sqrt(2)*sin(4*pi*t))**2,[x0,x1])\n", "print X" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 294 Example 8.17" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix of projection E in orthonormal basis is:\n", "A = 1/154 * [[ 9 36 -3]\n", " [ 36 144 -12]\n", " [ -3 -12 1]]\n", "A* = [[ 9 36 -3]\n", " [ 36 144 -12]\n", " [ -3 -12 1]]\n", "Since, E = E* and A = A*, then A is also the matrix of E*\n", "a1 = [154, 0, 0]\n", "a2 = [145, -36, 3]\n", "a3 = [-36, 10, 12]\n", "{a1,a2,a3} is the basis.\n", "Ea1 = [9, 36, -3]\n", "Ea2 = [0, 0, 0]\n", "Ea3 = [0, 0, 0]\n", "Matrix B of E in the basis is:\n", "B = \n", "[[-1 0 0]\n", " [-1 0 0]\n", " [ 0 0 0]]\n", "B* = \n", "[[-1 -1 0]\n", " [ 0 0 0]\n", " [ 0 0 0]]\n", "Since, B is not equal to B*, B is not the matrix of E*\n" ] } ], "source": [ "from numpy import vstack,array,transpose,conj\n", "#Equation given in example 14 is used.\n", "def transform(x,y,z):\n", " x1 = 3*x#\n", " x2 = 12*y#\n", " x3 = -z#\n", " m = [x1 ,x2, x3]\n", " return m\n", "\n", "print 'Matrix of projection E in orthonormal basis is:'\n", "t1 = transform(3,3,3)#\n", "t2 = transform(12,12,12)#\n", "t3 = transform(-1,-1,-1)#\n", "A = vstack([t1,t2,t3])#[t1# t2# t3]#\n", "print 'A = 1/154 * ',A\n", "\n", "A1 = transpose(conj(A))\n", "print 'A* = ',A1\n", "print 'Since, E = E* and A = A*, then A is also the matrix of E*'\n", "a1 = [154, 0, 0]#\n", "a2 = [145 ,-36, 3]#\n", "a3 = [-36 ,10 ,12]#\n", "print 'a1 = ',a1\n", "print 'a2 = ',a2\n", "print 'a3 = ',a3\n", "print '{a1,a2,a3} is the basis.'\n", "Ea1 = [9 ,36 ,-3]#\n", "Ea2 = [0 ,0, 0]#\n", "Ea3 = [0 ,0 ,0]#\n", "print 'Ea1 = ',Ea1\n", "print 'Ea2 = ',Ea2\n", "print 'Ea3 = ',Ea3\n", "B = array([[-1, 0, 0],[-1, 0 ,0],[0, 0, 0]])\n", "print 'Matrix B of E in the basis is:'\n", "print 'B = \\n',B\n", "B1 = transpose(conj(B))\n", "print 'B* = \\n',B1\n", "print 'Since, B is not equal to B*, B is not the matrix of E*'" ] } ], "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 }