JuliaAttic / ReverseDiffSource.jl

Reverse automated differentiation from source
MIT License
47 stars 12 forks source link

Support for `reshape`? #22

Closed jzthree closed 9 years ago

jzthree commented 9 years ago

Allowing using reshape in the function would be very useful. I have only scratched the surface of this package and haven't learned how it works internally. But trying something like @deriv_rule reshape(x, a, b) x reshape(ds, a, b) does not work. Is that possible to add support for reshape?

( I am encountering this problem mostly because I need to compute order >=2 derivatives for my function, but the input can only be real or array for order >=2, so I am hoping to use reshape within the function to convert an array to a matrix internally)

fredo-dedup commented 9 years ago

try this (it seems to work for me):

    @deriv_rule reshape(x::AbstractArray, a, b)     x reshape(ds, size(x))
    @deriv_rule reshape(x::AbstractArray, a, b)     a 0.
    @deriv_rule reshape(x::AbstractArray, a, b)     b 0.
    @deriv_rule reshape(x::AbstractArray, d::Tuple) x reshape(ds, size(x))
    @deriv_rule reshape(x::AbstractArray, d::Tuple) d 0.

The rule should output an Array the size of x, not the size of ds (= (a,b))

jzthree commented 9 years ago

Yes I just tested and it works (how dumb I am!). Thanks a lot!!

I think it might be helpful to add reshape to the list of base_rules. It is quite basic operation that will allow handling more flexible functions. Just my opinion :)

fredo-dedup commented 9 years ago

No worries, creating those rules is not always intuitive (at least not for me). I'll add the reshape to base_rules.