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

bc.cplx(x, y, "+")
## , , 1
## 
##                    [,1]               [,2] [,3]
## [1,]                 NA                 NA   NA
## [2,]                 NA                 NA   NA
## [3,]                 NA                 NA   NA
## [4,] 2.776478-1.964087i 2.855735-1.149884i   NA
## 
## , , 2
## 
##                      [,1]           [,2]                [,3]
## [1,]                   NA             NA                  NA
## [2,] 0.3464989+0.1879875i             NA 0.9025008-1.363228i
## [3,]       Inf-0.4606219i Inf+0.3525254i                  NA
## [4,] 1.7300304-0.7989848i             NA                  NA

bc.cplx(array(gen() + gen() * -1i), array(gen() + gen() * -1i), "==")
##  [1]    NA FALSE    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
## [13] FALSE FALSE    NA    NA    NA    NA
bc.cplx(array(gen() + gen() * -1i), array(gen() + gen() * -1i), "!=")
##  [1]   NA   NA   NA TRUE   NA   NA TRUE   NA   NA   NA   NA TRUE   NA   NA   NA
## [16]   NA   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,]                    NA        Inf+0.94155695i       NaN+      NaNi
##  [2,]        NaN+      NaNi                     NA                   NA
##  [3,]        NaN-      Infi                     NA       NaN+      NaNi
##  [4,]       -Inf-0.9535544i -0.4790863+0.78120667i       Inf-      Infi
##  [5,]  1.3718373-0.1728437i        NaN+       NaNi       NaN+      NaNi
##  [6,] -1.3837281+0.5646172i -1.0213715-0.03598777i 1.4336197-0.5268867i
##  [7,]  0.6111071+0.5680749i        NaN+       NaNi       NaN+      NaNi
##  [8,] -1.0499911-0.4871116i  0.8239428+2.89693845i 0.5459998-3.4431117i
##  [9,]        NaN+      Infi                     NA       Inf+      NaNi
## [10,] -1.7385643-0.3953773i        NaN+0.87000020i       NaN+      NaNi
## [11,]        NaN-      Infi        NaN+       Infi       Inf+      NaNi
## [12,]        NaN+      Infi        NaN-       Infi       Inf+      NaNi
## [13,]  0.1262318+2.0067546i        NaN+       Infi      -Inf+      Infi
## [14,]        NaN+      NaNi                     NA                   NA
## [15,]                    NA       -Inf-0.09450334i       NaN+      Infi
## [16,]        Inf-1.0307139i        NaN-       Infi       NaN-      Infi
## [17,]                    NA -1.4001893+0.94369402i                   NA
## [18,]        NaN-0.3678110i       -Inf-1.50733851i       NaN+      Infi
##                        out
##  [1,]                   NA
##  [2,]                   NA
##  [3,]                   NA
##  [4,]       Inf-      Infi
##  [5,]                   NA
##  [6,] 1.4336197-0.5268867i
##  [7,]                   NA
##  [8,] 0.5459998-3.4431117i
##  [9,]                   NA
## [10,]                   NA
## [11,]                   NA
## [12,]                   NA
## [13,]                   NA
## [14,]                   NA
## [15,]                   NA
## [16,]                   NA
## [17,]                   NA
## [18,]                   NA