Skip to content

Instantly share code, notes, and snippets.

@fmeulen
Created May 28, 2019 15:03
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 fmeulen/e6963480fbc7dca378b3d71a2c238a5b to your computer and use it in GitHub Desktop.
Save fmeulen/e6963480fbc7dca378b3d71a2c238a5b to your computer and use it in GitHub Desktop.
function f(x)
y = x
zz = zeros(typeof(y[1]),10)
zz[1] = sum(x)
for i in 2:10
zz[i] = zz[i-1] + 0.01 * sum(sin.(y))
end
sum(zz)^2
end
function f2(x)
y = x
z = copy(y)
A = rand(length(x),length(x))
for i in 2:10
for j in 1:length(z)
z[i] = z[i] + sin(dot(@view(A[i,:]),x))/1000
end
end
sum(z)^2
end
function f3(x)
y = x
z = copy(y)
r = 1:length(x)
A = rand(length(x),length(x))
for i in 2:10
z = z + sin.(A*x)/1000
end
# println("eval f")
sum(z)^2
end
using LinearAlgebra
using ForwardDiff
using ReverseDiff
N = 1_000
x = rand(N)
@time ForwardDiff.gradient(f2,rand(1000));
@time ReverseDiff.gradient(f3,rand(1000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment