Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168695
Image: ubuntu2004
%cython print "Hi Math 480"
Hi Math 480
%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
time integral(0.0, 1.0, 100000)
0.310264094377461 Time: CPU 1.50 s, Wall: 1.53 s
%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
time integral(0.0, 1.0, 100000)
0.310264094377461 Time: CPU 0.06 s, Wall: 0.06 s
1.5 / .06
25.0000000000000
time integral(0.0, 1.0, 1000000)
0.31026788098798791 Time: CPU 0.58 s, Wall: 0.58 s
%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
time integral(0.0, 1.0, 100000000)
0.31026829751610591 Time: CPU 3.15 s, Wall: 3.17 s
(1.5 / 100000) / (3.15 / 100000000)
476.190476190476
numerical_integral(sin(x^2), 0, 1)
(0.31026830172338099, 3.444670123846427e-15)
%cython def square(int i): return i*i
square(10^6)
-727379968
def square(i): return i*i
square(10^6)
1000000000000
%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
call_square_inplace(2)
x is 2 x is 4
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)
a = TwoNumbers(3, 5) a
The numbers 3 and 5
a.sum()
8
time a.sum_a_lot(1000000)
Time: CPU 0.67 s, Wall: 0.68 s
%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)
a = TwoNumbers(3, 5) time a.sum_a_lot(1000000)
Time: CPU 0.18 s, Wall: 0.18 s
.67/.18
3.72222222222222
%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)
a = TwoNumbers(3, 5) time a.sum_a_lot(1000000)
Time: CPU 0.01 s, Wall: 0.01 s
a = TwoNumbers(3, 5) time a.sum_a_lot(100000000)
Time: CPU 0.91 s, Wall: 0.92 s
.67 / .009
74.4444444444445
a = TwoNumbers(2^30, 2^30)
a.sum()
-2147483648
%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)
f()
long double Python object
%cython print eval("[n**2 for n in range(10)]")
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]