Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch order of compress and pad_to, make styling more concise #24

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,66 +428,66 @@ test_flip = make_test("flip", flip, flip_spec, add_sizes=["i"])
# run_test(test_flip)
```

## Puzzle 12 - compress
## Puzzle 12 - pad_to


Compute [compress](https://numpy.org/doc/stable/reference/generated/numpy.compress.html) - keep only masked entries (left-aligned).
Compute pad_to - eliminate or add 0s to change size of vector.


```python
def compress_spec(g, v, out):
j = 0
for i in range(len(g)):
if g[i]:
out[j] = v[i]
j += 1

def compress(g: TT["i", bool], v: TT["i"], i:int) -> TT["i"]:
def pad_to_spec(a, out):
for i in range(min(len(out), len(a))):
out[i] = a[i]


def pad_to(a: TT["i"], i: int, j: int) -> TT["j"]:
raise NotImplementedError


test_compress = make_test("compress", compress, compress_spec, add_sizes=["i"])
test_pad_to = make_test("pad_to", pad_to, pad_to_spec, add_sizes=["i", "j"])
```



![svg](Tensor%20Puzzlers_files/Tensor%20Puzzlers_44_0.svg)
![svg](Tensor%20Puzzlers_files/Tensor%20Puzzlers_47_0.svg)




```python
# run_test(test_compress)
# run_test(test_pad_to)
```

## Puzzle 13 - pad_to
## Puzzle 13 - compress


Compute pad_to - eliminate or add 0s to change size of vector.
Compute [compress](https://numpy.org/doc/stable/reference/generated/numpy.compress.html) - keep only masked entries (left-aligned).


```python
def pad_to_spec(a, out):
for i in range(min(len(out), len(a))):
out[i] = a[i]


def pad_to(a: TT["i"], i: int, j: int) -> TT["j"]:
def compress_spec(g, v, out):
j = 0
for i in range(len(g)):
if g[i]:
out[j] = v[i]
j += 1

def compress(g: TT["i", bool], v: TT["i"], i:int) -> TT["i"]:
raise NotImplementedError


test_pad_to = make_test("pad_to", pad_to, pad_to_spec, add_sizes=["i", "j"])
test_compress = make_test("compress", compress, compress_spec, add_sizes=["i"])
```



![svg](Tensor%20Puzzlers_files/Tensor%20Puzzlers_47_0.svg)
![svg](Tensor%20Puzzlers_files/Tensor%20Puzzlers_44_0.svg)




```python
# run_test(test_pad_to)
# run_test(test_compress)
```

## Puzzle 14 - sequence_mask
Expand Down
Loading