Farr-PHY-604 / ForwardAutodiff.jl

Basic library for forward autodiff in Julia coded in class today.
MIT License
1 stars 0 forks source link

nested derivatives #1

Open YihanWangAstro opened 5 years ago

YihanWangAstro commented 5 years ago

it's not too hard to figure it out

I believe Julia can overload the 'operator()' of a class.

overload the operator() of the class 'Infinitesimal' to make it callable (extract the derivative here and delete three 'extract_derivative' outside the class).

in this way, we could return an Infinitesimal object directly in the function 'D' instead of wrapping it with a plain function 'df'.

Now, the return type of D is another Infinitesimal that can be passed to nested D.

farr commented 5 years ago

Hi Yihan,

I think that could be a good start, but what would we do about the following

function f(a, b, c) return sqrt(aa + bb + c*c) end

ddf = D(1, D(2, f))

Here you need a way to keep track that the partials refer to different variables. I don't think your solution does that.

YihanWangAstro commented 5 years ago

Hi Yihan,

I think that could be a good start, but what would we do about the following

function f(a, b, c) return sqrt(a_a + b_b + c*c) end

ddf = D(1, D(2, f))

Here you need a way to keep track that the partials refer to different variables. I don't think your solution does that.

Hi Will,

It seems we have to overload a variadic argument version operator() of the 'Infinitesimal' for partial diff.

Add the partial diff index i as an argument of the ctor of the 'Infinitesimal', store it as a member. In the variadic operator() of the 'Infinitesimal', do the diff on the i-th parameter.

I guess this should work... Don't know if this is the correct direction...

farr commented 5 years ago

Getting there (sorry for sounding this way---I know how to do this, but it's worth it for you to think about it carefully, and you're moving forward, so let's continue).

But what if I define:

f(x, y, z) = .... g = D(1, f)

And then, later

h(p, q, r) = ... g(p*q, r/p, r) ...

and I want to take

D(1, h)

Now there are two "D(1, ...)"s flying around in the same computation, but these would share the same i parameter in your suggested implementation. How would you fix this?

YihanWangAstro commented 5 years ago

thank you! but I meant that for each D, a new Infinitesimal object that stores an independent i is created. Anyway, it's time for me to do the code work. May I ask what's the assignment this time.

farr commented 5 years ago

Yes! That's the solution---sorry that I didn't recognize that this was what you meant. One point: you will need to use some sort of data structure to keep track of the "chain" of Infinitesimals (a sorted list on i is probably sufficient, though a binary tree would be even better in high dimensional problems) so you don't have "repeated" copies of i with other infinitesimals between them.

I haven't released the assignment yet; I'll be sure to email you when I do (probably tonight---I hope).