Skip to content

Commit

Permalink
Make the output of ismember have the same shape as its first input (#…
Browse files Browse the repository at this point in the history
…657)

Also add documentation for the `ismember` function.
  • Loading branch information
AngelEzquerra committed Aug 12, 2024
1 parent 35adfc1 commit caf0d8d
Showing 1 changed file with 22 additions and 0 deletions.
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)

0 comments on commit caf0d8d

Please sign in to comment.