Ask Your Question
1

Using matrices in Cython

asked 2020-10-11 20:21:48 -0500

1571 gravatar image

I want to work with matrices in Cython. But I cannot even define a matrix. Here is the code in my Sage notebook:

sage:%%cython
sage:A=Matrix([[1,0],[0,1]])

Here is the error messsage:

RuntimeError: Error compiling Cython file:
------------------------------------------------------------
...
from sage import all
A=Matrix([[1,0],[0,1]]) ^
------------------------------------------------------------

_Users_macbook__sage_temp_bogon_52702_tmp_jzby0ki0_pyx_0.pyx:2:2: undeclared name not builtin: Matrix

In the end, my goal is to make use of this library in Cython: matrix/matrix_integer_dense.pyx

Thanks for your help

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-10-12 00:17:10 -0500

nbruin gravatar image

With the import command you use, you can access all.Matrix. Alternatively, you can use from sage.all import * and then Matrix should just work. For true cython work, you probably want to use a cimport. You cannot use sage.all for such imports, so you might as well start using more specific imports right away if real cython use is your goal.

edit flag offensive delete link more

Comments

Thanks, this sage.all import indeed works. So now how can I import the specific Cython library matrix/matrix_integer_dense.pyx?

1571 gravatar image1571 ( 2020-10-12 01:37:00 -0500 )edit

You're probably best served by reading the cython manual and the sage developer guide at this point.

Both describe profiling tools as well. Before you embark on "cythonizing" your code, first profile for where the bottleneck lies. It's very often not where you expect, and it can be very hard to predict if and where cython will be improving performance. Generally, for most of your code it will hardly make a difference. Only the innermost loops sometimes really benefit from cythonization, and often those loops are already in library code.

nbruin gravatar imagenbruin ( 2020-10-12 12:24:05 -0500 )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

1 follower

Stats

Asked: 2020-10-11 20:21:48 -0500

Seen: 61 times

Last updated: Oct 12