Skip to content

Instantly share code, notes, and snippets.

@cscherrer
Last active March 5, 2021 00:03
Show Gist options
  • Select an option

  • Save cscherrer/08eaab5c2118d39f068678cd4013b5ee to your computer and use it in GitHub Desktop.

Select an option

Save cscherrer/08eaab5c2118d39f068678cd4013b5ee to your computer and use it in GitHub Desktop.
partial at one coordinate
julia> using StructArrays
julia> using ForwardDiff
julia> function partiali(n,i)
ith = zeros(n)
ith[i] += 1
function (f,x)
sa = StructArray{ForwardDiff.Dual{}}((x, ith))
return f(sa)
end
end
partiali (generic function with 2 methods)
julia> f(x) = sum(x -> x^2, x)
f (generic function with 1 method)
julia> x = randn(1000);
julia> ∂₅₀ = partiali(1000,50);
julia> x[50]
2.1129129387789667
julia> @btime $f($x)
82.987 ns (0 allocations: 0 bytes)
1000.4848272576928
julia> @btime $∂₅₀($f,$x)
167.038 ns (0 allocations: 0 bytes)
Dual{Nothing}(1000.4848272576928,4.225825877557933)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment