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.Mon, 12 Oct 2020 19:24:05 +0200Using matrices in Cythonhttps://ask.sagemath.org/question/53856/using-matrices-in-cython/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 helpMon, 12 Oct 2020 03:21:48 +0200https://ask.sagemath.org/question/53856/using-matrices-in-cython/Answer by nbruin for <p>I want to work with matrices in Cython.
But I cannot even define a matrix. Here is the code in my Sage notebook:</p>
<pre><code>sage:%%cython
sage:A=Matrix([[1,0],[0,1]])
</code></pre>
<p>Here is the error messsage:</p>
<pre><code>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
</code></pre>
<p>In the end, my goal is to make use of this library in Cython:
matrix/matrix_integer_dense.pyx </p>
<p>Thanks for your help</p>
https://ask.sagemath.org/question/53856/using-matrices-in-cython/?answer=53858#post-id-53858With 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.Mon, 12 Oct 2020 07:17:10 +0200https://ask.sagemath.org/question/53856/using-matrices-in-cython/?answer=53858#post-id-53858Comment by nbruin for <p>With the import command you use, you can access <code>all.Matrix</code>. Alternatively, you can use <code>from sage.all import *</code> and then <code>Matrix</code> should just work. For true cython work, you probably want to use a <code>cimport</code>. You cannot use <code>sage.all</code> for such imports, so you might as well start using more specific imports right away if real cython use is your goal.</p>
https://ask.sagemath.org/question/53856/using-matrices-in-cython/?comment=53876#post-id-53876You'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.Mon, 12 Oct 2020 19:24:05 +0200https://ask.sagemath.org/question/53856/using-matrices-in-cython/?comment=53876#post-id-53876Comment by 1571 for <p>With the import command you use, you can access <code>all.Matrix</code>. Alternatively, you can use <code>from sage.all import *</code> and then <code>Matrix</code> should just work. For true cython work, you probably want to use a <code>cimport</code>. You cannot use <code>sage.all</code> for such imports, so you might as well start using more specific imports right away if real cython use is your goal.</p>
https://ask.sagemath.org/question/53856/using-matrices-in-cython/?comment=53859#post-id-53859Thanks, this sage.all import indeed works. So now how can I import the specific Cython library matrix/matrix_integer_dense.pyx?Mon, 12 Oct 2020 08:37:00 +0200https://ask.sagemath.org/question/53856/using-matrices-in-cython/?comment=53859#post-id-53859