{"nbformat": 3, "nbformat_minor": 0, "worksheets": [{"cells": [{"source": ["# Function/Matrix norms"], "metadata": {}, "cell_type": "markdown"}, {"collapsed": false, "metadata": {}, "input": ["import numpy as np\n", "import numpy.linalg as la"], "prompt_number": 1, "language": "python", "cell_type": "code", "outputs": []}, {"source": ["Here's a matrix of which we're trying to compute the norm:"], "metadata": {}, "cell_type": "markdown"}, {"collapsed": false, "metadata": {}, "input": ["n = 3\n", "\n", "A = np.random.randn(n, n)"], "prompt_number": 2, "language": "python", "cell_type": "code", "outputs": []}, {"source": ["Recall:\n", "\n", "$$||A||=\\max_{x\\ne 0} \\frac{\\|Ax\\|}{\\|x\\|},$$\n", "\n", "where the vector norm must be specified, and the value of the matrix norm $\\|A\\|$ depends on the choice of vector norm.\n", "\n", "For instance, for the $p$-norms, we often write:\n", "\n", "$$||A||_2=\\max_{x\\ne 0} \\frac{\\|Ax\\|_2}{\\|x\\|_2},$$\n", "\n", "and similarly for different values of $p$."], "metadata": {}, "cell_type": "markdown"}, {"source": ["--------------------\n", "We can approximate this by just producing very many random vectors and evaluating the formula:"], "metadata": {}, "cell_type": "markdown"}, {"collapsed": false, "metadata": {}, "input": ["xs = np.random.randn(1000, 3)"], "prompt_number": 4, "language": "python", "cell_type": "code", "outputs": []}, {"collapsed": false, "metadata": {}, "input": ["Axs = np.einsum(\"ij,kj->ki\", A, xs)"], "prompt_number": 8, "language": "python", "cell_type": "code", "outputs": []}, {"collapsed": false, "metadata": {}, "input": ["p = 2\n", "norm_xs = np.sum(np.abs(xs)**p, axis=1)**(1/p)\n", "norm_xs.shape"], "prompt_number": 9, "language": "python", "cell_type": "code", "outputs": [{"prompt_number": 9, "text": ["(1000,)"], "output_type": "pyout", "metadata": {}}]}, {"collapsed": false, "metadata": {}, "input": ["norm_Axs = np.sum(np.abs(Axs)**p, axis=1)**(1/p)\n", "norm_Axs.shape"], "prompt_number": 10, "language": "python", "cell_type": "code", "outputs": [{"prompt_number": 10, "text": ["(1000,)"], "output_type": "pyout", "metadata": {}}]}, {"collapsed": false, "metadata": {}, "input": ["np.max(norm_Axs/norm_xs)"], "prompt_number": 11, "language": "python", "cell_type": "code", "outputs": [{"prompt_number": 11, "text": ["3.1170636628682233"], "output_type": "pyout", "metadata": {}}]}, {"collapsed": false, "metadata": {}, "input": ["la.norm(A, p)"], "prompt_number": 12, "language": "python", "cell_type": "code", "outputs": [{"prompt_number": 12, "text": ["3.117921601396644"], "output_type": "pyout", "metadata": {}}]}], "metadata": {}}], "metadata": {"name": "", "signature": "sha256:3225eacfc00527e514b892a4425d71e6092fe073672692ab7e3dc0fb48528451"}}