Ask Your Question
1

Using matrices in Cython

asked 2020-10-12 03:21:48 +0100

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 07:17:10 +0100

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 08:37:00 +0100 )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 19:24:05 +0100 )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-12 03:21:48 +0100

Seen: 767 times

Last updated: Oct 12 '20