# Practical 0 - Model Solutions ## Problem 1 >>> import datetime >>> import numpy as np >>> today = datetime.date.today() >>> date = np.array([today.day, today.month, today.year % 100]) You can also use the datetime package from the python standard library >>> import datetime >>> datetime.date.today() ## Problem 2 >>> ints = np.arange(1, 101) >>> ints.min() >>> ints.max() >>> ints.mean() >>> ints.std() ## Problem 3 >>> pows_of_2 = np.logspace(0, 9, num=10, base=2) >>> pows_of_2.sum() >>> pows_of_2.mean() ## Problem 4 >>> pows_of_2[3] *= 10 >>> pows_of_2 = np.delete(pows_of_2, np.arange(4, 8)) >>> pows_of_2.mean() ## Problem 5 >>> A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> np.linalg.norm(A, ord=2) ## Problem 6 >>> A = np.arange(1, 1001).reshape(10, 100) >>> np.linalg.norm(A, ord=2) ## Problem 7 >>> A = np.arange(1, 10).reshape(3, 3) >>> x = np.linalg.solve(A + np.eye(3), np.array([1, 2, 3])) >>> x[1] ## Problem 8 >>> A[0] = A[0][::-1] >>> x = np.linalg.solve(A + np.eye(3), np.array([1, 2, 3])) >>> x[1] ## Problem 9 >>> import string >>> letters = np.random.choice(list(string.ascii_lowercase), (26,)) >>> len(np.unique(letters)) ## Problem 10 >>> import matplotlib.pyplot as plt >>> help(plt.plot) >>> x = linspace(0, 10, 1000) >>> plt.plot(x, np.sin(x), 'r-', label="sin(x)") >>> plt.plot(x, np.sin(x**2), 'b--', label="sin(x^2)") >>> plt.plot(x, np.sin(x) + np.sin(x**2), 'g-.', label="sin(x) + sin(x^2)") >>> plt.legend() >>> plt.title("sin(x) vs. sin(x^2) vs. sin(x) + sin(x^2)") >>> plt.grid() >>> plt.show() ## Problem 11 >>> x = np.linspace(0, 5, 1000) >>> plt.semilogy(x, np.exp(-x**2)) >>> plt.show() ## Problem 12 >>> A = np.random.rand(10, 10) >>> eigenvalues, eigenvectors = np.linalg.eig(A) >>> plt.subplot(1, 2, 1) >>> plt.scatter(np.real(eigenvalues), np.imag(eigenvalues), color='blue', marker='o') >>> plt.title("Eigenvalues") >>> plt.subplot(1, 2, 2) >>> plt.scatter(np.real(eigenvectors.reshape(-1)), np.imag(eigenvectors.reshape(-1)), color='red', marker='*') >>> plt.title("Eigenvectors") >>> plt.show()