Get started¶
Install¶
It needs Python 3.11 or later. It has no dependencies, and NumPy does not have to be installed.
Ask for a shape¶
Import the package and give a rule the shapes of the operands. A shape is a tuple with one size per axis, as in NumPy.
>>> import shaperules as sr
>>> sr.broadcast((8, 1, 3), (5, 3))
Known(value=(8, 5, 3))
>>> sr.matmul((2, 3), (3, 5))
Known(value=(2, 5))
>>> sr.reduce((4, 5, 6), axis=1)
Known(value=(4, 6))
Each rule has the name of the NumPy operation it stands for. broadcast is
np.broadcast_shapes, matmul is a @ b, and reduce is the shape of
np.sum and the other reductions. Nothing is computed, so the shapes can be
as large as you like.
The answer is a Known object. Its value is the shape.
When NumPy would raise¶
A call NumPy refuses gets a Refused answer. It names the check that failed,
gives a reason, and names the exception NumPy raises.
>>> answer = sr.broadcast((3, 4), (5,))
>>> answer.refusal.kind
'broadcast'
>>> answer.refusal.raises
'ValueError'
>>> print(answer.refusal.message)
shapes (3, 4) (5,) do not broadcast: they disagree at axis -1
The message is written by shaperules. It is not NumPy's message. To show NumPy's own words, make the call in NumPy.
Use names for sizes you do not know¶
A size can be a name. Write it as a string. A name stands for one non-negative
integer, the same one wherever it appears, so n in one shape is n in
another.
>>> answer = sr.matmul(("batch", "n", 64), (64, 10))
>>> sr.fmt_shape(answer.value)
'(batch, n, 10)'
fmt_shape prints a shape the way NumPy prints one. Without it you see how a
formula is stored, which is exact but harder to read.
Sizes can be formulas, and rules produce formulas:
>>> answer = sr.concatenate([("n", 3), ("n + 1", 3)])
>>> sr.fmt_shape(answer.value)
'(2*n+1, 3)'
>>> answer = sr.reshape(("batch", "h", "w"), (-1,))
>>> sr.fmt_shape(answer.value)
'(batch*h*w,)'
A Known answer with names in it is a statement about every value the names
can take. (2*n+1, 3) is the shape NumPy gives whatever n is.
When it depends¶
Some questions have no one answer. Whether (n,) and (m,) broadcast depends
on n and m. The rule says so with an Unknown answer:
>>> answer = sr.broadcast(("n",), ("m",))
>>> answer.reason
'sizes at axis -1 must be equal or 1: n and m'
Where one reading is the natural one, Unknown carries it as likely. Taking
the first row of an array with n rows works unless n is 0:
>>> answer = sr.index(("n", 4), 0)
>>> answer.reason
'index 0 needs n to be at least 1'
>>> answer.likely
Known(value=(4,))
likely is not a proof. It is there for a caller, such as a type checker,
that would rather carry on with (4,) than stop.
Get the value or an exception¶
unwrap returns the value of a Known answer and raises for the other two.
best returns the value, or the likely value, or None.
>>> sr.broadcast((3, 1), (4,)).unwrap()
(3, 4)
>>> sr.broadcast((3,), (4,)).unwrap()
Traceback (most recent call last):
...
shaperules.outcome.ShapeRefused: shapes (3,) (4,) do not broadcast: they disagree at axis -1
>>> sr.index(("n", 4), 0).best()
(4,)
Ask for an element type¶
The element type of an array is its dtype: float32, int64, bool. Write
one by its NumPy name.
>>> sr.result_type("int8", "uint8")
Known(value='int16')
>>> sr.ufunc_dtype("true_divide", "int32", "int32")
Known(value='float64')
>>> sr.reduce_dtype("mean", "float32")
Known(value='float32')
result_type is np.result_type. ufunc_dtype is the element type a ufunc
returns, where a ufunc is one of NumPy's element-by-element functions such as
np.add or np.sqrt. reduce_dtype is the element type of a reduction.
Put the two together¶
The shape and the element type of a / b, for an int32 array of shape
(batch, 1, 3) and a float32 array of shape (n, 3):
>>> shape = sr.broadcast(("batch", 1, 3), ("n", 3))
>>> dtype = sr.ufunc_dtype("true_divide", "int32", "float32")
>>> sr.fmt_shape(shape.unwrap()), dtype.unwrap()
('(batch, n, 3)', 'float64')
Where next¶
- Sizes: what a formula can be, and the arithmetic on sizes.
- The three answers: what each one claims, and how to read them in code.
- Shape rules: every rule with an example.
- Element types: promotion, casting, ufuncs and reductions.
- Writing a client: two worked sketches.