Skip to content

Instantly share code, notes, and snippets.

@neftlon
Created February 15, 2024 10:22
Show Gist options
  • Save neftlon/c0652e193bf856097152621ce9a1a3ba to your computer and use it in GitHub Desktop.
Save neftlon/c0652e193bf856097152621ce9a1a3ba to your computer and use it in GitHub Desktop.
import numpy as np, itertools, functools, operator
def matrix_exp(A, num_iters=100):
inv_facs = itertools.accumulate(range(1,num_iters+1), operator.mul, initial=1)
As = itertools.accumulate(range(num_iters), lambda Ap, _: np.dot(Ap, A), initial=A)
return sum(1. / inv_fac * A for inv_fac, A in zip(inv_facs, As))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment