Open
Description
Motivation
Functions for low-order polynomial fitting using (weighted) least squares regression are common in many scientific libraries.
Here's an interface for the non-weighted case, inspired by the numpy version cited below:
function polyfit(x, y, deg, rcond, rank, singular_values) result(p)
real, intent(in) :: x(:), y(:)
integer, intent(in) :: deg
real, intent(in), optional :: rcond
integer, intent(out), optional :: rank
real, intent(out), allocatable, optional :: singular_values(:)
Prior Art
Prior art in popular scripting languages:
numpy.polyfit
polyfit
(MATLAB)Polynomials.polyfit
(Julia)lm
(R) (in R one would use a combination of lm for linear regression and poly as demonstrated here)
Prior art in Fortran (in no particular order):
POLFIT
- A 704 program for polynomial least squares fitting (Fortran II)LSQFT
- A nonlinear least squares data fitting subroutine suitable for minicomputers- SLATEC:
dpolft
- Fit discrete data in a least squares sense by polynomials (related routines includedpcoef
anddp1vlu
) - IMSL Fortran Library:
RCURV
(also seepoly_regression
in the C interface) - NAG E02 Curve and Surface Fitting (see routines
e02adf
ande02aef
) - polyfit - Toolbox for performing and manipulating polynomial fits to data with statistical uncertainties.
- Least-Squares Polynomial Curve-Fitting Utilizing Orthogonal Polynomials (thesis by Robert E. Knight, contains Fortran code)
- FITLOS: A FORTRAN program for fitting low-order polynomial splices by the method of least squares
- An Evaluation of Linear Least Squares Computer Programs (article by Roy H. Wampler from National Bureau of Standard, lists over 20 codes, many in Fortran)
spfit
(part of MATH77/mathc90 library, available on Netlib)- Polyfit program users' guide : a statistical polynomial curvefitting program
- curvefit
- E201 Least Squares Polynomial Fit (part of CERNLIB)
- Numerical Algorithms in Fortran by Gisela Engeln-Müllges & Frank Uhlig (see Chapter 8 on linear and nonlinear approximation)
LS_POLY
routine by Jean-Pierre Moreau (part of theLSQPLY.F90
file)- PROGRESS: a program for robust regression
- IBM Scientific Subroutine Package (some Fortran source files are available here)
Prior art in other programming languages
- GSL: Linear Least-Squares Fitting (can be used via the Fortran interface fgsl, the relevant routine is called
multifit
which may be used for any linear model) - alglib (C++)
Additional Information
This will probably require LAPACK for the factorization of the Vandermonde matrix either using a rank-revealing QR or SVD factorization.
A function to evaluate the polynomial (and it's derivatives) using Horner's scheme (or alternatively, in some orthogonal form), should be added in parallel.
An advanced interface could consider using derived types for polynomials, similar to the numpy Polynomial sub-package.