# Chapter 3 - Linear transformation¶

## Page 70 Example 3.6¶

In :
from numpy import array
a1 = [1, 2]#
a2 = [3 ,4]#
print 'a1 = ',a1
print 'a2 = ',a2
print 'a1 and a2 are linearly independent and hence form a basis for R**2'
print 'According to theorem 1, there is a linear transformation from R**2 to R**3 with the transformation functions as:'
Ta1 = [3 ,2 ,1]#
Ta2 = [6, 5, 4]#
print 'Ta1 = ',Ta1
print 'Ta2 = ',Ta2
print 'Now, we find scalars c1 and c2 for that we know T(c1a1 + c2a2) = c1(Ta1) + c2(Ta2))'
print 'if(1,0) = c1(1,2) + c2(3,4), then '
#c = inv([a1#a2]') * [1#0]#
c=array([a1,a2]).dot(array([,]))
c1 = c[0,0]
c2 = c[1,0]
print 'c1 = ',c1
print 'c2 = ',c2
print 'The transformation function T(1,0) will be:'
T = c1*Ta1 + c2*Ta2#
print 'T(1,0) = ',T
#end

a1 =  [1, 2]
a2 =  [3, 4]
a1 and a2 are linearly independent and hence form a basis for R**2
According to theorem 1, there is a linear transformation from R**2 to R**3 with the transformation functions as:
Ta1 =  [3, 2, 1]
Ta2 =  [6, 5, 4]
Now, we find scalars c1 and c2 for that we know T(c1a1 + c2a2) = c1(Ta1) + c2(Ta2))
if(1,0) = c1(1,2) + c2(3,4), then
c1 =  1
c2 =  3
The transformation function T(1,0) will be:
T(1,0) =  [3, 2, 1, 6, 5, 4, 6, 5, 4, 6, 5, 4]


## Page 81 Example 3.12¶

In :
from numpy import array,random
#x = round(rand(1,2) * 10)#
x1 = random.randint(1,9)
x2 = random.randint(1,9)
T = [x1+x2 ,x1]
print 'x1 = ',x1
print 'x2 = ',x2
print 'T(%d,%d) = '%(x1,x2),
print T
print 'If, T(x1,x2) = 0, then'
print 'x1 = x2 = 0'
print 'So, T is non-singular'
print 'z1,z2 are two scalars in F'

z1 = random.randint(0,9)
z2 = random.randint(0,9)
print 'z1 = ',z1
print 'z2 = ',z2
x1 = z2#
x2 = z1 - z2#
print 'So, x1 = ',x1
print 'x2 = ',x2
print 'Hence, T is onto.'
Tinv = [z2, z1-z2]#
print 'inverse(T) = ',Tinv

x1 =  3
x2 =  8
T(3,8) =  [11, 3]
If, T(x1,x2) = 0, then
x1 = x2 = 0
So, T is non-singular
z1,z2 are two scalars in F
z1 =  0
z2 =  4
So, x1 =  4
x2 =  -4
Hence, T is onto.
inverse(T) =  [4, -4]


## Page 89 Example 3.14¶

In :
print 'T is a linear operator on F**2 defined as:'
print 'T(x1,x2) = (x1,0)'
print 'B = {e1,e2} is a standard ordered basis for F**2,then'
x1 = 1#
x2 = 0#
Te1 = [x1, 0]#
x1 = 0#
x2 = 1#
Te2 = [x1 ,0]#
print 'So, Te1 = T(1,0) = ',Te1
print 'So, Te2 = T(0,1) = ',Te2
print 'so,matrix T in ordered basis B is: '
T = [Te1,Te2]
print 'T = \n',T

T is a linear operator on F**2 defined as:
T(x1,x2) = (x1,0)
B = {e1,e2} is a standard ordered basis for F**2,then
So, Te1 = T(1,0) =  [1, 0]
So, Te2 = T(0,1) =  [0, 0]
so,matrix T in ordered basis B is:
T =
[[1, 0], [0, 0]]


## Page 89 Example 3.15¶

