Lately I have had some deep procrastinating thoughts coming from putting some crazy function into a minimizer. The issue is that I would prefer not to write out an analytic Jacobian as that introduces a lot of human errors. In julia ecosystem I found a JuliaDiff.jl which seems cool but I don’t feel quite confident of using something like that. I would rather like to generate a functions for a gradient and a Jacobian from existing code. As Mathematica can do that I thought that julia is also capable of that with Calculus.jl package. But I can not find a macros which could glue Calculus.jl together with an actual code.
The first ingredient for which I am looking for is how one can transform already defined function to an expression when symbolic arguments are passed. For example in Mathematica one could do something like:
f[x_,y_,z_] := x^2 + y + 2 z
f[1,2,3] (* this is a function *)
f[x,y,z] (* this is equivalent to an expression *)
In julia I would expect to be able to get the same behavior with some “@turntoexpr” macro as follows:
f(x,y,z) = x^2 + y + 2 z
f(1,2,3) # an evaluated function
@turntoexpr f(:x,:y,:z) # generates an expression
Does anyone already have such code which could simulate this behavior?
When I would have gotten expression for f (let’s name it fexpr) I would use it to generate an gradient expression:
∇fexpr = [differentiate(fexpr,si) for si in [:x,:y,:z]]
and similarly for a Jacobian. And now I actually want to define a functions using these expressions. For example a macros which would work as follows would be great:
@turntofunc ∇f(:x,:y,:z) = ∇fexpr
@turntofunc! ∇f!(:x,:y,:z) = ∇fexpr ### Which would create a function ∇f!(storage,x,y,z)
Had anyone already written such macros? Are there a design shortcomings why that could not work or would be inconvenient for a real problems? How could one proceed to implement such macros? I am dedicated to learn about julia macros, but I have fragmented view of how to develop them. What resources could be useful for learning about macro programming in julia and especially relating to this problem? Feedback appreciated.