Software Assignment
Software Assignment
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
14
19
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
45
55
62
74
2
81
87
94
117
128
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
148
151
157 return 0;
158 }
Listing 1: QR Algorithm Implementation in C
• 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.
4
1.4 Output of the Program
When the above code is run, the following results are printed:
• Verification steps (if required, can include Q and R matrices at each step).