{ "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": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n = 5\n", "a = [[ 1. 4. 2. 8. 8.]]\n", "b = [[ 10. 8. 3. 1. 8.]]\n", "Then, (a|b) = \n", "\n", "[[ 10. 40. 20. 80. 80.]\n", " [ 8. 32. 16. 64. 64.]\n", " [ 3. 12. 6. 24. 24.]\n", " [ 1. 4. 2. 8. 8.]\n", " [ 8. 32. 16. 64. 64.]]\n" ] } ], "source": [ "import numpy as np\n", "n=np.random.randint(2,9)\n", "a=np.random.rand(1,n)\n", "b=np.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*np.transpose(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 271 Example 8.2" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = [[ 4. 3.]]\n", "b = [[ 7. 5.]]\n", "Then, a|b = 47.0\n" ] } ], "source": [ "import numpy as np\n", "a=np.random.rand(1,2)\n", "b=np.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": 18, "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.383547227589\n", "x2 = 0.923521263539\n", "B = \n", "[[ 0.38354723 0.92352126 0. ]\n", " [ 0. 1. 0. ]\n", " [ 0. 0. 1. ]]\n", "Applying Gram-Schmidt process to B:\n", "a1 = [ 0.38354723 0.92352126 0. ]\n", "a2 = [-0.35421402 0.14710848 0. ]\n", "a3 = [0 0 1]\n", "U = \n", "[[[ 0.38354723 0.92352126 0. ]]\n", "\n", " [[-0.92352126 0.38354723 0. ]]\n", "\n", " [[ 0. 0. 1. ]]]\n", "M = \n", "[[ 1. 0. 0. ]\n", " [-2.40784236 2.60724085 0. ]\n", " [ 0. 0. 1. ]]\n", "inverse(M) * U = [[[ 3.83547228e-01 -4.25822963e-17 0.00000000e+00]\n", " [ 3.54214020e-01 3.54214020e-01 0.00000000e+00]\n", " [ 0.00000000e+00 0.00000000e+00 0.00000000e+00]]\n", "\n", " [[ -9.23521264e-01 -1.76848356e-17 0.00000000e+00]\n", " [ -8.52891524e-01 1.47108476e-01 0.00000000e+00]\n", " [ -0.00000000e+00 0.00000000e+00 0.00000000e+00]]\n", "\n", " [[ 0.00000000e+00 -0.00000000e+00 0.00000000e+00]\n", " [ 0.00000000e+00 0.00000000e+00 0.00000000e+00]\n", " [ 0.00000000e+00 0.00000000e+00 1.00000000e+00]]]\n", "So, B = inverse(M) * U\n" ] } ], "source": [ "import numpy as np\n", "print 'x1 and x2 are two real nos. i.e., x1**2 + x2**2 = 1'\n", "x1 = np.random.rand()\n", "x2 = np.sqrt(1 - x1**2)\n", "print 'x1 = ',x1\n", "print 'x2 = ',x2\n", "B = np.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 = np.array([x1, x2, 0])\n", "a2 = np.array([0 ,1 ,0]) - x2 * np.array([x1 ,x2 ,0])\n", "a3 = np.array([0, 0, 1])\n", "print 'a1 = ',a1\n", "print 'a2 = ',a2\n", "print 'a3 = ',a3\n", "U = np.array([[a1],[a2/x1],[a3]])\n", "print 'U = \\n',U\n", "M = np.array([[1, 0, 0],[-x2/x1, 1/x1, 0],[0, 0, 1]])\n", "print 'M = \\n',M\n", "print 'inverse(M) * U = ',np.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": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(x,y) = [[ 5. 3.]]\n", "(-y,x) = [-3.0, 5.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", "[[ 5. 3.]]\n", "is orthogonal to\n", "[-3.0, 5.0]\n" ] } ], "source": [ "import numpy as np\n", "#a = round(rand(1,2) * 10)#\n", "a=np.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 + np.sqrt(13))*x) or (1./2*(-3 - np.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": 20, "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": [ "import numpy as np\n", "b1 = np.array([3, 0, 4])\n", "b2 = np.array([-1 ,0 ,7])\n", "b3 = np.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-(np.transpose((b2*np.transpose(b1)))/25*b1)\n", "print 'a2 = ',a2\n", "a3 = b3-(np.transpose(b3*np.transpose(b1))/25*b1) - (np.transpose(b3*np.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": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A = [[ 1.25598176 1.81258697]\n", " [ 0.6193707 0.80341686]]\n", "b1 = [ 1.25598176 1.81258697]\n", "b2 = [ 0.6193707 0.80341686]\n", "Applying the orthogonalization process to b1,b2:\n", "[1.255981755902444, 1.8125869670307564] 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": [ "import numpy as np\n", "A = np.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 = (np.linalg.det(A)/(a**2 + b**2))*[-np.transpose(b), np.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": 22, "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": [ "import numpy as np\n", "v = np.array([-10 ,2 ,8])\n", "u = np.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 = (np.transpose(u*np.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": 23, "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 sympy.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": 24, "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": [ "import numpy as np\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 = np.vstack([t1,t2,t3])#[t1# t2# t3]#\n", "print 'A = 1/154 * ',A\n", "\n", "A1 = np.transpose(np.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 = np.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 = np.transpose(np.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 }