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

code to treat differentiate as a first class higher order function #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Eskatrem
Copy link

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

…another function, for example: @define myFunction x x*x; myDerivative = @makeDerivative(myFunction, x)
@johnmyleswhite
Copy link
Collaborator

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
Copy link
Collaborator

This code also doesn't pass tests.

@@ -9,14 +9,38 @@ export differentiate
#
#################################################################

differentiate(ex::SymbolicVariable, wrt::SymbolicVariable) = (ex == wrt) ? 1 : 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this?

@mlubin
Copy link
Collaborator

mlubin commented Dec 8, 2013

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.

@ivarne
Copy link
Collaborator

ivarne commented Dec 8, 2013

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.

@mlubin
Copy link
Collaborator

mlubin commented Dec 8, 2013

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.

@Eskatrem
Copy link
Author

Eskatrem commented Dec 9, 2013

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.

@mlubin
Copy link
Collaborator

mlubin commented Dec 9, 2013

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)))

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

Successfully merging this pull request may close these issues.

4 participants