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

Lapacke inputs

63 views
Skip to first unread message

the...@hotmail.com

unread,
Feb 23, 2017, 2:35:00 PM2/23/17
to
I am trying to find the eigenvalues of a square symmetric real matrix. I am supposed to use a lapacke routine for that. I have chosen to use the ssyev routine. I am now trying to write a .cc file that will calculate the eigenvalues of this matrix, however I don't understand the inputs for ssyev. I need the following:

jobz
uplo
n
a
lda
w

So far I have chosen the following:

char jobz = 'N'; // computes eigenvalues only
char uplo = 'U'; // stores the upper triangle of a in an array a
int n = 10; // the order of the matrix a
float a[] = [a 10 by 10 matrix which I will not copy-paste here] // the symmetric matrix a for which the eigenvalues will be found
int lda = n; // the leading dimension of the array a
float w = //

I don't know what the w is. Everywhere I looked it describes w as an output. But then I looked up lapack.h which I included at the top of my file and it has the following:

lapack_int LAPACKE_ssyev( int matrix_layout, char jobz, char uplo, lapack_int n, float* a, lapack_int lda, float* w );

so I need to be able to input something for w. But I don't know what that is. Any help would be much appreciated. Thanks!

Scott Lurndal

unread,
Feb 23, 2017, 3:22:53 PM2/23/17
to
the...@hotmail.com writes:
>I am trying to find the eigenvalues of a square symmetric real matrix. I am=
> supposed to use a lapacke routine for that. I have chosen to use the ssyev=
> routine. I am now trying to write a .cc file that will calculate the eigen=
>values of this matrix, however I don't understand the inputs for ssyev. I n=
>eed the following:
>
>jobz
>uplo
>n
>a
>lda
>w
>
>So far I have chosen the following:
>
> char jobz =3D 'N'; // computes eigenvalues only
> char uplo =3D 'U'; // stores the upper triangle of a in an array a
> int n =3D 10; // the order of the matrix a
> float a[] =3D [a 10 by 10 matrix which I will not copy-paste here] // =
>the symmetric matrix a for which the eigenvalues will be found
> int lda =3D n; // the leading dimension of the array a
> float w =3D //
>
>I don't know what the w is. Everywhere I looked it describes w as an output=
>. But then I looked up lapack.h which I included at the top of my file and =
>it has the following:
>
>lapack_int LAPACKE_ssyev( int matrix_layout, char jobz, char uplo, lapack_i=
>nt n, float* a, lapack_int lda, float* w );
>
>so I need to be able to input something for w. But I don't know what that i=
>s. Any help would be much appreciated. Thanks!

LAPACKE_ssyev( ... , &w);

the...@hotmail.com

unread,
Feb 23, 2017, 3:28:04 PM2/23/17
to
@Scott Lurndal
> LAPACKE_ssyev( ... , &w);

Yes, but I write that part after the declarations. So I need to declare something for w, don't I? Otherwise my compiler will complain because w has been unassigned. So shouldn't I give it a value? I don't quite understand what it is. A document I read described it as all the eigenvalues that are outputted, so that means it shouldn't be an input but rather an output. So why it exists inside LAPACKE_ssyev( ... , &w); I don't understand.

Sorry, I'm very new to programming. I appreciate your time.

Ben Bacarisse

unread,
Feb 23, 2017, 3:52:31 PM2/23/17
to
w is a pointer to where the n eigenvalues are to be put. It's what is
sometimes called an "out" parameter. You don't need to give any values,
just enough space for the results to be recorded.

So, a is 10x10 which means w should be 10 floats:

float w[10];

Because of the way arrays work in C (which C++ inherited) the name of
an array will be converted to a pointer to its first element which is
exactly what is needed here:

LAPACKE_ssyev(..., w);

--
Ben.

Scott Lurndal

