# Question about computing the partial derivatives of a multivariate polynomial

**URL:** <https://discourse.julialang.org/t/question-about-computing-the-partial-derivatives-of-a-multivariate-polynomial/36835>\
**Category:** Numerics\
**Created:** [April 1, 2020, 8:36am UTC](https://discourse.julialang.org/t/question-about-computing-the-partial-derivatives-of-a-multivariate-polynomial/36835 "2020-04-01T08:36:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yusen\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yusen_liu/32/13760_2.png) [@Yusen\_Liu](https://discourse.julialang.org/u/Yusen_Liu)\
**Post date:** [April 1, 2020, 8:36am UTC](https://discourse.julialang.org/t/question-about-computing-the-partial-derivatives-of-a-multivariate-polynomial/36835/1 "2020-04-01T08:36:22Z")

</div>

Hi all, I got stuck on computing the partial derivatives of a multivariate polynomial.

The following is my code:

```julia
using TypedPolynomials
using LinearAlgebra

@polyvar p1 p2 p3 p4 p5 p6 p11 p12 p13 p21 p22 p23 p31 p32 p33
A = [p1 p2 p3]
AT = transpose(A)

B = [p4 p5 p6]
BT = transpose(B)
P = AT*A + BT*B

M=copy(P)
M[1,1] = 2*M[1,1]
M[2,2] = 2*M[2,2]
M[3,3] = 2*M[3,3]

differentiate(det(M), P[1,1])

```

Here, matrix P is like:

```julia
 p1² + p4² p1p2 + p4p5 p1p3 + p4p6
 p1p2 + p4p5 p2² + p5² p2p3 + p5p6
 p1p3 + p4p6 p2p3 + p5p6 p3² + p6²  

```

I want to compute the partial derivative of det(M) with respect to P[1,1], i.e. p1² + p4². But I still couldn’t find a way to compute it. There’s always an error, which is shown below:

```julia
ERROR: MethodError: no method matching iterate(::Polynomial{Int64,Term{Int64,Monomial{(p1, p2, p3, p4, p5, p6),6}},Array{Term{Int64,Monomial{(p1, p2, p3, p4, p5, p6),6}},1}})

```

Is there a package that can be used to solve this problem? Really appreciate!

---

<div class="post-metadata">

**Author:** ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)\
**Post date:** [April 1, 2020, 1:27pm UTC](https://discourse.julialang.org/t/question-about-computing-the-partial-derivatives-of-a-multivariate-polynomial/36835/2 "2020-04-01T13:27:14Z")

</div>

I guess you have to apply the chain rule yourself for such a non-standard case ([this](https://math.stackexchange.com/questions/291376/differentiate-with-respect-to-a-function) may help).

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [April 1, 2020, 1:58pm UTC](https://discourse.julialang.org/t/question-about-computing-the-partial-derivatives-of-a-multivariate-polynomial/36835/3 "2020-04-01T13:58:32Z")

</div>

I don’t think there’s such a thing as the partial derivative of a multi-variate polynomial by another multi-variate polynomial; at least not without additional structure (e.g. a metric). What is the result you expect?

---

<div class="post-metadata">

**Author:** ![Yusen\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yusen_liu/32/13760_2.png) [@Yusen\_Liu](https://discourse.julialang.org/u/Yusen_Liu)\
**Post date:** [April 6, 2020, 5:45am UTC](https://discourse.julialang.org/t/question-about-computing-the-partial-derivatives-of-a-multivariate-polynomial/36835/4 "2020-04-06T05:45:59Z")

</div>

Yes, I agree with you. And there’s no package or methods that have been already written that can be used to solve this problem.

---

<div class="post-metadata">

**Author:** ![Yusen\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yusen_liu/32/13760_2.png) [@Yusen\_Liu](https://discourse.julialang.org/u/Yusen_Liu)\
**Post date:** [April 6, 2020, 5:47am UTC](https://discourse.julialang.org/t/question-about-computing-the-partial-derivatives-of-a-multivariate-polynomial/36835/5 "2020-04-06T05:47:41Z")

</div>

I’m expecting the derivative of a specific polynomial. For instance, computing the derivative of

```julia
x1^2 + x2^2 + x3^3

```

with respect to

```julia
x1+x2

```

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [April 6, 2020, 8:03am UTC](https://discourse.julialang.org/t/question-about-computing-the-partial-derivatives-of-a-multivariate-polynomial/36835/6 "2020-04-06T08:03:03Z")

</div>

Everyone knows what you means when you say “the derivative with respect to `x1`” or “the derivative with respect to `x2`”. It is not clear what you would mean by “the derivative with respect to `x1 + x2`” or, say, “the derivative with respect to `x1^2 * x2`”. Whatever definition you have in mind is probably not as standard as you seem to think it is. What definition are you using?

To expand a bit on the maths: the partial derivative \frac{\partial}{\partial x\_1} implicitly also depends on the coordinates x\_2, x\_3, \cdots because it computes a slope along the submanifold where x\_2,x\_3,\cdots are constant. So you can’t just ask for the result of, say, \frac{\partial}{\partial(x\_1^2x\_2)} without specifying the coordinate system that x\_1^2x\_2 is a part of.
