# Analytically take the derivtive of a matrix for use with JuMP variables

**URL:** <https://discourse.julialang.org/t/analytically-take-the-derivtive-of-a-matrix-for-use-with-jump-variables/1711>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [January 26, 2017, 10:34pm UTC](https://discourse.julialang.org/t/analytically-take-the-derivtive-of-a-matrix-for-use-with-jump-variables/1711 "2017-01-26T22:34:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![huckl3b3rry87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huckl3b3rry87/32/3163_2.png) [@huckl3b3rry87](https://discourse.julialang.org/u/huckl3b3rry87)\
**Post date:** [January 26, 2017, 10:34pm UTC](https://discourse.julialang.org/t/analytically-take-the-derivtive-of-a-matrix-for-use-with-jump-variables/1711/1 "2017-01-26T22:34:26Z")

</div>

I am solving optimal control problems where I need to calculate the [D or differentiation matrix](http://nloptcontroljl.readthedocs.io/en/latest/hp_psuedospectral.html#optimal-control-problem-approximation), it is full of derivartives of [Lagrange Polynomials](http://nloptcontroljl.readthedocs.io/en/latest/lagrange_poly.html) . This matrix is also a function of the final time (`tf`, which is a design variable within the optimization). My [first attempt (F2)](https://github.com/huckl3b3rry87/NLOptControl.jl/blob/master/src/utils.jl#L195) to calculate this matrix was to modify [this function (F1)](https://github.com/huckl3b3rry87/NLOptControl.jl/blob/master/src/utils.jl#L262) for JuMP by creating many `@NLexpression()`s. The output for `F1` and `F2` match perfectly until the final time is small `tf < 0.1` I am not sure why this is. Could it be a simplification that `@NLexpression()` is making?

Anyways, after all of this I decided that there must be a better way to accomplish my goal. What (I think) I need to do is generate an analytical expression of the `D matrix` in terms of `tf` before optimization. Then, during optimization, I can use the elements of the D matrix (which will be functions of `tf`) in an `@NLconstraint()`[current code](https://github.com/huckl3b3rry87/NLOptControl.jl/blob/master/src/ocp.jl#L75).

Any suggestions or thoughts ? It seems that automatic differentiation may not work for my case since I don’t know `tf` before optimization. I was thinking about using Calculus.jl, but I am not sure if that will be waisting more of my time (if there is a better package or better way to do this). Also, I was thinking about registering the functions of `tf` in the `D matrix` using [JuMP](https://jump.readthedocs.io/en/latest/nlp.html#user-defined-functions), but I am very concerned with speed and since the registering functionality uses ForwardDiff I am not sure if it will be fast enough.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [January 26, 2017, 10:41pm UTC](https://discourse.julialang.org/t/analytically-take-the-derivtive-of-a-matrix-for-use-with-jump-variables/1711/2 "2017-01-26T22:41:44Z")

</div>

> Could it be a simplification that `@NLexpression()` is making?

No. JuMP doesn’t do this.

> Any suggestions or thoughts ?

You’re free to derive symbolic expressions in an automated way and input those expression into constraints: [Nonlinear Modeling — JuMP -- Julia for Mathematical Optimization 0.15 documentation](http://www.juliaopt.org/JuMP.jl/0.15/nlp.html#raw-expression-input).

---

<div class="post-metadata">

**Author:** ![huckl3b3rry87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huckl3b3rry87/32/3163_2.png) [@huckl3b3rry87](https://discourse.julialang.org/u/huckl3b3rry87)\
**Post date:** [January 26, 2017, 10:50pm UTC](https://discourse.julialang.org/t/analytically-take-the-derivtive-of-a-matrix-for-use-with-jump-variables/1711/3 "2017-01-26T22:50:07Z")

</div>

@miles.lubin that is a good idea, thanks for the feedback.

Is there an example where variables or constraints are generated programatically using JuMP? I looked into this before and had some difficulty figuring it out.

Is there a package that you recommend to derive these symbolic expressions? Like Calculus.jl?

---

<div class="post-metadata">

**Author:** ![huckl3b3rry87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huckl3b3rry87/32/3163_2.png) [@huckl3b3rry87](https://discourse.julialang.org/u/huckl3b3rry87)\
**Post date:** [January 27, 2017, 12:06am UTC](https://discourse.julialang.org/t/analytically-take-the-derivtive-of-a-matrix-for-use-with-jump-variables/1711/4 "2017-01-27T00:06:47Z")

</div>

Or might SymPy.jl be better for my purpose as I have a lot of math to carry out on the variables.

And if I use SymPy.jl can the symbolic variables that it creates be used (or converted into JuMP variables)?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 27, 2017, 4:10am UTC](https://discourse.julialang.org/t/analytically-take-the-derivtive-of-a-matrix-for-use-with-jump-variables/1711/5 "2017-01-27T04:10:43Z")

</div>

> [@huckl3b3rry87](#):
>
> Is there a package that you recommend to derive these symbolic expressions? Like Calculus.jl?

I like using SymEngine.jl for this. Similar to SymPy but its core gets better performance.
