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

Evaluate symbolic derivatives at specific points #11

Open
johnmyleswhite opened this issue May 26, 2013 · 10 comments
Open

Evaluate symbolic derivatives at specific points #11

johnmyleswhite opened this issue May 26, 2013 · 10 comments

Comments

@johnmyleswhite
Copy link
Collaborator

It would be useful to provide a tool like differentiate(:(exp(x)), :x)) that returns a compiled function that is immediately evaluable at specific values of x.

@jckt
Copy link

jckt commented May 26, 2013

So sorry, this is literally the first time I've ever used git or github, and I'm not too sure how pulls work (but I'll figure it out in a moment). Anyway this seems to work:

# differentiate a function f(x) at given x
function symbolic_difference(ex::Expr, wrt::SymbolicVariable, atx::Float64)
    ex = differentiate(ex)
    eval(:($wrt = $atx))
    return eval(ex)
end

The only problem I guess is that here you're messing around with global variables, but I'm still figuring out Julia.

julia> expression = differentiate(:(exp(x)), :x)
:(exp(x))

julia> symbolic_difference(expression, :x, 1.0)
2.7182818284590455

julia> symbolic_difference(expression, :x, 0.0)
1.0

Hmm, also, using eval like this seems dangerous.

@johnmyleswhite
Copy link
Collaborator Author

Yeah, using eval isn't ideal here. Let me ponder this for a bit.

@Luthaf
Copy link

Luthaf commented Nov 3, 2014

Hey there !

I'd really like to do something like this in my code. What is the advancement of this point ?
There was a PR (#12) some time ago, is there anything I can do to help with this ?

I am very novice in metaprograming and symbolic computing, but I can try.

@johnmyleswhite
Copy link
Collaborator Author

I have a pretty clear sense how to implement this, but am very busy for the next few weeks. You might try working on it yourself for a while and submitting a PR, which I'll try to make time to review.

@johnmyleswhite
Copy link
Collaborator Author

Here's an overview:

(1) Understand the existing function that takes in a symbolic expression and produces its derivative.

(2) Write a macro that, given a symbolic expression,
a) generates its derivative using (1),
b) defines a function whose body is the derivative, and
c) calls that function on the input.

Thus, you'd end up with a macro like the following:

@differentiate(5.0, sin(x) + cos(x))

@mlubin
Copy link
Collaborator

mlubin commented Nov 4, 2014

This is implemented efficiently in https://github.com/JuliaDiff/ReverseDiffSource.jl

@Luthaf
Copy link

Luthaf commented Nov 4, 2014

Well, it would be a good thing to have it in this package too : you can fall back to finite differences if there is no way to differentiate analytically the function.

Could the two packages being merged into one ? Or is it worth implementing the ReverseDiffSource method into this one ?

@mlubin
Copy link
Collaborator

mlubin commented Nov 4, 2014

The methods in ReverseDiffSource/ReverseDiffSparse are way too complex to be merged into the basic Calculus package. If you just need to compile derivatives of a simple symbolic expression, John's approach is pretty straightforward and would be a good addition to Calculus.jl.

@johnmyleswhite
Copy link
Collaborator Author

One thing that would be nice at some point is to try to unify interfaces between the packages so that it's easier to transition from Calculus to ReverseDiffSparse.

@Luthaf
Copy link

Luthaf commented Nov 6, 2014

One thing that would be nice at some point is to try to unify interfaces between the packages so that it's easier to transition from Calculus to ReverseDiffSparse.

Sure !

If you just need to compile derivatives of a simple symbolic expression, John's approach is pretty straightforward and would be a good addition to Calculus.jl.

I may try to do this if I find some time for hacking around. But I am pretty busy right now too ...

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

No branches or pull requests

4 participants