Ask Your Question
1

Calculating eigenvectors in C

asked 2012-01-08 20:47:31 +0200

Dalmo gravatar image

Using Sage's notebook I was able to successfully calculate the eigenvectors of a Stochastic matrix with the method eigenvectors_right(). Now, if possible, I'd like to be able to perform the same kind calculation from a program in C. Does anyone know what framework and function is behind the eigenvectors_right() method? And would I be able to call it directly from a C program?

Thank you,

Dalmo

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2012-01-08 21:08:42 +0200

Shashank gravatar image

I assume you are looking for a C function because the python code is too slow. If that is the case you can use cython to convert the python script to C. In sage you can do that just by adding %cython in the beginning of the notebook. You can also get a library file (.so) which can be called from python. In order to do that just write the code in python with the extention pyx and compile it with following commands.

cython filename.pyx
gcc -O3 -I/usr/include/python2.6/ -I/Library/Python/2.6/site-packages/numpy-2.0.0.dev_f72c605_20110113-py2.6-macosx-10.6-universal.egg/numpy/core/include/ -c -o filename.o filename.c
gcc -dynamiclib -undefined dynamic_lookup -single_module -o filename.so filename.o

for Mac. For Linux use

cython -a filename.pyx
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.5 -o filename.so filename.c

You may find this link useful.

http://www.sagemath.org/doc/developer/coding_in_cython.html

This way the code runs almost as fast as a C code.

edit flag offensive delete link more

Comments

Hi Shashank, thanks for the reply!! In my case I was actually looking for running an self contained C binary that does not need to rely on Sage on the background. The program would often calculate the eigenvectors and use the result to determine the vector space. Knowing what C function is behind eigenvectors_right() would be ideal. I tried using LAPACK but never managed to get it to calculate the eigenvectors correctly, since the matrix is Stochastic and often the diagonal is made of zeros. Sage worked beautifully, thus my question and search for what lies under the hood.

Dalmo gravatar imageDalmo ( 2012-01-08 21:14:46 +0200 )edit

If that is the case have you tried using gsl. I have a good experience with gsl.

Shashank gravatar imageShashank ( 2012-01-08 22:02:12 +0200 )edit
0

answered 2012-01-08 21:54:52 +0200

Volker Braun gravatar image

Not a Sage question, really. Sage uses scipy for eigenvectors over floating-point numbers, which uses lapack under the hood. To use lapack you need solid C knowledge, but its no magic.

edit flag offensive delete link more

Comments

Hi Volker, thanks for chiming in. Your reply had very valuable hints! Moreover, I don't know if you'd be open to the possibility and please forgive me if here is not the best place to bring the subject up, but I could use some help sorting this problem out on a consulting basis. Would you have the availability and be open to work with me on this specific problem? If yes, please give a way to contact you where we could discuss details.

Dalmo gravatar imageDalmo ( 2012-01-09 11:26:39 +0200 )edit

This sounds more like it would be stackoverflow.com material, but in any case you can reach me at vbraun.name@gmail.com

Volker Braun gravatar imageVolker Braun ( 2012-01-10 14:05:07 +0200 )edit
0

answered 2012-01-08 21:44:25 +0200

Dalmo gravatar image

Looking into the source code I found /Applications/Sage-4.7.2-OSX-64bit-10.6.app/Contents/Resources/sage/devel/sage-main/sage/matrix/matrix2.c

and

/Applications/Sage-4.7.2-OSX-64bit-10.6.app/Contents/Resources/sage/devel/sage-main/sage/matrix/matrix2.pyx

both files contain references to eigenvectors_right(), so it may be safe to assume that somehow I can embed one of these header files and the respective library in my C program and compute the eigenvectors for a Stochastic matrix.

Would you know what is the library that goes with matrix2.c? Also, any suggestion in the best way to use it from my C program?

Thank you,

Dalmo

edit flag offensive delete link more

Comments

This is not going to work, you are completely off track her. The C source/headers is autogenerated from Cython and unlikely to be of any use without linking to Python.

Volker Braun gravatar imageVolker Braun ( 2012-01-08 21:56:36 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-01-08 20:47:31 +0200

Seen: 1,237 times

Last updated: Jan 08 '12