Calculus.jl icon indicating copy to clipboard operation
Calculus.jl copied to clipboard

code to treat differentiate as a first class higher order function

Open Eskatrem opened this issue 12 years ago • 7 comments

I added a macro @define to store the code of a function when creating it, and another one @makeDerivative to create functions that are derivatives. It becomes possible to do:

julia> @define myFunction x cos(x*x) (anonymous function)

julia> myDer = @makeDerivative myFunction x (anonymous function)

julia> myDer(sqrt(pi)) -2.0083812473182306e-15

julia> myDer2 = @makeDerivative((x_x_sin(x)),x) (anonymous function)

julia> myDer2(pi) -9.869604401089358

Eskatrem avatar Nov 27 '13 22:11 Eskatrem

I think this needs some revision.

(1) I don't want to introduce a global variable to get this functionality. Why does anything need to be cached here?

(2) It's not conventional in Julia to introduce a type with untyped fields. Please specify which types you have in mind.

johnmyleswhite avatar Dec 08 '13 16:12 johnmyleswhite

This code also doesn't pass tests.

johnmyleswhite avatar Dec 08 '13 16:12 johnmyleswhite

What's the point of coming up with a strange new syntax for defining functions? The source for any function is accessible via code_lowered.

mlubin avatar Dec 08 '13 17:12 mlubin

With code_lowered you need to supply types for the arguments, and you get a altered expression tree. I'm unsure if that is a problem though.

ivarne avatar Dec 08 '13 17:12 ivarne

Supplying types is unavoidable because of multiple dispatch. I agree that the output of code_lowered is pretty ugly, but it's definitely parseable. We should do the heavy lifting for users instead of making them define functions in an awkward way.

mlubin avatar Dec 08 '13 17:12 mlubin

Sorry, I didn't realize my code was that messy. I'll address the other issues mentioned by @johnmyleswhite and @ivarne, but I was trying to play with code_lowered, and I can't get the AST of my function. If I write in a file:

function test(x)
    x*x
end

I get in the REPL:

julia> code_lowered(test,())
0-element Array{Any,1}

And writing the function straight in the REPL returns a similar result. Not sure if that matters, but the version of Julia I am running is julia version 0.2.0-rc4+16.

Eskatrem avatar Dec 09 '13 01:12 Eskatrem

You need to provide a tuple corresponding to the input types, e.g.,

julia> code_lowered(test,(Float64,))
1-element Array{Any,1}:
 :($(Expr(:lambda, {:x}, {{},{{:x,:Any,0}},{}}, quote  # none, line 2:
        return *(x,x)
    end)))

mlubin avatar Dec 09 '13 01:12 mlubin