# Capture an output function

**URL:** <https://discourse.julialang.org/t/capture-an-output-function/75824>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 4, 2022, 10:34pm UTC](https://discourse.julialang.org/t/capture-an-output-function/75824 "2022-02-04T22:34:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wub7quxa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wub7quxa/32/33473_2.png) [@wub7quxa](https://discourse.julialang.org/u/wub7quxa)\
**Post date:** [February 4, 2022, 10:34pm UTC](https://discourse.julialang.org/t/capture-an-output-function/75824/1 "2022-02-04T22:34:00Z")

</div>

I use the Calculus module in julia to differentiate functions. For example,

```julia
using Calculus
differentiate("-exp(x)+exp(-x/2)*(2*cos((sqrt(3)/2)*x)+sin((sqrt(3)/2)*x))",:x)

```

Everything works beautifully. Now I want to define a new function that is the derivative. As of now, I copy and paste the output. That’s not very elegant! What could I do to capture the output? In sage, I can just say “fp(x)=diff(f(x),x)” – looking at other posts, the Base function redirect\_stdout may help, but I would need to see an example of how to make it work (and I am not sure it’s implemented in Julia 1.6).

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [February 4, 2022, 10:42pm UTC](https://discourse.julialang.org/t/capture-an-output-function/75824/2 "2022-02-04T22:42:53Z")

</div>

The Calculus package has been around for a long time now and it shows. There are much better packages for differentiation nowadays, so I’d strongly recommend looking at those first.

For numeric differentiation of scalar functions specifically, [https://github.com/JuliaDiff/ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl) is what most people use, so you could just define `fp` as `fp(x) = ForwardDiff.derivative(f, x)`. If your `f` turns out to not be automatically differentiable for some reason, e.g. because it’s calling out to C code, [https://github.com/JuliaDiff/FiniteDifferences.jl](https://github.com/JuliaDiff/FiniteDifferences.jl) also works very well and is robust to numerical error.

If you need symbolic differentiation, [https://github.com/JuliaSymbolics/Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl) should work well for that. Otherwise, there are also bindings to sympy in [https://github.com/JuliaPy/SymPy.jl](https://github.com/JuliaPy/SymPy.jl).

---

<div class="post-metadata">

**Author:** ![wub7quxa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wub7quxa/32/33473_2.png) [@wub7quxa](https://discourse.julialang.org/u/wub7quxa)\
**Post date:** [February 4, 2022, 11:48pm UTC](https://discourse.julialang.org/t/capture-an-output-function/75824/3 "2022-02-04T23:48:48Z")

</div>

> [@simeonschaub](#):
>
> fp(x) = ForwardDiff.derivative(f, x)

Thank you for this. ForwardDiff doesn’t give me the derivative itself, it only evaluates it at a certain point. Symbolics will do the job:

Symbolics.derivative(-exp(x)+exp(-x/2)_(2_cos((sqrt(3)/2)\*x)+sin((sqrt(3)/2)\*x)),x)

but the output is even less useful than what Calculus will give me. Symbolic’s output is LaTeX code, so I can’t copy and paste it to define a new function.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [February 4, 2022, 11:57pm UTC](https://discourse.julialang.org/t/capture-an-output-function/75824/4 "2022-02-04T23:57:47Z")

</div>

You are looking for `build_function`, see [Function Building and Compilation (build\_function) · Symbolics.jl](https://symbolics.juliasymbolics.org/dev/manual/build_function/)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 5, 2022, 12:08am UTC](https://discourse.julialang.org/t/capture-an-output-function/75824/5 "2022-02-05T00:08:53Z")

</div>

> [@wub7quxa](#):
>
> it only evaluates it at a certain poin

It appears to create a function that evaluates it at _any_ point. Is that different from the derivative itself?
