Last data update: 2014.03.03

R: Construct "mpfrArray" almost as by 'array()'
mpfrArrayR Documentation

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

Description

Utility to construct an R object 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","A"))

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

## 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))

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(Rmpfr)
Loading required package: gmp

Attaching package: 'gmp'

The following objects are masked from 'package:base':

    %*%, apply, crossprod, matrix, tcrossprod

C code of R package 'Rmpfr': GMP using 64 bits per limb


Attaching package: 'Rmpfr'

The following objects are masked from 'package:stats':

    dbinom, dnorm, dpois, pnorm

The following objects are masked from 'package:base':

    cbind, pmax, pmin, rbind

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Rmpfr/mpfrArray.Rd_%03d_medium.png", width=480, height=480)
> ### Name: mpfrArray
> ### Title: Construct "mpfrArray" almost as by 'array()'
> ### Aliases: mpfrArray
> ### Keywords: array
> 
> ### ** Examples
> 
> ## preallocating is possible here too
> ma <- mpfrArray(NA, prec = 80, dim = 2:4)
> validObject(A2 <- mpfrArray(1:24, prec = 64, dim = 2:4))
[1] TRUE
> 
> ## recycles, gives an "mpfrMatrix" and dimnames :
> mat <- mpfrArray(1:5, 64, dim = c(5,3), dimnames=list(NULL, letters[1:3]))
> mat
'mpfrMatrix' of dim(.) =  (5, 3) of precision  64   bits 
                          a                      b                      c
[1,] 1.00000000000000000000 1.00000000000000000000 1.00000000000000000000
[2,] 2.00000000000000000000 2.00000000000000000000 2.00000000000000000000
[3,] 3.00000000000000000000 3.00000000000000000000 3.00000000000000000000
[4,] 4.00000000000000000000 4.00000000000000000000 4.00000000000000000000
[5,] 5.00000000000000000000 5.00000000000000000000 5.00000000000000000000
> asNumeric(mat)
     a b c
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 3 3
[4,] 4 4 4
[5,] 5 5 5
> stopifnot(identical(asNumeric(mat),
+                     matrix(1:5 +0, 5,3, dimnames=dimnames(mat))))
> 
> ## Testing the apply() method :
> apply(mat, 2, range)
'mpfrMatrix' of dim(.) =  (2, 3) of precision  64   bits 
                          a                      b                      c
[1,] 1.00000000000000000000 1.00000000000000000000 1.00000000000000000000
[2,] 5.00000000000000000000 5.00000000000000000000 5.00000000000000000000
> apply(A2, 1:2, range)
'mpfrArray' of dim(.) =  (2, 2, 3) of precision  64   bits 
, , 1

     [,1]                   [,2]                  
[1,] 1.00000000000000000000 2.00000000000000000000
[2,] 19.0000000000000000000 20.0000000000000000000

, , 2

     [,1]                   [,2]                  
[1,] 3.00000000000000000000 4.00000000000000000000
[2,] 21.0000000000000000000 22.0000000000000000000

, , 3

     [,1]                   [,2]                  
[1,] 5.00000000000000000000 6.00000000000000000000
[2,] 23.0000000000000000000 24.0000000000000000000

> apply(A2, 2:3, max)
'mpfrMatrix' of dim(.) =  (3, 4) of precision  64   bits 
     [,1]                   [,2]                   [,3]                  
[1,] 2.00000000000000000000 8.00000000000000000000 14.0000000000000000000
[2,] 4.00000000000000000000 10.0000000000000000000 16.0000000000000000000
[3,] 6.00000000000000000000 12.0000000000000000000 18.0000000000000000000
     [,4]                  
[1,] 20.0000000000000000000
[2,] 22.0000000000000000000
[3,] 24.0000000000000000000
> apply(A2, 2, fivenum)
'mpfrMatrix' of dim(.) =  (5, 3) of precision  64   bits 
     [,1]                   [,2]                   [,3]                  
[1,] 1.00000000000000000000 3.00000000000000000000 5.00000000000000000000
[2,] 4.50000000000000000000 6.50000000000000000000 8.50000000000000000000
[3,] 10.5000000000000000000 12.5000000000000000000 14.5000000000000000000
[4,] 16.5000000000000000000 18.5000000000000000000 20.5000000000000000000
[5,] 20.0000000000000000000 22.0000000000000000000 24.0000000000000000000
> stopifnot(as(apply(A2, 2, range), "matrix") ==
+           apply(as(A2,"array"), 2, range))
> ## Don't show: 
>  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))
+    }
+  }
> 
> ## End(Don't show)
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>