(u, mesh)
return np.sqrt(val)
Calculate the L2 norm of a funciton on mesh (V,E).

    Parameters
    ----------
    u : array
        (nv,) list of function values

    mesh : object
        mesh object

    Returns
    -------
    val : float
        the value of the L2 norm of u, ||u||_2,V

    Notes
    -----
        - modepy is used to generate the quadrature points
          q = modepy.XiaoGimbutasSimplexQuadrature(4,2)

    Examples
    --------
    >>> import numpy as np
    >>> from pyamg.gallery import fem
    >>> V = np.array([[0,0], [1,0], [0,1], [1,1]])
    >>> E = np.array([[0,1,2], [1,3,2]])
    >>> mesh = Mesh(V, E, degree=1)
    >>> X, Y = mesh.V[:, 0], mesh.V[:, 1]
    >>> u = X + Y
    >>> unorm = fem.l2norm(u, mesh)
    >>> print(f'{unorm:2.6}')
    1.08012
    >>> mesh = Mesh(V, E, degree=2)
    >>> X, Y = mesh.V2[:, 0], mesh.V2[:, 1]
    >>> u = X + Y
    >>> unorm = fem.l2norm(u, mesh)
    >>> print(f'{unorm:2.6}')
    1.08012
Calculate the L2 norm of a funciton on mesh (V,E).
Calculate the L2 norm of a funciton on mesh ( V E ) .
def l2norm(u, mesh):
    """Calculate the L2 norm of a funciton on mesh (V,E).

    Parameters
    ----------
    u : array
        (nv,) list of function values

    mesh : object
        mesh object

    Returns
    -------
    val : float
        the value of the L2 norm of u, ||u||_2,V

    Notes
    -----
        - modepy is used to generate the quadrature points
          q = modepy.XiaoGimbutasSimplexQuadrature(4,2)

    Examples
    --------
    >>> import numpy as np
    >>> from pyamg.gallery import fem
    >>> V = np.array([[0,0], [1,0], [0,1], [1,1]])
    >>> E = np.array([[0,1,2], [1,3,2]])
    >>> mesh = Mesh(V, E, degree=1)
    >>> X, Y = mesh.V[:, 0], mesh.V[:, 1]
    >>> u = X + Y
    >>> unorm = fem.l2norm(u, mesh)
    >>> print(f'{unorm:2.6}')
    1.08012
    >>> mesh = Mesh(V, E, degree=2)
    >>> X, Y = mesh.V2[:, 0], mesh.V2[:, 1]
    >>> u = X + Y
    >>> unorm = fem.l2norm(u, mesh)
    >>> print(f'{unorm:2.6}')
    1.08012
    """
    if mesh.degree == 1:
        V = mesh.V
        E = mesh.E

    if mesh.degree == 2:
        V = mesh.V2
        E = mesh.E2

    if not isinstance(u, np.ndarray):
        raise ValueError('u must be ndarray')

    if V.shape[1] != 2:
        raise ValueError('V should be nv x 2')

    if mesh.degree == 1 and E.shape[1] != 3:
        raise ValueError('E should be nv x 3')

    if mesh.degree == 2 and E.shape[1] != 6:
        raise ValueError('E should be nv x 6')

    if mesh.degree not in [1, 2]:
        raise ValueError('degree = 1 or 2 supported')

    val = 0

    # quadrature points
    ww = np.array([0.44676317935602256, 0.44676317935602256, 0.44676317935602256,
                   0.21990348731064327, 0.21990348731064327, 0.21990348731064327])
    xy = np.array([[-0.10810301816807008, -0.78379396366385990],
                   [-0.10810301816806966, -0.10810301816807061],
                   [-0.78379396366386020, -0.10810301816806944],
                   [-0.81684757298045740, -0.81684757298045920],
                   [0.63369514596091700, -0.81684757298045810],
                   [-0.81684757298045870, 0.63369514596091750]])
    xx, yy = (xy[:, 0]+1)/2, (xy[:, 1]+1)/2
    ww *= 0.5

    if mesh.degree == 1:
        I = np.arange(3)

        def basis1(x, y):
            return np.array([1-x-y,
                             x,
                             y])
        basis = basis1

    if mesh.degree == 2:
        I = np.arange(6)

        def basis2(x, y):
            return np.array([(1-x-y)*(1-2*x-2*y),
                             x*(2*x-1),
                             y*(2*y-1),
                             4*x*(1-x-y),
                             4*x*y,
                             4*y*(1-x-y)])
        basis = basis2

    for e in E:
        x = V[e, 0]
        y = V[e, 1]

        # Jacobian
        jac = np.abs((x[1]-x[0])*(y[2]-y[0]) - (x[2]-x[0])*(y[1]-y[0]))

        # add up each quadrature point
        for wv, xv, yv in zip(ww, xx, yy):
            val += (jac / 2) * wv * np.dot(u[e[I]], basis(xv, yv))**2

    # take the square root for the norm
    return np.sqrt(val)
def l2norm ( u , mesh ) : if mesh . degree == 1 : V = mesh . V E = mesh . E if mesh . degree == 2 : V = mesh . V2 E = mesh . E2 if not isinstance ( u , np . ndarray ) : raise ValueError ( 'u must be ndarray' ) if V . shape [ 1 ] != 2 : raise ValueError ( 'V should be nv x 2' ) if mesh . degree == 1 and E . shape [ 1 ] != 3 : raise ValueError ( 'E should be nv x 3' ) if mesh . degree == 2 and E . shape [ 1 ] != 6 : raise ValueError ( 'E should be nv x 6' ) if mesh . degree not in [ 1 , 2 ] : raise ValueError ( 'degree = 1 or 2 supported' ) val = 0 # quadrature points ww = np . array ( [ 0.44676317935602256 , 0.44676317935602256 , 0.44676317935602256 , 0.21990348731064327 , 0.21990348731064327 , 0.21990348731064327 ] ) xy = np . array ( [ [ - 0.10810301816807008 , - 0.78379396366385990 ] , [ - 0.10810301816806966 , - 0.10810301816807061 ] , [ - 0.78379396366386020 , - 0.10810301816806944 ] , [ - 0.81684757298045740 , - 0.81684757298045920 ] , [ 0.63369514596091700 , - 0.81684757298045810 ] , [ - 0.81684757298045870 , 0.63369514596091750 ] ] ) xx , yy = ( xy [ : , 0 ] + 1 ) / 2 , ( xy [ : , 1 ] + 1 ) / 2 ww *= 0.5 if mesh . degree == 1 : I = np . arange ( 3 ) def basis1 ( x , y ) : return np . array ( [ 1 - x - y , x , y ] ) basis = basis1 if mesh . degree == 2 : I = np . arange ( 6 ) def basis2 ( x , y ) : return np . array ( [ ( 1 - x - y ) * ( 1 - 2 * x - 2 * y ) , x * ( 2 * x - 1 ) , y * ( 2 * y - 1 ) , 4 * x * ( 1 - x - y ) , 4 * x * y , 4 * y * ( 1 - x - y ) ] ) basis = basis2 for e in E : x = V [ e , 0 ] y = V [ e , 1 ] # Jacobian jac = np . abs ( ( x [ 1 ] - x [ 0 ] ) * ( y [ 2 ] - y [ 0 ] ) - ( x [ 2 ] - x [ 0 ] ) * ( y [ 1 ] - y [ 0 ] ) ) # add up each quadrature point for wv , xv , yv in zip ( ww , xx , yy ) : val += ( jac / 2 ) * wv * np . dot ( u [ e [ I ] ] , basis ( xv , yv ) ) ** 2 # take the square root for the norm return np . sqrt ( val )