def hermite_normal_form(A, *, D=None, check_rank=False): r""" Compute the Hermite Normal Form of a Matrix *A* of integers. Examples ======== >>> from sympy import Matrix >>> from sympy.matrices.normalforms import hermite_normal_form >>> m = Matrix([[12, 6, 4], [3, 9, 6], [2, 16, 14]]) >>> print(hermite_normal_form(m)) Matrix([[10, 0, 2], [0, 15, 3], [0, 0, 2]]) Parameters ========== A : $m \times n$ ``Matrix`` of integers. D : int, optional Let $W$ be the HNF of *A*. If known in advance, a positive integer *D* being any multiple of $\det(W)$ may be provided. In this case, if *A* also has rank $m$, then we may use an alternative algorithm that works mod *D* in order to prevent coefficient explosion. check_rank : boolean, optional (default=False) The basic assumption is that, if you pass a value for *D*, then you already believe that *A* has rank $m$, so we do not waste time checking it for you. If you do want this to be checked (and the ordinary, non-modulo *D* algorithm to be used if the check fails), then set *check_rank* to ``True``. Returns ======= ``Matrix`` The HNF of matrix *A*. Raises ====== DMDomainError If the domain of the matrix is not :ref:`ZZ`. DMShapeError If the mod *D* algorithm is used but the matrix has more rows than columns. References ========== .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.* (See Algorithms 2.4.5 and 2.4.8.) """ # Accept any of Python int, SymPy Integer, and ZZ itself: if D is not None and not ZZ.of_type(D): D = ZZ(int(D)) return _hnf(A._rep, D=D, check_rank=check_rank).to_Matrix()
def is_int(c): r""" Test whether an argument is of an acceptable type to be used as an integer. Explanation =========== Returns ``True`` on any argument of type ``int`` or :ref:`ZZ`. See Also ======== is_rat """ # If gmpy2 is installed then ``ZZ.of_type()`` accepts only # ``mpz``, not ``int``, so we need another clause to ensure ``int`` is # accepted. return isinstance(c, int) or ZZ.of_type(c)
def is_rat(c): r""" Test whether an argument is of an acceptable type to be used as a rational number. Explanation =========== Returns ``True`` on any argument of type ``int``, :ref:`ZZ`, or :ref:`QQ`. See Also ======== is_int """ # ``c in QQ`` is too accepting (e.g. ``3.14 in QQ`` is ``True``), # ``QQ.of_type(c)`` is too demanding (e.g. ``QQ.of_type(3)`` is ``False``). # # Meanwhile, if gmpy2 is installed then ``ZZ.of_type()`` accepts only # ``mpz``, not ``int``, so we need another clause to ensure ``int`` is # accepted. return isinstance(c, int) or ZZ.of_type(c) or QQ.of_type(c)