I have the following example of Chebyshev polynomials differentiation matrix in Julia.
function cheb(N)
N==0 ? (return D=0.0; x=0.0) : (D=0.0; x=0.0)
x = cos.(π*(0:N)./N)
c = [2; ones(N-1); 2].*(-1).^(0:N)
X = repeat(x, 1, N+1)
dX = X - X'
D = (c*(1 ./ c)') ./ (dX + I)
D = D - Diagonal(vec(sum(D',dims=1)))
D, x'
end
and I run a simple example such as,
tick()
N = 201
D, x = cheb(N)
tock()
and I get the following results:
Elapsed time are,
0.125761261s
0.133601933s
0.148246084s
0.130149234s
I ran the code 4 times to be sure of the average time execution.
However an equivalent code in Matlab, such as
function [D,x] = cheb(N)
if N==0, D=0; x=1; return, end
x = cos(pi*(0:N)/N)';
c = [2; ones(N-1,1); 2].*(-1).^(0:N)';
X = repmat(x,1,N+1);
dX = X-X';
D = (c*(1./c)')./(dX+(eye(N+1))); % off-diagonal entries
D = D - diag(sum(D'));
and again a simple example,
tic
N = 200;
[D,x] = cheb(N);
toc
Elapsed time is,
0.004155s
0.003415s
0.003218s
0.003986s
The Matlab code is on average 30 times faster than this Julia code, and I’m not an expert in Julia but I appreciate if someone can explain why the Julia example is slower than Matlab.