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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/differentiate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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?

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

Choose a reason for hiding this comment

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

Why add commented debug statements?

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

Expand Down
34 changes: 34 additions & 0 deletions src/storingFunctions.jl
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this function print?

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