#10249: c++ and %cython in the notebook
---------------------------+------------------------------------------------
   Reporter:  was          |       Owner:  jason     
       Type:  enhancement  |      Status:  new       
   Priority:  major        |   Milestone:  sage-4.6.1
  Component:  misc         |    Keywords:            
     Author:               |    Upstream:  N/A       
   Reviewer:               |      Merged:            
Work_issues:               |  
---------------------------+------------------------------------------------

Comment(by was):

 You could test this by pasting the following into a notebook cell:
 {{{
 open("/tmp/a.h",'w').write("""
 struct Elt10 {
     long a, b;
 };

 Elt10 operator+(Elt10 x, Elt10 y) {
     Elt10 z;
     z.a = x.a + y.a;
     z.b = x.a + y.a;
     return z;
 }

 void iadd(Elt10* x, Elt10 y) {
     x->a = x->a + y.a;
     x->b = x->b + y.b;
 }

 Elt10 operator*(Elt10 x, Elt10 y) {
     Elt10 z;
     z.a = x.a*y.a + x.b*y.b;
     z.b = x.b*y.a + x.a*y.b + x.b*y.b;
     return z;
 }
 """)
 }}}
 Then making another notebook cell and pasting the following in:
 {{{
 %cython
 #clang c++

 cdef extern from "stdlib.h":
     long random()

 cdef extern from "/tmp/a.h":
     cdef cppclass Elt10:
         long a, b
     Elt10 operator+(Elt10 x, Elt10 y)
     Elt10 operator*(Elt10 x, Elt10 y)
     void iadd(Elt10* x, Elt10 y)

 cdef class Vec4:
     cdef Elt10* elements
     cdef long n
     def __cinit__(self, Py_ssize_t n):
         # dynamic memory allocation on the heap
         self.elements = <Elt10*>malloc(n*sizeof(Elt10))
         self.n = n
     def __dealloc__(self):
         free(self.elements)
     def randomize(self):
         cdef Py_ssize_t m
         for m in range(self.n):
             self.elements[m].a = random(); self.elements[m].b = random()
     def dot(self, Vec4 other not None):
         cdef Elt10 ans, tmp
         ans.a = 0; ans.b = 0
         cdef Py_ssize_t m
         # This is the part that gets very worrisome, since the code does
         # not look easy to read.  And it can easily get massively more
 complicated
         # when the arithmetic isn't so simple!
         for m in range(self.n):
             ans = ans + self.elements[m] * other.elements[m]
         return ans.a, ans.b
 }}}

 Then in a third cell do:
 {{{
 v = Vec4(10^6); w = Vec4(10^6); v.randomize(); w.randomize()
 v.dot(w)
 }}}

 OK, not exactly a minimal example :-)

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/10249#comment:1>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to