Commit 8ccb020c authored by Martin Maechler's avatar Martin Maechler
Browse files

Ver 0.6-1: c_bigz(), c_bigq() e.g. for sapply*(); Doc: as.bigz(<dbl>)

parent 544f1613
2020-06-12 Martin Maechler <maechler@stat.math.ethz.ch>
* R/biginteger.R (c_bigz): export, to be used, e.g.,
* R/bigq.R (c_bigq): on lapply(<big>, Fn) to give a <big[qz]> result
2020-06-09 Martin Maechler <maechler@stat.math.ethz.ch>
* DESCRIPTION (Version): 0.6-1
......
Package: gmp
Version: 0.6-1
Date: 2020-06-10
Date: 2020-07-04
Title: Multiple Precision Arithmetic
Author: Antoine Lucas, Immanuel Scholz, Rainer Boehme <rb-gmp@reflex-studio.de>,
Sylvain Jasson <Sylvain.Jasson@inrae.fr>,
......@@ -9,7 +9,7 @@ Maintainer: Antoine Lucas <antoinelucas@gmail.com>
Description: Multiple Precision Arithmetic (big integers and rationals,
prime number tests, matrix computation), "arithmetic without limitations"
using the C library GMP (GNU Multiple Precision Arithmetic).
Depends: R (>= 3.4.0)
Depends: R (>= 3.5.0)
Imports: methods
Suggests: Rmpfr, MASS
SystemRequirements: gmp (>= 4.2.3)
......
......@@ -18,6 +18,7 @@ export(
## "as.matrix.bigq", "as.matrix.bigz",
## this should be a deprecated use:
"as.vector.bigq", "as.vector.bigz",
c_bigq, c_bigz,
## "c.bigq", "c.bigz", "cbind.bigq", "cbind.bigz", "rbind.bigq", "rbind.bigz",
## "cumsum.bigq", "cumsum.bigz",
"denominator", "denominator<-",
......
......@@ -362,12 +362,16 @@ sum.bigz <- function(..., na.rm = FALSE)
setMethod("which.max", "bigz", function(x) which.max(x == max(x)))
setMethod("which.min", "bigz", function(x) which.max(x == min(x)))
##' to be applied e.g. to the result of lapply(<bigz>, Fn)
c_bigz <- function(L) .Call(biginteger_c, L)
c.bigz <- function(..., recursive = FALSE)
{
argL <- list(...)
if(any(vapply(argL, inherits, NA, what="bigq")))
.Call(bigrational_c, argL)
else .Call(biginteger_c, argL)
c_bigq(argL)
else
c_bigz(argL)
}
## This is practically identical to grid :: rep.unit :
......
......@@ -85,7 +85,7 @@ print.bigq <- function(x, quote = FALSE, initLine = TRUE, ...)
invisible(x)
}
as.bigq <- function(n, d=1)
as.bigq <- function(n, d = 1L)
{
.Call(bigrational_as, n, d)
}
......@@ -227,10 +227,10 @@ cumsum.bigq <- function(x) .Call(bigrational_cumsum, x)
##' to be applied e.g. to the result of lapply(<bigq>, Fn)
c_bigq <- function(L) .Call(bigrational_c, L)
c.bigq <- function(..., recursive = FALSE) {
.Call(bigrational_c, list(...))
}
c.bigq <- function(..., recursive = FALSE) c_bigq(list(...))
## This is practically identical to grid :: rep.unit :
rep.bigq <- function(x, times=1, length.out=NA, each=1, ...) {
......
......@@ -9,6 +9,7 @@
\alias{print.bigz}
\alias{is.na.bigz}
\alias{NA_bigz_}
\alias{c_bigz}
% these two are C symbols, will be removed eventually:
\alias{biginteger_as}
\alias{biginteger_as_character}
......@@ -25,6 +26,7 @@ NA_bigz_
is.bigz(x)
\S3method{is.na}{bigz}(x)
\S3method{print}{bigz}(x, quote=FALSE, initLine = is.null(modulus(x)), \dots)
c_bigz(L)
}
\arguments{
\item{a}{either \code{\link{integer}}, \code{\link{numeric}} (i.e.,
......@@ -44,6 +46,10 @@ is.bigz(x)
\bold{init}ial line (with the class and length or dimension) should
be printed. The default prints it for those cases where the class
is not easily discernable from the print output.}
\item{L}{a \code{\link{list}} where each element contains \code{"bigz"}
numbers, for \code{c_bigz()}, this allows something like an
\code{\link{sapply}()} for \code{"bigz"} vectors, see \code{sapplyZ()}
in the examples.}
}
\value{
An \R object of (S3) class \code{"bigz"}, representing the argument
......@@ -163,6 +169,13 @@ as.character(c, b=16)
# Depict the "S4-class" bigz, i.e., the formal (S4) methods:
if(require("Rmpfr")) # mostly interesting there
showMethods(class="bigz")
}
# an sapply() version that works for big integers "bigz":
sapplyZ <- function(X, FUN, ...) c_bigz(lapply(X, FUN, ...))
# dummy example showing it works (here):
zz <- as.bigz(3)^(1000+ 1:999)
z1 <- sapplyZ(zz, function(z) z^2)
stopifnot( identical(z1, zz^2) )
}
\keyword{arith}
......@@ -10,6 +10,7 @@
\alias{is.na.bigq}
\alias{NA_bigq_}
\alias{as.bigz.bigq}
\alias{c_bigq}
\alias{denominator}
\alias{denominator<-}
\alias{numerator}
......@@ -31,6 +32,7 @@ is.bigq(x)
denominator(x)
numerator(x)
NA_bigq_
c_bigq(L)
}
\arguments{
......@@ -38,7 +40,7 @@ NA_bigq_
(String value: either starting with \code{0x} for hexadecimal, \code{0b} for
binary or without prefix for decimal values.
Any format error results in \code{0}).
\code{n} stands for numerator, \code{d} for denominator}
\code{n} stands for numerator, \code{d} for denominator.}
\item{a}{an element of class \code{"bigq"}}
\item{mod}{optional modulus to convert into biginteger}
\item{x}{a \dQuote{rational number} (vector), of class \code{"bigq"}.}
......@@ -53,12 +55,20 @@ NA_bigq_
% those cases where the class is not easily discernable from the
% print output.
}
\item{L}{a \code{\link{list}} where each element contains \code{"bigq"}
numbers, for \code{c_bigq()}, this allows something like an
\code{\link{sapply}()} for \code{"bigq"} vectors, see \code{sapplyQ()}
in the examples below.}
}
\value{
An \R object of (S3) class \code{"bigq"} representing the parameter value.
}
\details{
\code{as.bigq(x)} when \code{x} is \code{\link{numeric}} (aka
\code{\link{double}} precision) calls the \file{GMP} function
\code{mpq_set_d()} which is documented to be \emph{exact} (every finite double
precision number is a rational number).
\code{as.bigz.bigq()} returns the smallest integers not less than the
corresponding rationals bigq.
......@@ -95,6 +105,13 @@ stopifnot( is.na(NA_bigq_) )
# Depict the "S4-class" bigq, i.e., the formal (S4) methods:
if(require("Rmpfr")) # mostly interesting there
showMethods(class="bigq")
}
# an sapply() version that works for big rationals "bigq":
sapplyQ <- function(X, FUN, ...) c_bigq(lapply(X, FUN, ...))
# dummy example showing it works (here):
qq <- as.bigq(1, 1:999)
q1 <- sapplyQ(qq, function(q) q^2)
stopifnot( identical(q1, qq^2) )
}
\keyword{arith}
......@@ -39,7 +39,7 @@
}
\value{
return an element of class bigz or bigq.
an object of class \code{"\link{bigz}"} or \code{"\link{bigq}"}.
}
\author{Antoine Lucas}% Martin Maechler: which.min & which.max
......@@ -63,7 +63,9 @@
stopifnot(range(x) == c(1,10), 3*range(Q) == c(1,10))
}
\seealso{\code{\link[base]{max}}}
\seealso{
\code{\link{max}} etc in \pkg{base}.
}
\keyword{arith}
......@@ -91,6 +91,17 @@ stopifnot(identical(x, as.bigz(i %% 3)),
identical(unlist(lq),
unlist(lapply(Q, unclass))))
## Check that as.bigq(<double>) is exact *and* asNumeric() is its inverse --------------
set.seed(47)
summary(x1 <- rt(10000, df = 0.5)) # really long tailed
summary(x2 <- rlnorm(10000, 200, 100))
x <- c(x1, x2)
qx <- as.bigq(x)
nx <- asNumeric(qx) ## asNumeric()'s method for "bigq" is internal .bigq2num()
stopifnot(identical(x, nx),
identical(nx, gmp:::.bigq2num(qx))
)
## duplicated(), unique() : ----------------------
q7 <- as.bigq(-5:7, 7)
if(FALSE)# not yet {well, *HARD* / impossible(?) without S4 }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment