summaryrefslogtreecommitdiff
path: root/Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter2.ipynb
diff options
context:
space:
mode:
authorTrupti Kini2016-06-22 23:30:24 +0600
committerTrupti Kini2016-06-22 23:30:24 +0600
commit510a3b8a6a1c39d716875e3bfecb230f7b7c20e0 (patch)
tree698123825d3ead2b1c2da3ca283f2a71ad1727a1 /Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter2.ipynb
parent4c72b042d9aa9d98e3daa3ddb27c12eeb4d14451 (diff)
downloadPython-Textbook-Companions-510a3b8a6a1c39d716875e3bfecb230f7b7c20e0.tar.gz
Python-Textbook-Companions-510a3b8a6a1c39d716875e3bfecb230f7b7c20e0.tar.bz2
Python-Textbook-Companions-510a3b8a6a1c39d716875e3bfecb230f7b7c20e0.zip
Added(A)/Deleted(D) following books
A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter10_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter13_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter1_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter21_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter22_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter23_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter24_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter26_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter27_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter28_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter2_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter34_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter35_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter4_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter5_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter6_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/chapter9_2.ipynb A Higher_Engineering_Mathematics_by_B._S._Grewal/screenshots/screen1_2.png A Higher_Engineering_Mathematics_by_B._S._Grewal/screenshots/screen2_2.png A Higher_Engineering_Mathematics_by_B._S._Grewal/screenshots/screen3_2.png A Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter1.ipynb A Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter10.ipynb A Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter2.ipynb A Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter3.ipynb A Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter4.ipynb A Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter5.ipynb A Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter6.ipynb A Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter7.ipynb A Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter8.ipynb A Linear_Algebra_by_K._Hoffman_and_R._Kunze/screenshots/InverseMatrix.png A Linear_Algebra_by_K._Hoffman_and_R._Kunze/screenshots/determinant.png A Linear_Algebra_by_K._Hoffman_and_R._Kunze/screenshots/scalorpolynomial.png
Diffstat (limited to 'Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter2.ipynb')
-rw-r--r--Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter2.ipynb822
1 files changed, 822 insertions, 0 deletions
diff --git a/Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter2.ipynb b/Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter2.ipynb
new file mode 100644
index 00000000..cac42c12
--- /dev/null
+++ b/Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter2.ipynb
@@ -0,0 +1,822 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Chapter 2 - Vector spaces"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Page 37 Example 2.8"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "a1 = [1, 2, 0, 3, 0]\n",
+ "a2 = [0, 0, 1, 4, 0]\n",
+ "a3 = [0, 0, 0, 0, 1]\n",
+ "By theorem 3, vector a is in subspace W of F**5 spanned by a1, a2, a3\n",
+ "if and only if there exist scalars c1, c2, c3 such that\n",
+ "a= c1a1 + c2a2 + c3a3\n",
+ "So, a = (c1,2*c1,c2,3c1+4c2,c3)\n",
+ "c1 = -3\n",
+ "c2 = 1\n",
+ "c3 = 2\n",
+ "Therefore, a = [0, 0, 1, 4, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]\n",
+ "This shows, a is in W\n",
+ "And (2,4,6,7,8) is not in W as there is no value of c1 c2 c3 that satisfies the equation\n"
+ ]
+ }
+ ],
+ "source": [
+ "\n",
+ "a1 = [1, 2 ,0 ,3, 0]#\n",
+ "a2 =[0, 0 ,1 ,4 ,0]#\n",
+ "a3 = [0 ,0 ,0 ,0, 1]#\n",
+ "print 'a1 = ',a1\n",
+ "print 'a2 = ',a2\n",
+ "print 'a3 = ',a3\n",
+ "print 'By theorem 3, vector a is in subspace W of F**5 spanned by a1, a2, a3'\n",
+ "print 'if and only if there exist scalars c1, c2, c3 such that'\n",
+ "print 'a= c1a1 + c2a2 + c3a3'\n",
+ "print 'So, a = (c1,2*c1,c2,3c1+4c2,c3)'\n",
+ "c1 = -3#\n",
+ "c2 = 1#\n",
+ "c3 = 2#\n",
+ "a = c1*a1 + c2*a2 + c3*a3#\n",
+ "print 'c1 = ',c1\n",
+ "print 'c2 = ',c2\n",
+ "print 'c3 = ',c3\n",
+ "print 'Therefore, a = ',a\n",
+ "print 'This shows, a is in W'\n",
+ "print 'And (2,4,6,7,8) is not in W as there is no value of c1 c2 c3 that satisfies the equation'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Page 38 Example 2.10"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "A = \n",
+ "[[1 2 0 3 0]\n",
+ " [0 0 1 4 0]\n",
+ " [0 0 0 0 1]]\n",
+ "The subspace of F**5 spanned by a1 a2 a3(row vectors of A) is called row space of A.\n",
+ "a1 = [1 2 0 3 0]\n",
+ "a2 = [0 0 1 4 0]\n",
+ "a3 = [0 0 0 0 1]\n",
+ "And, it is also the row space of B.\n",
+ "B = \n",
+ "[[ 1 2 0 3 0]\n",
+ " [ 0 0 1 4 0]\n",
+ " [ 0 0 0 0 1]\n",
+ " [-4 -8 1 -8 0]]\n"
+ ]
+ }
+ ],
+ "source": [
+ "import numpy as np\n",
+ "\n",
+ "A = np.array([[1, 2, 0 ,3 ,0],[0, 0, 1, 4, 0],[0, 0, 0, 0, 1]])\n",
+ "print 'A = \\n',A\n",
+ "print 'The subspace of F**5 spanned by a1 a2 a3(row vectors of A) is called row space of A.'\n",
+ "a1 = A[0,:]\n",
+ "a2 = A[1,:]\n",
+ "a3 = A[2,:]\n",
+ "print 'a1 = ',a1\n",
+ "print 'a2 = ',a2\n",
+ "print 'a3 = ',a3\n",
+ "print 'And, it is also the row space of B.'\n",
+ "B = np.array([[1, 2, 0, 3, 0],[0, 0, 1, 4, 0],[0, 0, 0, 0, 1],[-4, -8, 1 ,-8, 0]])\n",
+ "print 'B = \\n',B"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Page 39 Example 2.11"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "V is the space of all polynomial functions over F.\n",
+ "S contains the functions as:\n",
+ "n = 17\n",
+ "f0(x) = 1\n",
+ "f1(x) = x\n",
+ "f2(x) = x**2\n",
+ "f3(x) = x**3\n",
+ "f4(x) = x**4\n",
+ "f5(x) = x**5\n",
+ "f6(x) = x**6\n",
+ "f7(x) = x**7\n",
+ "f8(x) = x**8\n",
+ "f9(x) = x**9\n",
+ "f10(x) = x**10\n",
+ "f11(x) = x**11\n",
+ "f12(x) = x**12\n",
+ "f13(x) = x**13\n",
+ "f14(x) = x**14\n",
+ "f15(x) = x**15\n",
+ "f16(x) = x**16\n",
+ "Then, V is the subspace spanned by set S.\n"
+ ]
+ }
+ ],
+ "source": [
+ "import sympy as sp\n",
+ "import numpy as np\n",
+ "print 'V is the space of all polynomial functions over F.'\n",
+ "print 'S contains the functions as:'\n",
+ "x = sp.Symbol(\"x\")\n",
+ "#n = round(rand()*10)#\n",
+ "n=np.random.randint(0,19)\n",
+ "print 'n = ',n\n",
+ "for i in range (0,n):\n",
+ " f = x**i#\n",
+ " print 'f%d(x) = '%(i,),f\n",
+ " \n",
+ "\n",
+ "print 'Then, V is the subspace spanned by set S.'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Page 41 Example 2.12"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "a1 = [ 3 0 -3]\n",
+ "a2 = [-1 1 2]\n",
+ "a3 = [ 4 2 -2]\n",
+ "a4 = [2 1 1]\n",
+ " Since, 2 * a1 + 2 * a2 - a3 + 0 * a4 = [0 0 0] = 0\n",
+ "a1,a2,a3,a4 are linearly independent\n",
+ "Now, e1 = [1, 0, 0]\n",
+ "e2 = [0, 1, 0]\n",
+ "e3 = [0, 0, 1]\n",
+ "Also, e1,e2,e3 are linearly independent.\n"
+ ]
+ }
+ ],
+ "source": [
+ "import numpy as np\n",
+ "a1 = np.array([3 ,0, -3])\n",
+ "a2 = np.array([-1 ,1 ,2])\n",
+ "a3 = np.array([4 ,2, -2])\n",
+ "a4 = np.array([2 ,1, 1])\n",
+ "print 'a1 = ',a1\n",
+ "print 'a2 = ',a2\n",
+ "print 'a3 = ',a3\n",
+ "print 'a4 = ',a4\n",
+ "t = 2 * a1 + 2 * a2 - a3 + 0 * a4\n",
+ "print ' Since, 2 * a1 + 2 * a2 - a3 + 0 * a4 = ',t,'= 0'\n",
+ "print 'a1,a2,a3,a4 are linearly independent'\n",
+ "e1 = [1, 0, 0]#\n",
+ "e2 = [0 ,1 ,0]#\n",
+ "e3 = [0 ,0, 1]#\n",
+ "print 'Now, e1 = ',e1\n",
+ "print 'e2 = ',e2\n",
+ "print 'e3 = ',e3\n",
+ "print 'Also, e1,e2,e3 are linearly independent.'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Page 41 Example 2.13"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "S is the subset of F**n consisting of n vectors.\n",
+ "n = 10\n",
+ "e1 = \n",
+ "[ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
+ "e2 = \n",
+ "[ 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
+ "e3 = \n",
+ "[ 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]\n",
+ "e4 = \n",
+ "[ 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]\n",
+ "e5 = \n",
+ "[ 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n",
+ "e6 = \n",
+ "[ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]\n",
+ "e7 = \n",
+ "[ 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]\n",
+ "e8 = \n",
+ "[ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]\n",
+ "e9 = \n",
+ "[ 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]\n",
+ "e10 = \n",
+ "[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
+ "x1,x2,x3...xn are the scalars in F\n",
+ "Putting a = x1*e1 + x2*e2 + x3*e3 + .... + xn*en\n",
+ "So, a = (x1,x2,x3,...,xn)\n",
+ "Therefore, e1,e2..,en span F**n\n",
+ "a = 0 if x1 = x2 = x3 = .. = xn = 0\n",
+ "So,e1,e2,e3,..,en are linearly independent.\n",
+ "The set S = {e1,e2,..,en} is called standard basis of F**n\n"
+ ]
+ }
+ ],
+ "source": [
+ "import numpy as np\n",
+ "print 'S is the subset of F**n consisting of n vectors.'\n",
+ "#n = round(rand() *10 + 1)#\\\n",
+ "n=np.random.randint(0,19)\n",
+ "print 'n = ',n\n",
+ "I = np.identity(n)\n",
+ "for i in range(0,n):\n",
+ " e = I[i,:]\n",
+ " print 'e%d = '%(i+1)\n",
+ " print e\n",
+ "\n",
+ "print 'x1,x2,x3...xn are the scalars in F'\n",
+ "print 'Putting a = x1*e1 + x2*e2 + x3*e3 + .... + xn*en'\n",
+ "print 'So, a = (x1,x2,x3,...,xn)'\n",
+ "print 'Therefore, e1,e2..,en span F**n'\n",
+ "print 'a = 0 if x1 = x2 = x3 = .. = xn = 0'\n",
+ "print 'So,e1,e2,e3,..,en are linearly independent.'\n",
+ "print 'The set S = {e1,e2,..,en} is called standard basis of F**n'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Page 54 Example 2.20"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "P = \n",
+ "[[-1 4 5]\n",
+ " [ 0 2 -3]\n",
+ " [ 0 0 8]]\n",
+ "\n",
+ "inverse(P) = \n",
+ "[[-1. 2. 1.375 ]\n",
+ " [ 0. 0.5 0.1875]\n",
+ " [ 0. 0. 0.125 ]]\n",
+ "The vectors forming basis of F**3 are a1, a2, a3\n",
+ "a1' = \n",
+ "[-1 0 0]\n",
+ "\n",
+ "a2' = \n",
+ "[4 2 0]\n",
+ "\n",
+ "a3' = \n",
+ "[ 5 -3 8]\n",
+ "The coordinates x1,x2,x3 of vector a = [x1,x2,x3] is given by inverse(P)*[x1# x2# x3]\n",
+ "And, -10*a1 - 1/2*a2 - a3 = [ 3. 2. -8.]\n"
+ ]
+ }
+ ],
+ "source": [
+ "import numpy as np\n",
+ "P = np.array([[-1, 4, 5],[ 0, 2, -3],[ 0, 0, 8]])\n",
+ "print 'P = \\n',P\n",
+ "print '\\ninverse(P) = \\n',np.linalg.inv(P)\n",
+ "a1 = P[:,0]\n",
+ "a2 = P[:,1]\n",
+ "a3 = P[:,2]\n",
+ "print 'The vectors forming basis of F**3 are a1'', a2'', a3'''\n",
+ "print \"a1' = \\n\",np.transpose(a1)\n",
+ "print \"\\na2' = \\n\",np.transpose(a2)\n",
+ "print \"\\na3' = \\n\",np.transpose(a3)\n",
+ "print 'The coordinates x1'',x2'',x3'' of vector a = [x1,x2,x3] is given by inverse(P)*[x1# x2# x3]'\n",
+ "t = -10*a1 - 1./2*a2 - a3#\n",
+ "print 'And, -10*a1'' - 1/2*a2'' - a3'' = ',t"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Page 60 Example 2.21"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Given row vectors are:\n",
+ "a1 = [1, 2, 2, 1]\n",
+ "a2 = [0, 2, 0, 1]\n",
+ "a3 = [-2, 0, -4, 3]\n",
+ "The matrix A from these vectors will be:\n",
+ "A = \n",
+ "[[ 1 2 2 1]\n",
+ " [ 0 2 0 1]\n",
+ " [-2 0 -4 3]]\n",
+ "Finding Row reduced echelon matrix of A that is given by R\n",
+ "And applying same operations on identity matrix Q such that R = QA\n",
+ "Q = \n",
+ "[[ 1. 0. 0.]\n",
+ " [ 0. 1. 0.]\n",
+ " [ 0. 0. 1.]]\n",
+ "Applying row transformations on A and Q,we get\n",
+ "R1 = R1-R2\n",
+ "A = \n",
+ "[[ 1 0 2 0]\n",
+ " [ 0 2 0 1]\n",
+ " [-2 0 -4 3]]\n",
+ "Q = \n",
+ "[[ 1. -1. 0.]\n",
+ " [ 0. 1. 0.]\n",
+ " [ 0. 0. 1.]]\n",
+ "R3 = R3 + 2*R1\n",
+ "A = \n",
+ "[[1 0 2 0]\n",
+ " [0 2 0 1]\n",
+ " [0 0 0 3]]\n",
+ "Q = \n",
+ "[[ 1. -1. 0.]\n",
+ " [ 0. 1. 0.]\n",
+ " [ 2. -2. 1.]]\n",
+ "R3 = R3/3\n",
+ "A = \n",
+ "[[1 0 2 0]\n",
+ " [0 2 0 1]\n",
+ " [0 0 0 1]]\n",
+ "Q = \n",
+ "[[ 1. -1. 0. ]\n",
+ " [ 0. 1. 0. ]\n",
+ " [ 0.66666667 -0.66666667 0.33333333]]\n",
+ "R2 = R2/2\n",
+ "A = \n",
+ "[[1 0 2 0]\n",
+ " [0 1 0 0]\n",
+ " [0 0 0 1]]\n",
+ "Q = \n",
+ "[[ 1. -1. 0. ]\n",
+ " [ 0. 5. 0. ]\n",
+ " [ 0.66666667 -0.66666667 0.33333333]]\n",
+ "R2 = R2 - 1/2*R3\n",
+ "A = \n",
+ "[[1 0 2 0]\n",
+ " [0 1 0 0]\n",
+ " [0 0 0 1]]\n",
+ "Q = \n",
+ "[[ 1. -1. 0. ]\n",
+ " [-0.33333333 5.33333333 -0.16666667]\n",
+ " [ 0.66666667 -0.66666667 0.33333333]]\n",
+ "Row reduced echelon matrix:\n",
+ "A = \n",
+ "[[1 0 2 0]\n",
+ " [0 1 0 0]\n",
+ " [0 0 0 1]]\n",
+ "Q = \n",
+ "[[ 1. -1. 0. ]\n",
+ " [-0.33333333 5.33333333 -0.16666667]\n",
+ " [ 0.66666667 -0.66666667 0.33333333]]\n",
+ "rank of R = 2\n",
+ "Since, Rank of R is 3, so a1, a2, a3 are independent\n",
+ "Now, basis for W can be given by row vectors of R i.e. p1,p2,p3\n",
+ "b is any vector in W. b = [b1 b2 b3 b4]\n",
+ "Span of vectors p1,p2,p3 consist of vector b with b3 = 2*b1\n",
+ "So,b = b1p1 + b2p2 + b4p3\n",
+ "And,[p1 p2 p3] = R = Q*A\n",
+ "So, b = [b1 b2 b3]* Q * A\n",
+ "hence, b = x1a1 + x2a2 + x3a3 where x1 = [b1 b2 b4] * Q(1) and so on\n",
+ "Now, given 3 vectors a1 a2 a3:\n",
+ "a1 = [1, 0, 2, 0]\n",
+ "a2 = [0, 2, 0, 1]\n",
+ "a3 = [0, 0, 0, 3]\n",
+ "Since a1 a2 a3 are all of the form (y1 y2 y3 y4) with y3 = 2*y1, hence they are in W.\n",
+ "So, they are independent.\n",
+ "Required matrix P such that X = PX is:\n",
+ "P = \n",
+ "[[ 0. -0. 2.]\n",
+ " [-0. 0. -2.]\n",
+ " [ 0. -0. 1.]]\n"
+ ]
+ }
+ ],
+ "source": [
+ "import numpy as np\n",
+ "a1 = [1 ,2 ,2, 1]#\n",
+ "a2 = [0 ,2 ,0 ,1]#\n",
+ "a3 = [-2, 0, -4, 3]#\n",
+ "print 'Given row vectors are:'\n",
+ "print 'a1 = ',a1\n",
+ "print 'a2 = ',a2\n",
+ "print 'a3 = ',a3\n",
+ "print 'The matrix A from these vectors will be:'\n",
+ "#A = [a1],[a2], [a3]]\n",
+ "A=np.array([a1,a2,a3])\n",
+ "print 'A = \\n',A\n",
+ "\n",
+ "print 'Finding Row reduced echelon matrix of A that is given by R'\n",
+ "print 'And applying same operations on identity matrix Q such that R = QA'\n",
+ "Q = np.identity(3)\n",
+ "print 'Q = \\n',Q\n",
+ "T = A# #Temporary matrix to store A\n",
+ "print 'Applying row transformations on A and Q,we get'\n",
+ "print 'R1 = R1-R2'\n",
+ "A[0,:] = A[0,:] - A[1,:]\n",
+ "Q[0,:] = Q[0,:] - Q[1,:]\n",
+ "print 'A = \\n',A\n",
+ "print 'Q = \\n',Q\n",
+ "print 'R3 = R3 + 2*R1'\n",
+ "A[2,:] = A[2,:] + 2*A[0,:]\n",
+ "Q[2,:] = Q[2,:] + 2*Q[0,:]\n",
+ "print 'A = \\n',A\n",
+ "print 'Q = \\n',Q\n",
+ "print 'R3 = R3/3'\n",
+ "A[2,:] = 1./3*A[2,:]\n",
+ "Q[2,:] = 1./3*Q[2,:]\n",
+ "print 'A = \\n',A\n",
+ "print 'Q = \\n',Q\n",
+ "print 'R2 = R2/2'\n",
+ "A[1,:] = 1./2*A[1,:]\n",
+ "Q[1,:] = 10/2*Q[1,:]\n",
+ "print 'A = \\n',A\n",
+ "print 'Q = \\n',Q\n",
+ "print 'R2 = R2 - 1/2*R3'\n",
+ "A[1,:] = A[1,:] - 1./2*A[2,:]\n",
+ "Q[1,:] = Q[1,:] - 1./2*Q[2,:]\n",
+ "print 'A = \\n',A\n",
+ "print 'Q = \\n',Q\n",
+ "R = A#\n",
+ "A = T#\n",
+ "print 'Row reduced echelon matrix:'\n",
+ "print 'A = \\n',A\n",
+ "print 'Q = \\n',Q\n",
+ "#part a\n",
+ "print 'rank of R = ',np.rank(R)\n",
+ "\n",
+ "print 'Since, Rank of R is 3, so a1, a2, a3 are independent'\n",
+ "#part b\n",
+ "print 'Now, basis for W can be given by row vectors of R i.e. p1,p2,p3'\n",
+ "print 'b is any vector in W. b = [b1 b2 b3 b4]'\n",
+ "print 'Span of vectors p1,p2,p3 consist of vector b with b3 = 2*b1'\n",
+ "print 'So,b = b1p1 + b2p2 + b4p3'\n",
+ "print 'And,[p1 p2 p3] = R = Q*A'\n",
+ "print 'So, b = [b1 b2 b3]* Q * A'\n",
+ "print 'hence, b = x1a1 + x2a2 + x3a3 where x1 = [b1 b2 b4] * Q(1) and so on' # #Equation 1\n",
+ "#part c\n",
+ "print 'Now, given 3 vectors a1'' a2'' a3'':'\n",
+ "c1 = [1, 0, 2, 0]#\n",
+ "c2 = [0 ,2 ,0, 1]#\n",
+ "c3 = [0 ,0 ,0 ,3]#\n",
+ "print 'a1'' = ',c1\n",
+ "print 'a2'' = ',c2\n",
+ "print 'a3'' = ',c3\n",
+ "print 'Since a1'' a2'' a3'' are all of the form (y1 y2 y3 y4) with y3 = 2*y1, hence they are in W.'\n",
+ "print 'So, they are independent.'\n",
+ "#part d\n",
+ "c = np.array([c1,c2,c3])\n",
+ "P = np.identity(3)\n",
+ "for i in range(0,3):\n",
+ " b1 = c[i,0]\n",
+ " b2 = c[i,1]\n",
+ " b4 = c[i,3]\n",
+ " x1 = np.array([b1, b2, b4]) * Q[:,0]\n",
+ " x2 = np.array([b1, b2, b4])*Q[:,1]\n",
+ " x3 = np.array([b1, b2, b4])*Q[:,2]\n",
+ " \n",
+ "\n",
+ "print 'Required matrix P such that X = PX'' is:'\n",
+ "P=np.vstack([x1,x2,x3])\n",
+ "print 'P = \\n',P\n",
+ "#print x1"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Page 63 Example 2.22"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "A = \n",
+ "[[ 1 2 0 3 0]\n",
+ " [ 1 2 -1 -1 0]\n",
+ " [ 0 0 1 4 0]\n",
+ " [ 2 4 1 10 1]\n",
+ " [ 0 0 0 0 1]]\n",
+ "Taking an identity matrix P:\n",
+ "P = \n",
+ "[[ 1. 0. 0. 0. 0.]\n",
+ " [ 0. 1. 0. 0. 0.]\n",
+ " [ 0. 0. 1. 0. 0.]\n",
+ " [ 0. 0. 0. 1. 0.]\n",
+ " [ 0. 0. 0. 0. 1.]]\n",
+ "Applying row transformations on P and A to get a row reduced echelon matrix R:\n",
+ "R2 = R2 - R1 and R4 = R4 - 2* R1\n",
+ "A = \n",
+ "[[ 1 2 0 3 0]\n",
+ " [ 0 0 -1 -4 0]\n",
+ " [ 0 0 1 4 0]\n",
+ " [ 0 0 1 4 1]\n",
+ " [ 0 0 0 0 1]]\n",
+ "P = \n",
+ "[[ 1. 0. 0. 0. 0.]\n",
+ " [-1. 1. 0. 0. 0.]\n",
+ " [ 0. 0. 1. 0. 0.]\n",
+ " [-2. 0. 0. 1. 0.]\n",
+ " [ 0. 0. 0. 0. 1.]]\n",
+ "R2 = -R2 , R3 = R3 - R1 + R2 and R4 = R4 - R1 + R2\n",
+ "A = \n",
+ "[[1 2 0 3 0]\n",
+ " [0 0 1 4 0]\n",
+ " [0 0 0 0 0]\n",
+ " [0 0 0 0 1]\n",
+ " [0 0 0 0 1]]\n",
+ "P = \n",
+ "[[ 1. 0. 0. 0. 0.]\n",
+ " [ 1. -1. -0. -0. -0.]\n",
+ " [-1. 1. 1. 0. 0.]\n",
+ " [-3. 1. 0. 1. 0.]\n",
+ " [-3. 1. 0. 1. 0.]]\n",
+ "Mutually interchanging R3, R4 and R5\n",
+ "Row reduced echelon matrix R = \n",
+ "[[1 2 0 3 0]\n",
+ " [0 0 1 4 0]\n",
+ " [0 0 0 0 1]\n",
+ " [0 0 0 0 1]\n",
+ " [0 0 0 0 0]]\n",
+ "Invertible Matrix P = \n",
+ "[[ 1. 0. 0. 0. 0.]\n",
+ " [ 1. -1. -0. -0. -0.]\n",
+ " [-3. 1. 0. 1. 0.]\n",
+ " [-3. 1. 0. 1. 0.]\n",
+ " [ 0. 0. 0. 0. 0.]]\n",
+ "Invertible matrix P is not unique. There can be many that depends on operations used to reduce A\n",
+ "-----------------------------------------\n",
+ "For the basis of row space W of A, we can take the non-zero rows of R\n",
+ "It can be given by p1, p2, p3\n",
+ "p1 = [1 2 0 3 0]\n",
+ "p2 = [0 0 1 4 0]\n",
+ "p3 = [0 0 0 0 1]\n",
+ "-----------------------------------------\n",
+ "The row space W consists of vectors of the form:\n",
+ "b = c1p1 + c2p2 + c3p3\n",
+ "i.e. b = (c1,2*c1,c2,3*c1+4*c2,c3) where, c1 c2 c3 are scalars.\n",
+ "So, if b2 = 2*b1 and b4 = 3*b1 + 4*b3 => (b1,b2,b3,b4,b5) = b1p1 + b3p2 + b5p3\n",
+ "then,(b1,b2,b3,b4,b5) is in W\n",
+ "-----------------------------------------\n",
+ "The coordinate matrix of the vector (b1,2*b1,b2,3*b1+4*b2,b3) in the basis (p1,p2,p3) is column matrix of b1,b2,b3 such that:\n",
+ " b1\n",
+ " b2\n",
+ " b3\n",
+ "-----------------------------------------\n",
+ "Now, to write each vector in W as a linear combination of rows of A:\n",
+ "Let b = (b1,b2,b3,b4,b5) and if b is in W, then\n",
+ "we know,b = (b1,2*b1,b3,3*b1 + 4*b3,b5) => [b1,b3,b5,0,0]*R\n",
+ "=> b = [b1,b3,b5,0,0] * P*A => b = [b1+b3,-b3,0,0,b5] * A\n",
+ "if b = (-5,-10,1,-11,20)\n",
+ "b = ( [-4, -1, 0, 0, 20] ) *[ [[1 2 0 3 0]\n",
+ " [0 0 1 4 0]\n",
+ " [0 0 0 0 1]\n",
+ " [0 0 0 0 1]\n",
+ " [0 0 0 0 0]] ]\n",
+ "-----------------------------------------\n",
+ "The equations in system RX = 0 are given by R * [x1 x2 x3 x4 x5]\n",
+ "i.e., x1 + 2*x2 + 3*x4\n",
+ "x3 + 4*x4\n",
+ "x5\n",
+ "so, V consists of all columns of the form\n",
+ "[ X=\n",
+ " -2*x2 - 3*x4\n",
+ " x2\n",
+ " -4*x4\n",
+ " x4\n",
+ " 0\n",
+ "where x2 and x4 are arbitrary ]\n",
+ "-----------------------------------------\n",
+ "Let x2 = 1,x4 = 0 then the given column forms a basis of V\n",
+ "[[-2], [1], [0], [0], [0]]\n",
+ "Similarly,if x2 = 0,x4 = 1 then the given column forms a basis of V\n",
+ "[[-3], [0], [-4], [1], [0]]\n",
+ "-----------------------------------------\n",
+ "The equation AX = Y has solutions X if and only if\n",
+ "-y1 + y2 + y3 = 0\n",
+ "-3*y1 + y2 + y4 -y5 = 0\n",
+ "where, Y = (y1 y2 y3 y4 y5)\n"
+ ]
+ }
+ ],
+ "source": [
+ "import numpy as np\n",
+ "A = np.array([[1, 2, 0, 3, 0],[1, 2, -1, -1, 0],[0 ,0 ,1 ,4 ,0],[2, 4 ,1 ,10, 1],[0 ,0 ,0 ,0 ,1]])\n",
+ "print 'A = \\n',A\n",
+ "#part a\n",
+ "T = A# #Temporary storing A in T\n",
+ "print 'Taking an identity matrix P:'\n",
+ "P = np.identity(5)\n",
+ "print 'P = \\n',P\n",
+ "print 'Applying row transformations on P and A to get a row reduced echelon matrix R:'\n",
+ "print 'R2 = R2 - R1 and R4 = R4 - 2* R1'\n",
+ "A[1,:] = A[1,:] - A[0,:]\n",
+ "P[1,:] = P[1,:] - P[0,:]\n",
+ "A[3,:] = A[3,:] - 2 * A[0,:]\n",
+ "P[3,:] = P[3,:] - 2 * P[0,:]\n",
+ "print 'A = \\n',A\n",
+ "print 'P = \\n',P\n",
+ "print 'R2 = -R2 , R3 = R3 - R1 + R2 and R4 = R4 - R1 + R2'\n",
+ "A[1,:] = -A[1,:]\n",
+ "P[1,:] = -P[1,:]\n",
+ "A[2,:] = A[2,:] - A[1,:]\n",
+ "P[2,:] = P[2,:] - P[1,:]\n",
+ "A[3,:] = A[3,:] - A[1,:]\n",
+ "P[3:] = P[3,:] - P[1,:]\n",
+ "print 'A = \\n',A\n",
+ "print 'P = \\n',P\n",
+ "print 'Mutually interchanging R3, R4 and R5'\n",
+ "x = A[2,:]\n",
+ "A[2,:] = A[4,:]\n",
+ "y = A[3,:]\n",
+ "A[3,:] = x#\n",
+ "A[4,:] = y - A[2,:]\n",
+ "x = P[2,:]\n",
+ "P[2,:] = P[4,:]\n",
+ "y = P[3,:]\n",
+ "P[3,:] = x#\n",
+ "P[4,:] = y - P[2,:]\n",
+ "R = A#\n",
+ "A = T#\n",
+ "print 'Row reduced echelon matrix R = \\n',R\n",
+ "print 'Invertible Matrix P = \\n',P\n",
+ "print 'Invertible matrix P is not unique. There can be many that depends on operations used to reduce A'\n",
+ "print '-----------------------------------------'\n",
+ "#part b\n",
+ "print 'For the basis of row space W of A, we can take the non-zero rows of R'\n",
+ "print 'It can be given by p1, p2, p3'\n",
+ "p1 = R[0,:]\n",
+ "p2 = R[1,:]\n",
+ "p3 = R[2,:]\n",
+ "print 'p1 = ',p1\n",
+ "print 'p2 = ',p2\n",
+ "print 'p3 = ',p3\n",
+ "print '-----------------------------------------'\n",
+ "#part c\n",
+ "print 'The row space W consists of vectors of the form:'\n",
+ "print 'b = c1p1 + c2p2 + c3p3'\n",
+ "print 'i.e. b = (c1,2*c1,c2,3*c1+4*c2,c3) where, c1 c2 c3 are scalars.'\n",
+ "print 'So, if b2 = 2*b1 and b4 = 3*b1 + 4*b3 => (b1,b2,b3,b4,b5) = b1p1 + b3p2 + b5p3'\n",
+ "print 'then,(b1,b2,b3,b4,b5) is in W'\n",
+ "print '-----------------------------------------'\n",
+ "#part d\n",
+ "print 'The coordinate matrix of the vector (b1,2*b1,b2,3*b1+4*b2,b3) in the basis (p1,p2,p3) is column matrix of b1,b2,b3 such that:'\n",
+ "print ' b1'\n",
+ "print ' b2'\n",
+ "print ' b3'\n",
+ "print '-----------------------------------------'\n",
+ "#part e\n",
+ "print 'Now, to write each vector in W as a linear combination of rows of A:'\n",
+ "print 'Let b = (b1,b2,b3,b4,b5) and if b is in W, then'\n",
+ "print 'we know,b = (b1,2*b1,b3,3*b1 + 4*b3,b5) => [b1,b3,b5,0,0]*R'\n",
+ "print '=> b = [b1,b3,b5,0,0] * P*A => b = [b1+b3,-b3,0,0,b5] * A'\n",
+ "print 'if b = (-5,-10,1,-11,20)'\n",
+ "b1 = -5#\n",
+ "b2 = -10#\n",
+ "b3 = 1#\n",
+ "b4 = -11#\n",
+ "b5 = 20#\n",
+ "x = [b1 + b3,-b3,0,0,b5]#\n",
+ "print 'b = (',x,')','*[',A,']'\n",
+ "print '-----------------------------------------'\n",
+ "#part f\n",
+ "print 'The equations in system RX = 0 are given by R * [x1 x2 x3 x4 x5]'\n",
+ "print 'i.e., x1 + 2*x2 + 3*x4'\n",
+ "print 'x3 + 4*x4'\n",
+ "print 'x5'\n",
+ "print 'so, V consists of all columns of the form'\n",
+ "print '[','X='\n",
+ "print ' -2*x2 - 3*x4'\n",
+ "print ' x2'\n",
+ "print ' -4*x4'\n",
+ "print ' x4'\n",
+ "print ' 0'\n",
+ "print 'where x2 and x4 are arbitrary',']'\n",
+ "print '-----------------------------------------'\n",
+ "#part g\n",
+ "print 'Let x2 = 1,x4 = 0 then the given column forms a basis of V'\n",
+ "\n",
+ "x2 = 1#\n",
+ "x4 = 0#\n",
+ "print [[-2*x2-3*x4],[ x2],[ -4*x4],[ x4],[ 0]]\n",
+ "print 'Similarly,if x2 = 0,x4 = 1 then the given column forms a basis of V'\n",
+ "x2 = 0#\n",
+ "x4 = 1#\n",
+ "print [[-2*x2-3*x4],[ x2],[ -4*x4],[ x4],[ 0]]\n",
+ "print '-----------------------------------------'\n",
+ "#part h\n",
+ "print 'The equation AX = Y has solutions X if and only if'\n",
+ "print '-y1 + y2 + y3 = 0'\n",
+ "print '-3*y1 + y2 + y4 -y5 = 0'\n",
+ "print 'where, Y = (y1 y2 y3 y4 y5)'"
+ ]
+ }
+ ],
+ "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
+}