ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 10 Jan 2012 14:05:07 +0100Calculating eigenvectors in Chttps://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/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
Sun, 08 Jan 2012 20:47:31 +0100https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/Answer by Dalmo for <p>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?</p>
<p>Thank you,</p>
<p>Dalmo</p>
https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?answer=13103#post-id-13103Looking 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
Sun, 08 Jan 2012 21:44:25 +0100https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?answer=13103#post-id-13103Comment by Volker Braun for <p>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</p>
<p>and </p>
<p>/Applications/Sage-4.7.2-OSX-64bit-10.6.app/Contents/Resources/sage/devel/sage-main/sage/matrix/matrix2.pyx </p>
<p>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.</p>
<p>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?</p>
<p>Thank you,</p>
<p>Dalmo</p>
https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?comment=20570#post-id-20570This 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.Sun, 08 Jan 2012 21:56:36 +0100https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?comment=20570#post-id-20570Answer by Shashank for <p>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?</p>
<p>Thank you,</p>
<p>Dalmo</p>
https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?answer=13102#post-id-13102I 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](http://www.sagemath.org/doc/developer/coding_in_cython.html)
This way the code runs almost as fast as a C code.Sun, 08 Jan 2012 21:08:42 +0100https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?answer=13102#post-id-13102Comment by Shashank for <p>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.</p>
<pre><code>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
</code></pre>
<p>for Mac. For Linux use</p>
<pre><code>cython -a filename.pyx
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.5 -o filename.so filename.c
</code></pre>
<p>You may find this link useful.</p>
<p><a href="http://www.sagemath.org/doc/developer/coding_in_cython.html">http://www.sagemath.org/doc/developer/coding_in_cython.html</a></p>
<p>This way the code runs almost as fast as a C code.</p>
https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?comment=20569#post-id-20569If that is the case have you tried using gsl. I have a good experience with gsl.Sun, 08 Jan 2012 22:02:12 +0100https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?comment=20569#post-id-20569Comment by Dalmo for <p>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.</p>
<pre><code>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
</code></pre>
<p>for Mac. For Linux use</p>
<pre><code>cython -a filename.pyx
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.5 -o filename.so filename.c
</code></pre>
<p>You may find this link useful.</p>
<p><a href="http://www.sagemath.org/doc/developer/coding_in_cython.html">http://www.sagemath.org/doc/developer/coding_in_cython.html</a></p>
<p>This way the code runs almost as fast as a C code.</p>
https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?comment=20574#post-id-20574Hi 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.Sun, 08 Jan 2012 21:14:46 +0100https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?comment=20574#post-id-20574Answer by Volker Braun for <p>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?</p>
<p>Thank you,</p>
<p>Dalmo</p>
https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?answer=13104#post-id-13104Not 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. Sun, 08 Jan 2012 21:54:52 +0100https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?answer=13104#post-id-13104Comment by Dalmo for <p>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. </p>
https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?comment=20565#post-id-20565Hi 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.Mon, 09 Jan 2012 11:26:39 +0100https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?comment=20565#post-id-20565Comment by Volker Braun for <p>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. </p>
https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?comment=20563#post-id-20563This sounds more like it would be stackoverflow.com material, but in any case you can reach me at vbraun.name@gmail.comTue, 10 Jan 2012 14:05:07 +0100https://ask.sagemath.org/question/8617/calculating-eigenvectors-in-c/?comment=20563#post-id-20563