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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382

// EigRSvalo - Program for calculating the Eigenvalues ONLY of a N X N real, symmetric matrix.

#include <iostream>
#include <fstream>
#include <cctype>
#include <cmath>
#include <new>
#include <float.h>

using namespace std;

double pythag(double a, double b);
void Echo(unsigned int N, double** matrix);

double pythag(double a, double b)
{  // Returns the square root of (a*a + b*b)
   // without overflow or destructive underflow

   double p, r, s, t, u;

   t = fabs(a);
   u = fabs(b);

   p = ((t >= u) ? t : u);
   if (p > 0){
       r = ((t <= u) ? t : u);
       r /= p;
       r *= r;
       t = 4.0 + r;

       while (t > 4.0){
            s = r/t;
            u = 1.0 + 2.0*s;
            p = u*p;
            t = s/u;
            r *= t*t;
            t = 4.0 + r;
       } // while (t > 4.0);
   } // End if (p > 0)

   return p;
} // End pythag

void Echo(unsigned int N, double** matrix) { //Routine to output a SQUARE Matrix
   cout << "\n";
   for (unsigned int i = 0; i < N; i++) {
       for (unsigned int j = 0; j < N; j++)
           cout << matrix[i][j] << " ";
       cout << "\n";
   } // End for i
   cout << "\n";
   return;
} // End Echo

