Learn R Programming

Rmpfr (version 0.5-3)

mpfrArray: Construct "mpfrArray" almost as by 'array()'

Description

Utility to construct an Robject of class mpfrArray, very analogously to the numeric array function.

Usage

mpfrArray(x, precBits, dim = length(x), dimnames = NULL,
          rnd.mode = c("N","D","U","Z"))

Arguments

x
numeric(like) vector, typically of length prod(dim) or shorter in which case it is recycled.
precBits
a number, the maximal precision to be used, in bits; i.e., 53 corresponds to double precision. Must be at least 2.
dim
the dimension of the array to be created, that is a vector of length one or more giving the maximal indices in each dimension.
dimnames
either NULL or the names for the dimensions. This is a list with one component for each dimension, either NULL or a character vector of the length given by dim for that dimension.
rnd.mode
a 1-letter string specifying how rounding should happen at C-level conversion to MPFR, see details of mpfr.

Value

  • an object of class "mpfrArray", specifically "mpfrMatrix" when length(dim) == 2.

See Also

mpfr, array; asNumeric() as inverse of mpfrArray(), to get back a numeric array.

mpfr2array(x) is for "mpfr" classed x, only, whereas mpfrArray(x) is for numeric (non-mpfr) x.

Examples

Run this code
## preallocating is possible here too
ma <- mpfrArray(NA, prec = 80, dim = 2:4)
validObject(A2 <- mpfrArray(1:24, prec = 64, dim = 2:4))

## recycles, gives an "mpfrMatrix" and dimnames :
mat <- mpfrArray(1:5, 64, dim = c(5,3), dimnames=list(NULL, letters[1:3]))
mat
asNumeric(mat)
stopifnot(identical(asNumeric(mat),
                    matrix(1:5 +0, 5,3, dimnames=dimnames(mat))))

## Testing the apply() method :
apply(mat, 2, range)
apply(A2, 1:2, range)
apply(A2, 2:3, max)
apply(A2, 2, fivenum)stopifnot(as(apply(A2, 2, range), "matrix") ==
          apply(as(A2,"array"), 2, range))
a2 <- as(A2, "array")
 for(nf in c("colSums", "colMeans", "rowSums", "rowMeans")) {
   FUN <- getFunction(nf)
   for(di in c(1,2)) {
     r <- FUN(a2, dims = di)
     R <- FUN(A2, dims = di)
     stopifnot(dim(r) == dim(R),
               all.equal(as(R, if(is.array(r)) "array" else "numeric"),
                         unname(r), tol = 1e-15))
   }
 }

Run the code above in your browser using DataLab