def kedmd(X, Y, k, epsilon=0, evs=5, operator='P'): ''' Kernel EDMD for the Koopman or Perron-Frobenius operator. The matrices X and Y contain the input data. :param k: kernel, see d3s.kernels :param epsilon: regularization parameter :param evs: number of eigenvalues/eigenvectors :param operator: 'K' for Koopman or 'P' for Perron-Frobenius (note that the default is P here) :return: eigenvalues d and eigenfunctions evaluated in X ''' if isinstance(X, list): # e.g., for strings n = len(X) else: n = X.shape[1] G_0 = _kernels.gramian(X, k) G_1 = _kernels.gramian2(X, Y, k) if operator == 'K': G_1 = G_1.T A = _sp.linalg.pinv(G_0 + epsilon * _np.eye(n), rcond=1e-15) @ G_1 d, V = sortEig(A, evs) if operator == 'K': V = G_0 @ V return (d, V)
k = kernels.gaussianKernel(0.5) #%% apply kernel generator EDMD epsilon = 0.1 S = np.einsum('ijk,ljk->ilk', Z, Z) # sigma \cdot sigma^T G_00 = kernels.gramian(X, k) G_10 = np.zeros((m, m)) for i in range(m): for j in range(m): G_10[i, j] = Y[:, i].T @ k.diff(X[:, i], X[:, j]) + 0.5 * np.sum( S[:, :, i] * k.ddiff(X[:, i], X[:, j]), axis=(0, 1)) A, _, _, _ = np.linalg.lstsq(G_00, G_10, rcond=1e-12) d, V = algorithms.sortEig(A, evs=m, which='LM') W = kernels.gramian2(Omega.midpointGrid(), X, k) @ V #%% plot eigenvalues plt.figure() plt.plot(d, '.') plt.title('Spectrum') #%% plot eigenfunctions ind, = np.where(d < -0.1) for i in ind[:6]: plt.figure() Omega.plot(np.real(W[:, i] / np.amax(abs(W[:, i])))) plt.ylim((-1, 1)) plt.title('lambda = %f' % np.real(d[i])) #%% direct kernelization for kernels with finite-dimensional feature space
k = kernels.gaussianKernel(0.3) #%% apply kernel generator EDMD evs = 4 G_00 = kernels.gramian(X, k) G_10 = np.zeros((m, m)) for i in range(m): for j in range(m): G_10[i, j] = c0(X[:, i]) * k(X[:, i], X[:, j]) + np.sum( c2(X[:, i]) * k.ddiff(X[:, i], X[:, j]), axis=(0, 1)) A, _, _, _ = np.linalg.lstsq(G_00, G_10, rcond=1e-12) d, V = algorithms.sortEig(A, evs=evs, which='SR') c = Omega.midpointGrid() W = kernels.gramian2(c, X, k) @ V #%% plot spectrum plt.figure() plt.plot(np.real(d), '.') plt.title('Spectrum') printVector(np.real(d), 'd') #%% plot eigenfunctions for i in range(evs): plt.figure() j = evs - i - 1 Omega.plot(np.real(W[:, j] / np.amax(abs(W[:, j])))) wa = psi[i](c)
N = G_00[isBoundary, :].T #%% solve BVP epsilon = 1e-12 G_10 = np.zeros((m, m)) for i in range(m): for j in range(m): G_10[i, j] = -h**2/(2*m0) * k_a.laplace(X[:, i], X[:, j]) # no term related to the potential since V is 0 inside the box d, V = ceig.hcgeig(G_10, G_00 + epsilon*np.eye(m), N) # evaluate eigenfunctions in midpoints of the grid Omega2 = domain.discretization(bounds, np.array([30, 30])) c, _ = Omega2.vertexGrid() W = kernels.gramian2(c, X, k_a) @ V #%% plot spectrum plt.figure(2) plt.clf() plt.plot(d, '.') plt.title('Spectrum') #%% plot eigenfunctions ind, = np.where(abs(d) > 1.0) fig = 3 for i in ind[-3:][::-1]: #plt.figure(fig) plt.figure() plt.clf() fig += 1