Weakly commensurable groups, with applications to differential geometry