0% found this document useful (0 votes)
9 views

Software Assignment

Uploaded by

jainarankit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Software Assignment

Uploaded by

jainarankit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1 Eigenvalue Computation Using QR Algorithm

1.1 Overview of QR Algorithm


The QR algorithm iteratively computes eigenvalues of a square matrix A by leveraging
the QR decomposition. The process can be summarized as:

1. Compute the QR decomposition of A such that A = Q·R, where Q is an orthogonal


matrix and R is an upper triangular matrix.

2. Update A by recomposing as A′ = R · Q.

3. Repeat the process until A converges to a diagonal matrix, where the diagonal
elements approximate the eigenvalues of A.

1.2 Implementation in C
Below is the implementation of the QR algorithm in C to compute the eigenvalues of a
matrix.
1 # include < stdio .h >
2 # include < math .h >
3 # include < stdlib .h >
4

5 # define MAX_ITER 1000


6 # define TOL 1e -6
7

9 void qr_decomposition ( int n , double A [ n ][ n ] , double Q [ n ][ n ] ,


double R [ n ][ n ]) ;
10 void multiply _matri ces ( int n , double A [ n ][ n ] , double B [ n ][ n ] ,
double C [ n ][ n ]) ;
11 void print_matrix ( int n , double matrix [ n ][ n ]) ;
12 int is_diagonal ( int n , double matrix [ n ][ n ]) ;
13

14

15 void qr_algorithm ( int n , double A [ n ][ n ] , double eigenvalues [ n ]) {


16 double Q [ n ][ n ] , R [ n ][ n ];
17 double Ak [ n ][ n ];
18

19

20 for ( int i = 0; i < n ; i ++) {


21 for ( int j = 0; j < n ; j ++) {
22 Ak [ i ][ j ] = A [ i ][ j ];
23 }
24 }
25

26 for ( int iter = 0; iter < MAX_ITER ; iter ++) {


27

28 qr_decomposition (n , Ak , Q , R ) ;
29

30

1
31 mult iply_m atrice s (n , R , Q , Ak ) ;
32

33

34 if ( is_diagonal (n , Ak ) ) {
35 break ;
36 }
37 }
38

39

40 for ( int i = 0; i < n ; i ++) {


41 eigenvalues [ i ] = Ak [ i ][ i ];
42 }
43 }
44

45

46 void qr_decomposition ( int n , double A [ n ][ n ] , double Q [ n ][ n ] ,


double R [ n ][ n ]) {
47

48 for ( int i = 0; i < n ; i ++) {


49 for ( int j = 0; j < n ; j ++) {
50 Q [ i ][ j ] = 0.0;
51 R [ i ][ j ] = 0.0;
52 }
53 }
54

55

56 for ( int j = 0; j < n ; j ++) {


57

58 for ( int i = 0; i < n ; i ++) {


59 Q [ i ][ j ] = A [ i ][ j ];
60 }
61

62

63 for ( int k = 0; k < j ; k ++) {


64 double dot = 0.0;
65 for ( int i = 0; i < n ; i ++) {
66 dot += Q [ i ][ k ] * Q [ i ][ j ];
67 }
68 R [ k ][ j ] = dot ;
69 for ( int i = 0; i < n ; i ++) {
70 Q [ i ][ j ] -= R [ k ][ j ] * Q [ i ][ k ];
71 }
72 }
73

74

75 double norm = 0.0;


76 for ( int i = 0; i < n ; i ++) {
77 norm += Q [ i ][ j ] * Q [ i ][ j ];
78 }
79 norm = sqrt ( norm ) ;
80

2
81

82 if ( fabs ( norm ) < TOL ) {


83 norm = TOL ;
84 }
85 R [ j ][ j ] = norm ;
86

87

88 for ( int i = 0; i < n ; i ++) {


89 Q [ i ][ j ] /= R [ j ][ j ];
90 }
91 }
92 }
93

94

95 void multiply _matri ces ( int n , double A [ n ][ n ] , double B [ n ][ n ] ,


double C [ n ][ n ]) {
96 for ( int i = 0; i < n ; i ++) {
97 for ( int j = 0; j < n ; j ++) {
98 C [ i ][ j ] = 0.0;
99 for ( int k = 0; k < n ; k ++) {
100 C [ i ][ j ] += A [ i ][ k ] * B [ k ][ j ];
101 }
102 }
103 }
104 }
105

106 int is_diagonal ( int n , double matrix [ n ][ n ]) {


107 for ( int i = 0; i < n ; i ++) {
108 for ( int j = 0; j < n ; j ++) {
109 if ( i != j && fabs ( matrix [ i ][ j ]) > TOL ) {
110 return 0;
111 }
112 }
113 }
114 return 1;
115 }
116

117

118 void print_matrix ( int n , double matrix [ n ][ n ]) {


119 for ( int i = 0; i < n ; i ++) {
120 for ( int j = 0; j < n ; j ++) {
121 printf ( " %8.4 f " , matrix [ i ][ j ]) ;
122 }
123 printf ( " \ n " ) ;
124 }
125 printf ( " \ n " ) ;
126 }
127

128

129 int main () {


130 int n ;

3
131 printf ( " Enter the size of the matrix ( n x n ) : " ) ;
132 scanf ( " % d " , & n ) ;
133

134 double A [ n ][ n ];
135 double eigenvalues [ n ];
136

137 printf ( " Enter the elements of the % d x % d matrix row by row :\
n", n, n);
138 for ( int i = 0; i < n ; i ++) {
139 for ( int j = 0; j < n ; j ++) {
140 printf ( " A [% d ][% d ]: " , i + 1 , j + 1) ;
141 scanf ( " % lf " , & A [ i ][ j ]) ;
142 }
143 }
144

145 printf ( " \ nOriginal matrix A :\ n " ) ;


146 print_matrix (n , A ) ;
147

148

149 qr_algorithm (n , A , eigenvalues ) ;


150

151

152 printf ( " Eigenvalues :\ n " ) ;


153 for ( int i = 0; i < n ; i ++) {
154 printf ( " %8.4 f \ n " , eigenvalues [ i ]) ;
155 }
156

157 return 0;
158 }
Listing 1: QR Algorithm Implementation in C

1.3 Explanation of the Code


• QR Decomposition: The qr decomposition() function implements the Modi-
fied Gram-Schmidt process to decompose A into Q and R.

• Matrix Multiplication: The multiply matrices() function computes the prod-


uct R · Q to update the matrix A iteratively.

• Diagonal Check: The is diagonal() function checks whether the matrix is suf-
ficiently close to a diagonal form by comparing the off-diagonal elements to a tol-
erance value (TOL).

• Iteration: The algorithm iteratively updates A for a maximum of MAX ITER iter-
ations or until the matrix is diagonal.

• Eigenvalues Extraction: Once convergence is achieved, the diagonal elements of


A are extracted as the eigenvalues.

4
1.4 Output of the Program
When the above code is run, the following results are printed:

• The original matrix A.

• The computed eigenvalues.

• Verification steps (if required, can include Q and R matrices at each step).

You might also like