Skip to content

Commit

Permalink
Merge branch 'release/v0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjp committed Sep 17, 2016
2 parents 8cbb911 + 6927625 commit 4665c00
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 73 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ sudo: required
# System dependencies for building
r_binary_packages:
- testthat

r_github_packages:
- jimhester/covr

after_success:
- Rscript -e 'covr::coveralls()'

11 changes: 7 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
Package: cmna
Type: Package
Title: Computational Methods for Numerical Analysis
Version: 0.1.2
Date: 2016-02-04
Author: "James P. Howard, II" <jh@jameshoward.us>
Maintainer: "James P. Howard, II" <jh@jameshoward.us>
Version: 0.2.0
Date: 2016-09-17
Authors@R: person(given = "James P.", family = "Howard, II",
email = "jh@jameshoward.us", role = c("aut", "cre"))
URL: https://jameshoward.us/cmna/,
https://github.com/howardjp/cmna
BugReports: https://github.com/howardjp/cmna/issues
Description: Provides the source and examples for James P. Howard, II,
"Computational Methods for Numerical Analysis with R,"
<https://jameshoward.us/cmna>, a forthcoming book on
Expand Down
5 changes: 3 additions & 2 deletions R/bisection.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ bisection <- function(f, a, b, tol = 1e-3, m = 100) {

while (abs(b - a) > tol) {
iter <- iter + 1
if (iter > m)
if (iter > m) {
warning("maximum number of iterations exceeded")
break

}
xmid <- (a + b) / 2
ymid <- f(xmid)
if (f.a * ymid > 0) {
Expand Down
1 change: 0 additions & 1 deletion R/cholesky.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ choleskymatrix <- function(m) {
count.rows <- nrow(m)
count.cols <- ncol(m)


L = diag(0, count.rows)
for(i in 1:count.rows) {
for(k in 1:i) {
Expand Down
4 changes: 3 additions & 1 deletion R/gdls.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ gdls <- function(A, b, alpha = 0.05, tol = 1e-6, m = 1e5) {
oldtheta = theta + 10 * tol

while(vecnorm(oldtheta - theta) > tol) {
if((iter <- iter + 1) > m)
if((iter <- iter + 1) > m) {
warning("maximum number of iterations exceeded")
return(theta)
}
e <- (A %*% theta - b)
d <- (t(A) %*% e) / length(b)
oldtheta <- theta
Expand Down
8 changes: 6 additions & 2 deletions R/goldsect.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ goldsectmin <- function(f, a, b, tol = 1e-3, m = 100) {

while (abs(b - a) > tol) {
iter <- iter + 1
if (iter > m)
if (iter > m) {
warning("maximum number of iterations exceeded")
break
}

if(f(a.star) < f(b.star)) {
b <- b.star
Expand All @@ -92,8 +94,10 @@ goldsectmax <- function(f, a, b, tol = 1e-3, m = 100) {

while (abs(b - a) > tol) {
iter <- iter + 1
if (iter > m)
if (iter > m) {
warning("maximum number of iterations exceeded")
break
}

if(f(a.star) > f(b.star)) {
b <- b.star
Expand Down
30 changes: 15 additions & 15 deletions R/horner.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
#' Use Horner's rule to evaluate a polynomial
#'
#' @param x a vector of x values to evaluate the polynomial
#' @param betas vector of coefficients of x
#' @param coefs vector of coefficients of x
#'
#' @details
#' This function implements Horner's rule for fast polynomial
#' evaluation. The implementation expects \code{x} to be a vector of x
#' values at which to evaluate the polynomial. The parameter \code{betas}
#' values at which to evaluate the polynomial. The parameter \code{coefs}
#' is a vector of coefficients of \emph{x}. The vector order is such
#' that the first element is the constant term, the second element is
#' the coefficient of \emph{x}, the so forth to the highest degreed
Expand Down Expand Up @@ -68,45 +68,45 @@
#' rhorner(x, b)

#' @export
horner <- function(x, betas) {
horner <- function(x, coefs) {
y <- rep(0, length(x))

for(i in length(betas):1)
y <- betas[i] + x * y
for(i in length(coefs):1)
y <- coefs[i] + x * y

return(y)
}

#' @rdname horner
#' @export
rhorner <- function(x, betas) {
n <- length(betas)
rhorner <- function(x, coefs) {
n <- length(coefs)

if(n == 1)
return(betas)
return(coefs)

return(betas[1] + x * rhorner(x, betas[2:n]))
return(coefs[1] + x * rhorner(x, coefs[2:n]))
}

#' @rdname horner
#' @export
naivepoly <- function(x, betas) {
naivepoly <- function(x, coefs) {
y <- rep(0, length(x))

for(i in 1:length(betas))
y <- y + betas[i] * (x ^ (i - 1))
for(i in 1:length(coefs))
y <- y + coefs[i] * (x ^ (i - 1))

return(y)
}

#' @rdname horner
#' @export
betterpoly <- function(x, betas) {
betterpoly <- function(x, coefs) {
y <- rep(0, length(x))
cached.x <- 1

for(i in 1:length(betas)) {
y <- y + betas[i] * cached.x
for(i in 1:length(coefs)) {
y <- y + coefs[i] * cached.x
cached.x <- cached.x * x
}

Expand Down
24 changes: 18 additions & 6 deletions R/iterativematrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ jacobi <- function(A, b, tol = 10e-7, maxiter = 100) {
x <- rep(0, n)
newx <- rep(tol, n)

while(vecnorm(newx - x) > tol & iter < maxiter) {
while(vecnorm(newx - x) > tol) {
if(maxiter > iter) {
warning("maximum number of iterations exceeded")
break
}
x <- newx
newx <- Dinv %*% (b - R %*% x)
iter <- iter + 1
}

return(as.vector(x))
return(as.vector(newx))
}

#' @rdname iterativematrix
Expand All @@ -94,13 +98,17 @@ gaussseidel <- function(A, b, tol = 10e-7, maxiter = 100) {
x <- rep(0, n)
newx <- rep(tol * 10, n)

while(vecnorm(newx - x) > tol & iter < maxiter) {
while(vecnorm(newx - x) > tol) {
if(maxiter > iter) {
warning("maximum number of iterations exceeded")
break
}
x <- newx
newx <- Linv %*% (b - U %*% x)
iter <- iter + 1
}

return(as.vector(x))
return(as.vector(newx))
}

#' @rdname iterativematrix
Expand All @@ -113,8 +121,12 @@ cgmmatrix <- function(A, b, tol = 10e-7, maxiter = 100) {
newx <- rep(tol * 10, n)

p <- r <- b - A %*% x
while(vecnorm(r) > tol & iter < maxiter) {
a <- as.numeric((t(r) %*% r) / t(p) %*% A %*% p)
while(vecnorm(r) > tol) {
if(maxiter > iter) {
warning("maximum number of iterations exceeded")
break
}
a <- as.numeric((t(r) %*% r) / t(p) %*% A %*% p)
newx <- x + a * p
newr <- r - a * A %*% p
beta <- as.numeric(t(newr) %*% newr / (t(r) %*% r))
Expand Down
4 changes: 2 additions & 2 deletions R/naivediv.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ naivediv <- function(m, n) {
r <- r - n
}

return(list(quot = quot, r = r))
return(list(quotient = quot, remainder = r))
}

#' @rdname division
Expand All @@ -84,5 +84,5 @@ longdiv <- function(m, n) {
}
}

return(list(quot = quot, r = r))
return(list(quotient = quot, remainder = r))
}
12 changes: 6 additions & 6 deletions R/naivesum.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@
#' @family intro
#'
#' @examples
#' l <- 1:10^6
#' n <- sample(l, 1)
#' bound <- sample(l, 2)
#' bound.u <- max(bound) - 10^6 / 2
#' bound.l <- min(bound) - 10^6 / 2
#' x <- runif(n, bound.l, bound.u)
#' k <- 1:10^6
#' n <- sample(k, 1)
#' bound <- sample(k, 2)
#' bound.upper <- max(bound) - 10^6 / 2
#' bound.lower <- min(bound) - 10^6 / 2
#' x <- runif(n, bound.lower, bound.upper)
#' naivesum(x)
#' kahansum(x)
#' pwisesum(x)
Expand Down
4 changes: 2 additions & 2 deletions R/polyinterp.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ polyinterp <- function(x, y) {
vandermonde <- rep(1, length(x))
for(i in 1:n) {
xi <- x^i
vandermonde <- cbind(xi, vandermonde)
vandermonde <- cbind(vandermonde, xi)
}
beta <- solve(vandermonde, y)

names(beta) <- NULL
return(rev(beta))
return(beta)
}
28 changes: 14 additions & 14 deletions R/tridiagmatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
#' @description
#' use the tridiagonal matrix algorithm to solve a tridiagonal matrix
#'
#' @param d vector of entries on the main diagonal
#' @param l vector of entries below the main diagonal
#' @param u vector of entries above the main diagonal
#' @param D vector of entries on the main diagonal
#' @param L vector of entries below the main diagonal
#' @param U vector of entries above the main diagonal
#' @param b vector of the right-hand side of the linear system
#'
#' @details
Expand All @@ -43,26 +43,26 @@
#' @family linear
#'
#' @export
tridiagmatrix <- function(l, d, u, b) {
n <- length(d)
l <- c(NA, l)
tridiagmatrix <- function(L, D, U, b) {
n <- length(D)
L <- c(NA, L)

## The forward sweep
u[1] <- u[1] / d[1]
b[1] <- b[1] / d[1]
U[1] <- U[1] / D[1]
b[1] <- b[1] / D[1]
for(i in 2:(n - 1)) {
u[i] <- u[i] / (d[i] - l[i] * u[i - 1])
b[i] <- (b[i] - l[i] * b[i - 1]) /
(d[i] - l[i] * u[i - 1])
U[i] <- U[i] / (D[i] - L[i] * U[i - 1])
b[i] <- (b[i] - L[i] * b[i - 1]) /
(D[i] - L[i] * U[i - 1])
}
b[n] <- (b[n] - l[n] * b[n - 1]) /
(d[n] - l[n] * u[n - 1])
b[n] <- (b[n] - L[n] * b[n - 1]) /
(D[n] - L[n] * U[n - 1])

## The backward sweep
x <- rep.int(0, n)
x[n] <- b[n]
for(i in (n - 1):1)
x[i] <- b[i] - u[i] * x[i + 1]
x[i] <- b[i] - U[i] * x[i + 1]

return(x)
}
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CMNA
# Computational Methods for Numerical Analysis

[![Build Status](https://travis-ci.org/howardjp/cmna.svg?branch=master,osx)](https://travis-ci.org/howardjp/cmna)
[![Coverage Status](https://coveralls.io/repos/howardjp/cmna/badge.svg?branch=master&service=github)](https://coveralls.io/github/howardjp/cmna?branch=master)
[![Downloads from the RStudio CRAN mirror](http://cranlogs.r-pkg.org/badges/cmna)](http://cran.rstudio.com/package=cmna)

This is the R package to support _Computational Methods for Numerical
Analysis with R_ by James P. Howard, II. This book is scheduled for
Expand Down Expand Up @@ -104,7 +106,7 @@ numerical analysis algorithms in pure R.
* Initial Value Problems
* Euler Method (euler)
* Midpoint Method (midptivp)
* Fouth-Order Runge-Kutta (rungekitta4)
* Fouth-Order Runge-Kutta (rungekutta4)
* Adams-Bashforth (adamsbashforth)
* Systems of Differential Equations
* Euler Method (eulersys)
Expand All @@ -124,6 +126,7 @@ numerical analysis algorithms in pure R.
* Use [Git Flow](http://nvie.com/posts/a-successful-git-branching-model/)
* Write unit tests using [testthat](https://github.com/hadley/testthat)
* Document functions using [roxygen2](https://github.com/yihui/roxygen2)
* Use [ZenHub](https://www.zenhub.com/) for project management on GitHub

## For more information

Expand Down
12 changes: 6 additions & 6 deletions man/horner.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/summation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4665c00

Please sign in to comment.