<jupyter_start><jupyter_text>Test matrix<jupyter_code>n = 7
A = normal(0, 1, (n, n))
around(A, 3)<jupyter_output><empty_output><jupyter_text>SVD<jupyter_code>U, sigma, VT = svd(A)
Ak = zeros([n, n])
for j in arange(n):
    uj = U[:, j]
    vj = VT[j, :]
    Ak += sigma[j]*uj[:, None]@vj[:, None].T
    print(norm(A - Ak, 'fro'), norm(sigma[j]*uj[:, None]@vj[:, None].T, 'fro'))<jupyter_output>5.141463243850217 4.9089336465493725
3.598783394261357 3.6719752680337456
2.3625407852062694 2.71470863207237
1.4026110221146302 1.9011263194237282
0.9074592629144854 1.069502391539236
0.28260687489302216 0.8623315302785243
1.48074467654008e-14 0.2826068748930217<jupyter_text>QR<jupyter_code>Q, R = qr(A)
Ak = zeros([n, n])
for j in arange(n):
    qj = Q[:, j]
    rj = R[j, :]
    Ak += qj[:, None]@rj[:, None].T
    print(norm(A - Ak, 'fro'), norm(qj[:, None]@rj[:, None].T, 'fro'))<jupyter_output>6.269923656492829 3.349676279260266
4.822693190759097 4.006691034513924
4.639897435307547 1.315188732471734
3.6432107761208012 2.8732670343932223
1.8963924009516753 3.1107363470495977
0.8493396297731177 1.695560772040956
2.9373412396009384e-15 0.8493396297731175<jupyter_text>Cholesky<jupyter_code>R = cholesky(A.T@A)
Ak = zeros([n, n])
for j in arange(n):
    rj = R[:, j]
    Ak += rj[:, None]@rj[:, None].T
    print(norm(A.T@A - Ak, 'fro'), norm(rj[:, None]@rj[:, None].T, 'fro'))<jupyter_output>24.570371331346216 11.220331175838894
14.70000698039827 16.053573046054254
14.257554298205397 1.7297214020206009
10.345457340959975 8.255663450930825
2.986200997280179 9.67668062085547
0.7213778067031367 2.8749263316841236
2.5182736389992033e-15 0.7213778067031361<jupyter_text>Example: Gaussian elimination with full pivotingFull pivoting is not used much in practice for computing a generalized LU factorization because it is computationally expensive with little improvement in stability (Trefethen and Bau). It is, however, the basis for the 2D Chebfun package.<jupyter_code>A = rand(3, 3)
A
E0 = A
i1, j1 = unravel_index(argmax(absolute(E0)), A.shape)
i1, j1
u1 = E0[:, j1]
v1 = E0[i1, :].T
A1 = u1[:, None]@v1[:, None].T/E0[i1, j1]
A1
E1 = E0 - A1
E1
i2, j2 = unravel_index(argmax(absolute(E1)), A.shape)
i2, j2
u2 = E1[:, j2]
v2 = E1[i2, :].T
A2 = u2[:, None]@v2[:, None].T/E1[i2, j2]
A2
A1 + A2
E2 = E1 - A2
E2
i3, j3 = unravel_index(argmax(absolute(E2)), A.shape)
i3, j3
u3 = E2[:, j3]
v3 = E2[i3, :].T
A3 = u3[:, None]@v3[:, None].T/E2[i3, j3]
A3
E2 - A3<jupyter_output><empty_output><jupyter_text>The vectors and pivots can be used to construct the L,U,P, and Q matrices$P$ and $Q$ are permutation matrices$$ PAQ = LU$$ Notice the pattern of zero entries in the vectors seen below<jupyter_code>print(u1)
print(u2)
print(u3)
print(v1)
print(v2)
print(v3)
print(norm(A1, 'fro'))
print(norm(A2, 'fro'))
print(norm(A3, 'fro'))<jupyter_output>1.0257075599381273
0.8570007883352344
0.08265396450993387
{'fraction_non_alphanumeric': 0.15787659427783524, 'fraction_numerical': 0.28852119958634953, 'mean_word_length': 5.595454545454546, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '289611', 'license': 'Apache-2.0', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1380, 'n_words': 278, 'path': 'Week 11.ipynb', 'repo_name': 'newby-jay/MATH381-Fall2021-JupyterNotebooks', 'size': '2901'}