NaArray-Arith-methods: 'Arith' operations on NaArray objects

NaArray-Arith-methodsR Documentation

'Arith' operations on NaArray objects

Description

EXPERIMENTAL!!!

NaArray objects support all operations from the Arith group. See ?S4groupGeneric in the methods package for more information about the Arith group generic.

Note that NaArray of type() "complex" don't support Arith operations at the moment.

Details

Three forms of 'Arith' operations involving NaArray objects are supported:

  1. Between an NaArray object naa and a single value y:

        naa op y
        y op naa
  2. Between two NaArray objects naa1 and naa2 of same dimensions (a.k.a. conformable arrays):

        naa1 op naa2
  3. Between an NaArray object naa and an SVT_SparseArray object svt of same dimensions (a.k.a. conformable arrays):

        naa op svt
        svt op naa

Value

An NaArray object of the same dimensions as the input object(s).

See Also

  • S4groupGeneric in the methods package.

  • NaArray objects.

  • SVT_SparseArray objects.

  • Ordinary array objects in base R.

Examples

nam1 <- NaArray(dim=c(15, 6), type="integer")
nam1[cbind(1:15, 2)] <- 100:114
nam1[cbind(1:15, 5)] <- -(114:100)
nam1

nam1 * -0.01

nam1 * 10   # result is of type "double"
nam1 * 10L  # result is of type "integer"

nam1 / 10L

nam1 ^ 3.5

nam1 %% 5L
nam1 %/% 5L

nam2 <- NaArray(dim=dim(nam1), type="double")
nam2[c(2, 6, 12:17, 22:33, 55, 59:62, 90)] <- runif(26)
nam2

nam2 + nam1
nam2 - nam1
nam2 * nam1
nam2 / nam1
nam2 ^ nam1
nam2 %% nam1
nam2 %/% nam1

## Sanity checks:
m1 <- as.matrix(nam1)
m2 <- as.matrix(nam2)
stopifnot(
  identical(as.matrix(nam1 * -0.01), m1 * -0.01),
  identical(as.matrix(nam1 * 10), m1 * 10),
  identical(as.matrix(nam1 * 10L), m1 * 10L),
  identical(as.matrix(nam1 / 10L), m1 / 10L),
  identical(as.matrix(nam1 ^ 3.5), m1 ^ 3.5),
  identical(as.matrix(nam1 %% 5L), m1 %% 5L),
  identical(as.matrix(nam1 %/% 5L), m1 %/% 5L),
  identical(as.matrix(nam2 + nam1), m2 + m1),
  identical(as.matrix(nam2 - nam1), m2 - m1),
  identical(as.matrix(nam2 * nam1), m2 * m1),
  identical(as.matrix(nam2 / nam1), m2 / m1),
  all.equal(as.matrix(nam2 ^ nam1), m2 ^ m1),
  identical(as.matrix(nam2 %% nam1), m2 %% m1),
  identical(as.matrix(nam2 %/% nam1), m2 %/% m1)
)

Bioconductor/SparseArray documentation built on Oct. 30, 2024, 12:14 p.m.