johnmyleswhite/Calculus.jl — pull request #7 by tshort (2013-03-29 16:52:43+00:00)
Title: [for discussion] an abstract symbolic type and chain rule code

John,

This is based on the julia-dev discussion here:

https://groups.google.com/d/msg/julia-dev/3rqlCOf2TCw/cpRk1e7fKrMJ

This pull request includes:

* A new abstract type for symbolic variables, so packages that manipulate expressions can use placeholder types as well as just symbols to represent variables. Here is an example:

* An adaptation of Miles Lubin's chain rule code from here:

https://github.com/IainNZ/NLTester/blob/master/julia/nlp.jl#L81

Here is an example:

```julia
julia> x = BasicVariable(:x)
x

julia> y = BasicVariable(:y)
y

julia> x^3 + sin(x^2 + 1)
SymbolicExpression :(+(^($(x),3),sin(+(^($(x),2),1))))

julia> differentiate(x^3 + sin(x^2 + 1), x)
:(+(*(3,^($(x),2)),*(cos(+(^($(x),2),1)),*(2,$(x)))))

julia> chainRule(x^3 + sin(x^2 + 1), x)
:(+(*(3,1,^($(x),2)),*(*(2,1,^($(x),1)),cos(+(^($(x),2),1)))))
```

Here are some items for discussion:

* In the big picture, is this the right place for this, and is this the direction we want to go?

* In my adaptation of Miles' code, I split up chainRule to operate by the symbol representing the function, so there's one function for `:+` another for `:cos` and so on. It seems like it works well. It relies on types being able to be parametrized by a symbol.

* chainRule and differentiate seem redundant. We should probably go with one or the other. I like the name `differentiate` better.

* `chainRule` doesn't have a simplifying step.

* In adapting my symbolic code from Sims, I came across some issues with John's code. The biggest is that comparison operations like `x == 3` return an expression if `x` is a symbolic variable. I ended up using `isequal` when I wanted to compare the actual contents rather than return an expression.

* I did not add the capability to do `:x^2 + cos(:z)`. That would be possible, but that'd be classified as monkey patching unless we got buy-in from Julia-core. Because we can't operate on Expr's, I added a SymbolicExpression type.