changed gram matrix function