ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 03 Jul 2013 07:52:26 -0500What are all the types we can declare in Cython?https://ask.sagemath.org/question/10304/what-are-all-the-types-we-can-declare-in-cython/Let me be more precise: I know we can declare "int", "list", "float", "double", but what else ?
Is it possible to declare a matrix ?
Is it possible to declare combined types, as list of list of integers...?
My goal is that the program doesn't lose time to find, for example, in which type are the elements of a list...
If it's to long to describe all such available types here, maybe you can indicate to me where I can find this information.Tue, 02 Jul 2013 04:43:20 -0500https://ask.sagemath.org/question/10304/what-are-all-the-types-we-can-declare-in-cython/Answer by niles for <p>Let me be more precise: I know we can declare "int", "list", "float", "double", but what else ? </p>
<p>Is it possible to declare a matrix ? <br/>
Is it possible to declare combined types, as list of list of integers...? </p>
<p>My goal is that the program doesn't lose time to find, for example, in which type are the elements of a list... </p>
<p>If it's to long to describe all such available types here, maybe you can indicate to me where I can find this information.</p>
https://ask.sagemath.org/question/10304/what-are-all-the-types-we-can-declare-in-cython/?answer=15176#post-id-15176The documentation at http://www.cython.org/ is probably a good place to start. After that, you could look at the Sage source code, either in your local install or at http://hg.sagemath.org/sage-main/src. For example, you can see that other types are declared in [sage/matrix/misc.pyx](http://hg.sagemath.org/sage-main/src/0f8fd922eaed351e39f913f1317d319dcceb4c01/sage/matrix/misc.pyx?at=default).Wed, 03 Jul 2013 02:30:53 -0500https://ask.sagemath.org/question/10304/what-are-all-the-types-we-can-declare-in-cython/?answer=15176#post-id-15176Answer by Volker Braun for <p>Let me be more precise: I know we can declare "int", "list", "float", "double", but what else ? </p>
<p>Is it possible to declare a matrix ? <br/>
Is it possible to declare combined types, as list of list of integers...? </p>
<p>My goal is that the program doesn't lose time to find, for example, in which type are the elements of a list... </p>
<p>If it's to long to describe all such available types here, maybe you can indicate to me where I can find this information.</p>
https://ask.sagemath.org/question/10304/what-are-all-the-types-we-can-declare-in-cython/?answer=15187#post-id-15187The biggest speed benefit comes from using C primitive types instead of Python objects, for example machine integers instead of Python / Sage integer objects. Of course thats a pretty small list of types.
You can also declare any Cython cdef class, but that mostly saves you from having to cast the Python object to the cdef class all the time. Not so much speed benefit.
Python lists are still hash tables containing Python objects even if you declare them as `cdef list foo`. In particuar, it is not possible to have a memory-contiguous array of a particular C datatype that way. You shoud use C arrays or C++ `std::vector` if you need arrays of a fixed C datatype.
Note that your question smells of premature optimization. If you haven't benchmarked your Python implementation yet then don't bother.
Wed, 03 Jul 2013 07:52:26 -0500https://ask.sagemath.org/question/10304/what-are-all-the-types-we-can-declare-in-cython/?answer=15187#post-id-15187