bc.cplx

Broadcasted Complex Numeric Operations

Description

The bc.cplx() function performs broadcasted complex numeric operations on pairs of arrays.

Note that bc.cplx() uses more strict NA checks than base ‘R’:
If for an element of either x or y, either the real or imaginary part is NA or NaN, than the result of the operation for that element is necessarily NA.


Usage

bc.cplx(x, y, op, ...)

## S4 method for signature 'ANY'
bc.cplx(x, y, op)

Arguments

x, y conformable atomic arrays of type complex.
op a single string, giving the operator.
Supported arithmetic operators: +, -, *, /.
Supported relational operators: ==, !=.
… further arguments passed to or from methods.

Value

For arithmetic operators:
A complex array as a result of the broadcasted arithmetic operation.

For relational operators:
A logical array as a result of the broadcasted relational comparison.

See Also

broadcast_operators

Examples

library("broadcast")

x.dim <- c(4:2)
x.len <- prod(x.dim)
gen <- function() sample(c(rnorm(10), NA, NA, NaN, NaN, Inf, Inf, -Inf, -Inf))
x <- array(gen() + gen() * -1i, x.dim)
y <- array(gen() + gen() * -1i, c(4,1,1))

bc.cplx(x, y, "==")
## , , 1
## 
##       [,1]  [,2]  [,3]
## [1,]    NA    NA FALSE
## [2,]    NA    NA    NA
## [3,] FALSE FALSE    NA
## [4,]    NA    NA    NA
## 
## , , 2
## 
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,]    NA    NA    NA
## [3,]    NA    NA    NA
## [4,]    NA    NA    NA
bc.cplx(x, y, "!=")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]   NA   NA TRUE
## [2,]   NA   NA   NA
## [3,] TRUE TRUE   NA
## [4,]   NA   NA   NA
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,]   NA   NA   NA
## [3,]   NA   NA   NA
## [4,]   NA   NA   NA

bc.cplx(x, y, "+")
## , , 1
## 
##                       [,1]             [,2]          [,3]
## [1,]                    NA               NA Inf+1.560884i
## [2,]                    NA               NA            NA
## [3,] -0.7313348+0.8950085i Inf-0.008635942i            NA
## [4,]                    NA               NA            NA
## 
## , , 2
## 
##                       [,1]            [,2]              [,3]
## [1,] 0.2682064-0.05053901i -Inf+0.6981354i 0.5045929+1.5143i
## [2,]                    NA              NA                NA
## [3,]                    NA              NA                NA
## [4,]                    NA              NA                NA

bc.cplx(array(gen() + gen() * -1i), array(gen() + gen() * -1i), "==")
##  [1]    NA    NA    NA    NA    NA    NA    NA FALSE FALSE FALSE FALSE    NA
## [13]    NA    NA    NA FALSE    NA FALSE
bc.cplx(array(gen() + gen() * -1i), array(gen() + gen() * -1i), "!=")
##  [1]   NA   NA   NA   NA   NA TRUE   NA   NA   NA   NA   NA   NA   NA   NA   NA
## [16]   NA   NA TRUE

x <- gen() + gen() * -1i
y <- gen() + gen() * -1i
out <- bc.cplx(array(x), array(y), "*")
cbind(x, y, x*y, out)
##                            x                      y                       
##  [1,] -0.67427071-0.1074557i         NaN+      NaNi        NaN+       NaNi
##  [2,]  0.80735967+1.4407615i                     NA       -Inf+       Infi
##  [3,]         NaN+0.2769170i         NaN-      Infi        Inf+       NaNi
##  [4,]         NaN-      Infi  0.44702425+1.0371062i        Inf-       Infi
##  [5,]         NaN+      NaNi         NaN+1.7093388i        NaN+       NaNi
##  [6,]                     NA  0.02236762-2.1621583i                     NA
##  [7,]         Inf+0.7733469i         NaN+      Infi        NaN+       Infi
##  [8,]                     NA         Inf-1.9436173i        NaN+       Infi
##  [9,]         NaN+      NaNi         NaN+      NaNi        NaN+       NaNi
## [10,]         NaN-      Infi                     NA        NaN+       NaNi
## [11,]        -Inf+0.4608776i  0.56834302-1.3204479i       -Inf+       Infi
## [12,]  1.74367898+0.1268254i -0.64595676-0.1150314i -1.1117523-0.28250146i
## [13,] -0.03972432+0.1890154i         NaN-      Infi        Inf+       Infi
## [14,]                     NA        -Inf-1.5376927i        NaN-       Infi
## [15,]         NaN+      Infi                     NA        NaN+       NaNi
## [16,]                     NA         NaN-0.1186844i        NaN+       NaNi
## [17,]  0.30541838+0.7460966i -2.32274123+1.0656906i -1.5045160-1.40750792i
## [18,]  0.77552261-0.9723268i -0.06002562+0.0156858i -0.0312995+0.07052921i
##                          out
##  [1,]                     NA
##  [2,]                     NA
##  [3,]                     NA
##  [4,]                     NA
##  [5,]                     NA
##  [6,]                     NA
##  [7,]                     NA
##  [8,]                     NA
##  [9,]                     NA
## [10,]                     NA
## [11,]       -Inf+       Infi
## [12,] -1.1117523-0.28250146i
## [13,]                     NA
## [14,]                     NA
## [15,]                     NA
## [16,]                     NA
## [17,] -1.5045160-1.40750792i
## [18,] -0.0312995+0.07052921i