Skip to main content

Build Status PyPI - Python Version PyPI version Coverage Status

Generalized Python Smith Normal Form

This project is a python package implementing the calculation of smith normal forms (SNFs) for matrices defined over arbitrary principal ideal domains.

Currently, this SNF library can calculate the SNF of matrices over either the integers or the Gaussian integers. Additionally it can be easily extended to any principal ideal domain.

While there appear to be several open source Smith Normal Form implementations in a variety of programming languages, many of these implementations only operate on the integers. This is, to our knowledge, the only open source Smith Normal Form calculator that operates over the generalized class of principal ideal domains.

What are Principal Ideal Domains?

Principal ideal domains are integral domains (rings that behave like the integers) where every ideal is a principal ideal. Speaking more generally, PIDs are a class of mathematical structures that are more structured than a commutative ring, but not necessarily as structured as a field. Two items in a PID will always have a greatest common denominator (although this GCD is not always easy to compute) and they will always have a unique factorization. Elements of a PID do not necessarily have inverses, which is why they are considered less structured than a field.

Some examples of PIDs include:

  • integers
  • Gaussian integers
  • fields (finite fields, rational numbers, real numbers, complex numbers)
  • single variable polynomials over a field

What is the Smith Normal Form of a matrix?

The Smith Normal form of a matrix is canonical way to represent a matrix defined over a PID. The smith normal form of a matrix A is a matrix J such that:

  • all non-diagonal elements of J are zero
  • along the diagonal of J, every element divides evenly into its predecessor until a zero is encountered and then all future diagonal elements are zero
  • there exists unimodular matrices S and T such that S*A*T = J.

As an example if the matrix A is

A = [ 1 2 3 ]
    [ 4 5 6 ],

the Smith Normal Form of this matrix would be

J = [ 1 0 0 ]
    [ 0 3 0 ]

with complementary matrices

S = [ 1  0 ]
    [ 4 -1 ]

and

T = [ 1 -1  1 ]
    [ 0 -1 -2 ]
    [ 0  1  1 ].

Example Usage

The following is an example of how to set up a Smith Normal Form problem over the integers, run the computation, and interpret the results.

>>> from smithnormalform import matrix, snfproblem, z
>>> original_matrix = matrix.Matrix(2, 2, [z.Z(1), z.Z(2), z.Z(3), z.Z(4)])
>>> prob = snfproblem.SNFProblem(original_matrix)
>>> prob.computeSNF()
>>> print(prob.isValid())
True
>>> print(prob.A)
[ 1 2 ]
[ 3 4 ]
>>> print(prob.J)
[ -2 1 ]
[ 3 -1 ]
>>> print(prob.S * prob.A * prob.T == prob.J)
True

Adding New Principal Ideal Domains

The Smith Normal Form algorithm can be run on any subclass of the principal ideal domain class smithnormalform.pid.PID. In order to subclass PID, you will need to define several basic operations that are well defined on PIDs such as addition, multiplication, division, negation, and GCD.

Since every PID is a GCD Domain, greatest common divisor is a well defined operation for two elements of PID. Just because GCD is well-defined, however, does not mean it is easy (or even tractable) to compute. One way to find the GCD of two elements is the Euclidean algorithm; however, the Euclidean algorithm can only be applied to Euclidean domains. While all Euclidean domains are PIDs, not all PIDs are Euclidean domains.

This leaves us in an unfortunate position. While the Smith Normal Form algorithm implemented here is efficient in-and-of-itself and works for all PIDs, it is only efficient if GCD can be computed efficiently, which is not generally speaking true for all PIDs.

We resolve this conflict in the following way: We provide an abstract class for Euclidean domains (smithnormalform.ed.ED) that implements the euclidean algorithm for you. Extending this class requires you define a norm for your Euclidean domain; however, once you do so the GCD function required for PIDs will be completed for you without you needing to implement the Euclidean algorithm for yourself.

If you would like to run this algorithm on a PID that is not a Euclidean domain, you can extend the PID class smithnormalform.pid.PID directly, bypassing the Euclidean domain class. Doing this will require you to implement the GCD function directly. Please note that GCDs are requested frequently during the Smith Normal Form calculation so if the GCD function isn't efficient the Smith Normal Form computation may be intractable.

Metadata

Release files for smithnormalform 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smithnormalform 0.6.0
File Size Uploaded Provenance Actions
smithnormalform-0.6.0.tar.gz 15.5 kB Without provenance Details for smithnormalform-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for smithnormalform 0.6.0
File Interpreter ABI Platform Actions
smithnormalform-0.6.0-py3-none-any.whl Python 3 none any Details for smithnormalform-0.6.0-py3-none-any.whl

Total release size: 44.1 kB

Release files / smithnormalform-0.6.0.tar.gz

Download URL smithnormalform-0.6.0.tar.gz
Size 15.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7cfdcc5e9666394cc5304ef6b712fec3483b9b40afe2d2407f042e50f6792eca
BLAKE2b-256 checksum
How to use checksums
c10444df3dd7eedb4f22d36bd71050313f5259786e3570100fa55f8e1d12344a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / smithnormalform-0.6.0-py3-none-any.whl

Download URL smithnormalform-0.6.0-py3-none-any.whl
Size 28.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
492d05230733002d592b8f66fd54e29c6ae7b135de12005c9d4bec784983b110
BLAKE2b-256 checksum
How to use checksums
a4c4d46011b9bbad7c943dfeb7eb29243cb3f9b2014b00b7010c9f7455eae985
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Security for smithnormalform 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page