integrate_f(a, b, n): dx = (b - a) / n dx2 = dx / 2 s = f(a) * dx2 for i in range(1, n): s += f(a + i * dx) * dx s += f(b) * dx2 return s http://bit.ly/aspp-cython
integrate_f(a, b, n): dx = (b - a) / n dx2 = dx / 2 s = f(a) * dx2 for i in range(1, n): s += f(a + i * dx) * dx s += f(b) * dx2 return s cdef double f(double x): cdef double y = x**4 - 3*x return y def integrate_f(double a, double b, int n): cdef: double dx = (b - a) / n double dx2 = dx / 2 double s = f(a) * dx2 int i = 0 for i in range(1, n): s += f(a + i * dx) * dx s += f(b) * dx2 return s http://bit.ly/aspp-cython
end function quadratic2(a, b, c) sqr_term = sqrt(b^2-4a*c) r1 = quadratic(a, sqr_term, b) r2 = quadratic(a, -sqr_term, b) return r1, r2 end adapted from https://samuelcolvin.github.io/JuliaByExample
integrate_f(a, b, n): dx = (b - a) / n dx2 = dx / 2 s = f(a) * dx2 for i in range(1, n): s += f(a + i * dx) * dx s += f(b) * dx2 return s import numba @numba.jit(nopython=True) def f(x): y = x**4 - 3*x return y @numba.jit(nopython=True) def integrate_f(a, b, n): dx = (b - a) / n dx2 = dx / 2 s = f(a) * dx2 for i in range(1, n): s += f(a + i * dx) * dx s += f(b) * dx2 return s