unread,
Feb 24, 2017, 8:41:23 AM2/24/17
to
the...@hotmail.com writes:
>@Scott Lurndal
>> LAPACKE_ssyev( ... , &w);
>
>Yes, but I write that part after the declarations. So I need to declare som=
>ething for w, don't I? Otherwise my compiler will complain because w has be=
>en unassigned. So shouldn't I give it a value? I don't quite understand wha=
>t it is. A document I read described it as all the eigenvalues that are out=
>putted, so that means it shouldn't be an input but rather an output. So why=
> it exists inside LAPACKE_ssyev( ... , &w); I don't understand.
>
>Sorry, I'm very new to programming. I appreciate your time.

It seems evident that the W parameter is an output parameter. Since the
function will be storing a value indirectly through the pointer,
you don't need to initialize the variable before passing its address
to the function.

the...@hotmail.com

unread,
Feb 24, 2017, 12:49:51 PM2/24/17
to
Thank you Ben and Scott for your help. It seems that my problems are not over however because now I'm having trouble printing my eigenvalues to a text file. Here is my code:

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

using namespace std;

int main(){
char jobz = 'N'; // computes eigenvalues only
char uplo = 'U'; // stores the upper triangle of a in an array a
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
int lda = n; // the leading dimension of the array a
float w[n]; // stores the eigenvalues
}; // end of declarations

ofstream fout("eigenvaluesA.txt");
fout << "eigenvalues of matrix A" << endl;
LAPACKE_ssyev(&jobz, &uplo, &n, a, &lda, &w);
fout.close();


I keep getting the following errors:
'fout' does not name a type. How can it not name a type if I specified the namespace std thing at the beginning? I also get the error
expected constructor, destructor, or type conversion before '(' token
LAPACKE_ssyev(&jobz, &uplo, &n, a, &lda, &w);
However I am following several articles right now with examples that all list Lapacke and its inputs as I did above. so I'm not sure what is wrong with that line either. Any input is much appreciated.

Scott Lurndal

unread,
Feb 24, 2017, 1:20:13 PM2/24/17
to
put your code _inside_ main, not outside of it.

Ben Bacarisse

unread,
Feb 24, 2017, 3:09:32 PM2/24/17
to
the...@hotmail.com writes:
I'll make a few more remarks...

> ... Here is my code:
>
> #include <iostream>
> #include <fstream>
> #include <string>
> #include "lapacke.h"
>
> using namespace std;
>
> int main(){
> char jobz = 'N'; // computes eigenvalues only
> char uplo = 'U'; // stores the upper triangle of a in an array a
> 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

C++ does not permit this sort of array. The size of a plain array must
be a compile-time constant and, despite is being obvious to you, n is
not a compile-time constant. Writing

float a[10][10]

would be fine, as would

float a[][] = { { ... }, { ... }, ... { ... } };

where the compiler will deduce that the size is 10x10.

> int lda = n; // the leading dimension of the array a
> float w[n]; // stores the eigenvalues
> }; // end of declarations
>
> ofstream fout("eigenvaluesA.txt");
> fout << "eigenvalues of matrix A" << endl;
> LAPACKE_ssyev(&jobz, &uplo, &n, a, &lda, &w);
> fout.close();

This needs to go into main. But then you will find other problems.
Firstly, you are not passing enough arguments to LAPACKE_ssyev and the
ones you are passing are of the wrong type. In general, you won't need
any of those &s. In fact, you don't need many of those variables -- you
can pass 'N' and 'U' directly rather than by putting them into jobz and
uplo.

<snip>
--
Ben.
Message has been deleted

the...@hotmail.com

unread,
Feb 25, 2017, 2:49:53 PM2/25/17
to
You were both right. I went back and made a lot of changes. Here is the updated code:

#include <iostream>
#include <fstream>
#include <string>
#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[10][10] = {
{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 = 10; // the leading dimension of the array a
float w[10]; // stores the eigenvalues

// print the eigenvalues
ofstream fout("eigenvaluesA.txt"); // title of text output
lapack_int LAPACKE_ssyev(int matrix_layout, char jobz, char uplo,
lapack_int n, float* a, lapack_int lda, float* w); // compute the eigenvalues
int i;
for (i = 0; i < n; i++);
fout << w[i] << endl; // record the eigenvalues
fout.close();

return 0;
};

