question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

scipy.linalg.expm does not give the expected result

See original GitHub issue

Hi all,

I have the following example and it gives the correct results when my matrix elements are not integers, however it does not work when they are integers:

Reproducing code example:

import numpy as np
import scipy

Q = np.array([[-500,500,0,0],[0,-550,360,190],[0,630,-630,0],[0,0,0,0]])   # Rate Matrix
pi = scipy.linalg.expm(Q*10) 

np.sum(pi,axis=1)
array([-0.63612061, -0.2620967 , -0.53480159,  1.        ])

But it works for the following case:

Q = np.array([[-500,500,0,0],[0,-550,360,190],[0,630,-630,0],[0,0,0,0]])   # Rate Matrix
pi = scipy.linalg.expm(Q*10.5) 

np.sum(pi,axis=1)
array([1., 1., 1., 1.])

Error message:

I do not get any error but the results are wrong.

Scipy/Numpy/Python version information:

scipy.__version__
'1.1.0'

Does anyone know why this is the case and why I am getting this error?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
WarrenWeckessercommented, Jun 28, 2018

Thanks for reporting the problem. This looks like a bug.

The expm code computes several even matrix powers of the input A while maintaining the data type of A. Q*10 is an integer array, and the values in it are large enough that the values in the matrix power A**6 overflow (using 64 bit integers). Once that happens, the result of the computation is garbage.

The work-around (as you discovered) is to be sure that the input array is floating point.

0reactions
ilayncommented, Jun 29, 2018

Strange. I can’t replicate it on 1.2.0.dev0+c79b48d. Must have installed from one of my branches.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.linalg.expm — SciPy v1.9.3 Manual
Implements the algorithm given in [1], which is essentially a Pade approximation with a variable order that is decided based on the array...
Read more >
Difference between scipy.linalg.expm versus hand-coded one
we see that the result is basically the same, so i think it's safe to use scipy.linalg.expm for matrix exponentiation.
Read more >
linalg/_expm_frechet.py · alkaline-ml/scipy - Gemfury
Disabling may give a performance gain, but may result in problems (crashes, ... M[3:, 3:] = A >>> expm_M = scipy.linalg.expm(M) >>> np.allclose(expm_A, ......
Read more >
scipy.optimize.minimize fails to converge but result is OK
However, the result matches reasonably closely the expected result ... L = jax.scipy.linalg.expm(C) @ L0 return ssr + jnp.linalg.norm(x - x ...
Read more >
Multistate Analysis with Infinite Mixtures of Markov Chains ...
In this appendix, we provide further details on the discrete- time model. ... 1998], our compound process does not: By construction, the.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found