Skip to content

Limitations and roadmap

This page lists what shaperules 0.1 does not do. The first part is what is not modelled at all. The second is the limits of what is modelled. The last is the roadmap.

Most limits show as an Unknown answer, or as a question that has no function to ask. Two can give an answer that differs from NumPy's, and each is described below: a Python integer that does not fit its array's type, and a platform whose default integer has 32 bits.

>>> import shaperules as sr

Not modelled

Python integers that do not fit

Under NumPy 2 a Python integer takes the element type of the array it meets. If its value does not fit that type, NumPy raises OverflowError: np.uint8(1) + 300 is an error. That depends on the value 300, and shaperules is given types only. It answers with the element type the call has when the value fits:

>>> sr.ufunc_dtype("add", "uint8", int)
Known(value='uint8')

A strict client that passes Python integers through has to check their values itself.

Shapes of unknown rank

The rank of an array is its number of axes. A shape in shaperules is a tuple with one size per axis, so the rank is always known. A size can be unknown, written ANY or "?". The number of sizes cannot.

There is no way to write "some axes, then n, then 3", and so no rule can be asked about an array whose rank is not known. The one place a rule's own result has an unknown rank is squeeze with no axis on a size that may be 1. It answers Unknown there.

Indexing by arrays and masks

index covers basic indexing: integers, slices, None and .... Indexing by an array of integers, by a list, or by a boolean mask is not covered, and neither is a mix of these with basic items. A key with such an item is not answered: index raises.

The length a boolean mask selects is not known before the mask is computed. A client can still give it a name with fresh and carry that name through the rules that are covered.

Functions with no rule

There is no rule for:

  • np.einsum, np.tensordot, np.dot, np.inner, np.outer and the other products apart from matmul;
  • numpy.linalg and numpy.fft;
  • the functions that build arrays, such as np.zeros, np.arange, np.linspace and np.eye;
  • anything else not listed in Shape rules, such as np.where, np.pad, np.diff, np.take, np.repeat, np.tile, sorting and searching.

Element-wise functions need no rule of their own. Their shape is broadcast of the operand shapes, and their element type is ufunc_dtype.

Arguments that change the result

The rules model the positional meaning of a call and the arguments shown in their signatures. They do not model out=, where=, dtype= or casting=. reduce_dtype("sum", "int8") is the element type of np.sum(a), not of np.sum(a, dtype=np.int16).

Limits of what is modelled

The formula algebra

A size is an integer, one of three forms, or ANY. The forms are a linear sum, a floored division of a linear sum, and a single product of names. There are no sums of products, so n*m + 1 and (n + 1) * m have no form and become ANY:

>>> sr.dim("n*m + 1")
ANY

This reaches the rules. The number of elements of a shape is the product of its sizes, and with a sum among the sizes the product has no form. reshape with -1 then cannot work the missing size out:

>>> answer = sr.reshape(("n + 1", "m"), (-1,))
>>> answer.reason
'reshape: the number of elements is not known'
>>> sr.fmt_shape(answer.likely.value)
'(?,)'

Division is by a positive integer only. n // m is not a formula.

Some equal formulas have two spellings, so they are not recognised as equal: (n + 2) // 2 and n // 2 + 1 are the same size for every n and do not compare equal. A rule that meets the two answers Unknown.

Names can be zero

Proofs hold for every assignment, and 0 is a legal size. Several rules that "obviously" work are therefore Unknown for a plain name, with the obvious result as the likely reading:

Call Why it is not a proof
a[0], a[-1] on an axis of size n Out of bounds when n is 0.
a[1:], a[:-1], a[:5] on an axis of size n The length is not n-1, n-1 or 5 when the axis is too short.
np.max, np.argmax over an axis of size n NumPy raises when n is 0.
a.reshape(batch, -1) NumPy raises when batch is 0.
np.squeeze(a) with a size n The axis disappears when n is 1.

Where the code guarantees a non-empty axis, writing its size as n + 1 turns the indexing and the reduction into proofs:

>>> sr.index(("n + 1", 4), 0)
Known(value=(4,))
>>> sr.fmt_shape(sr.index(("n + 1", 4), slice(1, None)).unwrap())
'(n, 4)'
>>> sr.reduce(("n + 1", 4), axis=0, needs_elements=True)
Known(value=(4,))

It does not help reshape with -1, where the sum runs into the limit of the formula algebra described above. There is no way yet to state "n is at least 1" about a name once and have every rule use it.

Slices

A slice with a negative step and a bound, such as a[5::-1], has no formula over a symbolic size. The answer is Unknown with ANY as the likely length. With integer sizes every slice is exact.

slice_length is precise only where one formula holds for every assignment. It does not answer with a min or a max of two formulas, so a slice whose length depends on which of two sizes is smaller is Unknown.

Reductions

reduce is tested against np.sum and np.max. np.prod, np.any, np.all, np.min and np.ptp follow the same shape rule.

np.mean, np.std, np.var, np.median and np.average differ on one input. For an array with no axes they raise AxisError for axis=0 and axis=-1, where np.sum accepts both. reduce follows np.sum unless it is told otherwise, so a client that models one of these passes scalar_axis=False, which is tested against np.mean:

>>> sr.reduce((), axis=0)
Known(value=())
>>> sr.reduce((), axis=0, scalar_axis=False).refusal.kind
'axis-out-of-range'

For arrays with at least one axis the shapes are the same either way.

reduce_shape takes the reduction's name and applies these differences itself. Each of its 25 names is tested against the NumPy function of that name. np.nanargmax and np.nanargmin are not among them: for a tuple of axes they raise AxisError for a float array and TypeError for an integer one. np.quantile and np.percentile are not among them either.

An axis is an integer. A bool is refused with TypeError by every rule, also by the four where NumPy lets one through as 0 or 1 (np.stack, np.swapaxes, np.moveaxis, np.expand_dims).

Element types

Fourteen element types are covered. datetime64, timedelta64, longdouble, clongdouble, object, strings, bytes and structured types answer Unknown.

Only element-wise ufuncs of one or two inputs are in the tables. Generalized ufuncs such as matmul and vecdot answer Unknown. The element type of a matrix product is not available in 0.1.

reduce_dtype covers sixteen reductions. The nan variants, such as np.nansum, are not among them.

The element-type rules are NumPy 2's. NumPy 1 promoted Python numbers by their value, and that is not modelled. The tables describe NumPy 2.1 and later, and the tests run on those. NumPy 2.0 differs in one place: np.floor, np.ceil and np.trunc of an integer or boolean array return floats there, and ufunc_dtype answers with the array's own type.

The tables hold the results of a 64-bit platform, where NumPy's default integer has 64 bits. reduce_dtype("sum", "int8") is int64 everywhere. On a platform where NumPy's default integer has 32 bits, NumPy's own answer differs. CI runs on 64-bit Linux only.

Messages

A refusal's message is written by shaperules. It is not NumPy's message and it may change between versions. Branch on refusal.kind, which is stable.

The likely reading

Unknown.likely is not a proof and is wrong for some assignments by construction. The tests check it only where no name is 0, 1 or equal to another, and do not check it for slices. Treat it as a reading for a diagnostic, never as a fact about a run.

Roadmap

Version 0.1.0 is the first release. Nothing below has a date.

hotkernels as a client. The lazy layer of hotkernels is an intended client and does not use shaperules yet.

More rules. numty, where these rules came from, has rules for more of NumPy than shaperules 0.1 does. They are candidates to move here. The items on this page that are candidates for later versions:

  • sums of products in the formula algebra;
  • a way to state that a name is at least 1;
  • indexing by arrays and masks;
  • the element type of matmul and the other generalized ufuncs;
  • rules for einsum, numpy.linalg, numpy.fft and the array constructors;
  • shapes of unknown rank.

A rule is added only with a test that holds it against NumPy for every assignment of small sizes, as Checked against NumPy describes. Development says how to add one.