I included the LAPACKE_ssyev part in the main, and I also fixed the printing part since I realized I wasn't actually printing my eigenvalues, I was printing some junk. I'm not getting any compiler errors now but my text file gives me the number "6.61179e+24" and nothing else. I'm not sure why that's happening.

Christian Gollwitzer

unread,
Feb 25, 2017, 3:24:54 PM2/25/17
to
Am 25.02.17 um 20:49 schrieb the...@hotmail.com:

> lapack_int LAPACKE_ssyev(int matrix_layout, char jobz, char uplo,
> lapack_int n, float* a, lapack_int lda, float* w); // compute the eigenvalues

This does not compute anything, instead it declares the function
LAPACKE_ssyev (i.e. tells the compiler that the function exists). Leave
out all the types and it should at least be closer to what you need.


> I included the LAPACKE_ssyev part in the main, and I also fixed the
> printing part since I realized I wasn't actually printing my
> eigenvalues, I was printing some junk. I'm not getting any compiler
> errors now but my text file gives me the number "6.61179e+24" and
> nothing else.


Not surprising - since you never actually called the Lapack function you
are printing uninitialized memory as float, which usually gives lots of
junk numbers or zero values.

Christiah

Ben Bacarisse

unread,
Feb 25, 2017, 3:36:49 PM2/25/17
to
the...@hotmail.com writes:

> Ok I considered what you both said and changed some things around.

You've fixed a couple of things and broken a couple of things that were
right before. In general, you can't write a program by trying things
out to see what happens!

The trouble is I don't know how much you want to learn about programming
or C++ or even if this is come course exercise. I'm going to point out
some things about your code and then show you a working example. If you
don't want to see a working example, just don't scroll down at the end
of this post.

> #include <iostream>
> #include <fstream>
> #include <string>

You don't need <string>

> #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

If you make this const you would be able to use it in the two array
declarations. It's generally better not to keep repeating numbers like
10, but to give them a name and use that instead.

> float* a[10][10] = {
> {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

This is the source of the error you report below. You have declared the
array a to be of pointers and you have then tried to put 100 number into
it. Remove the *.

> lapack_int lda = 10; // the leading dimension of the array a
> float* w[10]; // stores the eigenvalues

Remove the *. You want and array of floats.

> // print the eigenvalues
> ofstream fout("eigenvaluesA.txt"); // title of text output
> lapack_int LAPACKE_ssyev(int matrix_layout, char jobz, char uplo,
> lapack_int n, float* a, lapack_int lda, float* w); // compute the eigenvalues

This is not a function call. The call does not have the types, but
instead lists the values to be passed. And (as you will see if you look
at my example) you don't have to name every single one of them.

> int i;
> for (i = 0; i < n; i++);
> fout << w[i] << endl; // record the eigenvalues
> fout.close();
>
> return 0;
> };
>
>
> I realized that I wasn't actually printing my eigenvalues. I was
> printing...a statement. The error I get now is "error: invalid
> conversion from 'int' to 'float*' [-fpermissive]" which I haven't been
> able to fix. Is that because float shouldn't be a pointer?

Spoiler alert: here is a working version (after the dots).
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
#include <iostream>
#include "lapacke.h"

int main(){
const lapack_int n = 10;
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}
};
float w[n];

if (LAPACKE_ssyev(LAPACK_COL_MAJOR, 'N', 'U', n, a[0], n, w) == 0) {
for (int i = 0; i < n; i++)
std::cout << w[i] << " ";
std::cout << "\n";
}
}


--
Ben.

the...@hotmail.com

unread,
Feb 25, 2017, 3:37:21 PM2/25/17
to
> This does not compute anything, instead it declares the function
> LAPACKE_ssyev (i.e. tells the compiler that the function exists). Leave
> out all the types and it should at least be closer to what you need.
>
I've already tried that before though and I kept getting errors. I end up getting "expression list treated as compound expression in initializer [-fpermissive]" and "invalid conversion from 'float*' to 'int' [-fpermissive]".
>
>
> Not surprising - since you never actually called the Lapack function you
> are printing uninitialized memory as float, which usually gives lots of
> junk numbers or zero values.
>
I'm not sure how I'm supposed to call it? I'm following someone's blog (except they are solving a system of linear equations, not calculating eigenvalues) and after they write the lapacke part they have a loop and that's it.

