Skip to content

ModelingToolkit.reorder_unknowns() returns the wrong permutation? #3814

@gg2031

Description

@gg2031

Describe the bug 🐞

ModelingToolkit.reorder_unknowns() returns the wrong permutation.

Expected behavior

P * A * P' rather than P' * A * P.

Minimal Reproducible Example 👇

The issue is a math error not a code error, so apologies for not including a MRE and skipping the remaining sections.

ModelingToolkit.reorder_unknowns() calls ModelingToolkit.similarity_transform() for which I believe we should have T = P' and not T = P, i.e.,

similarity_transform(sys, P'; unitary = true)

rather than

similarity_transform(sys, P; unitary = true)

See:

function reorder_unknowns(sys::NamedTuple, old, new)
nx = length(old)
length(new) == nx || error("old and new must have the same length")
perm = [findfirst(isequal(n), old) for n in new]
issorted(perm) && return sys # shortcut return, no reordering
P = zeros(Int, nx, nx)
for i in 1:nx # Build permutation matrix
P[i, perm[i]] = 1
end
similarity_transform(sys, P; unitary = true)
end

"""
(; Ã, B̃, C̃, D̃) = similarity_transform(sys, T; unitary=false)
Perform a similarity transform `T : Tx̃ = x` on linear system represented by matrices in NamedTuple `sys` such that
```
à = T⁻¹AT
B̃ = T⁻¹ B
C̃ = CT
D̃ = D
```
If `unitary=true`, `T` is assumed unitary and the matrix adjoint is used instead of the inverse.
"""
function similarity_transform(sys::NamedTuple, T; unitary = false)
if unitary
A = T'sys.A * T
B = T'sys.B
else
Tf = lu(T)
A = Tf \ sys.A * T
B = Tf \ sys.B
end
C = sys.C * T
D = sys.D
(; A, B, C, D)
end

Error & Stacktrace ⚠️

N/A

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
N/A
  • Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
N/A
  • Output of versioninfo()
N/A

Additional context

Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions