Analytical Mechanics of Gears