Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Find largest eigenvalue

36 views
Skip to first unread message

the...@hotmail.com

unread,
Feb 27, 2017, 7:51:05 PM2/27/17
to
Hi all,

I am trying to find the largest eigenvalue from a set of eigenvalues. These eigenvalues exist in a textfile that is generated by the following code which works fine:

#include <iostream>
#include <fstream>
#include "lapacke.h"

using namespace std;

int main(){
// list the declarations
int matrix_layout = LAPACK_COL_MAJOR; // store arrays in column major
char jobz = 'N'; // computes eigenvalues only
char uplo = 'U'; // stores the upper triangle of a in an array a
lapack_int n = 10; // the order of the matrix a
float a[n][n] = {
{1, 11, 7, 9, 7, 11, 7, 9, 2, 11},
{11, 4, 10, 10, 6, 2, 9, 6, 10, 0},
{7, 10, 3, 5, 4, 4, 4, 4, 6, 10,},
{9, 10, 5, 3, 8, 8, 3, 5, 1, 8,},
{7, 6, 4, 8, 8, 10, 5, 6, 10, 0},
{11, 2, 4, 8, 10, 9, 4, 3, 5, 11},
{7, 9, 4, 3, 5, 4, 3, 10, 7, 2},
{9, 6, 4, 5, 6, 3, 10, 11, 1, 7},
{2, 10, 6, 1, 10, 5, 7, 1, 10, 5},
{11, 0, 10, 8, 0, 11, 2, 7, 5, 1}
}; // the symmetric matrix a for which the eigenvalues will be found
lapack_int lda = n; // the leading dimension of the array a
float w[n]; // stores the eigenvalues
string outfilename = "eigenvaluesA.txt"; // name of the file with the output data

// open output file
ofstream fout(outfilename);

// compute the eigenvalues; function returns 0 if successful
if (LAPACKE_ssyev(matrix_layout, jobz, uplo, n, &(a[0][0]), lda, w) == 0){
int i;
for (i = 0; i < n; i++)
// print the eigenvalues
fout << w[i] << "\n";
};

// close file
fout.close();
cout << "Results written to " << outfilename << endl;

return 0;
};



There is nothing wrong with this code and it outputs the eigenvalues from the 10x10 matrix I wrote above. The textfile contains 10 eigenvalues, ranging from negative to positive. I am trying to find the largest of them by reading in these eigenvalues. This is part of an assignment, which requests that I read the eigenvalues in and determine the largest of them using the power method. This is what I have so far:



#include <iostream>
#include <fstream>
#include "lapacke.h"

using namespace std;

// list external variables
extern lapack_int n;
extern string outfilename;

int main(){
// open output file
ifstream fin(outfilename);

// read the eigenvalues from the output file
float w;
while (fin >> w){
// create a variable which will become the largest eigenvalue
float wLargest = w;
// compare each eigenvalue to the temporary variable; if the
// eigenvalue is larger than the temporary variable, replace
// the value of the temporary variable with the eigenvalue
if (w > wLargest)
wLargest = w;
};

// print largest eigenvalue
cout << "Largest eigenvalue is " << wLargest << endl;

// close file
fin.close();

return 0;
};



I have the following problems:

For some reason, outfilename is not recognized even though I specified it as an extern.

Second, the while loop isn't actually doing what I want. I want to set the wLargest initially to the first value that is read from the textfile. I only want to do this once. Next I will compare wLargest, and if the w read from the file is larger than wLargest, wLargest will be replaced by that value. Then, a new w is read. But the problem with the above is that when a new w is read, it replaces the w that wLargest was equal to from the previous iteration and I don't want that. But I don't know how to fix it.

Any help would be much appreciated.

Louis Krupp

unread,
Feb 27, 2017, 11:44:39 PM2/27/17
to
This variable 'n' has no connection with the variable 'n' defined in
the previous program.

>extern string outfilename;

Likewise for 'outfilename'.

>
>int main(){
> // open output file
> ifstream fin(outfilename);
>
> // read the eigenvalues from the output file
> float w;
> while (fin >> w){
> // create a variable which will become the largest eigenvalue
> float wLargest = w;

You say you want to set wLargest to the first value read, but you're
actually setting it for *every* value you read.

There are at least a couple of ways to fix this, and they all involve
declaring wLargest somewhere other than inside the loop. Suppose, for
example, you were computing the sum of the values you were reading.
Would you declared the sum variable inside the loop, or would you put
it somewhere else?

> // compare each eigenvalue to the temporary variable; if the
> // eigenvalue is larger than the temporary variable, replace
> // the value of the temporary variable with the eigenvalue
> if (w > wLargest)
> wLargest = w;
> };
>
> // print largest eigenvalue
> cout << "Largest eigenvalue is " << wLargest << endl;
>
> // close file
> fin.close();
>
>return 0;
>};
>
>
>
>I have the following problems:
>
>For some reason, outfilename is not recognized even though I specified it as an extern.
>
>Second, the while loop isn't actually doing what I want. I want to set the wLargest initially to the first value that is read from the textfile. I only want to do this once. Next I will compare wLargest, and if the w read from the file is larger than wLargest, wLargest will be replaced by that value. Then, a new w is read. But the problem with the above is that when a new w is read, it replaces the w that wLargest was equal to from the previous iteration and I don't want that. But I don't know how to fix it.
>
>Any help would be much appreciated.

I remember learning to program. It was fun.

Louis

Manfred

unread,
Feb 28, 2017, 5:30:10 PM2/28/17
to
On 02/28/2017 05:44 AM, Louis Krupp wrote:
>
>> >
>> >int main(){
>> > // open output file
>> > ifstream fin(outfilename);
>> >
>> > // read the eigenvalues from the output file
>> > float w;
>> > while (fin >> w){
>> > // create a variable which will become the largest eigenvalue
>> > float wLargest = w;
> You say you want to set wLargest to the first value read, but you're
> actually setting it for *every* value you read.
>
> There are at least a couple of ways to fix this, and they all involve
> declaring wLargest somewhere other than inside the loop.
Well, there's at least one way that can work with wLargest declared as
it is, but with a different storage duration (storage class specifier).
This would make use of the fact that this function (main) will be
executed exactly once.
0 new messages