def dual_energy(u, alpha0, show_text, on_gpu=True):
    """Calculate and print dual energy

    Args:
        u (gpuarray): Array.
        alpha0 (float): Regularization parameter.
        show_text (bool): If `True`, prints dual energy.

    Returns:
        float: Dual energy.
    """
    if on_gpu:
        dual_en = (u.get()**2).sum()/2.0
    else:
        dual_en = (u**2).sum()/2.0
    if show_text:
        print("TGV2-L2-2D-PD: alpha0 = " + str(alpha0) + " dual energy = " +
              str(dual_en))
    return dual_en