Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

distance.py 864 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 18.06.2014
  4. @author: frank
  5. """
  6. import numpy as np
  7. def test():
  8. pass
  9. def cyclic_distance(a, b, base):
  10. """Calculate distance between two elements a and b of a residue class ring with modulo base
  11. :param a: first element (scalar or numpy array)
  12. :param b: second element (scalar or numpy array)
  13. :param base: modulo base of the residue class
  14. :return: shortest distance between a and b in the residue class
  15. Examples:
  16. >>> cyclic_distance(2, 1, 10)
  17. 1
  18. >>> cyclic_distance(2, 9, 10)
  19. 3
  20. >>> cyclic_distance(2, 9, 20)
  21. 7
  22. works as well with arrays:
  23. >>> cyclic_distance(np.array([2,3]), np.array([9,10]), 20)
  24. array([7, 7])
  25. """
  26. return np.minimum((a-b) % base,
  27. (b-a) % base,
  28. )
  29. if __name__ == '__main__':
  30. test()