#example 7.1
#inverse of matrix
#page 256
from numpy import matrix
A=matrix([[1,2,3],[0,1,2],[0,0,1]])
A_1=A.I #inverse of matrix
print A_1
#example 7.2
#Factorize by triangulation method
#page 259
from numpy import matrix
#from __future__ import division
A=[[2,3,1],[1,2,3],[3,1,2]]
L=[[1,0,0],[0,1,0],[0,1,0]]
U=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(0,3):
U[0][i]=A[0][i]
L[1][0]=1/U[0][0]
for i in range(0,3):
U[1][i]=A[1][i]-U[0][i]*L[1][0]
L[2][0]=A[2][0]/U[0][0]
L[2][1]=(A[2][1]-(U[0][1]*L[2][0]))/U[1][1]
U[2][2]=A[2][2]-U[0][2]*L[2][0]-U[1][2]*L[2][1]
print "The Matrix A in Triangle form\n \n"
print "Matrix L\n"
print L
print "\n \n"
print "Matrix U\n"
print U
#example 7.3
#Vector Norms
#page 262
import math
A=[[1,2,3],[4,5,6],[7,8,9]]
C=[0,0,0]
s=0
for i in range(0,3):
for j in range(0,3):
s=s+A[j][i]
C[i]=s
s=0
max=C[0]
for x in range(0,3):
if C[i]>max:
max=C[i]
print "||A||1=%d\n" %(max)
for i in range(0,3):
for j in range(0,3):
s=s+A[i][j]*A[i][j]
print "||A||e=%.3f\n" %(math.sqrt(s))
s=0
for i in range(0,3):
for j in range(0,3):
s=s+A[i][j]
C[i]=s
s=0
for x in range(0,3):
if C[i]>max:
max=C[i]
print "||A||~=%d\n" %(max)
#example 7.4
#Gauss Jordan
#page 266
from __future__ import division
A=[[2,1,1,10],[3,2,3,18],[1,4,9,16]] #augmented matrix
for i in range(0,3):
j=i
while A[i][i]==0&j<=3:
for k in range(0,4):
B[0][k]=A[j+1][k]
A[j+1][k]=A[i][k]
A[i][k]=B[0][k]
print A
j=j+1
print A
n=3
while n>=i:
A[i][n]=A[i][n]/A[i][i]
n=n-1
print A
for k in range(0,3):
if k!=i:
l=A[k][i]/A[i][i]
for m in range(i,4):
A[k][m]=A[k][m]-l*A[i][m]
print A
for i in range(0,3):
print "\nx(%i )=%g\n" %(i,A[i][3])
#LU decomposition method
#example 7.8
#page 273
from numpy import matrix
from __future__ import division
A=[[2, 3, 1],[1, 2, 3],[3, 1, 2]]
B=[[9],[6],[8]]
L=[[1,0,0],[0,1,0],[0,0,1]]
U=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(0,3):
U[0][i]=A[0][i]
L[1][0]=1/U[0][0]
for i in range(1,3):
U[1][i]=A[1][i]-U[0][i]*L[1][0]
L[2][0]=A[2][0]/U[0][0]
L[2][1]=(A[2][1]-U[0][1]*L[2][0])/U[1][1]
U[2][2]=A[2][2]-U[0][2]*L[2][0]-U[1][2]*L[2][1]
print "The Matrix A in Triangle form\n \n"
print "Matrix L\n"
print L
print "\n \n"
print "Matrix U\n"
print U
L=matrix([[1,0,0],[0,1,0],[0,0,1]])
U=matrix([[0,0,0],[0,0,0],[0,0,0]])
B=matrix([[9],[6],[8]])
Y=L.I*B
X=matrix([[1.944444],[1.611111],[0.277778]])
print "the values of x=%f,y=%f,z=%f" %(X[0][0],X[1][0],X[2][0])
#ill conditioned linear systems
#example 7.9
#page 276
from numpy import matrix
import math
A=matrix([[2, 1],[2,1.01]])
B=matrix([[2],[2.01]])
X=A.I*B
Ae=0
Ae=math.sqrt(Ae)
inv_A=A.I
invA_e=0
invA_e=math.sqrt(invA_e)
C=A_e*invA_e
k=2
if k<1:
print "the fuction is ill conditioned"
#ill condiioned linear systems
#example 7.10
#page 277
import numpy
from __future__ import division
A=[[1/2, 1/3, 1/4],[1/5, 1/6, 1/7],[1/8,1/9, 1/10]] #hilbert's matrix
de_A=numpy.linalg.det(A)
if de_A<1:
print "A is ill-conditioned"
#ill conditioned linear system
#example 7.11
#page 277
import numpy
import math
A=[[25, 24, 10],[66, 78, 37],[92, -73, -80]]
de_A=numpy.linalg.det(A)
for i in range(0,2):
s=0
for j in range(0,2):
s=s+A[i][j]**2
s=math.sqrt(s)
k=de_A/s
if k<1:
print" the fuction is ill conditioned"
#ill-conditioned system
#example 7.12
#page 278
from numpy import matrix
#the original equations are 2x+y=2 2x+1.01y=2.01
A1=matrix([[2, 1],[2, 1.01]])
C1=matrix([[2],[2.01]])
x1=1
y1=1 # approximate values
A2=matrix([[2, 1],[2, 1.01]])
C2=matrix([[3],[3.01]])
C=C1-C2
X=A1.I*C
x=X[0][0]+x1
y=X[1][0]+y1
print "the exact solution is X=%f \t Y=%f" %(x,y)
#solution of equations by iteration method
#example 7.14
#page 282
#jacobi's method
from numpy import matrix
from __future__ import division
C=matrix([[3.333],[1.5],[1.4]])
X=matrix([[3.333],[1.5],[1.4]])
B=matrix([[0, -0.1667, -0.1667],[-0.25, 0, 0.25],[-0.2, 0.2, 0]])
for i in range(1,11):
X1=C+B*X
print "X%d" %(i)
print X1
X=X1
print "the solution of the equation is converging at 3 1 1\n\n"
#gauss-seidel method
C=matrix([[3.333],[1.5],[1.4]])
X=matrix([[3.333],[1.5],[1.4]])
B=matrix([[0, -0.1667, -0.1667],[-0.25, 0, 0.25],[-0.2, 0.2, 0]])
X1=C+B*X
x=X1[0][0]
y=X1[1][0]
z=X1[2][0]
for i in range(0,5):
x=3.333-0.1667*y-0.1667*z
y=1.5-0.25*x+0.25*z
z=1.4-0.2*x+0.2*y
print "the value after %d iteration is : %f\t %f\t %f\t\n\n" %(i,x,y,z)
print "again we conclude that roots converges at 3 1 1"
#largest eigenvalue and eigenvectors
#example 7.16
#page 286
from numpy import matrix
A=matrix([[1,6,1],[1,2,0],[0,0,3]])
I=matrix([[1],[0],[0]]) #initial eigen vector
X0=A*I
print "X0="
print X0
X1=A*X0
print "X1="
print X1
X2=A*X1
print "X2="
print X2
X3=X2/3
print "X3="
print X3
X4=A*X3
X5=X4/4
print "X5="
print X5
X6=A*X5;
X7=X6/(4*4)
print "X7="
print X7
print "as it can be seen that highest eigen value is 4 \n\n the eigen vector is %d %d %d" %(X7[0],X7[1],X7[2])
#housrholder's method
#example 7.17
#page 290
from numpy import matrix
from __future__ import division
import math
A=[[1, 3, 4],[3, 2, -1],[4, -1, 1]]
print A[1][1]
S=math.sqrt(A[0][1]**2+A[0][2]**2)
v2=math.sqrt((1+A[0][1]/S)/2)
v3=A[0][2]/(2*S)
v3=v3/v2
V=matrix([[0],[v2],[v3]])
P1=matrix([[1, 0, 0],[0, 1-2*v2**2, -2*v2*v3],[0, -2*v2*v3, 1-2*v3**2]])
A1=P1*A*P1
print "the reduced matrix is \n\n"
print A1