def _gcdex(a, b): r""" This supports the functions that compute Hermite Normal Form. Explanation =========== Let x, y be the coefficients returned by the extended Euclidean Algorithm, so that x*a + y*b = g. In the algorithms for computing HNF, it is critical that x, y not only satisfy the condition of being small in magnitude -- namely that |x| <= |b|/g, |y| <- |a|/g -- but also that y == 0 when a | b. """ x, y, g = ZZ.gcdex(a, b) if a != 0 and b % a == 0: y = 0 x = -1 if a < 0 else 1 return x, y, g