During October (2017) I will write a program per day for some well-known numerical methods in both Python and Julia. It is intended to be an exercise then don't expect the code to be good enough for real use. Also, I should mention that I have almost no experience with Julia, so it probably won't be idiomatic Julia but more Python-like Julia.
Following are the codes.
from __future__ import division, print_function import numpy as np def trapz(y, x=None, dx=1.0): if x is None: inte = 0.5*dx*(2*np.sum(y[1:-1]) + y + y[-1]) else: dx = x[1:] - x[:-1] inte = 0.5*np.dot(y[:-1] + y[1:], dx) return inte dx = 0.001 x = np.arange(0, 10, dx) y = np.sin(x) print(trapz(y, dx=dx)) print(1 - np.cos(10))
Regarding number of lines we have: 18 in Python and 16 in Julia. The comparison
in execution time is done with
%timeit magic command in IPython and
@benchmark in Julia.
In this case, we can say that the Python code is roughly as fast as Julia.