From 4a9fab2673ebc4f72c1de3d8c280f0e9fc8dd4fe Mon Sep 17 00:00:00 2001 From: Samuel Le Date: Wed, 27 Nov 2013 23:43:13 +0100 Subject: [PATCH 1/2] macro to define a function and keep its code in memory --- src/storingFunctions.jl | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/storingFunctions.jl diff --git a/src/storingFunctions.jl b/src/storingFunctions.jl new file mode 100644 index 0000000..1ab943e --- /dev/null +++ b/src/storingFunctions.jl @@ -0,0 +1,34 @@ +export StoredFunction + + +global storedFunctions = Dict() + +type StoredFunction + args + code +end + +macro define(functionName, args, code) + storedFunctions[functionName] = StoredFunction(args,code) + :(global $functionName = $args -> $code) +end + + +function testStoredFunction(functionName::Expr) + print(storedFunctions) + return haskey(storedFunctions, functionName) +end + + +## function differentiate(ex::Expr,wrt) +## print("hello") +## print(haskey(storedFunctions, ex)) +## if haskey(storedFunctions, ex) +## print("differentiating a user-defined function") +## differentiate(storedFunctions[ex].code, wrt) +## end +## if ex.head != :call +## error("Unrecognized expression $ex") +## end +## simplify(differentiate(SymbolParameter(ex.args[1]), ex.args[2:end], wrt)) +## end From 9f6ac72ca9f0aa64c407f21dfe5add51001c5579 Mon Sep 17 00:00:00 2001 From: Samuel Le Date: Wed, 27 Nov 2013 23:45:00 +0100 Subject: [PATCH 2/2] added code to be able to create a function that is the derivative of another function, for example: @define myFunction x x*x; myDerivative = @makeDerivative(myFunction, x) --- src/differentiate.jl | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/differentiate.jl b/src/differentiate.jl index 16aba4c..4ceb603 100644 --- a/src/differentiate.jl +++ b/src/differentiate.jl @@ -9,14 +9,38 @@ export differentiate # ################################################################# -differentiate(ex::SymbolicVariable, wrt::SymbolicVariable) = (ex == wrt) ? 1 : 0 +macro makeDerivative(ex, wrt) + if haskey(storedFunctions, ex) + #in that case, ex is a function that was defined already + args = storedFunctions[ex].args + code = storedFunctions[ex].code + derivativeCode = differentiate(code, wrt) + res = eval(:($args -> $derivativeCode)) + return res + else + derivativeCode = differentiate(ex, wrt) + res = eval(:($wrt -> $derivativeCode)) + return res + end +end + +function differentiate(ex::SymbolicVariable, wrt::SymbolicVariable) + if haskey(storedFunctions, ex) + return differentiate(storedFunctions[ex].code, wrt) + end + (ex == wrt) ? 1 : 0 +end + differentiate(ex::Number, wrt::SymbolicVariable) = 0 function differentiate(ex::Expr,wrt) + #println("about to differentiate: ",ex," with respect to ", wrt) if ex.head != :call error("Unrecognized expression $ex") end + #println(SymbolParameter(ex.args[1])) + #println(ex.args[2:]) simplify(differentiate(SymbolParameter(ex.args[1]), ex.args[2:end], wrt)) end