In :
from numpy import array,zeros
from sympy import Symbol,diff
print 'Differentiation operator D is defined as:'
D = zeros([4,4])
x=Symbol('x')
for i in range(1,5):
t= i-1#
f = diff(x**t,'x')
print '(Df%d)(x) = '%(i),
print f
if  not(i == 1):
D[i-2,i-1] =  i-1#

print 'Matrix of D in ordered basis is:'
print '[D] = \n',D

Differentiation operator D is defined as:
(Df1)(x) =  0
(Df2)(x) =  1
(Df3)(x) =  2*x
(Df4)(x) =  3*x**2
Matrix of D in ordered basis is:
[D] =
[[ 0.  1.  0.  0.]
[ 0.  0.  2.  0.]
[ 0.  0.  0.  3.]
[ 0.  0.  0.  0.]]


## Page 92 Example 3.16¶

In :
from numpy import array,transpose,linalg
print 'T is a linear operator on R**2 defined as T(x1,x2) = (x1,0)'
print 'So, the matrix T in standard ordered basis B = {e1,e2} is '
T = array([[1, 0],[0, 0]])
print '[T]B = ',T
print 'Let B'' is the ordered basis for R**2 consisting of vectors:'
E1 = array([1, 1])
E2 = array([2 ,1])
print 'E1 = ',E1
print 'E2 = ',E2
P = transpose(([E1,E2]))
print 'So, matrix P = \n',P
Pinv=linalg.inv(P)
print 'P inverse = \n',Pinv
T1 = Pinv*T*P#
print 'So, matrix T in ordered basis B'' is [T]B'' = \n',T1

T is a linear operator on R**2 defined as T(x1,x2) = (x1,0)
So, the matrix T in standard ordered basis B = {e1,e2} is
[T]B =  [[1 0]
[0 0]]
Let B is the ordered basis for R**2 consisting of vectors:
E1 =  [1 1]
E2 =  [2 1]
So, matrix P =
[[1 2]
[1 1]]
P inverse =
[[-1.  2.]
[ 1. -1.]]
So, matrix T in ordered basis B is [T]B =
[[-1.  0.]
[ 0. -0.]]


## Page 93 Example 3.17¶

In :
from sympy import Symbol, Matrix
t = Symbol("t")
print 'g1 = f1'
print 'g2 = t*f1 + f2'
print 'g3 = t**2*f1 + 2*t*f2 + f3'
print 'g4 = t**3*f1 + 3*t**2*f2 + 3*t*f3 + f4'
P = Matrix(([1, t, t**2, t**3],[0 ,1 ,2*t, 3*t**2],[0, 0, 1, 3*t],[0, 0, 0, 1]))
print 'P = \n',P

print 'inverse P = \n',Matrix.inv(P)

print 'Matrix of differentiation operator D in ordered basis B is:'# #As found in example 15
D = Matrix(([0, 1, 0, 0],[0, 0, 2, 0],[0, 0, 0, 3],[0, 0, 0, 0]))
print 'D = \n',D
print 'Matrix of D in ordered basis B'' is:'
print 'inverse(P) * D * P = ',Matrix.inv(P)*D*P

g1 = f1
g2 = t*f1 + f2
g3 = t**2*f1 + 2*t*f2 + f3
g4 = t**3*f1 + 3*t**2*f2 + 3*t*f3 + f4
P =
Matrix([[1, t, t**2, t**3], [0, 1, 2*t, 3*t**2], [0, 0, 1, 3*t], [0, 0, 0, 1]])
inverse P =
Matrix([[1, -t, t**2, -t**3], [0, 1, -2*t, 3*t**2], [0, 0, 1, -3*t], [0, 0, 0, 1]])
Matrix of differentiation operator D in ordered basis B is:
D =
Matrix([[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 3], [0, 0, 0, 0]])
Matrix of D in ordered basis B is:
inverse(P) * D * P =  Matrix([[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 3], [0, 0, 0, 0]])


## Page 98 Example 3.19¶

