Create the following matrices without explicitly initializing them. Instead create a matrix of ones or zeros and use slicing to create the requested pattern. Print the matrix after creating it. Note that to access diagonals, you will have to create the indices first ( diag_indices
) and then slice using them.
In [1]:
import numpy as np
m = np.zeros( (10,10) )
m[:,1] = 3
m[np.diag_indices(10)] = 4
print(m)
In [2]:
import numpy as np
s = 12
m = np.ones( (s,s) )
m[np.diag_indices(s)] = 0
for i in range(s):
m[:,i] /= np.sum(m[:,i])
print(np.round(m,2))
In [3]:
import numpy as np
m = np.ones( (10,3) )
m[:-1:2,1] = 0
print(m)
Solve the following system of equations:
$$\begin{array}{ll} 3 x - 10 \sin y &= 4t\\ x - z &= 6t - 3 \\ 4t + 2z - 4\sin y &= x - 4\\ x + z + 2&= 3t \\ \end{array}$$To answer, first rearrange the equations so that they are in standard form with constants on the right and all coefficients are explicit on the left (first cell). Solve in Python (second cell) and report the answer in LaTeX (last cell).
In [4]:
import numpy as np
import numpy.linalg as lin
A = np.array([[3, -10, 0, 4], [1, 0, -1, -6], [-1, -4, 2, 4], [1, 0, 1, -3]])
Ainv= lin.inv(A)
B = np.array([0, -3, -4, -2])
x = Ainv.dot(B)
print(x)
print(x[0], np.arcsin(x[1]), x[2], x[3])
In [5]:
import numpy as np
import numpy.linalg as lin
A = np.array([[2, 1, 1], [2, -4, 2], [4, 3, -5]])
lin.eig(A)
Out[5]:
In [6]:
A = np.array([[4, 3, 4], [6, -4, 3], [2, -1, -5]]) - np.array([[1, 3, 1], [3, -4, 1], [4, 3, -5]])
lin.eig(A)
Out[6]:
In [7]:
#approximately Hermitian since complex portions are miniscule, so use that instead!
A = np.array([[1,0,-2], [0,-1,3], [-2,2,1]]) + 1/8 * (3 * np.sqrt(273) - 29) * np.array([[0,1,0], [0,0,0], [0,0,0]])
lin.eigh(A)
Out[7]: