-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: study for CuPy version of notebook solution
- Loading branch information
1 parent
8e24156
commit d607f75
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
studies/cuda-kernels/combinations/lexicographic_formula.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import cupy as cp | ||
|
||
def distincts(content, counts): | ||
counts_comb = counts * (counts - 1) // 2 | ||
offsets_comb = cp.cumsum(cp.concatenate((cp.array([0]), counts_comb))) | ||
parents_comb = cp.zeros(8, dtype=int) | ||
for i in range(1, len(offsets_comb)): | ||
parents_comb[offsets_comb[i-1]:offsets_comb[i]] = i - 1 | ||
|
||
local_indices = cp.arange(offsets_comb[-1]) - offsets_comb[parents_comb] | ||
|
||
n = counts[parents_comb] | ||
b = 2 * n - 1 | ||
i = cp.floor((b - cp.sqrt(b * b - 8 * local_indices)) / 2).astype(counts_comb.dtype) | ||
j = local_indices + i * (i - b + 2) // 2 + 1 | ||
|
||
starts_parents = cp.cumsum(cp.concatenate((cp.array([0]), counts)))[:-1][parents_comb] | ||
i += starts_parents | ||
j += starts_parents | ||
|
||
if max(i.max(), j.max()) >= len(content): | ||
raise IndexError("index exceeds the bounds of the content array.") | ||
|
||
out_content = cp.vstack((content[i], content[j])) | ||
|
||
return out_content | ||
|
||
content = cp.array([0, 1, 2, 3, 4, 5, 6, 7]) | ||
counts = cp.array([2, 0, 4, 2]) | ||
result = distincts(content, counts) | ||
|
||
print("combinations:\n", result) |