Skip to content

shaperules

shaperules is NumPy's shape and dtype rules as a pure-Python library with no dependencies. Give a rule the shapes of the operands and it answers with the shape of the result, without NumPy installed and without running anything.

>>> import shaperules as sr

>>> sr.broadcast((8, 1, 3), (5, 3))
Known(value=(8, 5, 3))

>>> sr.matmul((2, 3), (4, 5)).refusal.kind
'matmul-core'

The first call is what np.broadcast_shapes((8, 1, 3), (5, 3)) returns. The second is a matrix product NumPy raises ValueError for, and the rule says so without building a matrix.

Sizes can be names

A size is the length of one axis. It can be an integer, a formula over names, or unknown. A name stands for one non-negative integer, the same one wherever the name appears.

>>> product = sr.matmul(("batch", "n", 64), (64, 10))
>>> sr.fmt_shape(product.value)
'(batch, n, 10)'

>>> flat = sr.reshape(("batch", 28, 28), ("batch", 784))
>>> sr.fmt_shape(flat.value)
'(batch, 784)'

>>> halved = sr.index(("n", 3), slice(None, None, 2))
>>> sr.fmt_shape(halved.value)
'((n+1)//2, 3)'

Each of these holds for every value batch and n can take, zero included. Sizes describes the formulas and their arithmetic.

Three answers

A rule cannot always give a shape. Whether (n,) and (3,) broadcast depends on n. So every rule answers one of three things:

Answer Meaning
Known(value) The result, proven for every assignment of the names.
Refused(refusal) NumPy raises, for every assignment of the names.
Unknown(reason, likely) It depends on sizes the rule was not told.

An assignment gives each name a non-negative integer. Known and Refused are proofs. Unknown carries a reason in words and, where there is one, a likely reading for a caller that wants a best answer all the same:

>>> doubt = sr.broadcast(("n",), (3,))
>>> doubt.reason
'sizes at axis -1 must be equal or 1: n and 3'
>>> doubt.likely
Known(value=(3,))

A caller that must agree with NumPy exactly acts on Known and Refused and asks NumPy otherwise. A type checker can carry on with likely. The three answers has the details, and Writing a client works through both kinds of caller.

Element types

The element type of a result, its dtype, is answered the same way:

>>> sr.result_type("int8", "uint8").value
'int16'
>>> sr.ufunc_dtype("sqrt", "int8").value
'float16'
>>> sr.reduce_dtype("sum", "int8").value
'int64'

Fourteen element types are covered, from bool to complex128, along with Python numbers as NumPy 2 promotes them. See Element types.

What is covered

  • Broadcasting: broadcast, broadcast_pair, match_pair.
  • The matrix product: matmul.
  • Reductions and running totals: reduce, arg_reduce, cumulative.
  • Axes: transpose, swapaxes, moveaxis, squeeze, expand_dims.
  • Regrouping: reshape with -1, concatenate, stack.
  • Basic indexing: index and slice_length, for integers, slices, None and ....
  • Element types: result_type, can_cast, ufunc_dtype, ufunc_dtypes, reduce_dtype.

Shape rules goes through them one by one. What is not covered is listed in Limitations and roadmap.

Checked against NumPy

NumPy is not needed to use shaperules. It is the reference in the tests. Every shape rule is run against real NumPy calls for every assignment of small sizes to the names, and every element-type answer is compared with NumPy for every combination covered. The examples on these pages are run by the test suite too. Checked against NumPy describes how.

Where it comes from

The rules were extracted from numty, a mypy plugin that checks NumPy shapes statically, so that clients other than a type checker can use them. numty imports its size algebra and its dtype promotion from shaperules. A second intended client is the lazy layer of hotkernels, which needs the shape and element type of a NumPy call at run time without computing it. hotkernels does not use shaperules yet.

Next

  • Get started: install it and ask the first questions.
  • API reference: every public function.
  • Design: why there are three answers, and what a proof means here.