A unified theory for the origin of grid cells through the lens of pattern formation