def helper(self,T,prec=10): k = 7 Nvals = [10, 50, 100, 1000, 5000] for n in Nvals: A = array(random.random((n,n)), dtype=T) v = ones((n,1), dtype=T) # C++ version V = zeros((n,k), dtype=T) H = zeros((k+1,k), dtype=T) a.arnoldi(A,v,k,V,H) # Python version Vloc,Hloc = arnoldi(A,v,k) # assertion assert_almost_equal(norm(H)/n,norm(Hloc)/n,prec)
def matrix_exp_arnoldi_C(A, v, factor, k): r"""Compute the solution of :math:`v' = A v` via :math:`k` steps of a the Arnoldi krylov method. Uses a C++ implementation. :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`. :param k: The number :math:`k` of Krylov steps performed. :return: The (approximate) value of :math:`\exp\left(-i \alpha A\right) v`. """ # try to import compiled module from carnoldi import arnoldi k = min(min(A.shape), k) V = zeros((A.shape[0],k),dtype=A.dtype) H = zeros((k+1,k),dtype=A.dtype) arnoldi(A, v, k, V, H) eH = mat(expm(-1.0j*factor*H[:-1,:])) r = V * eH[:,0] return asarray(r * norm(v))
def matrix_exp_arnoldi(A, v, factor, k): r"""Compute the solution of :math:`v' = A v` via :math:`k` steps of a the Arnoldi krylov method. :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`. :param k: The number :math:`k` of Krylov steps performed. :return: The (approximate) value of :math:`\exp\left(-i \alpha A\right) v`. """ V, H = arnoldi(A, v, min(min(A.shape), k)) eH = mat(expm(-1.0j*factor*H[:-1,:])) r = V[:,:-1] * eH[:,0] return asarray(r * norm(v))
print "current type:", type_to_name[T] Tc = [] Tp = [] for n in Nvals: print "n:", n #A = array(diag(2*ones(n)) - diag(ones(n-1),1) - diag(ones(n-1),-1), dtype=t) A = array(random.random((n, n)), dtype=T) v = ones((n, 1), dtype=T) ctime = inf ptime = inf for r in range(Creps): # time C++ version V = zeros((n, k), dtype=T) H = zeros((k + 1, k), dtype=T) t = time.time() a.arnoldi(A, v, k, V, H) ctime = min(ctime, time.time() - t) for r in range(Preps): # time python version t = time.time() Vloc, Hloc = arnoldi(A, v, k) ptime = min(ptime, time.time() - t) Tc.append(ctime) Tp.append(ptime) normHH = norm(H - Hloc) / norm(H) # "relative" norm epsilon = 1e-5 if normHH > epsilon: print "H-Hloc is not small (%f>%f)!" % (normHH, epsilon) del A, v, V, H, Vloc, Hloc plt.loglog(Nvals, Tp, '-o', label="Python")
for T in [float32, float64, complex64, complex128]: print "current type:", type_to_name[T] Tc = [] Tp = [] for n in Nvals: print "n:",n #A = array(diag(2*ones(n)) - diag(ones(n-1),1) - diag(ones(n-1),-1), dtype=t) A = array(random.random((n,n)), dtype=T) v = ones((n,1), dtype=T) ctime = inf; ptime = inf for r in range(Creps): # time C++ version V = zeros((n,k), dtype=T) H = zeros((k+1,k), dtype=T) t = time.time() a.arnoldi(A,v,k,V,H) ctime = min(ctime, time.time()-t) for r in range(Preps):# time python version t = time.time() Vloc,Hloc = arnoldi(A,v,k) ptime = min(ptime, time.time()-t) Tc.append(ctime) Tp.append(ptime) normHH = norm(H-Hloc)/norm(H) # "relative" norm epsilon = 1e-5 if normHH > epsilon: print "H-Hloc is not small (%f>%f)!"%(normHH,epsilon) del A, v, V, H, Vloc, Hloc plt.loglog(Nvals, Tp, '-o', label="Python")