-
-
Notifications
You must be signed in to change notification settings - Fork 227
Description
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
ModelingToolkit.jl/src/linearization.jl
Line 816 in 091c445
similarity_transform(sys, P; unitary = true) |
See:
ModelingToolkit.jl/src/linearization.jl
Lines 807 to 817 in 091c445
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 |
ModelingToolkit.jl/src/linearization.jl
Lines 765 to 791 in 091c445
""" | |
(; Ã, 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.