From ae37cc25c3bc517e52705135278a8955ed467e53 Mon Sep 17 00:00:00 2001 From: Avraham Adler Date: Wed, 30 Aug 2023 10:34:26 -0400 Subject: [PATCH] Write documentation and unit tests, and update other documentation to reference minimaxErr. Also remove Pade mention. --- inst/tinytest/test_MiniMaxApprox.R | 28 ++++++++++++++-------- man/MiniMaxApprox.Rd | 6 +---- man/minimaxErr.Rd | 38 ++++++++++++++++++++++++++++++ man/minimaxEval.Rd | 4 ++-- 4 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 man/minimaxErr.Rd diff --git a/inst/tinytest/test_MiniMaxApprox.R b/inst/tinytest/test_MiniMaxApprox.R index 532605d..45dbc83 100644 --- a/inst/tinytest/test_MiniMaxApprox.R +++ b/inst/tinytest/test_MiniMaxApprox.R @@ -144,16 +144,6 @@ expect_error(minimaxApprox(fn, -1, 1, c(3L, 3L), xi = xi), errMess) expect_error(minimaxApprox(sin, 0.75 * pi, 1.25 * pi, c(2L, 3L)), "The 3 degree polynomial in the denominator has a zero at 2.80961") -# Test evaluation function -x <- seq(0.1, 0.4, 0.025) -mmA <- minimaxApprox(exp, 0, 0.5, 5L) -expect_true(all(exp(x) - minimaxEval(x, mmA) <= mmA$EE)) -mmA <- minimaxApprox(exp, 0, 0.5, c(2L, 3L)) -expect_true(all(exp(x) - minimaxEval(x, mmA) <= mmA$EE)) -## Check error trap -errMess <- "This function only works with 'minimaxApprox' objects." -expect_error(minimaxEval(x, sin), errMess) - # Test HW Borchers request of returning n degree if n fails but n + 1 works with # uppermost effectively 0 with Runge function between -1 and 1 and degree 10. ## Test successful restart @@ -213,3 +203,21 @@ if (Sys.info()["nodename"] == "HOME") { # This should test RATIONAL failover to QR expect_error(minimaxApprox(sin, 0, pi / 2, c(13L, 0L))) + +# Test evaluation function +x <- seq(0.1, 0.4, 0.025) +mmA <- minimaxApprox(exp, 0, 0.5, 5L) +expect_true(all(exp(x) - minimaxEval(x, mmA) <= mmA$EE)) +mmA <- minimaxApprox(exp, 0, 0.5, c(2L, 3L)) +expect_true(all(exp(x) - minimaxEval(x, mmA) <= mmA$EE)) +## Check error trap +errMess <- "This function only works with 'minimaxApprox' objects." +expect_error(minimaxEval(x, sin), errMess) + +# Test error function +x <- seq(0.1, 0.4, 0.025) +mmA <- minimaxApprox(exp, 0, 0.5, 5L) +expect_identical(minimaxEval(x, mmA) - exp(x), minimaxErr(x, mmA)) +## Check error trap +errMess <- "This function only works with 'minimaxApprox' objects." +expect_error(minimaxErr(x, sin), errMess) diff --git a/man/MiniMaxApprox.Rd b/man/MiniMaxApprox.Rd index 42b9ae6..6246ce0 100644 --- a/man/MiniMaxApprox.Rd +++ b/man/MiniMaxApprox.Rd @@ -186,11 +186,7 @@ using barycentric representations instead of monomials. } } -\seealso{ -\code{\link{minimaxEval}} for a convenience function to calculate approximation -values and \code{\link[Pade:Pade]{Pade}} for a function to calculate -\enc{Padé}{Pade} coefficients given suitable Taylor series coefficients. -} +\seealso{\code{\link{minimaxEval}}, \code{\link{minimaxErr}}} \examples{ minimaxApprox(exp, 0, 1, 5) # Built-in & polynomial diff --git a/man/minimaxErr.Rd b/man/minimaxErr.Rd new file mode 100644 index 0000000..06deb22 --- /dev/null +++ b/man/minimaxErr.Rd @@ -0,0 +1,38 @@ +\name{minimaxErr} +\alias{minimaxErr} +\title{ +Evaluate the Minimax Approximation Error +} +\description{ +Evaluates the difference between the function and the minimax approximation at +\code{x}. +} +\usage{ +minimaxErr(x, mmA) +} +\arguments{ + \item{x}{a numeric vector} + \item{mmA}{a \code{"minimaxApprox"} return object} +} +\details{ +This is a convenience function to evaluate the approximation error at \code{x}. +} +\value{ +A vector of the same length as \code{x} containing the approximation error +values. +} +\author{Avraham Adler \email{Avraham.Adler@gmail.com}} +\seealso{\code{\link{minimaxApprox}}, \code{\link{minimaxEval}}} +\examples{ +# Show results +x <- seq(0, 0.5, length.out = 11L) +mmA <- minimaxApprox(exp, 0, 0.5, 5L) +err <- minimaxEval(x, mmA) - exp(x) +all.equal(err, minimaxErr(x, mmA)) + +# Plot results +x <- seq(0, 0.5, length.out = 1001L) +plot(x, minimaxErr(x, mmA), type = "l") +} + +\keyword{NumericalMathematics} diff --git a/man/minimaxEval.Rd b/man/minimaxEval.Rd index c1e658b..da5c7e4 100644 --- a/man/minimaxEval.Rd +++ b/man/minimaxEval.Rd @@ -24,13 +24,13 @@ This is a convenience function to evaluate the approximation at \code{x}. A vector of the same length as \code{x} containing the approximated values. } \author{Avraham Adler \email{Avraham.Adler@gmail.com}} -\seealso{\code{\link{minimaxApprox}}} +\seealso{\code{\link{minimaxApprox}}, \code{\link{minimaxErr}}} \examples{ # Show results x <- seq(0, 0.5, length.out = 11L) mmA <- minimaxApprox(exp, 0, 0.5, 5L) apErr <- abs(exp(x) - minimaxEval(x, mmA)) -all.equal(max(apErr), mmA$EE) +all.equal(max(apErr), mmA$EE) # Plot results curve(exp, 0.0, 0.5)