On Wednesday, November 18, 2015 at 2:53:02 PM UTC+1, Christoph Ortner wrote:
>
> Not quite answering your question, but here is how I like to do this:
>
> abstract ObjectiveFunction
> type MyOF <: ObjectiveFunction
>    params
> end
>
> function evaluate(f::ObjectiveFunction, x)
>      . ..
> end
>
> Here, I think it should be `evaluate(f::MyOF,x)`
 

> function evaluate_grad(f::ObjectiveFunction, x)
>    . . . 
> end
>
> Then, to get nice syntax, I add do the following:
>
> import Base.call
> @inline call(pp::ObjectiveFunction, varargs...) = evaluate(pp, varargs...)
> @inline call(pp::ObjectiveFunction, ::Type{Val{:D}}, varargs...) = 
> evaluate_grad(pp, varargs...)
> macro D(fsig::Expr)
>     @assert fsig.head == :call
>     insert!(fsig.args, 2, Val{:D})
>     for n = 1:length(fsig.args)
>         fsig.args[n] = esc(fsig.args[n])
>     end
>     return fsig
> end
>
> The result is that I can now call objective functions as follows:
>
> # construct objective
> of = MyOF(params)
> # evaluate objective fun
> F = of(x)
> # evaluate its gradient
> DF = @D of(x)
>
> I had some fun cooking this up - I'd love hear what people think.
>
> Christoph
>
>
>
>
>
>
>

Reply via email to