Algebraic Kaluza-Klein cosmology