# Why does DiffEqFlux.NeuralODEMM require a constraint equation and a mass matrix?

**URL:** <https://discourse.julialang.org/t/why-does-diffeqflux-neuralodemm-require-a-constraint-equation-and-a-mass-matrix/57428>\
**Category:** Machine Learning\
**Tags:** diffeq\
**Created:** [March 18, 2021, 1:05am UTC](https://discourse.julialang.org/t/why-does-diffeqflux-neuralodemm-require-a-constraint-equation-and-a-mass-matrix/57428 "2021-03-18T01:05:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bad\_at\_math](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@bad\_at\_math](https://discourse.julialang.org/u/bad_at_math)\
**Post date:** [March 18, 2021, 1:05am UTC](https://discourse.julialang.org/t/why-does-diffeqflux-neuralodemm-require-a-constraint-equation-and-a-mass-matrix/57428/1 "2021-03-18T01:05:34Z")

</div>

Hello everyone!

I’m looking at the “Enforcing physical constraints via universal differential-algebraic equations” example ([https://diffeqflux.sciml.ai/dev/examples/physical\_constraints/](https://diffeqflux.sciml.ai/dev/examples/physical_constraints/)), which trains a neural ordinary differential equation to solve a stiff ODE with constraints. The model is defined as follows:

```julia
model_stiff_ndae = NeuralODEMM(nn_dudt2, (u, p, t) -> [u[1] + u[2] + u[3] - 1],
                               tspan, M, Rodas5(autodiff=false), saveat = 0.1)

```

As best I can tell, the mass matrix `M = [1. 0 0 // 0 1. 0 // 0 0 0]` and the constraint equation `(u, p, t) -> [u[1] + u[2] + u[3] - 1]` convey the same information: that the state variables of the ODE must always sum to 1. Why are both supplied in this example?

Thank you!

---

<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:** [March 18, 2021, 12:12pm UTC](https://discourse.julialang.org/t/why-does-diffeqflux-neuralodemm-require-a-constraint-equation-and-a-mass-matrix/57428/2 "2021-03-18T12:12:33Z")

</div>

> [@bad\_at\_math](#):
>
> As best I can tell, the mass matrix `M = [1. 0 0 // 0 1. 0 // 0 0 0]` and the constraint equation `(u, p, t) -> [u[1] + u[2] + u[3] - 1]` convey the same information: that the state variables of the ODE must always sum to 1. Why are both supplied in this example?

The algebraic equation isn’t well-defined without the MM since it can be scaled.

---

<div class="post-metadata">

**Author:** ![bad\_at\_math](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@bad\_at\_math](https://discourse.julialang.org/u/bad_at_math)\
**Post date:** [March 18, 2021, 2:23pm UTC](https://discourse.julialang.org/t/why-does-diffeqflux-neuralodemm-require-a-constraint-equation-and-a-mass-matrix/57428/3 "2021-03-18T14:23:22Z")

</div>

@ChrisRackauckas Thank you, that makes sense. Can you also elaborate on the difference between the `NeuralDAE` ([https://github.com/SciML/DiffEqFlux.jl/blob/master/src/neural\_de.jl#L332](https://github.com/SciML/DiffEqFlux.jl/blob/master/src/neural_de.jl#L332)) and `NeuralODEMM` ([https://github.com/SciML/DiffEqFlux.jl/blob/master/src/neural\_de.jl#L419](https://github.com/SciML/DiffEqFlux.jl/blob/master/src/neural_de.jl#L419)) functions? I see that the former only requires specification of a constraint function, whereas the latter also needs the mass matrix. Does having the mass matrix enable more efficient calculation of adjoints or something?

---

<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:** [March 18, 2021, 2:24pm UTC](https://discourse.julialang.org/t/why-does-diffeqflux-neuralodemm-require-a-constraint-equation-and-a-mass-matrix/57428/4 "2021-03-18T14:24:40Z")

</div>

One is semi-explicit while the other is fully implicit. NeuralDAE itself is a bad architecture and the MM form is more stable.
