Chapter 5 - Vectors & Matrices

Ex5.2 Pg 103

In [2]:
from __future__ import division
from numpy.linalg import det,norm
from numpy import mat, add, dot, multiply, inner
u=mat([[2,3,-4]])
v=mat([[1,-5,8]])
print "u+v = ",add(u,v)
print "5*u = ",multiply(5,u)
print "-v = ",multiply(-1,v)
print "2*u-3*v = ",add(multiply(2,u),multiply(-3,v))
print 'dot product of the two vectors, k = u.v = ',inner(u,v)
l=norm(u)# 
print 'norm or length of the vector u = ',round(l,4)
u+v =  [[ 3 -2  4]]
5*u =  [[ 10  15 -20]]
-v =  [[-1  5 -8]]
2*u-3*v =  [[  1  21 -32]]
dot product of the two vectors, k = u.v =  [[-45]]
norm or length of the vector u =  5.3852

Ex5.3 Pg 104

In [3]:
from __future__ import division
from numpy.linalg import det,norm
from numpy import mat, add, dot, multiply, inner
u=mat([[5],[3],[-4]])
v=mat([[3],[-1],[-2]])
print "2*u-3*v =\n",add(multiply(2,u),multiply(-3,v))
print 'The dot product of the two vectors u and v is:', sum(multiply(u,v))
l=norm(u)#
print 'norm or length of the vector u = ',round(l,4)
2*u-3*v =
[[ 1]
 [ 9]
 [-2]]
The dot product of the two vectors u and v is: [[20]]
norm or length of the vector u =  7.0711

Ex5.5 Pg 105

In [4]:
from __future__ import division
from numpy.linalg import det,norm
from numpy import mat, add, dot, multiply, inner
A=mat([[1,-2,3],[0,4,5]])
B=mat([[4,6,8],[1,-3,-7]])
k=add(A,B)
print 'The addition of the two matrices A and B is:\n',k
m=multiply(3,A)
print '\nThe multiplication of a vector with a scalar is:\n',m
p=add(multiply(2,A),multiply(-3,B))
print "\n2*A-3*B = \n",p
The addition of the two matrices A and B is:
[[ 5  4 11]
 [ 1  1 -2]]

The multiplication of a vector with a scalar is:
[[ 3 -6  9]
 [ 0 12 15]]

2*A-3*B = 
[[-10 -22 -18]
 [ -3  17  31]]

Ex5.6 Pg 106

In [5]:
from __future__ import division
from numpy.linalg import det,norm
from numpy import mat, add, dot, multiply, inner
a=mat([[7,-4,5]])
b=mat([[3,2,-1]])
k=inner(a,b)
print 'product of a and b is : ',k
p=mat([[6,-1,8,3]])
q=mat([[4,-9,-2,5]])
l=inner(p,q)
print 'product of p and q is:',l
product of a and b is :  [[8]]
product of p and q is: [[32]]

Ex5.7 Pg 107

In [6]:
from __future__ import division
from numpy.linalg import det,norm
from numpy import mat, add, dot, multiply, inner
A=mat([[1 ,3],[2, -1]])
B=mat([[2, 0, -4],[5, -2, 6]])
print "A*B = \n", dot(A,B)
A=mat([[1, 2],[3, 4]])
B=mat([[5, 6],[0, -2]])
print "A*B = \n",dot(A,B)
print "B*A = \n", dot(B,A)
print 'matrix mulitplication is not commutative since AB may not be equal to BA'
A*B = 
[[ 17  -6  14]
 [ -1   2 -14]]
A*B = 
[[ 5  2]
 [15 10]]
B*A = 
[[23 34]
 [-6 -8]]
matrix mulitplication is not commutative since AB may not be equal to BA

Ex5.8 Pg 109

In [7]:
from __future__ import division
from numpy.linalg import det,norm
from numpy import mat, add, dot, multiply, inner
from numpy import identity as idt
A=mat([[1, 2],[3, -4]])
A2=dot(A,A)  #multiplying A by itself
A3=dot(A2,A)
f=add(add(multiply(2,A2),multiply(-3,A)),multiply(5,idt(2)))
print 'for the function f(x)=2x**2-3x+5,f(A) is :\n',f
g=add(add(A2,multiply(3,A)),multiply(-10,idt(2)))
print 'for the function g(x)=x**2+3x-10,g(A) is\n',g
for the function f(x)=2x**2-3x+5,f(A) is :
[[ 16. -18.]
 [-27.  61.]]
for the function g(x)=x**2+3x-10,g(A) is
[[ 0.  0.]
 [ 0.  0.]]

Ex5.9 Pg 110

In [8]:
from __future__ import division
from numpy.linalg import det,norm
from numpy import mat, add, dot, multiply, inner
A=mat([[1 ,0 ,2],[2 ,-1, 3],[4, 1, 8]])
B=mat([[-11, 2 ,2],[-4, 0 ,1],[6, -1, -1]])
print "A*b = \n",dot(A,B)
print 'since A*B is identity matrix,A and B are invertible and inverse of each other'
A*b = 
[[1 0 0]
 [0 1 0]
 [0 0 1]]
since A*B is identity matrix,A and B are invertible and inverse of each other

Ex5.10 Pg 111

In [9]:
from __future__ import division
from numpy.linalg import det,norm
from numpy import mat, add, dot, multiply, inner
A=mat([[5 ,4],[2, 3]])
print 'determinant of A',det(A)
B=mat([[2, 1],[-4, 6]])
print 'determinant of B',det(B)
C=mat([[2, 1, 3],[4, 6, -1],[5 ,1 ,0]])
print 'determinant of C',det(C)
determinant of A 7.0
determinant of B 16.0
determinant of C -81.0

Ex5.13 Pg 115

In [10]:
from __future__ import division
from numpy.linalg import det,norm,solve
from numpy import mat, add, dot, multiply, inner,divide


A=mat([[1, 2, 1],[2, 5, -1],[3, -2, -1]])     #left hand side of the system of equations
B=mat([[3] ,[-4] ,[5]])                   #right hand side or the constants in the equations
X=divide(A,B) #   #unique solution for the system of equations
X = solve(A, B)
print "x = ",X[0]
print "y = ",X[1]
print "z = ",X[2]
x =  [[ 2.]]
y =  [[-1.]]
z =  [[ 3.]]

Ex5.14 Pg 116

In [11]:
from __future__ import division
from numpy import mat

A=mat([[1 ,0 ,2],[2, -1, 3],[4, 1, 8]])
A_inv = A**-1
print "Inverse of A = \n", A_inv
Inverse of A = 
[[-11.   2.   2.]
 [ -4.   0.   1.]
 [  6.  -1.  -1.]]