Metadata-Version: 2.4
Name: grobx
Version: 0.202.1
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Rust
Classifier: Topic :: Scientific/Engineering :: Mathematics
License-File: LICENSE
Summary: Fast reduced Gröbner (Groebner) bases by F4 over GF(2), GF(p), and the rationals
Keywords: groebner,gröbner,groebner basis,polynomial ideal,computer algebra,finite field,f4,gf2,boolean polynomial,grobx
Home-Page: https://grobx.com
License: GPL-3.0-only
Requires-Python: >=3.12
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://grobx.com
Project-URL: Source Code, https://github.com/code1-de/grobx

# grobx

grobx computes reduced Gröbner bases of polynomial ideals over prime fields
GF(p), p a prime below 2^64, and over the rationals ℚ. This is its Python
module. It takes the ideal's generators as text and returns the basis as text, takes and returns
python-flint's polynomials, or takes Sage's polynomials and returns the basis as text.

Over ℚ, GF(p), and GF(2), grobx is at least 10× faster than any other software.
[grobx.com/benchmarks.html](https://grobx.com/benchmarks.html) gives the time and memory of every
instance.

The package is a wheel for CPython 3.12 and later on Linux on x86-64 and aarch64, on Windows on
x86-64, and on macOS on Apple Silicon and Intel.

## Install

```sh
pip install grobx
```

## Example

```python
import grobx

r = grobx.groebner("x,y\n7\nx^2+y,\nx*y-1\n")
r.basis    # '[y^2+x,\nx*y+6,\nx^2+y]:\n'
```

The input is msolve's format: the variables on line 1, separated by
commas, the largest first; the characteristic on line 2, a prime below
2^64 for GF(p) or 0 for ℚ; then the generators, separated by commas. Over
ℚ:

```python
r = grobx.groebner("x,y,z\n0\nx+y+z,\nx*y+y*z+z*x,\nx*y*z-1\n")
print(r.basis)
# [x+y+z,
# y^2+y*z+z^2,
# z^3-1]:
```

## The call

```python
grobx.groebner(input, *, order=None, threads=0, seed=None, deadline=None, cancel=None,
               memory=None, checker=None, cofactors=False, exact_reduction=False, report=False)
```

`input` is the instance text, or python-flint's polynomials (_python-flint_ below).

| Keyword | Meaning |
|---|---|
| `order` | `None` for the input's order, grevlex for a text; or `"grevlex"`, `"deglex"`, `"lex"`, or `"block:k1,…,km"`: grevlex blocks of k1, …, km variables and a last block of the rest |
| `threads` | How many threads the call may use, 0 for all the machine has |
| `seed` | The one source of every random choice, 0 to 2^64 − 1; `None` draws one |
| `deadline` | A time limit on the whole call, in milliseconds |
| `cancel` | A `grobx.Cancel` that another thread raises by `set()` to stop the call |
| `memory` | A ceiling on the memory grobx allocates, in bytes |
| `checker` | Has the checker rule on the result: an error bound in [0, 1), 0 for exact |
| `cofactors` | Whether the result carries the cofactor matrix |
| `exact_reduction` | Whether every row is reduced on its own, none ruled zero by random draws; slower on some systems over GF(p) |
| `report` | Whether the result carries the call report |

The call releases the GIL while it computes. A Ctrl-C stops it: the call raises its cancel flag,
waits for grobx to stop, and raises `KeyboardInterrupt`. This holds for every call of the module.
It returns a `grobx.Result`, which always holds a basis, one the checker accepted where the call
asked for it:

| Attribute | Meaning |
|---|---|
| `basis` | The basis: its text for a text, a list of polynomials for python-flint's |
| `cofactors` | The cofactor text, or for python-flint's a list per element of a polynomial per generator, where the call asked for them |
| `report` | The call report, where the call asked for it |
| `sink_error` | The first error a write to `report_file` met, else `None` |
| `seed` | The seed the call ran under |

What grobx refuses or fails at raises a `grobx.Error`:

| Exception | When | Attributes |
|---|---|---|
| `grobx.Invalid` | An instance or a keyword's value the call cannot take; also a `ValueError` | |
| `grobx.Stopped` | A bound stopped the call | `stopped`: `"deadline"`, `"cancel"`, or `"memory"`; `seed`; `report`; `sink_error` |
| `grobx.Rejected` | The checker rejected the basis, which the call does not return | `flaw`, `seed`, `report`, `sink_error` |
| `grobx.Error` | Any other failure, such as the lift over ℚ dropping its records | |

The instance and the seed give a rejected basis again, by the same call without the checker. A
keyword's value of a type the call does not take raises `TypeError`, an integer out of its type's
range `OverflowError`, and a `report_file` that cannot be created `OSError`.

## python-flint

```python
import flint
import grobx

ctx = flint.nmod_mpoly_ctx.get(names=("x", "y"), ordering="degrevlex", modulus=7)
x, y = ctx.gens()
grobx.groebner([x**2 + y, x*y - 1]).basis
# [y^2 + x, x*y + 6, x^2 + y]
```

`grobx.groebner` takes the generators as python-flint's `nmod_mpoly`, of a prime modulus below
2^64, or `fmpq_mpoly`, all of one context, and its `basis` is the reduced Gröbner basis as a list
of monic polynomials of that context, in increasing order of their leading monomials; the zero
ideal's basis is `[]`. The context's ordering is the call's order, and an `order` given must be it.
The polynomials reach grobx as FLINT's own structs, with no text between: grobx reads their terms
and writes the basis's through the functions of the libflint python-flint loaded. grobx does not
depend on python-flint, and needs it only where it is given its polynomials. The checker rules on
them as on their text. With `cofactors=True`, `cofactors[i][j]` is the cofactor of generator `j`
in element `i`, a polynomial of the context: `basis[i] == sum(cofactors[i][j] * polys[j] for j)`,
the generators as given.

| Ordering of the context | grobx's order |
|---|---|
| `degrevlex` | grevlex |
| `deglex` | deglex |
| `lex` | lex |

The call raises `TypeError` for no polynomial, a type it does not take, or polynomials of two types
or contexts; `grobx.Invalid` for what grobx refuses, such as a modulus that is no prime, a degree
past 2^31 − 1, or an order that is not the context's; `grobx.Stopped` where a bound stopped the
call; and `grobx.Error` where python-flint's objects match no layout. The zero ideal is given as
`[ctx.from_dict({})]`.

python-flint does not say where an object keeps its FLINT struct. The first call that meets
python-flint loaded finds it for each type: of the offsets inside the object, the one at which
FLINT's functions read a probe context and probe polynomials as python-flint reads them. It then
checks the offsets by a basis it computes and writes, against the basis known. A python-flint whose
objects match no offset leaves the call raising `grobx.Error` at every call, with what failed; it
never reads through an offset it has not checked. On Linux and macOS, grobx finds libflint through
python-flint's own extension module; on Windows it opens the one `flint*.dll` beside python-flint,
as its wheel or conda installs it.

## Sage

```sage
import grobx
R.<x,y> = PolynomialRing(GF(7))
grobx.groebner_sage([x^2 + y, x*y - 1]).basis
# [{(0, 2): 1, (1, 0): 1}, {(1, 1): 1, (0, 0): 6}, {(2, 0): 1, (0, 1): 1}]
```

`grobx.groebner_sage(polys, *, order="grevlex", threads=0, seed=None, deadline=None, cancel=None,
memory=None, checker=None, cofactors=False, exact_reduction=False, report=False, report_file=None)`
takes the generators as Sage's
polynomials of one ring over ℚ or GF(p), p a prime below 2^64, and returns a `grobx.SageResult`.
Its basis holds the elements of the basis text, in its order, each a dict from a term's tuple of
exponents, one per generator of the ring, to its coefficient: a residue over GF(p), and over ℚ an
int, the element's coefficients with no common factor and the leading one positive. `order`
is grobx's order, as `grobx.groebner` takes it: the call does not read the ring's term order. The
other keywords are those of `grobx.groebner`. Where `grobx.groebner` raises
`grobx.Stopped` or `grobx.Rejected`, this call returns, and the result's fields say so:

| Attribute | Meaning |
|---|---|
| `basis` | The list of the elements' dicts; `None` where a bound stopped the call or the checker rejected the basis |
| `stopped` | The bound that stopped the call, `"deadline"`, `"cancel"`, or `"memory"`; else `None` |
| `certified` | The checker's verdict; `None` where it did not rule |
| `flaw` | Why the checker rejected the result |
| `cofactors` | A list per element of a dict per generator, as `basis` holds an element, over ℚ each coefficient a `fractions.Fraction`; `None` where the call did not ask for them or the result holds no basis |
| `report`, `sink_error`, `seed` | As `grobx.Result`'s |

The polynomials reach grobx as Sage holds them, with no text between, and the checker rules on them
as read: Sage's `MPolynomial_libsingular`, whose terms grobx reads from Singular's polynomial in
place, and its `MPolynomial_polydict`, of a ring Singular does not hold, such as one of
`implementation="generic"`, whose terms grobx reads from the dict Sage keeps them in. grobx does not
depend on Sage; the call is there in every install and
needs Sage only where it is given its polynomials. The PyPI package `grobx-sage` calls it and
returns Sage's own basis.

The call raises `TypeError` for no polynomial, a type it does not take, polynomials of two rings, or
a ring over another field than ℚ or GF(p); `grobx.Invalid` for what grobx refuses, such as a prime
past 2^64, or a term of an `MPolynomial_polydict` that is none of its ring: Sage takes a key of any
length and a negative exponent into its dict, so grobx checks each term's count of exponents, their
signs, and that its coefficient is an element of the base ring. It raises `RuntimeError` where
Sage's objects or Singular's polynomials match no layout grobx reads.

Sage does not say where its objects keep their pointers to Singular's polynomial and ring. The first
call that meets Sage's libsingular loaded finds them by the values of the objects' words alone,
before it reads
through any, and checks them by a basis it computes, against the basis known. Singular's own structs
are read at the offsets of Singular 4.4.1 as Sage 10.10 builds it. Each call checks them on its
ring before it reads a term: Singular's count of the ring's variables, the read of every exponent
of monomials of the ring, against Sage's exponents and against Singular's own `pGetExp_Var`, and
how Singular holds the ring's coefficients: over GF(p) as residues below 2^29 and as GMP integers
above, over ℚ by constants that Singular holds in each of its forms, an integer in the word itself,
one larger in a GMP integer, and a fraction of two, each read for its value and its form. The first
call's bases are over two primes and over ℚ. A
Sage whose objects match no layout leaves the call raising `RuntimeError`, with what failed; it
never reads a basis from an offset it has not checked. The call runs on Linux and macOS, where Sage
does.

## Documentation

The [manual](https://github.com/code1-de/grobx/blob/master/MANUAL.md#python)
describes the formats, the bounds, the checker, and the engine.
[grobx.com](https://grobx.com) has the benchmarks and the instance corpus,
and [GitHub](https://github.com/code1-de/grobx) the source.

## License

GPL-3.0-only. Copyright Code1 GmbH.

Code1 GmbH also offers grobx under a commercial license.