the...@hotmail.com

unread,
Feb 25, 2017, 4:01:38 PM2/25/17
to
> You've fixed a couple of things and broken a couple of things that were
> right before. In general, you can't write a program by trying things
> out to see what happens!

Sorry, I realized I misinterpreted the first post you wrote.

>
> The trouble is I don't know how much you want to learn about programming
> or C++ or even if this is come course exercise. I'm going to point out
> some things about your code and then show you a working example. If you
> don't want to see a working example, just don't scroll down at the end
> of this post.
>

I want to learn how to use c++ properly and to understand why I am doing the things I'm doing. Yes, this is for a course assignment, sorry if that makes it not allowed to post. I had been working on this assignment for 2 days and I have a week to do it. The course lecture notes I have found very unhelpful for my learning because they are mostly just copy paste of someone's uncommented code. I have been using the web to learn about lapack and how to use it but not everything out there answers my questions.

> Remove the *. You want and array of floats.

I realized the issue with the pointers yes, so I had fixed them. But thanks for the confirmation!

I just want to explain a little my layout of my code. I realize that to a c++ expert it looks clunky. But I have had marks taken off for insufficient comments so that's why I defined everything explicitly rather than passing them to the lapacke function as you did- because it gives me an opportunity to add extra comments and define everything in an obvious way to prevent marks from being removed.

So supposing if I wanted to define everything at the top explicitly, I would need to then include types and variable names in the lapacke function, is that correct? Because I noticed that when I did that, I didn't get an error. Also could you please explain why the lapacke function in your example ==0?

Thank you for your help!


Christian Gollwitzer

unread,
Feb 25, 2017, 4:14:36 PM2/25/17
to
Am 25.02.17 um 22:01 schrieb the...@hotmail.com:
> I just want to explain a little my layout of my code. I realize that
> to a c++ expert it looks clunky. But I have had marks taken off for
> insufficient comments so that's why I defined everything explicitly
> rather than passing them to the lapacke function as you did- because
> it gives me an opportunity to add extra comments and define
> everything in an obvious way to prevent marks from being removed.

That is generally good advice. Especially with the obscure LAPACK
functions - they come from old FORTRAN code, which is very different
from C - it is hard to understand what the parameters are supposed to
do. Naming and commenting them makes it much easier, stick with that.

> Also could you please explain why the lapacke
> function in your example ==0?

The lapack function returns 0 if it was successful, so this tests if the
functino could compute the eigenvalues. Possible reasons for failure are
listed in the documentation; for example if you set the jobz to 'A'
(only 'N' or 'V' are allowed), then it does not compute anything and
returns -1.

If you are just learning, I want to point out some more subtlety from
Ben's solution. You have "float a[10][10]" and LAPACK expects a pointer
to the first element in this array "float *a". Ben passes

a[0]

which is correct, but obscure for a non-guru. Instead, you can write

&(a[0][0])

which means take the address of the element at 0,0 (the first element).

Christian


the...@hotmail.com

unread,
Feb 25, 2017, 5:23:08 PM2/25/17
to
Thanks so much for the explanations Christian, that cleared some things up!

I seem to be stuck again with an error. Here is my updated code:


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

using namespace std;

// allocate space for function LAPACKE_ssyev
extern lapack_int LAPACKE_ssyev(int* matrix_layout, char* jobz, char* uplo, const lapack_int* n,
float* a, const lapack_int* lda, float* w);

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
const 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
const lapack_int lda = n; // the leading dimension of the array a
float w[n]; // stores the eigenvalues

// open output file
ofstream fout("eigenvaluesA.txt");

// 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] << endl;
};
// close file
fout.close();

return 0;
};


