6 Linear Algebra Solutions


title: "Linear algebra" date: 2026-05-24T13:51:52Z

Linear algebra

1import numpy as np
1np.__version__
'1.11.2'

Matrix and vector products

Q1. Predict the results of the following code.

1x = [1,2]
2y = [[4, 1], [2, 2]]
3print np.dot(x, y)
4print np.dot(y, x)
5print np.matmul(x, y)
6print np.inner(x, y)
7print np.inner(y, x)
[8 5]
[6 6]
[8 5]
[6 6]
[6 6]

Q2. Predict the results of the following code.

1x = [[1, 0], [0, 1]]
2y = [[4, 1], [2, 2], [1, 1]]
3print np.dot(y, x)
4print np.matmul(y, x)
[[4 1]
 [2 2]
 [1 1]]
[[4 1]
 [2 2]
 [1 1]]

Q3. Predict the results of the following code.

1x = np.array([[1, 4], [5, 6]])
2y = np.array([[4, 1], [2, 2]])
3print np.vdot(x, y)
4print np.vdot(y, x)
5print np.dot(x.flatten(), y.flatten())
6print np.inner(x.flatten(), y.flatten())
7print (x*y).sum()
30
30
30
30
30

Q4. Predict the results of the following code.

1x = np.array(['a', 'b'], dtype=object)
2y = np.array([1, 2])
3print np.inner(x, y)
4print np.inner(y, x)
5print np.outer(x, y)
6print np.outer(y, x)
abb
abb
[['a' 'aa']
 ['b' 'bb']]
[['a' 'b']
 ['aa' 'bb']]

Decompositions

Q5. Get the lower-trianglular L in the Cholesky decomposition of x and verify it.

1x = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]], dtype=np.int32)
2L = np.linalg.cholesky(x)
3print L
4assert np.array_equal(np.dot(L, L.T.conjugate()), x)
[[ 2.  0.  0.]
 [ 6.  1.  0.]
 [-8.  5.  3.]]

Q6. Compute the qr factorization of x and verify it.

1x = np.array([[12, -51, 4], [6, 167, -68], [-4, 24, -41]], dtype=np.float32)
2q, r = np.linalg.qr(x)
3print "q=\n", q, "\nr=\n", r
4assert np.allclose(np.dot(q, r), x)
q=
[[-0.85714287  0.39428571  0.33142856]
 [-0.42857143 -0.90285712 -0.03428571]
 [ 0.2857143  -0.17142858  0.94285715]] 
r=
[[ -14.  -21.   14.]
 [   0. -175.   70.]
 [   0.    0.  -35.]]

Q7. Factor x by Singular Value Decomposition and verify it.

1x = 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)
2U, s, V = np.linalg.svd(x, full_matrices=False)
3print "U=\n", U, "\ns=\n", s, "\nV=\n", v
4assert np.allclose(np.dot(U, np.dot(np.diag(s), V)), x)
U=
[[ 0.  1.  0.  0.]
 [ 1.  0.  0.  0.]
 [ 0.  0.  0. -1.]
 [ 0.  0.  1.  0.]] 
s=
[ 3.          2.23606801  2.          0.        ] 
V=
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

Matrix eigenvalues

Q8. Compute the eigenvalues and right eigenvectors of x. (Name them eigenvals and eigenvecs, respectively)

1x = np.diag((1, 2, 3))
2eigenvals = np.linalg.eig(x)[0]
3eigenvals_ = np.linalg.eigvals(x)
4assert np.array_equal(eigenvals, eigenvals_)
5print "eigenvalues are\n", eigenvals
6eigenvecs = np.linalg.eig(x)[1]
7print "eigenvectors are\n", eigenvecs
eigenvalues are
[ 1.  2.  3.]
eigenvectors are
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

Q9. Predict the results of the following code.

1print np.array_equal(np.dot(x, eigenvecs), eigenvals * eigenvecs)
True

Norms and other numbers

Q10. Calculate the Frobenius norm and the condition number of x.

1x = np.arange(1, 10).reshape((3, 3))
2print np.linalg.norm(x, 'fro')
3print np.linalg.cond(x, 'fro')
16.8819430161
4.56177073661e+17

Q11. Calculate the determinant of x.

1x = np.arange(1, 5).reshape((2, 2))
2out1 = np.linalg.det(x)
3out2 = x[0, 0] * x[1, 1] - x[0, 1] * x[1, 0]
4assert np.allclose(out1, out2)
5print out1
-2.0

Q12. Calculate the rank of x.

1x = np.eye(4)
2out1 = np.linalg.matrix_rank(x)
3out2 = np.linalg.svd(x)[1].size
4assert out1 == out2
5print out1
4

Q13. Compute the sign and natural logarithm of the determinant of x.

1x = np.arange(1, 5).reshape((2, 2))
2sign, logdet = np.linalg.slogdet(x)
3det = np.linalg.det(x)
4assert sign == np.sign(det)
5assert logdet == np.log(np.abs(det))
6print sign, logdet
-1.0 0.69314718056

Q14. Return the sum along the diagonal of x.

1x = np.eye(4)
2out1 = np.trace(x)
3out2 = x.diagonal().sum()
4assert out1 == out2
5print out1
4.0

Solving equations and inverting matrices

Q15. Compute the inverse of x.

1x = np.array([[1., 2.], [3., 4.]])
2out1 = np.linalg.inv(x)
3assert np.allclose(np.dot(x, out1), np.eye(2))
4print out1
[[-2.   1. ]
 [ 1.5 -0.5]]