import numpy as np
import numpy.linalg as la
Here's a matrix of which we're trying to compute the norm:
n = 3
A = np.random.randn(n, n)
Recall:
$$||A||=\max_{x\ne 0} \frac{\|Ax\|}{\|x\|},$$
where the vector norm must be specified, and the value of the matrix norm $\|A\|$ depends on the choice of vector norm.
For instance, for the $p$-norms, we often write:
$$||A||_2=\max_{x\ne 0} \frac{\|Ax\|_2}{\|x\|_2},$$
and similarly for different values of $p$.
We can approximate this by just producing very many random vectors and evaluating the formula:
xs = np.random.randn(1000, 3)
Axs = np.einsum("ij,kj->ki", A, xs)
p = 2
norm_xs = np.sum(np.abs(xs)**p, axis=1)**(1/p)
norm_xs.shape
norm_Axs = np.sum(np.abs(Axs)**p, axis=1)**(1/p)
norm_Axs.shape
np.max(norm_Axs/norm_xs)
la.norm(A, p)