Skip to content

Commit

Permalink
Do not implement nor test copySign for nim versions earlier than 1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelEzquerra committed Oct 22, 2023
1 parent a50f45f commit 8dfb4b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
27 changes: 14 additions & 13 deletions src/arraymancer/tensor/math_functions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,20 @@ proc sgn*[T: SomeNumber](t: Tensor[T]): Tensor[int] {.noinit.} =
## - 0 for positive zero, negative zero and NaN
t.map_inline(sgn(x))

proc copySign*[T: SomeFloat](t1, t2: Tensor[T]): Tensor[T] {.noinit.} =
## Element-wise copySign function (combines 2 tensors, taking the magnitudes from t1 and the signs from t2)
##
## This uses nim's copySign under the hood, and thus has the same properties. That is, it works for values
## which are NaN, infinity or zero (all of which can carry a sign) but does not work for integers.
t1.map2_inline(t2, copySign(x, y))

proc mcopySign*[T: SomeFloat](t1: var Tensor[T], t2: Tensor[T]) =
## In-place element-wise copySign function (changes the signs of the elements of t1 to match those of t2)
##
## This uses nim's copySign under the hood, and thus has the same properties. That is, it works for values
## which are NaN, infinity or zero (all of which can carry a sign) but does not work for integers.
t1.apply2_inline(t2, copySign(x, y))
when (NimMajor, NimMinor, NimPatch) >= (1, 6, 0):
proc copySign*[T: SomeFloat](t1, t2: Tensor[T]): Tensor[T] {.noinit.} =
## Element-wise copySign function (combines 2 tensors, taking the magnitudes from t1 and the signs from t2)
##
## This uses nim's copySign under the hood, and thus has the same properties. That is, it works for values
## which are NaN, infinity or zero (all of which can carry a sign) but does not work for integers.
t1.map2_inline(t2, copySign(x, y))

proc mcopySign*[T: SomeFloat](t1: var Tensor[T], t2: Tensor[T]) =
## In-place element-wise copySign function (changes the signs of the elements of t1 to match those of t2)
##
## This uses nim's copySign under the hood, and thus has the same properties. That is, it works for values
## which are NaN, infinity or zero (all of which can carry a sign) but does not work for integers.
t1.apply2_inline(t2, copySign(x, y))

proc floorMod*[T: SomeNumber](t1, t2: Tensor[T]): Tensor[T] {.noinit.} =
## Broadcasted floorMod operation: floorMod(tensor, tensor).
Expand Down
10 changes: 5 additions & 5 deletions tests/tensor/test_math_functions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ proc main() =
var a = [-5.3, 42.0, -0.0, 0.01, 10.7, -0.001, 0.9, -125.3].toTensor
let expected_signs = [-1, 1, 0, 1, 1, -1, 1, -1].toTensor()
check: a.sgn() == expected_signs

let new_signs = arange(-4.0, 4.0)
a.mcopySign(new_signs)
let expected = [-5.3, -42.0, -0.0, -0.01, 10.7, 0.001, 0.9, 125.3].toTensor
check: a == expected
when (NimMajor, NimMinor, NimPatch) >= (1, 6, 0):
let new_signs = arange(-4.0, 4.0)
a.mcopySign(new_signs)
let expected = [-5.3, -42.0, -0.0, -0.01, 10.7, 0.001, 0.9, 125.3].toTensor
check: a == expected

test "Modulo functions":
var a = arange(-70.7, 50.0, 34.7)
Expand Down

0 comments on commit 8dfb4b0

Please sign in to comment.