Sage Worksheet: sage days 8 cython talk

jsMath
sage days 8 cython talk

# Yes, C++ and Cython Work Together

## 1. Cython generates code that C++ compilers compile

## 2. C name space specifiers

%cython cdef extern from "stdio.h": int abs(int) cdef int a = -10 print abs(a)

    10
    10
__Users_wa...01_code_sage9_spyx.c __Users_wa..._sage9_spyx.pyx.html

%cython cdef extern from "stdio.h": int c_abs "abs" (int) # generated code using abs, but refer to it as c_abs! cdef int a = -10 print c_abs(a)

    10
    10
__Users_wa...1_code_sage13_spyx.c __Users_wa...sage13_spyx.pyx.html

z = 5

%python print z^7

    2
    2

python.eval('print z^7', globals())

    2
    ''
    2
    ''

%cython cdef extern from "stdio.h": void goto "goto out; //"() # evil and bad -- all calls to goto get replaced... void receive "out: //"() print "1" goto() print "2" receive() print "3"

    1
    3
    1
    3
__Users_wa...1_code_sage78_spyx.c __Users_wa...sage78_spyx.pyx.html

%cython # clang c++ cdef extern from "vector.h": ctypedef struct intvec "std::vector<unsigned int>": void push_back(int elem) intvec intvec_factory "std::vector<unsigned int>"(int len) cdef intvec v = intvec_factory(10) v.push_back(0)

| __Users_wa...code_sage93_spyx.cpp __Users_wa...sage93_spyx.pyx.html

%cython # clang c++ cdef extern from "vector.h": ctypedef struct intvec "std::vector<unsigned int>": int get_entry "operator[]"(int n) intvec intvec_factory "std::vector<unsigned int>"(int len) cdef intvec v = intvec_factory(10) print v.get_entry(2) print v[2]

    Exception (click to the left for traceback):
    ...
    /Users/was/.sage/temp/teragon.local/88048/spyx/_Users_was__sage_sage_notebook_worksheets_admin_101_code_sage97_spyx/_Users_was__sage_sage_notebook_worksheets_admin_101_code_sage97_spyx_0.pyx:15:7: Attempting to index non-array type 'intvec'
    Traceback (most recent call last):
      File "/Users/was/build/sage-2.10.3.alpha0/local/lib/python2.5/site-packages/sage/server/support.py", line 300, in cython_import_all
        create_local_c_file=create_local_c_file)
      File "/Users/was/build/sage-2.10.3.alpha0/local/lib/python2.5/site-packages/sage/server/support.py", line 281, in cython_import
        create_local_c_file=create_local_c_file)
      File "/Users/was/build/sage-2.10.3.alpha0/local/lib/python2.5/site-packages/sage/misc/cython.py", line 243, in cython
        raise RuntimeError, "Error converting %s to C:\n%s\n%s"%(filename, log, err)
    RuntimeError: Error converting /Users/was/.sage/sage_notebook/worksheets/admin/101/code/sage97.spyx to C:

    Error converting Pyrex file to C:
    ------------------------------------------------------------
    ...

    cdef intvec v = intvec_factory(10)
    print v.get_entry(2)
    print v[2]      ^
    ------------------------------------------------------------

    /Users/was/.sage/temp/teragon.local/88048/spyx/_Users_was__sage_sage_notebook_worksheets_admin_101_code_sage97_spyx/_Users_was__sage_sage_notebook_worksheets_admin_101_code_sage97_spyx_0.pyx:15:7: Attempting to index non-array type 'intvec'

%cython # clang c++ cdef extern from "vector.h": ctypedef struct intvec "std::vector<unsigned int>": int get_entry "operator[]"(int n) intvec intvec_factory "std::vector<unsigned int>"(int len) cdef extern from "": void DEFINE_SET "#define SET(a,b,c) a[b]=c; //"() void SET(intvec, int, int) DEFINE_SET() cdef intvec v = intvec_factory(10) SET(v,2,399283) print v.get_entry(2)

    399283
    399283
__Users_wa...code_sage99_spyx.cpp __Users_wa...sage99_spyx.pyx.html

%cython # clang c++ cdef extern from "vector.h": ctypedef unsigned long size_type ctypedef struct intvec "std::vector<unsigned int>": int get_entry "operator[]"(int n) size_type size() intvec intvec_factory "std::vector<unsigned int>"(int len) cdef extern from "": void DEFINE_SET "#define SET(a,b,c) a[b]=c; //"() void SET(intvec, int, int) DEFINE_SET() cdef intvec v = intvec_factory(10) SET(v,2,5) print v.get_entry(2) print v.size()

    5
    10
    5
    10
__Users_wa...code_sage67_spyx.cpp __Users_wa...sage67_spyx.pyx.html

# Tour

## 1. Givaro

## 2. NTL