{{{id=1| %cython print "Hi Math 480" /// Hi Math 480 }}} {{{id=2| %cython from math import sin def f(x): return sin(x**2) def integral(a, b, N): dx = (b-a)/N s = 0 for i in range(N): s += f(a+dx*i) return s * dx /// }}} {{{id=3| time integral(0.0, 1.0, 100000) /// 0.310264094377461 Time: CPU 1.50 s, Wall: 1.53 s }}} {{{id=6| %cython from math import sin def f(x): return sin(x**2) def integral(double a, double b, int N): cdef double dx = (b-a)/N cdef int i cdef double s = 0 for i in range(N): s += f(a+dx*i) return s * dx /// }}} {{{id=7| time integral(0.0, 1.0, 100000) /// 0.310264094377461 Time: CPU 0.06 s, Wall: 0.06 s }}} {{{id=8| 1.5 / .06 /// 25.0000000000000 }}} {{{id=9| time integral(0.0, 1.0, 1000000) /// 0.31026788098798791 Time: CPU 0.58 s, Wall: 0.58 s }}} {{{id=10| %cython cdef extern from "math.h": # external library double sin(double) cdef double f(double x): return sin(x**2) def integral(double a, double b, int N): cdef double dx = (b-a)/N cdef int i cdef double s = 0 for i in range(N): s += f(a+dx*i) return s * dx /// }}} {{{id=11| time integral(0.0, 1.0, 100000000) /// 0.31026829751610591 Time: CPU 3.15 s, Wall: 3.17 s }}} {{{id=12| (1.5 / 100000) / (3.15 / 100000000) /// 476.190476190476 }}} {{{id=4| numerical_integral(sin(x^2), 0, 1) /// (0.31026830172338099, 3.444670123846427e-15) }}} {{{id=5| %cython def square(int i): return i*i /// }}} {{{id=13| square(10^6) /// -727379968 }}} {{{id=14| def square(i): return i*i /// }}} {{{id=15| square(10^6) /// 1000000000000 }}} {{{id=17| %cython cdef square_inplace(int* x): x[0] = x[0]**2 def call_square_inplace(int x): print "x is", x square_inplace(&x) print "x is", x /// }}} {{{id=18| call_square_inplace(2) /// x is 2 x is 4 }}} {{{id=19| class TwoNumbers: def __init__(self, a, b): self.a = a self.b = b def sum(self): return self.a + self.b def sum_a_lot(self, N): for i in range(N): self.sum() def __repr__(self): return "The numbers %s and %s" % (self.a, self.b) def __str__(self): return repr(self) /// }}} {{{id=20| a = TwoNumbers(3, 5) a /// The numbers 3 and 5 }}} {{{id=21| a.sum() /// 8 }}} {{{id=22| time a.sum_a_lot(1000000) /// Time: CPU 0.67 s, Wall: 0.68 s }}} {{{id=23| %cython cdef class TwoNumbers: cdef int a,b def __init__(self, a, b): self.a = a self.b = b def sum(self): return self.a + self.b def sum_a_lot(self, N): for i in range(N): self.sum() def __repr__(self): return "The numbers %s and %s" % (self.a, self.b) def __str__(self): return repr(self) /// }}} {{{id=24| a = TwoNumbers(3, 5) time a.sum_a_lot(1000000) /// Time: CPU 0.18 s, Wall: 0.18 s }}} {{{id=25| .67/.18 /// 3.72222222222222 }}} {{{id=26| %cython cdef class TwoNumbers: cdef int a, b def __init__(self, a, b): self.a = a self.b = b cpdef int sum(self): return self.a + self.b def sum_a_lot(self, int N): cdef int i for i in range(N): self.sum() def __repr__(self): return "The numbers %s and %s" % (self.a, self.b) def __str__(self): return repr(self) /// }}} {{{id=27| a = TwoNumbers(3, 5) time a.sum_a_lot(1000000) /// Time: CPU 0.01 s, Wall: 0.01 s }}} {{{id=28| a = TwoNumbers(3, 5) time a.sum_a_lot(100000000) /// Time: CPU 0.91 s, Wall: 0.92 s }}} {{{id=29| .67 / .009 /// 74.4444444444445 }}} {{{id=30| a = TwoNumbers(2^30, 2^30) /// }}} {{{id=31| a.sum() /// -2147483648 }}} {{{id=32| %cython cimport cython @cython.infer_types(True) def f(): x = 4 print cython.typeof(x) y = 5.1 print cython.typeof(y) z = 1 z = "hi" print cython.typeof(z) /// }}} {{{id=33| f() /// long double Python object }}} {{{id=34| %cython print eval("[n**2 for n in range(10)]") /// [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] }}} {{{id=35| /// }}}