shaperules 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- shaperules/__init__.py +138 -0
- shaperules/_assemble.py +163 -0
- shaperules/_tables.py +933 -0
- shaperules/axes.py +316 -0
- shaperules/dims.py +716 -0
- shaperules/dtypes.py +450 -0
- shaperules/elementwise.py +127 -0
- shaperules/forms.py +538 -0
- shaperules/indexing.py +229 -0
- shaperules/outcome.py +177 -0
- shaperules/py.typed +0 -0
- shaperules/reshaping.py +203 -0
- shaperules-0.1.0.dist-info/METADATA +153 -0
- shaperules-0.1.0.dist-info/RECORD +16 -0
- shaperules-0.1.0.dist-info/WHEEL +4 -0
- shaperules-0.1.0.dist-info/licenses/LICENSE +21 -0
shaperules/__init__.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"""NumPy's shape and dtype rules as a library.
|
|
2
|
+
|
|
3
|
+
Give a rule the shapes of the operands and it answers with the shape of
|
|
4
|
+
the result, without NumPy and without running anything. Sizes can be
|
|
5
|
+
integers or formulas over names, and every answer is one of three:
|
|
6
|
+
|
|
7
|
+
- `Known(value)`: the result, proven for every assignment of the names;
|
|
8
|
+
- `Refused(refusal)`: NumPy raises, for every assignment;
|
|
9
|
+
- `Unknown(reason, likely)`: it depends on sizes the rule was not told.
|
|
10
|
+
|
|
11
|
+
>>> import shaperules as sr
|
|
12
|
+
>>> sr.matmul((32, "n", 64), (64, 10)).value == sr.shape(32, "n", 10)
|
|
13
|
+
True
|
|
14
|
+
>>> sr.broadcast((3, 4), (5,)).refusal.kind
|
|
15
|
+
'broadcast'
|
|
16
|
+
>>> sr.fmt_shape(sr.reshape((2, "n"), (-1,)).value)
|
|
17
|
+
'(2*n,)'
|
|
18
|
+
"""
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from importlib.metadata import version as _installed_version
|
|
22
|
+
|
|
23
|
+
from . import dims, dtypes, forms
|
|
24
|
+
from .axes import (
|
|
25
|
+
arg_reduce,
|
|
26
|
+
cumulative,
|
|
27
|
+
expand_dims,
|
|
28
|
+
moveaxis,
|
|
29
|
+
normalize_axes,
|
|
30
|
+
normalize_axis,
|
|
31
|
+
reduce,
|
|
32
|
+
squeeze,
|
|
33
|
+
swapaxes,
|
|
34
|
+
transpose,
|
|
35
|
+
)
|
|
36
|
+
from .dims import (
|
|
37
|
+
ANY,
|
|
38
|
+
AnyDim,
|
|
39
|
+
Dim,
|
|
40
|
+
DimLike,
|
|
41
|
+
Shape,
|
|
42
|
+
ShapeLike,
|
|
43
|
+
as_shape,
|
|
44
|
+
dim,
|
|
45
|
+
fmt,
|
|
46
|
+
fmt_shape,
|
|
47
|
+
fresh,
|
|
48
|
+
is_known,
|
|
49
|
+
parse,
|
|
50
|
+
shape,
|
|
51
|
+
substitute,
|
|
52
|
+
substitute_shape,
|
|
53
|
+
sym,
|
|
54
|
+
)
|
|
55
|
+
from .dtypes import (
|
|
56
|
+
DTYPES,
|
|
57
|
+
can_cast,
|
|
58
|
+
reduce_dtype,
|
|
59
|
+
result_type,
|
|
60
|
+
ufunc_dtype,
|
|
61
|
+
ufunc_dtypes,
|
|
62
|
+
)
|
|
63
|
+
from .elementwise import broadcast, broadcast_pair, match_pair, matmul
|
|
64
|
+
from .forms import DivForm, Form, LinForm, MonoForm
|
|
65
|
+
from .indexing import index, slice_length
|
|
66
|
+
from .outcome import (
|
|
67
|
+
Known,
|
|
68
|
+
Outcome,
|
|
69
|
+
Refusal,
|
|
70
|
+
Refused,
|
|
71
|
+
RuleError,
|
|
72
|
+
ShapeRefused,
|
|
73
|
+
ShapeUnknown,
|
|
74
|
+
Unknown,
|
|
75
|
+
)
|
|
76
|
+
from .reshaping import concatenate, reshape, stack
|
|
77
|
+
|
|
78
|
+
__version__ = _installed_version("shaperules")
|
|
79
|
+
|
|
80
|
+
__all__ = [
|
|
81
|
+
"ANY",
|
|
82
|
+
"DTYPES",
|
|
83
|
+
"AnyDim",
|
|
84
|
+
"Dim",
|
|
85
|
+
"DimLike",
|
|
86
|
+
"DivForm",
|
|
87
|
+
"Form",
|
|
88
|
+
"Known",
|
|
89
|
+
"LinForm",
|
|
90
|
+
"MonoForm",
|
|
91
|
+
"Outcome",
|
|
92
|
+
"Refusal",
|
|
93
|
+
"Refused",
|
|
94
|
+
"RuleError",
|
|
95
|
+
"Shape",
|
|
96
|
+
"ShapeLike",
|
|
97
|
+
"ShapeRefused",
|
|
98
|
+
"ShapeUnknown",
|
|
99
|
+
"Unknown",
|
|
100
|
+
"arg_reduce",
|
|
101
|
+
"as_shape",
|
|
102
|
+
"broadcast",
|
|
103
|
+
"broadcast_pair",
|
|
104
|
+
"can_cast",
|
|
105
|
+
"concatenate",
|
|
106
|
+
"cumulative",
|
|
107
|
+
"dim",
|
|
108
|
+
"dims",
|
|
109
|
+
"dtypes",
|
|
110
|
+
"expand_dims",
|
|
111
|
+
"fmt",
|
|
112
|
+
"fmt_shape",
|
|
113
|
+
"forms",
|
|
114
|
+
"fresh",
|
|
115
|
+
"index",
|
|
116
|
+
"is_known",
|
|
117
|
+
"match_pair",
|
|
118
|
+
"matmul",
|
|
119
|
+
"moveaxis",
|
|
120
|
+
"normalize_axes",
|
|
121
|
+
"normalize_axis",
|
|
122
|
+
"parse",
|
|
123
|
+
"reduce",
|
|
124
|
+
"reduce_dtype",
|
|
125
|
+
"reshape",
|
|
126
|
+
"result_type",
|
|
127
|
+
"shape",
|
|
128
|
+
"slice_length",
|
|
129
|
+
"squeeze",
|
|
130
|
+
"stack",
|
|
131
|
+
"substitute",
|
|
132
|
+
"substitute_shape",
|
|
133
|
+
"swapaxes",
|
|
134
|
+
"sym",
|
|
135
|
+
"transpose",
|
|
136
|
+
"ufunc_dtype",
|
|
137
|
+
"ufunc_dtypes",
|
|
138
|
+
]
|
shaperules/_assemble.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""Putting per-axis answers together into the answer for a whole shape."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from collections.abc import Sequence
|
|
5
|
+
|
|
6
|
+
from .dims import ANY, AnyDim, Dim, Shape, can_be, equal, fmt, unequal
|
|
7
|
+
from .outcome import Known, Outcome, Refusal, Refused, Unknown
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def assemble(parts: Sequence[Dim | Outcome[Dim] | Outcome[Shape]]) -> Outcome[Shape]:
|
|
11
|
+
"""Join sizes and answers, in order, into one answer for the shape.
|
|
12
|
+
|
|
13
|
+
A plain size stands for itself. An answer about one size gives one
|
|
14
|
+
axis, an answer about a shape gives its axes. One refusal refuses the
|
|
15
|
+
whole; one doubt makes the whole an `Unknown`, whose likely reading
|
|
16
|
+
joins the likely readings of its parts when each has one.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
parts: The sizes and answers, in axis order.
|
|
20
|
+
|
|
21
|
+
Returns:
|
|
22
|
+
The answer for the shape.
|
|
23
|
+
"""
|
|
24
|
+
dims: list[Dim] = []
|
|
25
|
+
reasons: list[str] = []
|
|
26
|
+
likely_refusal: Refused | None = None
|
|
27
|
+
no_likely = False
|
|
28
|
+
for part in parts:
|
|
29
|
+
if isinstance(part, Refused):
|
|
30
|
+
return part
|
|
31
|
+
if isinstance(part, Unknown):
|
|
32
|
+
reasons.append(part.reason)
|
|
33
|
+
if isinstance(part.likely, Refused):
|
|
34
|
+
likely_refusal = likely_refusal or part.likely
|
|
35
|
+
elif part.likely is None:
|
|
36
|
+
no_likely = True
|
|
37
|
+
else:
|
|
38
|
+
_extend(dims, part.likely.value)
|
|
39
|
+
elif isinstance(part, Known):
|
|
40
|
+
_extend(dims, part.value)
|
|
41
|
+
else:
|
|
42
|
+
dims.append(part)
|
|
43
|
+
if not reasons:
|
|
44
|
+
return Known(tuple(dims))
|
|
45
|
+
reason = "; ".join(dict.fromkeys(reasons))
|
|
46
|
+
if likely_refusal is not None:
|
|
47
|
+
return Unknown(reason, likely_refusal)
|
|
48
|
+
if no_likely:
|
|
49
|
+
return Unknown(reason, None)
|
|
50
|
+
return Unknown(reason, Known(tuple(dims)))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _extend(dims: list[Dim], value: object) -> None:
|
|
54
|
+
if isinstance(value, tuple) and not hasattr(value, "_fields"):
|
|
55
|
+
dims.extend(value)
|
|
56
|
+
else:
|
|
57
|
+
dims.append(value) # type: ignore[arg-type]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def check_only(answer: Outcome[Dim]) -> Outcome[Shape]:
|
|
61
|
+
"""Keep an answer's verdict and drop its size.
|
|
62
|
+
|
|
63
|
+
For a check whose size does not appear in the result, such as the
|
|
64
|
+
inner sizes of a matrix product.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
answer: The answer about one size.
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
The same verdict about the empty shape.
|
|
71
|
+
"""
|
|
72
|
+
if isinstance(answer, Refused):
|
|
73
|
+
return answer
|
|
74
|
+
if isinstance(answer, Known):
|
|
75
|
+
return Known(())
|
|
76
|
+
likely = answer.likely
|
|
77
|
+
if isinstance(likely, Known):
|
|
78
|
+
return Unknown(answer.reason, Known(()))
|
|
79
|
+
return Unknown(answer.reason, likely)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def meet(dims: Sequence[Dim], refusal: Refusal, what: str) -> Outcome[Dim]:
|
|
83
|
+
"""The one size several sizes must all equal.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
dims: The sizes that must agree, at least one.
|
|
87
|
+
refusal: The refusal to answer with when two of them never agree.
|
|
88
|
+
what: What the sizes are, for the reason of an `Unknown`.
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
`Known` when they are provably one size, `Refused` when two of
|
|
92
|
+
them provably differ, else `Unknown`. The likely size is the
|
|
93
|
+
integer among them, else the one formula, else `ANY`; with two
|
|
94
|
+
different formulas there is none.
|
|
95
|
+
"""
|
|
96
|
+
distinct = _distinct(dims)
|
|
97
|
+
if len(distinct) == 1:
|
|
98
|
+
return Known(distinct[0])
|
|
99
|
+
for i, a in enumerate(distinct):
|
|
100
|
+
for b in distinct[i + 1:]:
|
|
101
|
+
if unequal(a, b):
|
|
102
|
+
return Refused(refusal)
|
|
103
|
+
return Unknown(f"{what} must be equal: {_listing(distinct)}", _likely_one(distinct))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def stretch(dims: Sequence[Dim], refusal: Refusal, what: str) -> Outcome[Dim]:
|
|
107
|
+
"""The size several sizes broadcast to.
|
|
108
|
+
|
|
109
|
+
NumPy's rule for one axis: the sizes that are not 1 must all be equal,
|
|
110
|
+
and that size is the result; all 1 gives 1.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
dims: The sizes of one axis across the operands, at least one.
|
|
114
|
+
refusal: The refusal to answer with when they never broadcast.
|
|
115
|
+
what: What the sizes are, for the reason of an `Unknown`.
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
`Known` when the result is the same for every assignment,
|
|
119
|
+
`Refused` when no assignment broadcasts, else `Unknown`.
|
|
120
|
+
"""
|
|
121
|
+
rest = [d for d in dims if not (isinstance(d, int) and d == 1)]
|
|
122
|
+
if not rest:
|
|
123
|
+
return Known(1)
|
|
124
|
+
distinct = _distinct(rest)
|
|
125
|
+
if len(distinct) == 1:
|
|
126
|
+
return Known(distinct[0])
|
|
127
|
+
differ = False
|
|
128
|
+
for i, a in enumerate(distinct):
|
|
129
|
+
for b in distinct[i + 1:]:
|
|
130
|
+
if not unequal(a, b):
|
|
131
|
+
continue
|
|
132
|
+
differ = True
|
|
133
|
+
# Two sizes that are never equal broadcast only when one is 1.
|
|
134
|
+
if not can_be(a, 1) and not can_be(b, 1):
|
|
135
|
+
return Refused(refusal)
|
|
136
|
+
reason = f"{what} must be equal or 1: {_listing(distinct)}"
|
|
137
|
+
if differ:
|
|
138
|
+
return Unknown(reason, Refused(refusal))
|
|
139
|
+
return Unknown(reason, _likely_one(distinct))
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _distinct(dims: Sequence[Dim]) -> list[Dim]:
|
|
143
|
+
out: list[Dim] = []
|
|
144
|
+
for d in dims:
|
|
145
|
+
if not any(equal(d, seen) for seen in out):
|
|
146
|
+
out.append(d)
|
|
147
|
+
return out
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _likely_one(distinct: Sequence[Dim]) -> Known[Dim] | None:
|
|
151
|
+
ints = [d for d in distinct if isinstance(d, int)]
|
|
152
|
+
if ints:
|
|
153
|
+
return Known(ints[0])
|
|
154
|
+
named = [d for d in distinct if not isinstance(d, AnyDim)]
|
|
155
|
+
if len(named) == 1:
|
|
156
|
+
return Known(named[0])
|
|
157
|
+
if not named:
|
|
158
|
+
return Known(ANY)
|
|
159
|
+
return None
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _listing(dims: Sequence[Dim]) -> str:
|
|
163
|
+
return " and ".join(fmt(d) for d in dims)
|