Linear-time Legendre Transform