Skip to content

Instantly share code, notes, and snippets.

@aviks
Created January 11, 2017 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aviks/2e85bdb5da192d02593fdc6d10c86336 to your computer and use it in GitHub Desktop.
Save aviks/2e85bdb5da192d02593fdc6d10c86336 to your computer and use it in GitHub Desktop.
function sum_diff_fast(x)
n=length(x); d = 1/(n-1)
s = zero(eltype(x))
@fastmath s = s + (x[2] - x[1]) / d
@fastmath for i = 2:n-1
s = s + (x[i+1] - x[i+1]) / (2*d)
end
@fastmath s = s + (x[n] - x[n-1])/d
end
function sum_diff(x)
n = length(x); d = 1/(n-1)
s = zero(eltype(x))
s = s + (x[2] - x[1]) / d
for i = 2:length(x)-1
s = s + (x[i+1] - x[i+1]) / (2*d)
end
s = s + (x[n] - x[n-1])/d
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment