R: Rmpfr - Utilities for Precision Setting, Printing, etc
mpfr-utils
R Documentation
Rmpfr – Utilities for Precision Setting, Printing, etc
Description
This page documents utilities from package Rmpfr which are
typically not called by the user, but may come handy in some
situations.
Usage
getPrec(x, base = 10, doNumeric = TRUE, is.mpfr = NA, bigq. = 128L)
getD(x)
mpfr_default_prec(prec)
## S3 method for class 'mpfrArray'
print(x, digits = NULL, drop0trailing = FALSE,
right = TRUE, ...)
## S3 method for class 'mpfr'
print(x, digits = NULL, drop0trailing = TRUE,
right = TRUE, ...)
toNum(from, rnd.mode = c('N','D','U','Z','A'))
mpfr2array(x, dim, dimnames = NULL, check = FALSE)
mpfrXport(x)
mpfrImport(mxp)
Arguments
x, from
typically, an R object of class "mpfr", or
"mpfrArray", respectively. For
getPrec(), any number-like R object, or NULL.
base
(only when x is character) the base
with respect to which x[i] represent numbers; baseb must fulfill 2 <= b <= 36.
doNumeric
logical indicating integer or
double typed x should be accepted and a default
precision be returned. Should typically be kept at default TRUE.
is.mpfr
logical indicating if class(x) is already
known to be "mpfr"; typically should be kept at default, NA.
bigq.
for getPrec(), the precision to use for a big
rational (class "bigq"); if not specified gives warning when used.
prec
a positive integer, or missing.
drop0trailing
logical indicating if trailing "0"s should
be omitted.
right
logical indicating print()ing should right
justify the strings; see print.default() to which it
is passed.
digits, ...
further arguments to print methods.
rnd.mode
a 1-letter string specifying how rounding
should happen at C-level conversion to MPFR, see details of
mpfr.
dim, dimnames
for "mpfrArray"
construction.
check
logical indicating if the mpfrArray construction should
happen with internal safety check. Previously, the implicit default
used to be true.
mxp
an "mpfrXport" object, as resulting from
mpfrXport().
Details
The print method is currently built on the format
method for class mpfr. This, currently does
not format columns jointly which leads to suboptimally looking
output. There are plans to change this.
Value
getPrec(x) returns a integer vector of the same
length as x when that is positive, whereas getPrec(NULL)
returns mpfr_default_prec(), see below.
If you need to change the precision of x, i.e., need
something like “setPrec”, use roundMpfr().
getD(x) is intended to be a fast version of x@.Data,
and should not be used outside of lower level functions.
mpfr_default_prec() returns the current MPFR default precision,
an integer. This is currently
not made use of, in all of package Rmpfr, where functions have
their own default precision where needed.
mpfr_default_prec(prec)sets the current MPFR default
precision and returns the previous one; see above.
toNum(m) returns a numeric array or
matrix, when m is of class
"mpfrArray" or "mpfrMatrix",
respectively. It should be equivalent to as(m, "array") or
... "matrix". Note that the slightly more general
asNumeric() is preferred now.
mpfr2array() a slightly more flexible alternative to
dim(.) <- dd.
Note
mpfrXport() and mpfrImport() are experimental and
used to explore reported platform incompatibilities of
save()d and load()ed "mpfr"
objects between Windows and non-Windows platforms.
In other words, the format of the result of mpfrXport() and
hence the mxp argument to mpfrImport() are considered
internal, not part of the API and subject to change.
See Also
Start using mpfr(..), and compute with these numbers.
mpfrArray(x) is for numeric (“non-mpfr”)
x, whereas mpfr2array(x) is for "mpfr" classed
x, only.
Examples
getPrec(as(c(1,pi), "mpfr")) # 128 for both
(opr <- mpfr_default_prec()) ## typically 53, the MPFR system default
stopifnot(opr == (oprec <- mpfr_default_prec(70)),
70 == mpfr_default_prec())
## and reset it:
mpfr_default_prec(opr)
## Explore behavior of rounding modes 'rnd.mode':
x <- mpfr(10,99)^512 # too large for regular (double prec. / numeric):
sapply(c("N", "D", "U", "Z", "A"), function(RM)
sapply(list(-x,x), function(.) toNum(., RM)))
## N D U Z A
## -Inf -Inf -1.797693e+308 -1.797693e+308 -Inf
## Inf 1.797693e+308 Inf 1.797693e+308 Inf
## Printing of "MPFR" matrices is less nice than R's usual matrix printing:
m <- outer(c(1, 3.14, -1024.5678), c(1, 1e-3, 10,100))
m[3,3] <- round(m[3,3])
m
mpfr(m, 50)
B6 <- mpfr2array(Bernoulli(1:6, 60), c(2,3),
dimnames = list(LETTERS[1:2], letters[1:3]))
B6
## Looking at internal representation [for power users only!]:
i8 <- mpfr(-2:5, 32)
x4 <- mpfr(c(NA, NaN, -Inf, Inf), 32)
## The output of the following depends on the GMP "numb" size
## (32 bit vs. 64 bit), and may be even more platform specifics:
str( .mpfr2list(i8) )
str( .mpfr2list(x4) )
str(xp4 <- mpfrXport(x4))
stopifnot(identical(x4, mpfrImport(mpfrXport(x4))),
identical(i8, mpfrImport(mpfrXport(i8))))
if(FALSE) ## FIXME: not yet working:
stopifnot(identical(B6, mpfrImport(mpfrXport(B6))))
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/mpfr-utils.Rd_%03d_medium.png", width=480, height=480)
> ### Name: mpfr-utils
> ### Title: Rmpfr - Utilities for Precision Setting, Printing, etc
> ### Aliases: getPrec getD mpfr_default_prec mpfr2array mpfrImport mpfrXport
> ### .mpfr2list print.mpfr print.mpfrArray toNum
> ### Keywords: utilities
>
> ### ** Examples
>
> getPrec(as(c(1,pi), "mpfr")) # 128 for both
[1] 128 128
>
> (opr <- mpfr_default_prec()) ## typically 53, the MPFR system default
[1] 53
> stopifnot(opr == (oprec <- mpfr_default_prec(70)),
+ 70 == mpfr_default_prec())
> ## and reset it:
> mpfr_default_prec(opr)
[1] 70
>
> ## Explore behavior of rounding modes 'rnd.mode':
> x <- mpfr(10,99)^512 # too large for regular (double prec. / numeric):
> sapply(c("N", "D", "U", "Z", "A"), function(RM)
+ sapply(list(-x,x), function(.) toNum(., RM)))
N D U Z A
[1,] -Inf -Inf -1.797693e+308 -1.797693e+308 -Inf
[2,] Inf 1.797693e+308 Inf 1.797693e+308 Inf
> ## N D U Z A
> ## -Inf -Inf -1.797693e+308 -1.797693e+308 -Inf
> ## Inf 1.797693e+308 Inf 1.797693e+308 Inf
>
> ## Printing of "MPFR" matrices is less nice than R's usual matrix printing:
> m <- outer(c(1, 3.14, -1024.5678), c(1, 1e-3, 10,100))
> m[3,3] <- round(m[3,3])
> m
[,1] [,2] [,3] [,4]
[1,] 1.000 0.001000 10.0 100.0
[2,] 3.140 0.003140 31.4 314.0
[3,] -1024.568 -1.024568 -10246.0 -102456.8
> mpfr(m, 50)
'mpfrMatrix' of dim(.) = (3, 4) of precision 50 bits
[,1] [,2] [,3]
[1,] 1.0000000000000000 0.0010000000000000009 10.000000000000000
[2,] 3.1400000000000006 0.0031400000000000004 31.400000000000006
[3,] -1024.5678000000007 -1.0245677999999998 -10246.000000000000
[,4]
[1,] 100.00000000000000
[2,] 314.00000000000000
[3,] -102456.78000000003
>
> B6 <- mpfr2array(Bernoulli(1:6, 60), c(2,3),
+ dimnames = list(LETTERS[1:2], letters[1:3]))
> B6
'mpfrMatrix' of dim(.) = (2, 3) of precision 60 bits
a b c
A 0.50000000000000000000 -0.0000000000000000000 -0.0000000000000000000
B 0.16666666666666666674 -0.033333333333333333359 0.023809523809523809557
>
> ## Looking at internal representation [for power users only!]:
>
> i8 <- mpfr(-2:5, 32)
> x4 <- mpfr(c(NA, NaN, -Inf, Inf), 32)
> ## The output of the following depends on the GMP "numb" size
> ## (32 bit vs. 64 bit), and may be even more platform specifics:
> str( .mpfr2list(i8) )
List of 8
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 2 0
..$ sign: int -1
..$ d : int [1:2] 0 NA
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 1 0
..$ sign: int -1
..$ d : int [1:2] 0 NA
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 1 NA
..$ sign: int 1
..$ d : int [1:2] 0 NA
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 1 0
..$ sign: int 1
..$ d : int [1:2] 0 NA
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 2 0
..$ sign: int 1
..$ d : int [1:2] 0 NA
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 2 0
..$ sign: int 1
..$ d : int [1:2] 0 -1073741824
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 3 0
..$ sign: int 1
..$ d : int [1:2] 0 NA
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 3 0
..$ sign: int 1
..$ d : int [1:2] 0 -1610612736
> str( .mpfr2list(x4) )
List of 4
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 2 NA
..$ sign: int 1
..$ d : int [1:2] 0 0
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 2 NA
..$ sign: int 1
..$ d : int [1:2] 0 0
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 3 NA
..$ sign: int -1
..$ d : int [1:2] 0 0
$ :List of 4
..$ prec: int 32
..$ exp : int [1:2] 3 NA
..$ sign: int 1
..$ d : int [1:2] 0 0
>
> str(xp4 <- mpfrXport(x4))
List of 5
$ gmp.numb.bits: int 64
$ mpfr.version : chr "3.1.4"
$ Machine :List of 4
..$ sizeof.long : int 8
..$ sizeof.longlong : int 8
..$ sizeof.longdouble: int 16
..$ sizeof.pointer : int 8
$ Sys.info : Named chr [1:2] "Linux" "x86_64"
..- attr(*, "names")= chr [1:2] "sysname" "machine"
$ mpfr :List of 4
..$ :List of 4
.. ..$ prec: int 32
.. ..$ exp : int [1:2] 2 NA
.. ..$ sign: int 1
.. ..$ d : int [1:2] 0 0
..$ :List of 4
.. ..$ prec: int 32
.. ..$ exp : int [1:2] 2 NA
.. ..$ sign: int 1
.. ..$ d : int [1:2] 0 0
..$ :List of 4
.. ..$ prec: int 32
.. ..$ exp : int [1:2] 3 NA
.. ..$ sign: int -1
.. ..$ d : int [1:2] 0 0
..$ :List of 4
.. ..$ prec: int 32
.. ..$ exp : int [1:2] 3 NA
.. ..$ sign: int 1
.. ..$ d : int [1:2] 0 0
- attr(*, "class")= chr "mpfrXport"
> stopifnot(identical(x4, mpfrImport(mpfrXport(x4))),
+ identical(i8, mpfrImport(mpfrXport(i8))))
> if(FALSE) ## FIXME: not yet working:
+ stopifnot(identical(B6, mpfrImport(mpfrXport(B6))))
>
>
>
>
>
> dev.off()
null device
1
>