def test_normalization(mesh, FunctionSpace, bilinear_form, isclose):
    V = FunctionSpace(mesh)
    
    A = assemble(bilinear_form(V))
    
    z1 = Function(V)
    z1.vector()[:] = 2.
    
    z1_normalized = function_from_ufl_operators(z1/sqrt(transpose(z1)*A*z1))
    assert isclose(z1_normalized.vector().get_local(), 1).all()
def test_transpose(mesh, FunctionSpace, bilinear_form, linear_form, isclose):
    V = FunctionSpace(mesh)
    
    A = assemble(bilinear_form(V))
    b = assemble(linear_form(V))
    
    z1 = Function(V)
    z1.vector()[:] = 1.
    assert isclose(transpose(z1)*A*z1, 1.)
    assert isclose(transpose(b)*z1, 1.)
    assert isclose(transpose(z1)*b, 1.)

    assert isclose(transpose(z1)*A*(2*z1), 2.)
    assert isclose(transpose(2*z1)*A*z1, 2.)
    assert isclose(transpose(b)*(2*z1), 2.)
    assert isclose(transpose(2*z1)*b, 2.)

    assert isclose(transpose(z1)*A*(z1*2), 2.)
    assert isclose(transpose(z1*2)*A*z1, 2.)
    assert isclose(transpose(b)*(z1*2), 2.)
    assert isclose(transpose(z1*2)*b, 2.)

    assert isclose(transpose(z1)*A*(z1/2.), 0.5)
    assert isclose(transpose(z1/2.)*A*z1, 0.5)
    assert isclose(transpose(b)*(z1/2.), 0.5)
    assert isclose(transpose(z1/2.)*b, 0.5)

    z2 = Function(V)
    z2.vector()[:] = 2.
    
    assert isclose(transpose(z1)*A*(z1 + z2), 3.)
    assert isclose(transpose(z1 + z2)*A*z1, 3.)
    assert isclose(transpose(z1 + z2)*A*(z1 + z2), 9.)
    assert isclose(transpose(b)*(z1 + z2), 3.)
    assert isclose(transpose(z1 + z2)*b, 3.)

    assert isclose(transpose(z1)*A*(z1 - z2), -1.)
    assert isclose(transpose(z1 - z2)*A*z1, -1.)
    assert isclose(transpose(z1 - z2)*A*(z1 - z2), 1.)
    assert isclose(transpose(z1 - z2)*A*(z1 + z2), -3.)
    assert isclose(transpose(b)*(z1 - z2), -1.)
    assert isclose(transpose(z1 - z2)*b, -1.)

    assert isclose(transpose(z1)*A*(z1 - 2*z2), -3.)
    assert isclose(transpose(z1 - 2*z2)*A*z1, -3.)
    assert isclose(transpose(z1 - 2*z2)*A*(z1 - 2*z2), 9.)
    assert isclose(transpose(b)*(z1 - 2*z2), -3.)
    assert isclose(transpose(z1 - 2*z2)*b, -3.)

    assert isclose(transpose(z1)*A*(z1 - z2*2), -3.)
    assert isclose(transpose(z1 - z2*2)*A*z1, -3.)
    assert isclose(transpose(z1 - z2*2)*A*(z1 - z2*2), 9.)
    assert isclose(transpose(b)*(z1 - z2*2), -3.)
    assert isclose(transpose(z1 - z2*2)*b, -3.)

    assert isclose(transpose(z1)*A*(z1 - 3*z2*2), -11.)
    assert isclose(transpose(z1 - 3*z2*2)*A*z1, -11.)
    assert isclose(transpose(z1 - 3*z2*2)*A*(z1 - 3*z2*2), 121.)
    assert isclose(transpose(b)*(z1 - 3*z2*2), -11.)
    assert isclose(transpose(z1 - 3*z2*2)*b, -11.)

    assert isclose(transpose(z1)*A*(z1 - z2/4.), 0.5)
    assert isclose(transpose(z1 - z2/4.)*A*z1, 0.5)
    assert isclose(transpose(z1 - z2/4.)*A*(z1 - z2/4.), 0.25)
    assert isclose(transpose(b)*(z1 - z2/4.), 0.5)
    assert isclose(transpose(z1 - z2/4.)*b, 0.5)
    
    assert isclose(transpose(z1)*A*((z1 - z2)/2.), -0.5)
    assert isclose(transpose((z1 - z2)/2.)*A*z1, -0.5)
    assert isclose(transpose((z1 - z2)/2.)*A*((z1 - z2)/2.), 0.25)
    assert isclose(transpose(b)*((z1 - z2)/2.), -0.5)
    assert isclose(transpose((z1 - z2)/2.)*b, -0.5)
    
    z3 = Function(V)
    z3.vector()[:] = 3.
    
    assert isclose(transpose(z1)*A*(z1 - z2 + z3), 2.)
    assert isclose(transpose(z1 - z2)*A*(z1 - z2 + z3), -2.)
    assert isclose(transpose(z1 - z2 + z3)*A*z1, 2.)
    assert isclose(transpose(z1 - z2 + z3)*A*(z1 - z2), -2.)
    assert isclose(transpose(z1 - z2 + z3)*A*(z1 - z2 + z3), 4.)
    assert isclose(transpose(b)*(z1 - z2 + z3), 2.)
    assert isclose(transpose(z1 - z2 + z3)*b, 2.)