## Work with Complex Numbers on a GPU

### Conditions for Working With Complex Numbers on a GPU

If the output of a function running on a GPU could potentially be complex, you must explicitly specify its input arguments as complex using the `complex` function. This applies to functions operating on `gpuArray` objects directly and to functions operating on `gpuArray` data using `arrayfun`.

When creating a `gpuArray` that has negative elements, use `G = gpuArray(complex(x))`, then you can successfully execute `sqrt(G)`. For example,

```x = [-1 0 1]; G = gpuArray(complex(x)); rootG = sqrt(G)```
` 0.0000 + 1.0000i 0.0000 + 0.0000i 1.0000 + 0.0000i`

If the result is a `gpuArray` of complex data and all the imaginary parts are zero, these parts are retained and the data remains complex. This could result in unnecessary calculations being performed when operating on the data using, for example, `sort` and `isreal`.

If using `arrayfun`, the function input can be specified as complex within the `arrayfun` call. For example,

```x = [-1 0 1]; G = gpuArray(x); A = arrayfun(@(p) sqrt(complex(p)), G)```
` 0.0000 + 1.0000i 0.0000 + 0.0000i 1.0000 + 0.0000i`

### Functions That Return Complex Data

This table lists the functions that might return complex data, along with the input range over which the output remains real.

FunctionInput Range for Real Output
`acos(x)``abs(x) <= 1`
`acosh(x)``x >= 1`
`acoth(x)``abs(x) >= 1`
`acsc(x)``abs(x) >= 1`
`asec(x)``abs(x) >= 1`
`asech(x)``0 <= x <= 1`
`asin(x)``abs(x) <= 1`
`atanh(x)``abs(x) <= 1`
`log(x)``x >= 0`
`log1p(x)``x >= -1`
`log10(x)``x >= 0`
`log2(x)``x >= 0`
`power(x,y)``x >= 0`
`reallog(x)``x >= 0`
`realsqrt(x)``x >= 0`
`sqrt(x)``x >= 0`