Skip to content

Commit

Permalink
Add cswap function
Browse files Browse the repository at this point in the history
`cswap` "swaps" the real and imaginary components of a complex tensor)
  • Loading branch information
AngelEzquerra committed Mar 2, 2024
1 parent f564231 commit 328f939
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/arraymancer/tensor/complex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ proc conjugate*[T: Complex32 | Complex64](A: Tensor[T]): Tensor[T] =
## Return the element-wise complex conjugate of a tensor of complex numbers.
## The complex conjugate of a complex number is obtained by changing the sign of its imaginary part.
A.map_inline(x.conjugate)

proc cswap*[T: Complex32 | Complex64](t: Tensor[T]): Tensor[T] {.inline, noinit.} =
## Swap the real and imaginary components of the elements of a complex Tensor
map_inline(t, complex(x.im, x.re))
15 changes: 15 additions & 0 deletions tests/tensor/test_complex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,20 @@ proc main() =

check: c.conjugate == expected_c_conjugate

test "Complex Component Swap":
var c = [
complex(1.0, -300.0),
complex(-10.0, 20.0),
complex(20.0, -1.0),
].toTensor

var expected_c_swapped = [
complex(-300.0, 1.0),
complex(20.0, -10.0),
complex(-1.0, 20.0),
].toTensor

check: c.cswap == expected_c_swapped

main()
GC_fullCollect()

0 comments on commit 328f939

Please sign in to comment.