<jupyter_start><jupyter_text>Linear algebra<jupyter_code>import numpy as np
np.__version__<jupyter_output><empty_output><jupyter_text>Matrix and vector products Q1. Predict the results of the following code.<jupyter_code>x = [1,2]
y = [[4, 1], [2, 2]]
#print np.dot(x, y)
#print np.dot(y, x)
#print np.matmul(x, y)
#print np.inner(x, y)
#print np.inner(y, x)
print(np.dot(x, y))
print(np.dot(y, x))
print(np.matmul(x, y))
print(np.inner(x, y))
print(np.inner(y, x))
print(np.matmul(y, x))<jupyter_output>[8 5]
[6 6]
[8 5]
[6 6]
[6 6]
[6 6]<jupyter_text>Q2. Predict the results of the following code.<jupyter_code>x = [[1, 0], [0, 1]]
y = [[4, 1], [2, 2], [1, 1]]
#print np.dot(y, x)
#print np.matmul(y, x)

x = [[1, 0], [0, 1]]
y = [[4, 1], [2, 2], [1, 1]]
print(np.dot(y, x))
print(np.matmul(y, x))<jupyter_output>[[4 1]
 [2 2]
 [1 1]]
[[4 1]
 [2 2]
 [1 1]]<jupyter_text>Q3. Predict the results of the following code.<jupyter_code>x = np.array([[1, 4], [5, 6]])
y = np.array([[4, 1], [2, 2]])
#print np.vdot(x, y)
#print np.vdot(y, x)
#print np.dot(x.flatten(), y.flatten())
#print np.inner(x.flatten(), y.flatten())
#print (x*y).sum()
x = np.array([[1, 4], [5, 6]])
y = np.array([[4, 1], [2, 2]])
print(np.vdot(x, y))
print(np.vdot(y, x))
print(np.dot(x.flatten(), y.flatten()))
print(np.inner(x.flatten(), y.flatten()))
print((x*y).sum())<jupyter_output>30
30
30
30
30<jupyter_text>Q4. Predict the results of the following code.<jupyter_code>x = np.array(['a', 'b'], dtype=object)
y = np.array([1, 2])
#print np.inner(x, y)
#print np.inner(y, x)
#print np.outer(x, y)
#print np.outer(y, x)
x = np.array(['a', 'b'], dtype=object)
y = np.array([1, 2])
print(np.inner(x, y))
print(np.inner(y, x))
print(np.outer(x, y))
print(np.outer(y, x))<jupyter_output>abb
abb
[['a' 'aa']
 ['b' 'bb']]
[['a' 'b']
 ['aa' 'bb']]<jupyter_text>Decompositions Q5. Get the lower-trianglular `L` in the Cholesky decomposition of x and verify it.<jupyter_code>x = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]], dtype=np.int32)

x = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]], dtype=np.int32)
np.linalg.cholesky(x)<jupyter_output><empty_output><jupyter_text>Q6. Compute the qr factorization of x and verify it.<jupyter_code>x = np.array([[12, -51, 4], [6, 167, -68], [-4, 24, -41]], dtype=np.float32)

x = np.array([[12, -51, 4], [6, 167, -68], [-4, 24, -41]], dtype=np.float32)
q,r = np.linalg.qr(x)
print('q=',q,'\nr=',r)<jupyter_output>q= [[-0.85714287  0.3942857   0.33142856]
 [-0.42857143 -0.9028571  -0.03428571]
 [ 0.2857143  -0.17142858  0.94285715]] 
r= [[ -14.  -21.   14.]
 [   0. -175.   70.]
 [   0.    0.  -35.]]<jupyter_text>Q7. Factor x by Singular Value Decomposition and verify it.<jupyter_code>x = np.array([[1, 0, 0, 0, 2], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0]], dtype=np.float32)

x = np.array([[1, 0, 0, 0, 2], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0]], dtype=np.float32)
np.linalg.svd(x)<jupyter_output><empty_output><jupyter_text>Matrix eigenvalues Q8. Compute the eigenvalues and right eigenvectors of x. (Name them eigenvals and eigenvecs, respectively)<jupyter_code>x = np.diag((1, 2, 3))

x = np.diag((1, 2, 3))
print(np.linalg.eigvals(x))
print(np.linalg.eig(x)[1])
eigenvals = np.linalg.eigvals(x)
eigenvecs = np.linalg.eig(x)[1]<jupyter_output><empty_output><jupyter_text>Q9. Predict the results of the following code.<jupyter_code>#print np.array_equal(np.dot(x, eigenvecs), eigenvals * eigenvecs)
print(np.array_equal(np.dot(x, eigenvecs), eigenvals * eigenvecs))<jupyter_output>True<jupyter_text>Norms and other numbers Q10. Calculate the Frobenius norm and the condition number of x.<jupyter_code>x = np.arange(1, 10).reshape((3, 3))

x = np.arange(1, 10).reshape((3, 3))
print(np.linalg.norm(x,ord="fro"))
print(np.linalg.cond(x,p="fro"))<jupyter_output>16.881943016134134
4.561770736605098e+17<jupyter_text>Q11. Calculate the determinant of x.<jupyter_code>x = np.arange(1, 5).reshape((2, 2))

x = np.arange(1, 5).reshape((2, 2))
np.linalg.det(x)<jupyter_output><empty_output><jupyter_text>Q12. Calculate the rank of x.<jupyter_code>x = np.eye(4)

x = np.eye(4)
np.linalg.svd(x)[1].size<jupyter_output><empty_output><jupyter_text>Q13. Compute the sign and natural logarithm of the determinant of x.<jupyter_code>x = np.arange(1, 5).reshape((2, 2))


x = np.arange(1, 5).reshape((2, 2))
sign, logdet = np.linalg.slogdet(x)
print(sign,' ',logdet)
np.log(-np.linalg.det(x))<jupyter_output><empty_output><jupyter_text>Q14. Return the sum along the diagonal of x.<jupyter_code>x = np.eye(4)

x = np.eye(4)
sum(np.diag(x))
np.trace(x)<jupyter_output><empty_output><jupyter_text>Solving equations and inverting matrices Q15. Compute the inverse of x.<jupyter_code>x = np.array([[1., 2.], [3., 4.]])


x = np.array([[1., 2.], [3., 4.]])
np.linalg.inv(x)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2211419114604882, 'fraction_numerical': 0.08171286719073231, 'mean_word_length': 5.097099621689786, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '217033', 'license': 'MIT', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2241, 'n_words': 502, 'path': '6_Linear_algebra.ipynb', 'repo_name': 'kis0403/numpy_exercises', 'size': '4834'}