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)

Arguments

x, y conformable atomic arrays of type complex.
op a single string, giving the operator.
Supported arithmetic operators: +, -, *, /.
Supported relational operators: ==, !=.

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.

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,] FALSE    NA   NA
## [2,]    NA    NA   NA
## [3,]    NA    NA   NA
## [4,]    NA FALSE   NA
## 
## , , 2
## 
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,]    NA    NA    NA
## [3,]    NA    NA    NA
## [4,] FALSE FALSE    NA
bc.cplx(x, y, "!=")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,] TRUE   NA   NA
## [2,]   NA   NA   NA
## [3,]   NA   NA   NA
## [4,]   NA TRUE   NA
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,]   NA   NA   NA
## [3,]   NA   NA   NA
## [4,] TRUE TRUE   NA

bc.cplx(x, y, "+")
## , , 1
## 
##                     [,1]          [,2] [,3]
## [1,] 2.213249-0.5778792i            NA   NA
## [2,]                  NA            NA   NA
## [3,]                  NA            NA   NA
## [4,]                  NA NaN+1.091747i   NA
## 
## , , 2
## 
##                     [,1]                 [,2]           [,3]
## [1,] 2.187562+0.7009029i 0.6965877-0.8832054i Inf-0.5304328i
## [2,]                  NA                   NA             NA
## [3,]                  NA                   NA             NA
## [4,]     -Inf-0.1552838i      -Inf+1.2456380i             NA

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

x <- gen() + gen() * -1i
y <- gen() + gen() * -1i
out <- bc.cplx(array(x), array(y), "*")
cbind(x, y, x*y, out)
##                            x                     y                      
##  [1,]        NaN-0.41763214i -1.3673795+0.4474673i        NaN+      NaNi
##  [2,]        Inf-0.12505665i        NaN+      NaNi        NaN+      NaNi
##  [3,]                     NA                    NA        NaN+      NaNi
##  [4,]        NaN-       Infi                    NA        NaN+      NaNi
##  [5,]  1.0888352-0.74023923i        NaN-      Infi       -Inf-      Infi
##  [6,]  0.4550647+0.12954745i       -Inf-0.5407217i       -Inf-      Infi
##  [7,]                     NA        NaN+      NaNi        NaN+      NaNi
##  [8,]  1.3575757-0.55321038i -0.1588378-0.6236658i -0.5606527-0.7588028i
##  [9,] -0.3575468-0.02553002i        NaN-      Infi       -Inf+      Infi
## [10,]  0.1577465+0.50925241i -0.6522608-0.9244976i  0.3679108-0.4780017i
## [11,]        Inf+0.57894251i        NaN+      Infi        NaN+      Infi
## [12,]        NaN-       Infi  1.1028783-1.4988861i       -Inf-      Infi
## [13,]       -Inf+0.56565038i  0.7897284-0.8139758i       -Inf+      Infi
## [14,]        NaN+       Infi  0.9757333+0.4933849i       -Inf+      Infi
## [15,]                     NA -0.3978522-1.2171176i                    NA
## [16,]        NaN+       NaNi  1.4476399+0.4616097i        NaN+      NaNi
## [17,]                     NA        NaN+      Infi        Inf+      NaNi
## [18,]        NaN+       NaNi                    NA                    NA
##                         out
##  [1,]                    NA
##  [2,]                    NA
##  [3,]                    NA
##  [4,]                    NA
##  [5,]                    NA
##  [6,]       -Inf-      Infi
##  [7,]                    NA
##  [8,] -0.5606527-0.7588028i
##  [9,]                    NA
## [10,]  0.3679108-0.4780017i
## [11,]                    NA
## [12,]                    NA
## [13,]       -Inf+      Infi
## [14,]                    NA
## [15,]                    NA
## [16,]                    NA
## [17,]                    NA
## [18,]                    NA