more general distance matrix functionality