def convert_mag_ham(H, T):
    """Converts the tensor representing the magnetic Hamiltonian to a different basis.

    Note: This is just a tensor transformation, there are several other functions very
    similar to this. The only difference is which indeces transform by T and which by
    T^-1. This just corresponds to covariant and contravariant indeces and this would be
    nice to eventually implement in the code.

    Args:
        H (tensor): the magnetic Hamiltonian in basis A
        T (matrix): . If it is set, the symmetry operations
            will be transformed by this matrix. T transforms from A to B, ie Tx_A = x_B.
    Returns:
        H_T: the magnetic Hamiltonian in basis B
    """
    Ti = T.inv()
    H_T = H.copy0()
    for ind1 in H:
        for ind2 in H_T:
            factor = 1
            for i in range(H.dim2):
                factor *= Ti[ind1[i], ind2[i]]
            H_T[ind1] += factor * H[ind2]
    return H_T
