Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic chain differential for system of equations? #30

Closed
ovainola opened this issue Jul 29, 2015 · 3 comments
Closed

Automatic chain differential for system of equations? #30

ovainola opened this issue Jul 29, 2015 · 3 comments
Labels

Comments

@ovainola
Copy link

Hi,

I'm working on a problem, which has a system of equations. I would like to get partial derivates of the whole system.

julia> using ForwardDiff

julia> f1(s) = (s/3)^2
f1 (generic function with 1 method)

julia> f2(s, l) = s^2 + exp(3*l/sqrt(4))
f2 (generic function with 1 method)

julia> function f(x)
           return f1(x[1]) + f2(x[1], x[2])
       end
f (generic function with 1 method)

julia> g = forwarddiff_gradient(f, Float64)
g (generic function with 1 method)

julia> g([0.1, 56.2])
ERROR: BoundsError: attempt to access 1-element Array{DualNumbers.Dual{Float64},
1}:
 0.1+0.0du
  at index [2]
 in dual_fad at C:\Users\ovax03\.julia\v0.4\ForwardDiff\src\dual_fad\univariate_
range.jl:6
 in g at C:\Users\ovax03\.julia\v0.4\ForwardDiff\src\dual_fad\univariate_range.j
l:22

Is there an error in my code or should I make a macro, which would derivate all the terms?

@jrevels
Copy link
Member

jrevels commented Jul 29, 2015

That does indeed look like a bug on our end, thanks for filing!

This package is undergoing a huge refactor at the moment, explained in this comment here. This bug will be fixed once #27 is merged (hopefully within the next 2 weeks). Here's some example code, working directly from the nduals-refactor branch:

julia> using ForwardDiff

julia> f1(s) = (s/3)^2
f1 (generic function with 1 method)

julia> f2(s, l) = s^2 + exp(3*l/sqrt(4))
f2 (generic function with 1 method)

julia> function f(x)
                  return f1(x[1]) + f2(x[1], x[2])
              end
f (generic function with 1 method)

julia> g = gradient_func(f, Partials{2,Float64}, mutates=false)
gradf (generic function with 1 method)

julia> g([0.1, 56.2])
2-element Array{Float64,1}:
 0.222222
 6.12514e36

In the meantime, you could try changing this line in the code you posted:

julia> g = forwarddiff_gradient(f, Float64)

to this:

julia> g = forwarddiff_gradient(f, Float64, fadtype=:typed, n=2)

and that should work as a temporary fix.

@jrevels
Copy link
Member

jrevels commented Aug 14, 2015

This now fixed on master as of the merging of #27.

@jrevels jrevels closed this as completed Aug 14, 2015
@ovainola
Copy link
Author

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants