コード例 #1
0
def test_NumpyEigenvalues_diagonal():
    """
    Function for testing that NumpyEigenvalues returns the analytic eigenvalues
    for a diagonal matrix.
    """
    A = np.diag([1, 2, 3])
    Eig = EigenvalueProblem()

    calc_eig, vec = Eig(A, "NumPy")
    exp_eig = [1, 2, 3]
    tol = 1e-5

    assert abs(calc_eig[0] - exp_eig[0]) < tol
    assert abs(calc_eig[1] - exp_eig[1]) < tol
    assert abs(calc_eig[2] - exp_eig[2]) < tol
コード例 #2
0
def test_JacobiEigenvalues():
    """
    Function for testing that JacobiEigenvalues returns the analytic eigenvalues
    for a non-diagonal symmetric matrix.
    """
    A = np.array([[8,2,2,-6,9], [2,8,2,-6,9],[2,2,8,-6,9],[-6,-6,-6,24,9],\
                 [9,9,9,9,-21]])
    Eig = EigenvalueProblem()

    calc_eig, vec = Eig(A, "Jacobi", 300)
    exp_eig = [-30., 6., 6., 15., 30.]
    tol = 1e-5

    assert abs(calc_eig[0] - exp_eig[0]) < tol
    assert abs(calc_eig[1] - exp_eig[1]) < tol
    assert abs(calc_eig[2] - exp_eig[2]) < tol
    assert abs(calc_eig[3] - exp_eig[3]) < tol
    assert abs(calc_eig[4] - exp_eig[4]) < tol
コード例 #3
0
def test_JacobiEigenvalues_orthogonality():
    """
    Function for testing that JacobiEigenvalues preserves orthogonality after
    performing the similarity transform.
    """
    A = np.array([[8,2,2,-6,9], [2,8,2,-6,9],[2,2,8,-6,9],[-6,-6,-6,24,9],\
                 [9,9,9,9,-21]])
    N = A.shape[0]
    tol = 1e-5

    Eig = EigenvalueProblem()

    calc_eig, calc_vec = Eig(A, "Jacobi")

    for i in range(N):
        for j in range(N):
            if i == j:
                assert abs(np.dot(calc_vec[i], calc_vec[j]) - 1) < tol
            if i != j:
                assert abs(np.dot(calc_vec[i], calc_vec[j])) < tol
コード例 #4
0
import numpy as np
import matplotlib.pyplot as plt
from eigenvalue_problem import EigenvalueProblem

omega_r = [0.01, 0.5, 1., 5.]
rho = np.linspace(0, 5, 1e2 + 2)
rho = rho[1:-1]
h = rho[1]-rho[0]

for i in range(len(omega_r)):
    V = (omega_r[i]**2) * (rho**2) + 1/rho
    d = np.ones(len(rho)) * (2/(h**2) + V)
    e = np.ones(len(rho)) * (-1/h**2)

    T = np.diag(d) + np.diag(e[:-1], k=-1) + np.diag(e[:-1], k=1)
    f = EigenvalueProblem()
    val_j, vec_j = f(T, "Jacobi_cpp", 1e4)
    val_arma, vec_arma = f(T, "Armadillo")

    print(f'omega_r={omega_r[i]}')
    print('Armadillo:', val_arma[:5])
    print('Jacobi:', val_j[:5])

    plt.plot(rho, vec_j[0], label=f'$\lambda$={val_j[0]:.4f}')
    plt.plot(rho, vec_j[1], label=f'$\lambda$={val_j[1]:.4f}')
    plt.plot(rho, vec_j[2], label=f'$\lambda$={val_j[2]:.4f}')
    plt.title(f'Eigenvectors of 2 electrons in a H.O. potential, $\omega_r=${omega_r[i]}')
    plt.xlabel(r'$\rho$', fontsize=13)
    plt.ylabel(r'$\psi(\rho)$', fontsize=13)
    plt.legend()
    plt.savefig(f'Figures/eigvec_qdots2_{i}')