From e17b5627a8ff8900f1903f06f35d16dc6a96e90f Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Sun, 26 May 2013 12:02:55 -0700 Subject: [PATCH 1/2] Added a function to allow differentiate() to make a finite difference based on a symbolic expression --- src/differentiate.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/differentiate.jl b/src/differentiate.jl index 11aa16a..dc7a9da 100644 --- a/src/differentiate.jl +++ b/src/differentiate.jl @@ -20,6 +20,13 @@ function differentiate(ex::Expr,wrt) simplify(differentiate(SymbolParameter(ex.args[1]), ex.args[2:end], wrt)) end +# a finite difference based on a symbolic expression, given a specific x to differentiate at +function differentiate(ex::Expr, wrt::SymbolicVariable, atx::Float64) + ex = differentiate(ex) + eval(:($wrt = $atx)) + return eval(ex) +end + differentiate{T}(x::SymbolParameter{T}, args, wrt) = error("Derivative of function " * string(T) * " not supported") # The Power Rule: From 4206b9879b45a5ff28bf876e760e38568c88ad65 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Mon, 27 May 2013 00:38:38 -0700 Subject: [PATCH 2/2] Updated symbolic-finite differentiation to use localvars, arguably safer use of eval --- src/differentiate.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/differentiate.jl b/src/differentiate.jl index dc7a9da..04ef7dd 100644 --- a/src/differentiate.jl +++ b/src/differentiate.jl @@ -22,10 +22,11 @@ end # a finite difference based on a symbolic expression, given a specific x to differentiate at function differentiate(ex::Expr, wrt::SymbolicVariable, atx::Float64) - ex = differentiate(ex) - eval(:($wrt = $atx)) - return eval(ex) -end + ex = differentiate(ex, wrt) # symbolic only + local fex = :(($wrt)->($ex)) + local f = eval(fex) + return f(atx) +end differentiate{T}(x::SymbolParameter{T}, args, wrt) = error("Derivative of function " * string(T) * " not supported")