# Using ModelingToolkit to invert a symbolic matrix

Hi. I’m trying to invert a mass matrix with the purpose of decoupling the second order derivatives of a dynamic system, using ModelingToolkit.jl. The matrix is something like this:

In Julia, the elements of this matrix are ModelingToolkit.Constants or Operations. Using inv (M), the code runs for a long time and fails to get an answer. Is there any alternative? Sorry if this question is stupid, i’m trying to switch from Wolfram Mathematica to Julia.

Thank you!

1 Like

Probably the answer most will give you is to post a small code example to show how you’re setting up the computation (the proverbial minimum working example, mwe).

I’ve also move from Mathematica to Julia and there were a few rough spots on the way, but the Julia passes my critical test on when to adopt a new tool: I naturally keep using it and don’t even think about Mathematica much any more. Except when I need to use its symbolic tools to develop/derive analytic results!

In any case, a code example would help answer your question - and I’ve been working on similar speed issues and am interested in seeing solutions 2 Likes

Symbolic inverses of such a matrix are pretty huge. Are you sure it’s something that can be reasonable represented?

2 Likes

How long does this take on Mathematica?

@shashi is this why you were talking about lu pivoting?

The code, developed with the purpose of explaining the equations of motion, is a little long. Basically, I use the formalism of analytical mechanics to deduce the equations of motion of a system with 11 degrees of freedom. However, the second derivatives are coupled. For example, the beginning of my system of differential equations is something like this (just to get an idea about the symbolic variables).

This is a vector with 11 terms, each referring to a big differential equation, obtained analytically.
It is important to decouple the second derivatives. For this, I used the following command, in order to obtain the mass matrix, where d_q_2 are the second order derivatives of my variables.

``````Matrix_M  = ModelingToolkit.jacobian(EOM, d_q_2)
``````

The output is equal to the matrix I mentioned:

1 Like

Two minutes, maybe. But, there’s one faster alternative: use “solve” with equations of motion and the second order derivatives as inputs. This command can decouple the system. Is there something like this in Julia?

1 Like

You are right. It also takes an inappropriate time in Wolfram. Perhaps, decoupling the system at each time step is appropriate

If it’s just a linear solve that you need, I am working on that and trying to make it pretty fast! It is a solve based on LU with a special kind of symbolic pivoting.

1 Like

But it may not work as smartly as Mathematica, will be interesting to see what else you will need.

Have you tried it with Reduce.jl?