int main()
{char rflag = 0; //Readiness flag

cout << " EigRSvalo (4 March 2006)\n";
cout << "=========================================================================== \n";
cout << "This program calculates the eigenvalues ONLY of a N X N real,\n";
cout << "symmetric Matrix, A.\n";
cout << "\nThe A Matrix to be input should have been saved beforehand in a file named\n";
cout << "EigSysRS.txt, which should be in the same folder as the EigRSvalo executable.\n";
cout << "The first entry in this file should be N, the size of the N X N matrix.\n";
cout << "The entries for A should follow, with data for row 1 first, then row 2,\n";
cout << "then row 3, etc.\n";
cout << "\nThe data is assumed to be of type double. Variables used within this program\n";
cout << "are type double.\n";
cout << "\nOutput--eigenvalues--is written to the file EigOutRS.txt.\n";

cout << "Note the Error Code output.\n";
cout << "If normal return, ierr = 0. If Error Code > 0,\n";
cout << "it indicates that more than 30 iterations of a subroutine were required to\n";
cout << "determine an eigenvalue. In this case, the subroutine terminated.\n";
cout << "Error Code gives the index of the eigenvalue for which the failure occurred.\n";
cout << "Eigenvalue[1], Eigenvalue[2], . . . Eigenvalue[ErCode - 1] should be correct.\n";
cout << "\nIs everything ready (are you ready to continue?)? If yes, Enter y. \n";
cout << "Otherwise Enter any other key. \n";
cin >> rflag;

if (toupper(rflag) == 'Y') {
double** A = NULL; //Pointer to rows of A Matrix
double* fv1 = NULL; // Temporary vector
double* wr = NULL; // Vector for eigenvalues
int i, ii, ierr = -1, j, k, l, l1, l2, mDim;
double c, c2, c3, dl1, el1, f, g, h, p, r, s, s2, scale, tst1, tst2;

ifstream in("EigSysRS.txt", ios::in);

if (!in) {
    cout << "Cannot open the input file.\n";
    return 0;
}

in >> mDim; //Input the Matrix dimension from the file
if (mDim < 1) {
    cout << "Invalid dimension entered. Program terminated. \n";
    return 0;
}

//Allocate space for pointers to rows of A matrix
if (!(A = new double*[mDim])){
    cout << "Allocation for A failed. \n";
    return 0;
} //End if

// Allocate space for entries of the columns of the A Matrix
for (i = 0; i < mDim; i++) {
    if (!(A[i] = new double[mDim])){//If failure, release memory allocated before ending program
        for (j = (i - 1); j >= 0; j--)
            delete [] A[j];
        delete [] A;
        cout << "Allocation for columns of A failed.\n";
        return 0;
    } //End if !A
}//End for i

// Allocate space for fv1 vector
if (!(fv1 = new double[mDim])){
    for (i = 0; i < mDim; i++) //Release all allocated memory before ending program
        delete [] A[i];
    delete [] A;
    cout << "Allocation for fv1 failed. \n";
    return 0;
} //End if

// Allocate space for wr vector
if (!(wr = new double[mDim])){
    for (i = 0; i < mDim; i++) {
        delete [] A[i];
    } // End for i
    delete [] A;
    delete [] fv1; //Release the memory allocated to fv1 before ending program
    cout << "Allocation for wr failed. \n";
    return 0;
} //End if

for (i = 0; i < mDim; i++){ //Input the A Matrix from the file
    for (j = 0; j < mDim; j++)
        in >> A[i][j];
}//End for i

in.close(); //Close the input file

// Echo(mDim, A);

// ======BEGINNING OF TRED1 ===================================

ii = mDim - 1;
for (i = 0; i < ii; i++){
    wr[i] = A[ii][i];
    A[ii][i] = A[i][i];
}//End for i
wr[ii] = A[ii][ii]; // Take last assignment out of loop

for (i = (mDim - 1); i >= 0; i--){

    l = i - 1;
    scale = h = 0.0;

    if (l < 0){
        fv1[i] = 0.0;
        continue;
    } // End if (l < 0)

    for (j = 0; j <= l; j++)
        scale += fabs(wr[j]);

    if (scale == 0.0){
        for (j = 0; j <= l; j++){
            wr[j] = A[l][j];
            A[l][j] = A[i][j];
            A[i][j] = 0.0;
        }//End for j

        fv1[i] = 0.0;
        continue;
    } // End if (scale == 0.0)

    for (j = 0; j <= l; j++){
        wr[j] /= scale;
        h += wr[j]*wr[j];
    }//End for j

    f = wr[l];
    g = ((f >= 0) ? -sqrt(h) : sqrt(h));
    fv1[i] = g*scale;
    h -= f*g;
    wr[l] = f - g;

    if (l != 0){

        for (j = 0; j <= l; j++)
            fv1[j] = 0.0;

        for (j = 0; j <= l; j++){
            f = wr[j];
            g = fv1[j] + f*A[j][j];
            for (ii = (j + 1); ii <= l; ii++){
                g += wr[ii]*A[ii][j];
                fv1[ii] += f*A[ii][j];
            } // End for ii
            fv1[j] = g;
        }//End for j

        // Form p

        f = 0.0;
        for (j = 0; j <= l; j++){
            fv1[j] /= h;
            f += fv1[j]*wr[j];
        }//End for j

        h = f/(h*2);

        // Form q

        for (j = 0; j <= l; j++)
            fv1[j] -= h*wr[j];

        // Form reduced A

        for (j = 0; j <= l; j++){
            f = wr[j];
            g = fv1[j];

           for (ii = j; ii <= l; ii++)
                A[ii][j] -= f*fv1[ii] + g*wr[ii];

        }//End for j

    } // End if (l != 0)

    for (j = 0; j <= l; j++){
        f = wr[j];
        wr[j] = A[l][j];
        A[l][j] = A[i][j];
        A[i][j] = f*scale;
    }//End for j

}//End for i

// ======END OF TRED1 =========================================

// ======BEGINNING OF TQL1 ===================================

for (i = 1; i < mDim; i++)
    fv1[i - 1] = fv1[i];

fv1[mDim - 1] = tst1 = f = 0.0;

for (l = 0; l < mDim; l++){

    j = 0;
    h = fabs(wr[l]) + fabs(fv1[l]);

    tst1 = ((tst1 < h) ? h : tst1);

   // Look for small sub-diagonal element

    for (k = l; k < mDim; k++){
        tst2 = tst1 + fabs(fv1[k]);
        if (tst2 == tst1) break; // fv1[mDim-1] is always 0, so there is no exit through the bottom of the loop
    }//End for k

    if (k != l){

        do {

            if (j == 30){
                ierr = l;
                break;
            } // End if (j == 30)

            j++;

            // Form shift

            l1 = l + 1;
            l2 = l1 + 1;
            g = wr[l];
            p = (wr[l1] - g)/(2.0*fv1[l]);
            r = pythag(p, 1.0);
            scale = ((p >= 0) ? r : -r); // Use scale as a dummy variable
            scale += p;
            wr[l] = fv1[l]/scale;
            dl1 = wr[l1] = fv1[l]*scale;
            h = g - wr[l];

            for (i = l2; i < mDim; i++)
                wr[i] -= h;

            f += h;

            // q1 transformation

            p = wr[k];
            c2 = c = 1.0;
            el1 = fv1[l1];
            s = 0.0;

            // Look for i = k - 1 until l in steps of -1

            for (i = (k - 1); i >= l; i--){
                c3 = c2;
                c2 = c;
                s2 = s;
                g = c*fv1[i];
                h = c*p;
                r = pythag(p, fv1[i]);
                fv1[i + 1] = s*r;
                s = fv1[i]/r;
                c = p/r;
                p = c*wr[i] - s*g;
                wr[i + 1] = h + s*(c*g + s*wr[i]);
            }//End for i

            p = -s*s2*c3*el1*fv1[l]/dl1;
            fv1[l] = s*p;
            wr[l] = c*p;
            tst2 = tst1 + fabs(fv1[l]);
        } while (tst2 > tst1); // End do-while loop

    } // End if (k != l)

    if (ierr >= 0) //This check required to ensure we break out of for loop too, not just do-while loop
        break;

    p = wr[l] + f;

    // Order eigenvalues

    // For i = l to 1, in steps of -1
    for (i = l; i >= 1; i--){
        if (p >= wr[i - 1])
            break;
        wr[i] = wr[i - 1];
    }//End for i

    wr[i] = p;

}//End for l

// ======END OF TQL1 =========================================

ofstream out("EigOutRS.txt", ios::out);
if (!out) {
    for (i = 0; i < mDim; i++) {
        delete [] A[i]; //Free up memory used by A Matrix before ending program
    }
    delete [] A;
    delete [] fv1; //Release the memory allocated to fv1 before ending program
    delete [] wr;
    cout << "Cannot open the output file. \n";
    return 0;
}

out.precision(DBL_DIG);

out << "ierr = " << (ierr + 1) << "\n";
out << "The eigenvalues are:\n";
out << "\n";
for (i = 0; i < mDim; i++)
    out << wr[i] << " \n";

out << "\n";

out.close();
cout << "\nDone! The solution is in the text file EigOutRS.txt \n";

for (i = 0; i < mDim; i++) {
    delete [] A[i]; //Free up memory used by A Matrix before ending program
}
delete [] A;
delete [] fv1; //Release the memory allocated to fv1 before ending program
delete [] wr;
} //End if rflag = 'Y'
    else cout << "\nNot ready. Try again when ready with information. \n";
cout << "\nEnter any key to continue. \n";
cin >> rflag;
return 0;
} // End main program.