Skip to content

Add alternative matrix exponential function#97

Merged
clinssen merged 5 commits into
nest:mainfrom
clinssen:alternative_expm
Jun 3, 2026
Merged

Add alternative matrix exponential function#97
clinssen merged 5 commits into
nest:mainfrom
clinssen:alternative_expm

Conversation

@clinssen

@clinssen clinssen commented Nov 26, 2025

Copy link
Copy Markdown
Contributor

Add contributed expMt function for faster matrix exponentiation.

CI failure will be fixed after merging with #93.

Before merging this, check in with @oscarbenjamin about licensing, citing, and functionality.

@oscarbenjamin

Copy link
Copy Markdown

Feel free to use the code under whichever license is most convenient for ode-toolbox. Presumably that is GPL V2?

I don't know if you would usually put someone's name in the copyright headers but if so feel free to put my name there.

Sorry that I haven't gotten round to including this in SymPy itself. One thing that I haven't quite resolved with this code is handling the conversion to sin/cos rather than exp:

In [2]: M = Matrix([[1, 1], [-1, 1]])

In [3]: M.exp()
Out[3]: 
⎡cos(1)   sin(1)⎤
⎢                   ⎥
⎣-sin(1)  cos(1)⎦

In [6]: expMt(M, t)
Out[6]: 
⎡            ⎛ 2                 at⎞              ⎛ 2                         at⎞⎤
⎢     RootSumw  - 2w + 2, aRootSumw  - 2w + 2, a ↦ (1 - a)⋅   ⎠⎥
⎢     ───────────────────────────────       ───────────────────────────────────────⎥
⎢                    2                                         2                   ⎥
⎢                                                                                  ⎥
⎢        ⎛ 2                         at⎞              ⎛ 2                 at⎞    ⎥
⎢-RootSumw  - 2w + 2, a ↦ (1 - a)⋅RootSumw  - 2w + 2, a   ⎠    ⎥
⎢─────────────────────────────────────────      ───────────────────────────────    ⎥
⎣                    2                                         2In [7]: expMt(M, t).doit()
Out[7]: 
⎡    t⋅(1 - )    t⋅(1 + )        t⋅(1 - )      t⋅(1 + )⎤
⎢                                                  ⎥
⎢   ────────── + ──────────     ──────────── - ────────────⎥
⎢       2            2               2              2      ⎥
⎢                                                          ⎥
⎢     t⋅(1 - )      t⋅(1 + )     t⋅(1 - )    t⋅(1 + )  ⎥
⎢                                                  ⎥
⎢- ──────────── + ────────────    ────────── + ──────────  ⎥
⎣       2              2              2            2

There should be something to handle the complex conjugate root pairs but in general symbolically it is difficult to separate real roots from complex conjugate pairs. When working with actual complex numbers it is most likely best just to stick with exponentials.

@clinssen

Copy link
Copy Markdown
Contributor Author

Thank you for the very kind reply!

I'm afraid these internal magics are a little over my head, so I have added a link to this discussion thread too just for reference.

@clinssen clinssen added this to the v3.0.0 milestone Feb 25, 2026
@clinssen clinssen force-pushed the alternative_expm branch from 5e059fc to 43eff18 Compare June 3, 2026 07:16
@clinssen clinssen force-pushed the alternative_expm branch from 43eff18 to 543ba59 Compare June 3, 2026 07:32

@pnbabu pnbabu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@clinssen

clinssen commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the review!

@clinssen clinssen merged commit c44d9c1 into nest:main Jun 3, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants