Skip to content

Commit

Permalink
Write documentation and unit tests, and update other documentation to…
Browse files Browse the repository at this point in the history
… reference minimaxErr. Also remove Pade mention.
  • Loading branch information
aadler committed Aug 30, 2023
1 parent 2bb47ff commit ae37cc2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
28 changes: 18 additions & 10 deletions inst/tinytest/test_MiniMaxApprox.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
6 changes: 1 addition & 5 deletions man/MiniMaxApprox.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions man/minimaxErr.Rd
Original file line number Diff line number Diff line change
@@ -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}
4 changes: 2 additions & 2 deletions man/minimaxEval.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ae37cc2

Please sign in to comment.