In :
from numpy import array,random
def trace_matrix(M,n):
tr=0
for i in range(0,n):
tr = tr + M[i,i]#
return tr
#n = round(rand() * 10 + 2)#
n=random.randint(1,9)
print 'n = ',n
#A = round(rand(n,n) * 10)#
A=random.rand(n,n)
for x in range(0,n):
for y in range(0,n):
A[x,y]=round(A[x,y]*10)
print 'A = \n',A

tr = 0#
print 'Trace of A:'
tr1 = trace_matrix(A,n)#
print 'tr(A) = ',tr1
print '--------------------------------'
#c = round(rand() * 10 + 2)#
c=random.randint(2,9)
print 'c = ',c

B=random.rand(n,n)
for x in range(0,n):
for y in range(0,n):
B[x,y]=round(B[x,y]*10)
print 'B = \n',B

print 'Trace of B:'
tr2 = trace_matrix(B,n)#
print 'tr(B) = ',tr2
print 'tr(cA + B) = ',(c*tr1+tr2)

n =  5
A =
[[  5.   9.   7.   8.   6.]
[  3.   3.   2.   7.   6.]
[  5.   1.   3.   2.   6.]
[  1.   2.  10.   4.   1.]
[  9.   0.   7.   5.   7.]]
Trace of A:
tr(A) =  22.0
--------------------------------
c =  2
B =
[[  6.   8.   8.   2.   4.]
[  7.   6.   4.   3.   7.]
[  6.   9.   8.   4.   8.]
[  1.   4.   8.   4.   6.]
[ 10.   8.   2.   1.   6.]]
Trace of B:
tr(B) =  30.0
tr(cA + B) =  74.0


## Page 103 Example 3.23¶

In :
from numpy import array
print 'Matrix represented by given linear functionals on R**4:'
A = array([[1, 2 ,2 ,1],[0, 2, 0, 1],[-2 ,0 ,-4, 3]])
print 'A = \n',A
T = A              #Temporary matrix to store A
print 'To find Row reduced echelon matrix of A given by R:'
print 'Applying row transformations on A,we get'
print 'R1 = R1-R2'
A[0,:] = A[0,:] - A[1,:]
print 'A = \n',A
print 'R3 = R3 + 2*R1'
A[2,:] = A[2,:] + 2*A[0,:]
print 'A = \n',A
print 'R3 = R3/3'
A[2,:] = 1./3*A[2,:]#
print 'A = \n',A
print 'R2 = R2/2'
A[1,:] = 1./2*A[1,:]
print 'A = \n',A
print 'R2 = R2 - 1/2*R3'
A[1,:] = A[1,:] - 1./2*A[2,:]
print 'A = \n',A
R = A#
A = T#
print 'Row reduced echelon matrix of A is:'
print 'R = \n',R
print 'Therefore,linear functionals g1,g2,g3 span the same subspace of (R**4)* as f1,f2,f3 are given by:'
print 'g1(x1,x2,x3,x4) = x1 + 2*x3'
print 'g1(x1,x2,x3,x4) = x2'
print 'g1(x1,x2,x3,x4) = x4'
print 'The subspace consists of the vectors with'
print 'x1 = -2*x3'
print 'x2 = x4 = 0'

Matrix represented by given linear functionals on R**4:
A =
[[ 1  2  2  1]
[ 0  2  0  1]
[-2  0 -4  3]]
To find Row reduced echelon matrix of A given by R:
Applying row transformations on A,we get
R1 = R1-R2
A =
[[ 1  0  2  0]
[ 0  2  0  1]
[-2  0 -4  3]]
R3 = R3 + 2*R1
A =
[[1 0 2 0]
[0 2 0 1]
[0 0 0 3]]
R3 = R3/3
A =
[[1 0 2 0]
[0 2 0 1]
[0 0 0 1]]
R2 = R2/2
A =
[[1 0 2 0]
[0 1 0 0]
[0 0 0 1]]
R2 = R2 - 1/2*R3
A =
[[1 0 2 0]
[0 1 0 0]
[0 0 0 1]]
Row reduced echelon matrix of A is:
R =
[[1 0 2 0]
[0 1 0 0]
[0 0 0 1]]
Therefore,linear functionals g1,g2,g3 span the same subspace of (R**4)* as f1,f2,f3 are given by:
g1(x1,x2,x3,x4) = x1 + 2*x3
g1(x1,x2,x3,x4) = x2
g1(x1,x2,x3,x4) = x4
The subspace consists of the vectors with
x1 = -2*x3
x2 = x4 = 0


