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

Make the output of ismember have the same shape as its first input #657

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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
22 changes: 22 additions & 0 deletions src/arraymancer/tensor/algorithms.nim
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,28 @@ proc contains*[T](t: Tensor[T], item: T): bool {.inline.}=
return find(t, item) >= 0

proc ismember*[T](t1, t2: Tensor[T]): Tensor[bool] {.noinit.} =
## Element-wise check whether elements of a Tensor are contained in another Tensor
##
## Inputs:
## - t1: Tensor whose elements will be looked for, one by one, in `t2`
## - t2: Tensor in which elements of `t1` will be looked for
##
## Result:
## - A boolean tensor of the same shape as `t1`.
## Each element indicates if `t2` contains the `t1` element that
## is found in that particular position.
##
## Example:
## ```nim
## let t1 = arange(6).reshape(2, 3)
## let t2 = [-3, 0, 2, 5].toTensor
##
## echo t1.ismember(t2)
## # Tensor[system.bool] of shape "[2, 3]" on backend "Cpu"
## # |true false true|
## # |false false true|
## ```
result = newTensor[bool](t1.len)
for n, it in t1.enumerate():
result[n] = it in t2
result = result.reshape(t1.shape)
Loading