-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat3diagonal.f
111 lines (101 loc) · 2.47 KB
/
mat3diagonal.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
! program to diagonalise a 3x3 matrix
implicit real*4 (a-h,o-z)
parameter (l=50)
dimension a(l,l), u(l,l), ut(l,l), u1(l,l), u2(l,l)
print *, 'enter the dimension of matrix A'
read *, n
print *, 'enter the values in matrix A'
read *, ((a(i,j), j=1,n), i=1,n)
print *, 'matrix A is:'
do i=1,n
write (*,*) (a(i,j), j=1,n)
end do
!****************************************************************
do m=1,n
! ********** to find the highest off-diagonal element **********
temp = a(1,2)
iloc = 1
jloc = 2
do i=1,n
do j=1,n
if (j .ne. i) then
if (a(i,j) .gt. temp) then
alarge = a(i,j)
iloc = i
jloc = j
end if
end if
end do
end do
! print *, 'the highest off-diagonal element is:',alarge
! print *, 'its location is:',iloc,',',jloc
! ******************** to make alarge zero ********************
!********** setting up the U matrix **********
theta = 0.5*atan((2*a(iloc,jloc))/(a(iloc,iloc)-a(jloc,jloc)))
!**************************************************************
do i=1,n
do j=1,n
u(iloc,iloc) = cos(theta)
u(jloc,iloc) = sin(theta)
u(iloc,jloc) = -sin(theta)
u(jloc,jloc) = cos(theta)
if ((i .eq. iloc) .or. (j .eq. jloc)) then
continue
else
if (i .eq. j) then
u(i,i) = 1
else
u(i,j) = 0
end if
end if
end do
end do
!print *, 'U matrix is:'
!do i=1,n
!write (*,*) (u(i,j), j=1,n)
!end do
!**************************************************************
!********** transposing U and storing it in UT **********
do i=1,n
do j=1,n
ut(j,i) = u(i,j)
end do
end do
!print *, 'the transposed matrix UT is:'
!do i=1,n
!write (*,*) (ut(i,j), j=1,n)
!end do
!********** multiply:AxU=U1 **********
do i = 1, n
do j = 1, n
u1(i,j) = 0.0
do k = 1, n
u1(i,j) = u1(i,j) + a(i,k)*u(k,j)
end do
end do
end do
!print *, 'the product of AxU is:'
!do i=1,n
!write (*,*) (u1(i,j), j=1,n)
!end do
!********** multiply:UTxU1=U2 **********
do i = 1, n
do j = 1, n
u2(i,j) = 0.0
do k = 1, n
u2(i,j) = u2(i,j) + ut(i,k)*u1(k,j)
end do
end do
end do
print *, 'the diagonalised matrix(after performing: U"AU) is:'
do i=1,n
write (*,*) (u2(i,j), j=1,n)
end do
! **************** storing U2 in A ****************
do i=1,n
do j=1,n
a(i,j) = u2(i,j)
end do
end do
end do ! end of outer 'm' loop
end