# -*- coding: utf-8 -*- """ Created on 18.06.2014 @author: frank """ import numpy as np def test(): pass def cyclic_distance(a, b, base): """Calculate distance between two elements a and b of a residue class ring with modulo base :param a: first element (scalar or numpy array) :param b: second element (scalar or numpy array) :param base: modulo base of the residue class :return: shortest distance between a and b in the residue class Examples: >>> cyclic_distance(2, 1, 10) 1 >>> cyclic_distance(2, 9, 10) 3 >>> cyclic_distance(2, 9, 20) 7 works as well with arrays: >>> cyclic_distance(np.array([2,3]), np.array([9,10]), 20) array([7, 7]) """ return np.minimum((a-b) % base, (b-a) % base, ) if __name__ == '__main__': test()