def helper(self,T,prec=10): Nvals = [10, 50, 100, 200] for n in Nvals: A = array(random.random((n,n)), dtype=T) A = A/norm(A) v = ones((n,1), dtype=T) # C++ version expA = zeros_like(A) e.expm(A,expA) # assertion assert_almost_equal(expm(A),expA,prec)
def matrix_exp_pade_C(A, v, factor): r"""Compute the solution of :math:`v' = A v` with a full matrix exponential via Pade approximation. Uses an efficient C++ implementation via the Eigen matrix library. If the compiled module is not importable, it falls back to the scipy version. :param A: The matrix :math:`A` of shape :math:`N \times N`. :param v: The vector :math:`v` of length :math:`N`. :param factor: An additional scalar factor :math:`\alpha`. :return: The (approximate) value of :math:`\exp\left(-i \alpha A\right) v` """ import cpade Anew = -1.0j*A*factor expA = zeros_like(Anew) cpade.expm(Anew,expA) return dot(expA, v)
def runtiming(T, Nvals, Tc, reps): """runs timing with given type T""" print "current type:", type_to_name[T] Tc[T] = [] for n in Nvals: print " n:", n, A = array(random.random((n, n)), dtype=T) ctime = inf # time C++ version for r in range(reps): expA = zeros_like(A) t = time.time() e.expm(A, expA) # call C version with Eigen Pade approx. ctime = min(ctime, time.time() - t) print ctime Tc[T].append(ctime) del A, expA
for T in [float32, float64, complex64, complex128]: thistime = time.time() print "current type:", type_to_name[T] Tc = [] Tp = [] Tp2= [] for n in Nvals: print "n:",n, A = array(random.random((n,n)), dtype=T) ctime = inf; ptime = inf; p2time = inf # time C++ version for r in range(Creps): expA = zeros_like(A) t = time.time() e.expm(A,expA) # call C version with Eigen Pade approx. ctime = min(ctime, time.time()-t) # time scipy version for r in range(Preps): t = time.time() expA2 = expm(A) ptime = min(ptime, time.time()-t) # time new python version for r in range(P2reps): t = time.time() expA3 = e2.expm(A) p2time = min(p2time, time.time()-t) Tc.append(ctime)