Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] exp of SMatrix fails numerically #1295

Closed
juliohm opened this issue Feb 20, 2025 · 7 comments · Fixed by #1296
Closed

[BUG] exp of SMatrix fails numerically #1295

juliohm opened this issue Feb 20, 2025 · 7 comments · Fixed by #1296

Comments

@juliohm
Copy link
Contributor

juliohm commented Feb 20, 2025

MWE:

julia> using StaticArrays

julia> M = 800.0 * [-1 1; 1 -1]
2×2 Matrix{Float64}:
 -800.0   800.0
  800.0  -800.0

julia> exp(M)
2×2 Matrix{Float64}:
 0.5  0.5
 0.5  0.5

julia> S = SMatrix{2,2}(M)
2×2 SMatrix{2, 2, Float64, 4} with indices SOneTo(2)×SOneTo(2):
 -800.0   800.0
  800.0  -800.0

julia> exp(S)
2×2 SMatrix{2, 2, Float64, 4} with indices SOneTo(2)×SOneTo(2):
 NaN  NaN
 NaN  NaN
@mikmoore
Copy link

It looks like the method is described in Corollary 2.4 of this paper.

It seems a fix will require re-doing the algebra. Check my math, but every term appears to be of the form

$$\exp(x) \left(a \cosh(y) + b \sinh(y)\right) = \frac{a + b}{2} \exp(x + y) + \frac{a - b}{2} \exp(x - y)$$

Using the right-hand version should avoid the range issues of computing the exp and hyperbolics separately. It should be a pretty simple PR.

@juliohm
Copy link
Contributor Author

juliohm commented Feb 24, 2025

That is precisely the method, thank you for sharing @mikmoore.

I am trying to work my way to reach the result you shared, but no success so far. Could you perhaps share the steps so that we could both be sure of the derivation before a PR is submitted?

Which identities are used in your derivation?

@juliohm
Copy link
Contributor Author

juliohm commented Feb 24, 2025

From the linked paper, the terms have a $\delta$ in the denominator though, so can't be expressed as $a\cosh(y) + b\sinh(y)$, see

Image

That would be $a\cosh(y) + b\sinh(y) / y$

@mikmoore
Copy link

I just used the definitions $\cosh(x) = (\exp(x) + \exp(-x))/2$ and $\sinh(x) = (\exp(x) - \exp(-x))/2$. The equation above simply regroups the $\exp$'s from the hyperbolic functions with the leading $\exp$. The goal was to permit them to cancel to avoid the overflow issue you were seeing.

Sorry for recycling $a$ and $b$ in my statement when they were already used for matrix entries. Maybe that made it confusing. The (1,1) term uses $a = 1$, $b = \frac{a-d}{2\delta}$, $x = \frac{a+d}{2}$, $y=\delta$.

@juliohm
Copy link
Contributor Author

juliohm commented Feb 25, 2025

You used the identity $\cosh(x) = (\exp(x) + \exp(-x)) / 2$. But isn't that missing the imaginary term?

The identities I know are displayed below:

Image

Reference: https://hepweb.ucsd.edu/ph110b/110b_notes/node49.html

@mikmoore
Copy link

If $\cosh(ix) = \frac{\exp(ix) + \exp(-ix)}{2}$ then $\cosh(x) = \frac{\exp(x) + \exp(-x)}{2}$. The definitions you showed are intended to show the relationship between the trigonometric and hyperbolic functions, which is why they use imaginary arguments to the hyperbolics. For simpler definitions, see https://en.wikipedia.org/wiki/Hyperbolic_functions#Exponential_definitions.

@juliohm
Copy link
Contributor Author

juliohm commented Feb 25, 2025

Thank you. I thought that the identity only worked with the imaginary part. I will try to check the expression you shared above and submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants