# \[diffeqpy\] with sparse mass-matrix / jacobian

**URL:** https://discourse.julialang.org/t/diffeqpy-with-sparse-mass-matrix-jacobian/91984
**Category:** General Usage
**Tags:** diffeq
**Created:** [December 21, 2022, 11:04pm UTC](https://discourse.julialang.org/t/diffeqpy-with-sparse-mass-matrix-jacobian/91984 "2022-12-21T23:04:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ChrisProv](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@ChrisProv](https://discourse.julialang.org/u/ChrisProv)
#### Post date: [December 21, 2022, 11:04pm UTC](https://discourse.julialang.org/t/diffeqpy-with-sparse-mass-matrix-jacobian/91984/1 "2022-12-21T23:04:19Z")

</div>

Hello,

I have a moderately-complex Python script which involves a DAE system solve at its core:  
M(u) dudt = F(u)  
The matrices M and J (the Jacobian of F(x)) are both sparse, and I need to exploit for this large system. It appears that Julia can solve equations of this class:

> [@State Dependent Mass Matrix](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238):
>
> Hello, I’ve looked at the documentation for Handling Mass Matrices for DifferentialEquations.jl. In the example was a singular matrix. I would like to know how to implement a state dependent mass matrix, where the matrix M in equation Mu’ =f(u,p,t) has the dependent variables in the matrix itself. The image below is the equation I’m trying to replicate: I’m trying to integrate from Matlab to Julia for this type of problem. I thought replacing the mass\_matrix option with a functi…

Since it would be very difficult and time-consumming to convert my entire Python implementation over to Julia, I attempted to use “diffeqpy” package, which calls many of Julia’s powerful solvers:

> **[GitHub - SciML/diffeqpy: Solving differential equations in Python using...](https://github.com/SciML/diffeqpy)**
>
> Solving differential equations in Python using DifferentialEquations.jl and the SciML Scientific Machine Learning organization - GitHub - SciML/diffeqpy: Solving differential equations in Python us...

Unfortunately, these functions do not appear to be compatible with SciPy sparse matrices as input. In this example, I have to pass dense A,J matrices for it to work:

fun = de.ODEFunction(System\_F\_Julia,mass\_matrix=A.toarray(),jac\_prototype=J.toarray())  
prob = de.ODEProblem(fun, initial\_state, tspan, args)  
sol = de.solve(prob,de.Rosenbrock23(autodiff=False))

So here is the question:  
Is there an effective method to use the diffeqpy package with sparse mass matrix / jacobian? Is there a way to convert/reconstruct the SciPy sparse matrices into Julia-compatible sparse matrices for input to the above code?

Any help is much appreciated!  
-Chris

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 22, 2022, 3:10am UTC](https://discourse.julialang.org/t/diffeqpy-with-sparse-mass-matrix-jacobian/91984/2 "2022-12-22T03:10:16Z")

</div>

> [@ChrisProv](#):
>
> Is there a way to convert/reconstruct the SciPy sparse matrices into Julia-compatible sparse matrices for input to the above code?

See, for example: [Convert to sparse · Issue #204 · JuliaPy/PyCall.jl · GitHub](https://github.com/JuliaPy/PyCall.jl/issues/204#issuecomment-146557380)

---

<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: [October 21, 2023, 1:44pm UTC](https://discourse.julialang.org/t/diffeqpy-with-sparse-mass-matrix-jacobian/91984/3 "2023-10-21T13:44:59Z")

</div>

There is now a part of the documentation in diffeqpy which addresses how to do this:

> **[GitHub - SciML/diffeqpy: Solving differential equations in Python using...](https://github.com/SciML/diffeqpy#mass-matrices-sparse-arrays-and-more)**
>
> Solving differential equations in Python using DifferentialEquations.jl and the SciML Scientific Machine Learning organization - GitHub - SciML/diffeqpy: Solving differential equations in Python us...