## Page 104 Example 3.24¶

In :
from numpy import array
print 'W be the subspace of R**5 spanned by vectors:'
a1 = [2, -2, 3 ,4 ,-1]#
a2 = [-1, 1, 2, 5, 2]#
a3 = [0 ,0 ,-1, -2, 3]#
a4 = [1 ,-1, 2, 3 ,0]#
print 'a1 = ',a1
print 'a2 = ',a2
print 'a3 = ',a3
print 'a4 = ',a4
print 'Matrix A by the row vectors a1,a2,a3,a4 will be:'
A = array([a1,a2,a3,a4])
print 'A = \n',A
print 'After Applying row transformations, we get the row reduced echelon matrix R of A'

T = A#                   #Temporary matrix to store A
#R1 = R1 - R4 and R2 = R2 + R4
A[0,:] = A[0,:] - A[3,:]#
A[1,:] = A[1,:] + A[3,:]#
#R2 = R2/2
A[1,:] = 1./2 * A[1,:]#
#R3 = R3 + R2 and R4 = R4 - R1
A[2,:] = A[2,:] + A[1,:]#
A[3,:] = A[3,:] - A[0,:]#
#R3 = R3 - R4
A[2,:] = A[2,:] - A[3,:]#
#R3 = R3/3
A[2,:] = 1./3 * A[2,:]#
#R2 = R2 - R3
A[1,:] = A[1,:] - A[2,:]#
#R2 = R2/2 and R4 = R4 - R2 - R3
A[1,:] = 1./2 * A[1,:]#
A[3,:] = A[3,:] - A[1,:] - A[2,:]#
#R1 = R1 - R2 + R3
A[0,:] = A[0,:] - A[1,:] + A[2,:]#
R = A#
A = T#
print 'R = \n',R
print 'Then we obtain all the linear functionals f by assigning arbitrary values to c2 and c4'
print 'Let c2 = a, c4 = b then c1 = a+b, c3 = -2b, c5 = 0.'
print 'So, W0 consists all linear functionals f of the form'
print 'f(x1,x2,x3,x4,x5) = (a+b)x1 + ax2 -2bx3 + bx4'
print 'Dimension of W0 = 2 and basis {f1,f2} can be found by first taking a = 1, b = 0. Then a = 0,b = 1'

W be the subspace of R**5 spanned by vectors:
a1 =  [2, -2, 3, 4, -1]
a2 =  [-1, 1, 2, 5, 2]
a3 =  [0, 0, -1, -2, 3]
a4 =  [1, -1, 2, 3, 0]
Matrix A by the row vectors a1,a2,a3,a4 will be:
A =
[[ 2 -2  3  4 -1]
[-1  1  2  5  2]
[ 0  0 -1 -2  3]
[ 1 -1  2  3  0]]
After Applying row transformations, we get the row reduced echelon matrix R of A
R =
[[ 1 -1  0 -1  0]
[ 0  0  1  2  0]
[ 0  0  0  0  1]
[ 0  0  0  0  0]]
Then we obtain all the linear functionals f by assigning arbitrary values to c2 and c4
Let c2 = a, c4 = b then c1 = a+b, c3 = -2b, c5 = 0.
So, W0 consists all linear functionals f of the form
f(x1,x2,x3,x4,x5) = (a+b)x1 + ax2 -2bx3 + bx4
Dimension of W0 = 2 and basis {f1,f2} can be found by first taking a = 1, b = 0. Then a = 0,b = 1