<jupyter_start><jupyter_text>This notebook shows some basic linear algebra operations in python. We will use the linear algebra package in numpy. Detailed documentation can be found in this [link](https://numpy.org/doc/stable/reference/routines.linalg.html).  Vector Operation  Dot product<jupyter_code># real vectors
a = [1, 2, 3]
b = [4, 5, 6]
print(np.dot(a, b))   # a dot b = 1*4 + 2*5 + 3*6
# complex vectors
a = [1 + 1j*2, 3 + 1j*0.5]
b = [2, 4]
print(np.dot(a, b))
print(np.dot(a, np.conj(a)))    # conjugate np.conj(a)<jupyter_output>(14+6j)
(14.25+0j)<jupyter_text>Matrix  Multiplication<jupyter_code>A = np.array([[1, 3],
              [2, 1]])
B = np.array([[4, 1],
              [2, 2]])
# AB != BA
print("AB = \n", np.matmul(A, B))
print("BA = \n", np.matmul(B, A))
A = np.array([[1, 3, 1],
              [2, 1, 2]])
B = np.array([[4, 1],
              [2, 2],
              [7, 3]])
print("A: ", A.shape, ", B: ", B.shape)
# AB != BA
print("AB = \n", np.matmul(A, B))
print("BA = \n", np.matmul(B, A))<jupyter_output>A:  (2, 3) , B:  (3, 2)
AB = 
 [[17 10]
 [24 10]]
BA = 
 [[ 6 13  6]
 [ 6  8  6]
 [13 24 13]]<jupyter_text>Determinant<jupyter_code>A = np.array([[1, 2],
              [3, 4]])
print("det(A) = {0:.2f}".format(np.linalg.det(A)))
A = np.array([[1, 2, 1],
              [2, 1, 2],
              [1, 3, 5]])
print("det(A) = {0:.2f}".format(np.linalg.det(A)))
A = np.random.random([5, 5])   # A random 5x5 matrix
print("A = \n", A)
print("det(A) = {0:.4e}".format(np.linalg.det(A)))<jupyter_output>A = 
 [[0.309 0.162 0.568 0.928 0.005]
 [0.352 0.188 0.678 0.755 0.654]
 [0.595 0.173 0.505 0.64  0.466]
 [0.161 0.187 0.237 0.792 0.939]
 [0.027 0.281 0.369 0.292 0.429]]
det(A) = 2.2206e-02<jupyter_text>Inverse<jupyter_code>A = np.array([[1, 2],
              [3, 4]])
Ainv = np.linalg.inv(A)
print("A = \n", A)
print("A^{-1} = \n", Ainv)
print("AA^{-1} = \n", np.matmul(A, Ainv))
A = np.random.random([4, 4])  # A random 4x4 matrix
Ainv = np.linalg.inv(A)
print("A = \n", A)
print("A^{-1} = \n", Ainv)
print("AA^{-1} = \n", np.matmul(A, Ainv))<jupyter_output>A = 
 [[0.72  0.423 0.867 0.092]
 [0.596 0.672 0.989 0.173]
 [0.673 0.656 0.539 0.983]
 [0.158 0.438 0.899 0.797]]
A^{-1} = 
 [[ 2.154 -1.541  0.741 -0.827]
 [-3.576  3.909  0.654 -1.244]
 [ 1.077 -0.494 -1.     1.216]
 [ 0.321 -1.284  0.622  0.73 ]]
AA^{-1} = 
 [[ 1.000e+00 -1.006e-16  6.474e-17  1.515e-16]
 [ 1.094e-16  1.000e+00 -1.371e-16 -1.064e-16]
 [ 3.287e-17  2.071e-16  1.000e+00  1.029e-17]
 [ 4.667e-17  2.337e-16 -1.485e-16  1.000e+00]]<jupyter_text>Eigenvalue and Eigenvector<jupyter_code>A = np.array([[1, -1],
              [1, 1]])
w, v = np.linalg.eig(A)
print("A = \n", A)
for i in range(len(w)):
    print("eigenvalue {0:d}: {1:.3f}, eigenvector: {2}".format(i, w[i], v[:, i]))
A = np.random.random([4, 4])  # A random 4x4 matrix
w, v = np.linalg.eig(A)
print("A = \n", A)
for i in range(len(w)):
    print("eigenvalue {0:d}: {1:.3f}, eigenvector: {2}".format(i, w[i], v[:, i]))<jupyter_output>A = 
 [[0.934 0.37  0.013 0.718]
 [0.615 0.53  0.977 0.865]
 [0.441 0.937 0.154 0.779]
 [0.033 0.341 0.619 0.934]]
eigenvalue 0: 2.271+0.000j, eigenvector: [-0.401+0.j -0.635+0.j -0.516+0.j -0.411+0.j]
eigenvalue 1: 0.467+0.192j, eigenvector: [-0.513-0.335j -0.38 +0.152j -0.233+0.127j  0.622+0.j   ]
eigenvalue 2: 0.467-0.192j, eigenvector: [-0.513+0.335j -0.38 -0.152j -0.233-0.127j  0.622-0.j   ]
eigenvalue 3: -0.654+0.000j, eigenvector: [-0.21 +0.j  0.606+0.j -0.749+0.j  0.166+0.j]<jupyter_text>Degeneracy<jupyter_code># The eigenvectors for the degenerate eigenvalue are orthogonal to each other
A = np.array([[1, 0, 1],
              [0, 2, 0],
              [1, 0, 1]])
w, v = np.linalg.eig(A)
print("A = \n", A)
for i in range(len(w)):
    print("eigenvalue {0:d}: {1:.3f}, eigenvector: {2}".format(i, w[i], v[:, i]))
# Matrix A is a defective
# The returned two eigen vectors are linear dependent of each other
A = np.array([[1, 1],
              [0, 1]])
w, v = np.linalg.eig(A)
print("A = \n", A)
for i in range(len(w)):
    print("eigenvalue {0:d}: {1:.3f}, eigenvector: {2}".format(i, w[i], v[:, i]))<jupyter_output>A = 
 [[1 1]
 [0 1]]
eigenvalue 0: 1.000, eigenvector: [1. 0.]
eigenvalue 1: 1.000, eigenvector: [-1.00e+00  2.22e-16]
{'fraction_non_alphanumeric': 0.23782639378969656, 'fraction_numerical': 0.16278522700541048, 'mean_word_length': 2.827182718271827, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 1, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '501362', 'license': 'MIT', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2065, 'n_words': 470, 'path': 'demos/LinearAlgebra.ipynb', 'repo_name': 'chunshen1987/WSU_PHYS5100_repo', 'size': '4251'}