Matching
Utilities for comparing and partitioning named dimension tuples, including wildcard/ellipsis support.
torchlinops.nameddim.isequal
isequal(
shape1: Sequence,
shape2: Sequence,
shape1_consume_all: bool = False,
shape2_consume_all: bool = False,
) -> tuple[bool, Optional[dict[int, list]]]
Test if two sequences with ellipses are length-compatible and value-compatible.
Implemented with bottom-up DP
| PARAMETER | DESCRIPTION |
|---|---|
shape1
|
The sequences of tokens to compare.
TYPE:
|
shape2
|
The sequences of tokens to compare.
TYPE:
|
shape1_consume_all
|
Whether to consume all elements of the sequence greedily. This usually means maximizing matches with ELLIPSES tokens.
TYPE:
|
shape2_consume_all
|
Whether to consume all elements of the sequence greedily. This usually means maximizing matches with ELLIPSES tokens.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether shape1 and shape2 are compatible. |
dict[int, list]
|
A mapping from |
Examples:
>>> isequal(("A", "B"), ("A", "B"))[0]
True
>>> isequal(("A", "C"), ("A",))[0]
False
>>> isequal(("A", "C"), tuple())[0]
False
>>> isequal(("A", "C"), ("...",))[0]
True
>>> isequal(("A", "C", "..."), ("...",))[0]
True
>>> isequal(("A", "B", "C"), ("A", "...", "C"))[0]
True
>>> isequal(("...", "A", "C", "..."), ("...",))[0]
True
>>> isequal(("...", "A", "C"), ("B", "C"))[0]
False
Wildcards
Think about this one...
Source code in src/torchlinops/nameddim/_matching.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
torchlinops.nameddim.iscompatible
Whether the two shapes are length-compatible.
Greedily consume shape1, but not shape2
iscompatible(("A","B"), ("C", "D"))[0] True iscompatible(("...",), ("A","B"))[0] True iscompatible(("B","..."), ("A","..."))[0] True iscompatible(("...",), tuple())[0] True iscompatible(("C", "C"), ("D", "E"))[0] True
Source code in src/torchlinops/nameddim/_matching.py
torchlinops.nameddim.partition
Split a sequence on the first occurence of some value
Examples: