summaryrefslogtreecommitdiff
path: root/Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter8.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter8.ipynb')
-rw-r--r--[-rwxr-xr-x]Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter8.ipynb171
1 files changed, 85 insertions, 86 deletions
diff --git a/Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter8.ipynb b/Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter8.ipynb
index 6f1bfe72..6759a185 100755..100644
--- a/Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter8.ipynb
+++ b/Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter8.ipynb
@@ -16,7 +16,7 @@
},
{
"cell_type": "code",
- "execution_count": 16,
+ "execution_count": 1,
"metadata": {
"collapsed": false
},
@@ -25,31 +25,30 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "n = 5\n",
- "a = [[ 1. 4. 2. 8. 8.]]\n",
- "b = [[ 10. 8. 3. 1. 8.]]\n",
+ "n = 4\n",
+ "a = [[ 1. 5. 4. 1.]]\n",
+ "b = [[ 8. 6. 6. 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"
+ "[[ 8. 40. 32. 8.]\n",
+ " [ 6. 30. 24. 6.]\n",
+ " [ 6. 30. 24. 6.]\n",
+ " [ 8. 40. 32. 8.]]\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",
+ "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*np.transpose(b)"
+ "print 'Then, (a|b) = \\n\\n',a*transpose(b)"
]
},
{
@@ -61,7 +60,7 @@
},
{
"cell_type": "code",
- "execution_count": 17,
+ "execution_count": 2,
"metadata": {
"collapsed": false
},
@@ -70,16 +69,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "a = [[ 4. 3.]]\n",
- "b = [[ 7. 5.]]\n",
- "Then, a|b = 47.0\n"
+ "a = [[ 9. 4.]]\n",
+ "b = [[ 1. 9.]]\n",
+ "Then, a|b = 68.0\n"
]
}
],
"source": [
- "import numpy as np\n",
- "a=np.random.rand(1,2)\n",
- "b=np.random.rand(1,2)\n",
+ "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",
@@ -102,7 +101,7 @@
},
{
"cell_type": "code",
- "execution_count": 18,
+ "execution_count": 3,
"metadata": {
"collapsed": false
},
@@ -112,62 +111,62 @@
"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",
+ "x1 = 0.248003219206\n",
+ "x2 = 0.968759208092\n",
"B = \n",
- "[[ 0.38354723 0.92352126 0. ]\n",
+ "[[ 0.24800322 0.96875921 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",
+ "a1 = [ 0.24800322 0.96875921 0. ]\n",
+ "a2 = [-0.2402554 0.0615056 0. ]\n",
"a3 = [0 0 1]\n",
"U = \n",
- "[[[ 0.38354723 0.92352126 0. ]]\n",
+ "[[[ 0.24800322 0.96875921 0. ]]\n",
"\n",
- " [[-0.92352126 0.38354723 0. ]]\n",
+ " [[-0.96875921 0.24800322 0. ]]\n",
"\n",
" [[ 0. 0. 1. ]]]\n",
"M = \n",
"[[ 1. 0. 0. ]\n",
- " [-2.40784236 2.60724085 0. ]\n",
+ " [-3.90623642 4.03220572 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",
+ "inverse(M) * U = [[[ 0.24800322 -0. -0. ]\n",
+ " [ 0.2402554 0.2402554 0. ]\n",
+ " [ 0. 0. 0. ]]\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",
+ " [[-0.96875921 -0. -0. ]\n",
+ " [-0.9384944 0.0615056 0. ]\n",
+ " [-0. 0. 0. ]]\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",
+ " [[ 0. -0. -0. ]\n",
+ " [ 0. 0. 0. ]\n",
+ " [ 0. 0. 1. ]]]\n",
"So, B = inverse(M) * U\n"
]
}
],
"source": [
- "import numpy as np\n",
+ "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 = np.random.rand()\n",
- "x2 = np.sqrt(1 - x1**2)\n",
+ "x1 = random.rand()\n",
+ "x2 = 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",
+ "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 = 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",
+ "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 = np.array([[a1],[a2/x1],[a3]])\n",
+ "U = 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",
+ "M = 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 'inverse(M) * U = ',linalg.inv(M) * U\n",
"print 'So, B = inverse(M) * U'"
]
},
@@ -180,7 +179,7 @@
},
{
"cell_type": "code",
- "execution_count": 19,
+ "execution_count": 4,
"metadata": {
"collapsed": false
},
@@ -189,8 +188,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "(x,y) = [[ 5. 3.]]\n",
- "(-y,x) = [-3.0, 5.0]\n",
+ "(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",
@@ -202,16 +201,16 @@
"or\n",
"y = 1/2(-3 - sqrt(13))*x\n",
"Hence,\n",
- "[[ 5. 3.]]\n",
+ "[[ 7. 4.]]\n",
"is orthogonal to\n",
- "[-3.0, 5.0]\n"
+ "[-4.0, 7.0]\n"
]
}
],
"source": [
- "import numpy as np\n",
+ "from numpy import array,random,transpose,linalg,sqrt\n",
"#a = round(rand(1,2) * 10)#\n",
- "a=np.random.rand(1,2)\n",
+ "a=random.rand(1,2)\n",
"for j in [0,1]:\n",
" a[0,j]=round(a[0,j]*10)\n",
"\n",
@@ -233,7 +232,7 @@
"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",
+ "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",
@@ -252,7 +251,7 @@
},
{
"cell_type": "code",
- "execution_count": 20,
+ "execution_count": 5,
"metadata": {
"collapsed": false
},
@@ -284,19 +283,19 @@
}
],
"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",
+ "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-(np.transpose((b2*np.transpose(b1)))/25*b1)\n",
+ "a2 = b2-(transpose((b2*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",
+ "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",
@@ -326,7 +325,7 @@
},
{
"cell_type": "code",
- "execution_count": 21,
+ "execution_count": 6,
"metadata": {
"collapsed": false
},
@@ -335,12 +334,12 @@
"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",
+ "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.255981755902444, 1.8125869670307564] a1 = \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"
@@ -348,8 +347,8 @@
}
],
"source": [
- "import numpy as np\n",
- "A = np.random.rand(2,2)\n",
+ "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",
@@ -363,7 +362,7 @@
"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",
+ "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",
@@ -379,7 +378,7 @@
},
{
"cell_type": "code",
- "execution_count": 22,
+ "execution_count": 7,
"metadata": {
"collapsed": false
},
@@ -400,13 +399,13 @@
}
],
"source": [
- "import numpy as np\n",
- "v = np.array([-10 ,2 ,8])\n",
- "u = np.array([3, 12, -1])\n",
+ "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 = (np.transpose(u*np.transpose(v)))/(u[0]**2 + u[1]**2 + u[2]**2) * u\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",
@@ -423,7 +422,7 @@
},
{
"cell_type": "code",
- "execution_count": 23,
+ "execution_count": 8,
"metadata": {
"collapsed": false
},
@@ -438,7 +437,7 @@
}
],
"source": [
- "from sympy.mpmath import quad,cos,sin,pi,sqrt\n",
+ "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",
@@ -458,7 +457,7 @@
},
{
"cell_type": "code",
- "execution_count": 24,
+ "execution_count": 9,
"metadata": {
"collapsed": false
},
@@ -496,7 +495,7 @@
}
],
"source": [
- "import numpy as np\n",
+ "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",
@@ -509,10 +508,10 @@
"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",
+ "A = vstack([t1,t2,t3])#[t1# t2# t3]#\n",
"print 'A = 1/154 * ',A\n",
"\n",
- "A1 = np.transpose(np.conj(A))\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",
@@ -528,10 +527,10 @@
"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",
+ "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 = np.transpose(np.conj(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*'"
]