Jacobian of Symbolic Matrix multiplications

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.

``````@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.

``````@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))
``````

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!

I filed two issues (issue 1, issue 2).

2 Likes