Learn R Programming

comphy is an R package accompanying the book This package accompanies the book
Computational Physics with R. It collects all the functions described and used throughout the book, providing ready-to-use implementations of numerical algorithms in computational physics.

The package includes routines for:

  • Numerical differentiation and integration

  • Solvers for ordinary differential equations

  • Sturm–Liouville eigenvalue problems

  • Monte Carlo methods

  • Other algorithms relevant to physics and applied mathematics

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("jfoadi/comphy")

Example

This is a simple example where a numerical derivative of sin(x) is computed using comphy:

library(comphy)

# Grid of known values
x <- seq(0,pi,length.out=11)

# Grid of values at which derivative must be calculated
# Same as x
x0 <- x

# Function known at tabulated points
f <- sin(x)+cos(x)

# Obtain a numerical derivative at those points
# using central differences (discard first and last value)
df <- deriv_reg(x0,x,f)

# Print derivative values
print(df)
#>  [1]         NA  0.6315304  0.2176105 -0.2176105 -0.6315304 -0.9836316
#>  [7] -1.2394482 -1.3739389 -1.3739389 -1.2394482         NA

The two NA’s are there because the derivative is calculated using a centred difference.

Documentation

All exported functions include documentation and usage examples. For a comprehensive discussion and context, see the book Computational Physics with R.

License

GPL (>= 2)

Copy Link

Version

Install

install.packages('comphy')

Version

1.0.5

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

James Foadi

Last Published

February 1st, 2026

Functions in comphy (1.0.5)

oddity

Parity of a permutation
polysolveLS

Polynomial Least Squares
polydivdif

Approximating polynomial for divided differences
roots_bisec

Bisection method for roots
linpol

1D linear interpolation
roots_newton

Newton method for roots
decidepoly_n

Degree of best-interpolating polynomial
solve_tridiag

Tridiagonal linear system
deriv_irr

First derivative for an irregular grid
which_poly

Find optimal polynomial model
transform_upper

Transform to upper triangular
solveLS

Multilinear Least Squares
roots_secant

Secant method for roots
PJacobi

The Jacobi method
BVPshoot2

Solves a second-order BVP using the shooting method
GSeidel

The Gauss-Seidel algorithm
HeunODE

Heun method for systems of ODEs
EulerODE

Euler method for systems of ODEs
LUdeco

LU decomposition
Gquad

Numerical integration using \(n\)-point Gaussian quadrature.
EPSturmLiouville2

Sturm–Liouville eigenproblem with homogeneous Dirichlet boundary conditions
RK4ODE

Runge-Kutta 4th order method for systems of ODEs
BVPlinshoot2

Linear shooting method for second-order linear BVPs
forwdif

Forward differences
gauss_elim

Gaussian Elimination
divdif

Divided differences
deriv_reg

First derivative on a regular grid
backdif

Backward differences
illcond_sample

Ill-conditioned sampling
nevaitpol

Neville-Aitken algorithm for polynomial interpolation
numint_reg

Numerical integration using the trapezoid or simpson's rule
condet

Determinant of a square matrix