<jupyter_start><jupyter_text>Diagonizing matrices in pythonThis shows you a couple of ways to find eigenvalues and eigenvectors of matrices in python.  The first method is numerical and the second method is analytical.  Numerical eigenvalues/eigenvectorsUsing the `numpy` library.<jupyter_code>import numpy as np # import the numpy numerical calculation library
#define our matrix below.
matrix = np.array([[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]])
print(matrix)<jupyter_output>[[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]<jupyter_text>The numpy library function to calculate eigenvalues and eigenvectors is called `np.linalg.eig()`.<jupyter_code># calculate the eigenvalues and eigenvectors of matrix
eigenvalues, eigenvectors = np.linalg.eig(matrix)
print(eigenvalues)
# note that each column is one eigenvector
print(eigenvectors)
help(np.linalg.eig)<jupyter_output>Help on function eig in module numpy.linalg.linalg:

eig(a)
    Compute the eigenvalues and right eigenvectors of a square array.
    
    Parameters
    ----------
    a : (..., M, M) array
        Matrices for which the eigenvalues and right eigenvectors will
        be computed
    
    Returns
    -------
    w : (..., M) array
        The eigenvalues, each repeated according to its multiplicity.
        The eigenvalues are not necessarily ordered. The resulting
        array will be of complex type, unless the imaginary part is
        zero in which case it will be cast to a real type. When `a`
        is real the resulting eigenvalues will be real (0 imaginary
        part) or occur in conjugate pairs
    
    v : (..., M, M) array
        The normalized (unit "length") eigenvectors, such that the
        column ``v[:,i]`` is the eigenvector corresponding to the
        eigenvalue ``w[i]``.
    
    Raises
    ------
    LinAlgError
        If the eigenvalue computation does not [...]<jupyter_text>If the matrix is symmetric, the function `np.linalg.eigh()` is faster and more robust.<jupyter_code># This is a hermitian matrix for testing the eigh() function
matrix2 = np.array([[1.,2.,3.],[2.,4.,5.],[3.,5.,6.]])
print(matrix2)
eigenvalues2, eigenvectors2 = np.linalg.eigh(matrix2)
print(eigenvalues2)
print(eigenvectors2)<jupyter_output>[[-0.73697623  0.59100905 -0.32798528]
 [-0.32798528 -0.73697623 -0.59100905]
 [ 0.59100905  0.32798528 -0.73697623]]<jupyter_text>Analytic eigenvalues/eigenvectorsUsing the `sympy` library<jupyter_code>import sympy
sympy.init_printing() # turn on pretty printing of math expressions<jupyter_output><empty_output><jupyter_text>With `sympy` we have to first declare any variables that we wish to use.Note the form below.  The names of the new variables appear twice.<jupyter_code>a, b, c = sympy.symbols('a, b, c')
matrixA = sympy.Matrix([[a,b],[b,c]])
matrixA
# This returns the eigenvalues, their degeneracy, and the eigenvectors corresponding to each eigenvalue
matrixA.eigenvects()
help(matrixA.eigenvects)<jupyter_output>Help on method eigenvects in module sympy.matrices.matrices:

eigenvects(error_when_incomplete=True, iszerofunc=<function _iszero at 0x000001F9BD792598>, **flags) method of sympy.matrices.dense.MutableDenseMatrix instance
    Return list of triples (eigenval, multiplicity, basis).
    
    The flag ``simplify`` has two effects:
        1) if bool(simplify) is True, as_content_primitive()
        will be used to tidy up normalization artifacts;
        2) if nullspace needs simplification to compute the
        basis, the simplify flag will be passed on to the
        nullspace routine which will interpret it there.
    
    Parameters
    ==========
    
    error_when_incomplete : bool
        Raise an error when not all eigenvalues are computed. This is
        caused by ``roots`` not returning a full list of eigenvalues.
    
    If the matrix contains any Floats, they will be changed to Rationals
    for computation purposes, but the answers will be returned after being
    evaluat[...]
{'fraction_non_alphanumeric': 0.09609533468559837, 'fraction_numerical': 0.033468559837728194, 'mean_word_length': 3.61943793911007, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '454372', 'license': 'MIT', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1174, 'n_words': 429, 'path': 'Eigenvalues.ipynb', 'repo_name': 'corcoted/Phys475', 'size': '3944'}