# Jacobian of Symbolic Matrix multiplications

**URL:** https://discourse.julialang.org/t/jacobian-of-symbolic-matrix-multiplications/80002
**Category:** General Usage
**Tags:** question
**Created:** [April 25, 2022, 5:53pm UTC](https://discourse.julialang.org/t/jacobian-of-symbolic-matrix-multiplications/80002 "2022-04-25T17:53:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![achrafmam2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/achrafmam2/32/35672_2.png) [@achrafmam2](https://discourse.julialang.org/u/achrafmam2)
#### Post date: [April 25, 2022, 5:53pm UTC](https://discourse.julialang.org/t/jacobian-of-symbolic-matrix-multiplications/80002/1 "2022-04-25T17:53:21Z")

</div>

Hi,

Consider the following operation M \* x where A is a matrix and x a vector. I want to know if we can do the following using the Symbolics package.

1. Calculate the Jacobian of M \* x with respect to M[1], M[2], \dots .
2. Use the symbolic expression from one and evaluate it using arbitrary values.
3. (Optional) can I compile the function to be equivalent to a normal Julia function. I am mainly interested in speed here.

Here is what I thought should work, but didn’t.

```julia
@variables M[1:3, 1:3], x[1:3]
Symbolics.jacobian(M*x, reshape(M, 9)) # Does not work
Symbolics.jacobian(M*x, [M[1,1], M[1,2]]) # Does not work

```

Update:

I managed to get a working version but it’s the API does not feel intuitive.

```julia
@variables M[1:3, 1:3], x[1:3]
c = reshape(M, 3, 3) * reshape(x, 3, 1)
Symbolics.jacobian(reshape(c, 3), reshape(M, 9))

```

---

<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: [April 25, 2022, 6:45pm UTC](https://discourse.julialang.org/t/jacobian-of-symbolic-matrix-multiplications/80002/2 "2022-04-25T18:45:05Z")

</div>

Another @shashi one. @achrafmam2 can you open an issue on Symbolics.jl? There’s just a set of issues right now with differentiation of array variables.

The workaround of course is to `collect(M); collect(x)` etc. to transform them from array symbolics to arrays of symbolics, but since we’re currently working through these kinds of issues it would be good to have it in the list so we can address it. Thanks!

---

<div class="post-metadata">

### Author: ![achrafmam2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/achrafmam2/32/35672_2.png) [@achrafmam2](https://discourse.julialang.org/u/achrafmam2)
#### Post date: [April 26, 2022, 9:06pm UTC](https://discourse.julialang.org/t/jacobian-of-symbolic-matrix-multiplications/80002/3 "2022-04-26T21:06:47Z")

</div>

I filed two issues ([issue 1](https://github.com/JuliaSymbolics/Symbolics.jl/issues/583), [issue 2](https://github.com/JuliaSymbolics/Symbolics.jl/issues/584)).
