First time here? Check out the FAQ!

Ask Your Question
1

Using matrices in Cython

asked 4 years ago

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

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 4 years ago

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.

Preview: (hide)
link

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 ( 4 years ago )

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 ( 4 years ago )

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: 4 years ago

Seen: 875 times

Last updated: Oct 12 '20