Title: | Functional Bases |
---|---|
Description: | Easy-to-use, very fast implementation of various functional bases. Easily used together with other packages. A functional basis is a collection of basis functions [\phi_1, ..., \phi_n] that can represent a smooth function, i.e. $f(t) = \sum c_k \phi_k(t)$. First- and second-order derivatives are also included. These are the mathematically correct ones, no approximations applied. As of version 1.1, this package includes B-splines, Fourier bases and polynomials. |
Authors: | Niels Olsen |
Maintainer: | Niels Olsen <[email protected]> |
License: | GPL-3 |
Version: | 1.1.1 |
Built: | 2024-11-21 06:26:49 UTC |
Source: | CRAN |
fctbases is a fast and easy implementation of functional bases in R. Simply initialize the desired basis, which returns function of class fctbasis
.
Internally, functions are stored as C++ objects, which are masked by the package. The package maintains the bookkeeping of fctbasis objects. Parameters are validated at initialization which also reduces some of the overhead. fctbases objects cannot be saved across sessions and must be re-initialised.
Derivatives are provided. These are the mathematically correct ones and are as fast as the non-derivatives.
A fctbases object is a function of class fctbasis
which takes three arguments (t, x, deriv)
t |
time points |
x |
vector or matrix of coefficients (optional) |
deriv |
Should the derivative be used and which order? Defaults to |
If deriv
is zero or FALSE
, the function itself is evaluated.
If deriv
is one or TRUE
, the first derivative is evaluated.
If deriv
is two, the second derivative is evaluated.
The dimension of x
must match the number of basis functions.
Returns a matrix of dimension length(t)
times no. of bases if x
is missing.
If x
is provided and is a vector, it returns a vector of same length as t
.
If x
is provided and is a matrix, it returns a matrix of dimension length(t)
times ncol(x)
## Create basis (here a b spline) bf <- make.bspline.basis(knots = 0:12/12) ## Use a functional basis bf(0.2) tt <- seq(0,1, length = 50) bf(tt) ## evaluates bf in tt bf(tt, deriv = TRUE) ## evaluates derivative of bf in tt ## Apply bf to some coefficients set.seed(661) x <- runif(15) bf(tt, x) ## Evaluate bf in tt with coefficients x. bf(0.2, deriv = 2) ## Second derivative. bf(0.2, x, deriv = 2) ## Second derivative with coefficients x.
## Create basis (here a b spline) bf <- make.bspline.basis(knots = 0:12/12) ## Use a functional basis bf(0.2) tt <- seq(0,1, length = 50) bf(tt) ## evaluates bf in tt bf(tt, deriv = TRUE) ## evaluates derivative of bf in tt ## Apply bf to some coefficients set.seed(661) x <- runif(15) bf(tt, x) ## Evaluate bf in tt with coefficients x. bf(0.2, deriv = 2) ## Second derivative. bf(0.2, x, deriv = 2) ## Second derivative with coefficients x.
Make B-spline basis
make.bspline.basis(knots, order = 4)
make.bspline.basis(knots, order = 4)
knots |
Knots of the basis, including endpoints |
order |
Spline order. Defaults to 4. |
Function of class "fctbasis"
Functional basis function, make.std.bspline.basis
## B-spline with equidistant knots with 13 basis function bf <- make.bspline.basis(knots = 0:10, order = 4) ## B-spline of order 2 (ie. a linear approximation) with some uneven knots bf <- make.bspline.basis(knots = c(-1.3, 0, 0.5, 0.7, 1.1), order = 2)
## B-spline with equidistant knots with 13 basis function bf <- make.bspline.basis(knots = 0:10, order = 4) ## B-spline of order 2 (ie. a linear approximation) with some uneven knots bf <- make.bspline.basis(knots = c(-1.3, 0, 0.5, 0.7, 1.1), order = 2)
Make fourier basis
make.fourier.basis(range, order, use.trig.id = FALSE)
make.fourier.basis(range, order, use.trig.id = FALSE)
range |
Left and right end points. |
order |
Order of harmonics |
use.trig.id |
Use trigonometrical identities with this function? |
The number of basis elements (degrees of freedom) is 2 * order + 1.
The basis functions are ordered [1, sin(t), cos(t), sin(2t), cos(2t), ...]
Using trigonometrical identities is faster, but introduces (negligible) round-off errors.
Function of class "fctbasis"
## A fourier basis with period 1 and 11 basis functions. bf <- make.fourier.basis(c(0,1), order = 5)
## A fourier basis with period 1 and 11 basis functions. bf <- make.fourier.basis(c(0,1), order = 5)
Make polynomial basis
make.pol.basis(order)
make.pol.basis(order)
order |
Order of polynomial (= degree + 1) |
The polynomial basis is ordered [1, t, t^2, t^3, ..., t^n]
Function of class "fctbasis"
## A four-degree polynomial mypol <- make.pol.basis(order = 5)
## A four-degree polynomial mypol <- make.pol.basis(order = 5)
This initializes a bspline of order 4 with uniformly places knots. df = intervals + 3.
make.std.bspline.basis(range = c(0, 1), intervals)
make.std.bspline.basis(range = c(0, 1), intervals)
range |
End points of spline |
intervals |
Number of intervals |
make.std.bspline.basis
uses a different implementation than make.bspline.basis
,
but is not faster in all uses.
function
Functional basis function, make.bspline.basis
## 16 equidistant knots between 0 and 2 (both included) bf <- make.std.bspline.basis(range = c(0,2), intervals = 15)
## 16 equidistant knots between 0 and 2 (both included) bf <- make.std.bspline.basis(range = c(0,2), intervals = 15)
This function returns details about a functional basis.
object.info(fctbasis)
object.info(fctbasis)
fctbasis |
object of class |
A named list including no. of basis, type of basis, and possibly additional information.