def transform_pm_cov(c, cov, to_frame):
    """Transform a proper motion covariance matrix to a new frame.

    Parameters
    ----------
    c : `~astropy.coordinates.SkyCoord`
        The sky coordinates of the sources in the initial coordinate frame.
    cov : array_like
        The covariance matrix of the proper motions. Must have same length as
        the input coordinates.
    to_frame : `~astropy.coordinates.BaseCoordinateFrame` subclass
        The frame to transform to as an Astropy coordinate frame class or
        instance.

    Returns
    -------
    new_cov : array_like
        The transformed covariance matrix.

    """
    if c.isscalar and cov.shape != (2, 2):
        raise ValueError('If input coordinate object is a scalar coordinate, '
                         'the proper motion covariance matrix must have shape '
                         '(2, 2), not {}'.format(cov.shape))

    elif not c.isscalar and len(c) != cov.shape[0]:
        raise ValueError('Input coordinates and covariance matrix must have '
                         'the same number of entries ({} vs {}).'
                         .format(len(c), cov.shape[0]))

    # 3D rotation matrix, to be projected onto the tangent plane
    if hasattr(c, 'frame'):
        frame = c.frame
    else:
        frame = c
    R = get_transform_matrix(frame.__class__, to_frame)

    # Get input coordinates in the desired frame:
    c_to = c.transform_to(to_frame)

    # Get tangent plane coordinates:
    uv_in = get_uv_tan(c)
    uv_to = get_uv_tan(c_to)

    if not c.isscalar:
        G = np.einsum('nab,nac->nbc', uv_to,
                      np.einsum('ji,nik->njk', R, uv_in))

        # transform
        cov_to = np.einsum('nba,nac->nbc', G,
                           np.einsum('nij,nki->njk', cov, G))
    else:
        G = np.einsum('ab,ac->bc', uv_to,
                      np.einsum('ji,ik->jk', R, uv_in))

        # transform
        cov_to = np.einsum('ba,ac->bc', G,
                           np.einsum('ij,ki->jk', cov, G))

    return cov_to
Transform a proper motion covariance matrix to a new frame.

    Parameters
    ----------
    c : `~astropy.coordinates.SkyCoord`
        The sky coordinates of the sources in the initial coordinate frame.
    cov : array_like
        The covariance matrix of the proper motions. Must have same length as
        the input coordinates.
    to_frame : `~astropy.coordinates.BaseCoordinateFrame` subclass
        The frame to transform to as an Astropy coordinate frame class or
        instance.

    Returns
    -------
    new_cov : array_like
        The transformed covariance matrix.