Skip to content

Commit

Permalink
Support appending one or more individual values (#627)
Browse files Browse the repository at this point in the history
In addition to appending a tensor or an openArray, also support appending individual values (without putting them inside a seq or array).
  • Loading branch information
AngelEzquerra authored Feb 27, 2024
1 parent 30ec970 commit 7dac879
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/arraymancer/tensor/shapeshifting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,12 @@ proc append*[T](t: Tensor[T], values: Tensor[T]): Tensor[T] {.noinit.} =
result[0 ..< t.size] = t
result[t.size ..< result.size] = values

proc append*[T](t: Tensor[T], values: openArray[T]): Tensor[T] {.noinit.} =
## Create a copy of an rank-1 input tensor with values appended to its end
proc append*[T](t: Tensor[T], values: varargs[T]): Tensor[T] {.noinit.} =
## Create a copy of an rank-1 input tensor with one or more values appended to its end
##
## Inputs:
## - Rank-1 tensor of type T
## - An open array of values of type T
## - An open array or a list of values of type T
## Returns:
## - A copy of the input tensor t with the extra values appended at the end.
## Notes:
Expand All @@ -328,14 +328,23 @@ proc append*[T](t: Tensor[T], values: openArray[T]): Tensor[T] {.noinit.} =
## support the `axis` parameter. If you want to append values along a
## specific axis, you should use `concat` instead.
## Examples:
## - Append a single value
## > echo append([1, 2, 3].toTensor, 4)
## > # Tensor[system.int] of shape "[9]" on backend "Cpu"
## > # 1 2 3 4
## - Append a multiple values
## > echo append([1, 2, 3].toTensor, [4, 5, 6, 7])
## > # Tensor[system.int] of shape "[9]" on backend "Cpu"
## > # 1 2 3 4 5 6 7
## >
## - Append an openArray of values
## > echo append([1, 2, 3].toTensor, [4, 5, 6, 7])
## > # Tensor[system.int] of shape "[9]" on backend "Cpu"
## > # 1 2 3 4 5 6 7
## - Only rank-1 tensors are supported
## > echo append([[1, 2, 3], [4, 5, 6]].toTensor, [7, 8, 9])
## > # Error: unhandled exception: `t.rank == 1` append only works
## > # on rank-1 tensors but first input tensor has rank 2 [AssertionDefect]
## >
## - Flatten higher ranked tensors before appending
## > echo append([[1, 2, 3], [4, 5, 6]].toTensor.flatten, [7, 8, 9])
## > # 1 2 3 4 5 6 7 8 9
doAssert t.rank == 1,
Expand Down
7 changes: 5 additions & 2 deletions tests/tensor/test_shapeshifting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ proc main() =
test "Append":
let a = toSeq(1..4).toTensor()
let b = toSeq(5..8)
let expected = [1,2,3,4,5,6,7,8].toTensor()

check: a.append(b) == [1,2,3,4,5,6,7,8].toTensor()
check: a.append(b.toTensor()) == [1,2,3,4,5,6,7,8].toTensor()
check: a.append(5) == [1,2,3,4,5].toTensor()
check: a.append(5, 6, 7, 8) == expected
check: a.append(b) == expected
check: a.append(b.toTensor()) == expected

test "Squeeze":
block:
Expand Down

0 comments on commit 7dac879

Please sign in to comment.