I keep getting the error
"/tmp/ccA0hQz3.o: In function `main':
eigen.cc:(.text+0x97): undefined reference to `LAPACKE_ssyev'
collect2: error: ld returned 1 exit status"
I looked up what that meant and mostly people are saying its because the function name is not spelled right. But mine is. So then I thought that maybe the compiler doesn't understand what this function is because I haven't allocated space to it beforehand. So I did that at the top, before my main function, but that doesn't seem to help. Some people who have had this problem have solved it by writing extern "C"{ extern function name and inputs}; but I tried that and it didn't work either.

Paavo Helde

unread,
Feb 25, 2017, 6:09:26 PM2/25/17
to
On 26.02.2017 0:22, the...@hotmail.com wrote:
> I keep getting the error
> "/tmp/ccA0hQz3.o: In function `main':
> eigen.cc:(.text+0x97): undefined reference to `LAPACKE_ssyev'
> collect2: error: ld returned 1 exit status"

This is a linker error, not compiler error. This is good since it shows
that your code actually compiled and now you are stuck at the next
level, linking.

Most probably the problem is that you have not instructed your compiler
to actually link with the lapack library. You need to add correct -L and
-l options to your compiler command-line for specifying the directory of
the lapack library and its name, or specify them indirectly via some
menus and dialogs if you are using some kind of IDE.

The details depend a lot on which build system you use. The final
options passed to the linker (via compiler command-line) should look
something like

g++ ...other..options... -L/usr/local/lib -llapack


Ben Bacarisse

unread,
Feb 25, 2017, 6:11:04 PM2/25/17
to
the...@hotmail.com writes:
<snip>

> I just want to explain a little my layout of my code. I realize that
> to a c++ expert it looks clunky. But I have had marks taken off for
> insufficient comments so that's why I defined everything explicitly
> rather than passing them to the lapacke function as you did- because
> it gives me an opportunity to add extra comments and define everything
> in an obvious way to prevent marks from being removed.

Obviously do what you must to get the marks, but I'd like to offer an
alternative to the answer you've already had about this. I don't object
to comments, but I don't think that adding names like 'jobz' and 'uplo'
really helps much at all.

And as for the first argument, the named constant is arguably more
helpful in the call than in a variable:

LAPACKE_ssyev(LAPACK_COL_MAJOR, // hardly needs much comment

whereas

LAPACKE_ssyev(matrix_layout, // OK, but *what* layout is being used?

So, by all means document what you are doing. There is no need to avoid
comments just because you don't use extra variables and, when you do,
you can choose more expressive names than the ones chosen for the
function's prototype:

float eigenvalues[n];
LAPACKE_ssyev(LAPACK_COL_MAJOR,
'N', // compute only the eigenvalues
'U', // store only the upper triangle
n, // the dimension of the square input array
a[0], // the symmetric nxn input array, passed as a
// pointer to the first element (a[0] == &a[0][0])
n, // the leading dimension of the array
eigenvalues // where to store the computed results, passed as
// a pointer to the first element.
);

(My comments may not be good ones -- I don't know the library well.)

> So supposing if I wanted to define everything at the top explicitly, I
> would need to then include types and variable names in the lapacke
> function, is that correct?

I think you asking about the

lapack_int LAPACKE_ssyev(int matrix_layout, char jobz, char uplo,
lapack_int n, float* a, lapack_int lda, float* w);

part that you included. That's a function prototype and it is needed,
but it's already in the "include file" you reference at the start:

#include "lapacke.h"

which will also define all the special types like lapack_int and so on.

> Because I noticed that when I did that, I
> didn't get an error.

But this then makes me unsure that I understood your question about
types are names.

> Also could you please explain why the lapacke
> function in your example ==0?

All of these functions indicate success by returning 0. You should
always check this sort of return value as printing anything from the w
array will be meaningless if LAPACKE_ssyev did not return 0.

--
Ben.

the...@hotmail.com

unread,
Feb 26, 2017, 1:07:39 PM2/26/17
to
@Paavo- yes, you are totally right. I was being careless and wasn't typing the correct commands in.

@Ben- I agree, your code is much more elegant than mine. I might go back and change things later, I'm not sure. I'm just a bit apprehensive because my prof for the course loves any possible reason to take off more marks...

I'm back to the same problem as before- I'm getting rubbish being printed to my textfile. Here is my updated code:


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

using namespace std;

// allocate space for function LAPACKE_ssyev
extern lapack_int LAPACKE_ssyev(int* matrix_layout, char* jobz, char* uplo, const lapack_int* n,
float* a, const lapack_int* lda, float* w);

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
I'm not sure what the issue is with the printing. I also *have* to print my results to a textfile, that's the requirement. It seems to me I'm doing everything right so why isn't it printing? I'm getting "3.54082e-35" and that's it.

Paavo Helde

unread,
Feb 26, 2017, 2:51:57 PM2/26/17
to
On 26.02.2017 20:07, the...@hotmail.com wrote:
> @Paavo- yes, you are totally right. I was being careless and wasn't typing the correct commands in.
>
> @Ben- I agree, your code is much more elegant than mine. I might go back and change things later, I'm not sure. I'm just a bit apprehensive because my prof for the course loves any possible reason to take off more marks...
>
> I'm back to the same problem as before- I'm getting rubbish being printed to my textfile. Here is my updated code:
>
>
> #include <iostream>
> #include <fstream>
> #include "lapacke.h"
>
> using namespace std;
>
> // allocate space for function LAPACKE_ssyev

This comment is wrong, there is no space allocation going on, it's just
a declaration of a function.

> extern lapack_int LAPACKE_ssyev(int* matrix_layout, char* jobz, char* uplo, const lapack_int* n,
> float* a, const lapack_int* lda, float* w);

There is no need to declare lapack library functions like that, such
declarations should come from the included header file (lapacke.h).

Besides, this declaration seems to be all wrong, the correct signature
according to
"http://www.netlib.org/lapack/explore-html/de/dfb/lapacke__ssyev_8c.html" is:

lapack_int LAPACKE_ssyev( int matrix_layout, char jobz, char uplo,
lapack_int n, float* a, lapack_int lda, float* w );

I wonder how you were able to compile your code at all, but in any case
you should remove this invalid declaration.

the...@hotmail.com

unread,
Feb 26, 2017, 3:06:47 PM2/26/17
to
Thanks for the tip Paavo, I got rid of that line. Somehow it had been compiling no problem which is why I hadn't suspected that there was an issue with that line. I also figured out why my results weren't being printed to the textfile- I added an extra ";" after my for loop and so basically the for loop was useless and wasn't doing anything. So now I'm getting my eigenvalues printed to the textfile as expected.

Thanks to all the posters in this thread for your help! I really appreciate it.

Paavo Helde

unread,
Feb 26, 2017, 3:31:58 PM2/26/17
to
On 26.02.2017 22:06, the...@hotmail.com wrote:
> Thanks for the tip Paavo, I got rid of that line. Somehow it had been compiling no problem which is why I hadn't suspected that there was an issue with that line. I also figured out why my results weren't being printed to the textfile- I added an extra ";" after my for loop and so basically the for loop was useless and wasn't doing anything. So now I'm getting my eigenvalues printed to the textfile as expected.

Good to hear!

In some other languages like Perl the for loop content must be always
placed inside braces, I always follow this style and wish it would be
mandatory in C++ as well! With mandated braces this kind of error would
be much easier to spot, and much harder to make in the first place.

Paavo Helde

unread,
Feb 26, 2017, 3:45:10 PM2/26/17
to
On 26.02.2017 22:06, the...@hotmail.com wrote:
> Thanks for the tip Paavo, I got rid of that line. Somehow it had been compiling no problem which is why I hadn't suspected that there was an issue with that line.

Now I understood why your code was compiling with the invalid
declaration. I was expecting errors because the lapack library is a C
library, and in C one cannot overload the same function with different
argument types. Alas, in your declaration there was no 'extern "C"'
specifier which would be actually needed in a C function declaration.
So for the compiler is was just another C++ overload of some function
which you declared but never called, so it was just ignored.



the...@hotmail.com

unread,
Feb 27, 2017, 7:52:33 PM2/27/17
to
I see now, thank you so much for the tips that really cleared things up!
0 new messages