NuMFor 9f2ab49 (2024-04-08)
Numerical (Modern) Fortran. Library for Simple Numerical computing
polynomial Module Reference

polynomials provides a framework for simple (and quite naive) work with polynomials Further description in Submodule interpolate More...

Data Types

interface  polyval
 Computes the value of the polynomial when applied to a number or list of numbers. More...
 

Functions/Subroutines

real(dp) function, dimension(:), allocatable, public polyder (p, m)
 polyder Computes the derivative of a polynomial. Returns an array with the coefficients
 
real(dp) function, dimension(:), allocatable, public polyint (p, m, k)
 polyint Computes m-esima antiderivative
 
subroutine, public bisect_pol (x0, dx, p, toler, x)
 bisect_pol Classical bisection method for root finding on polynomials
 

Detailed Description

polynomials provides a framework for simple (and quite naive) work with polynomials Further description in Submodule interpolate

It allows to easily evaluate, derivate, and integrate a polynomial

Examples:

program ex_polynomial
USE numfor, only: dp, zero, m_pi, center, arange, str
USE numfor, only: polyval, polyder, polyint
implicit none
real(dp), dimension(5) :: p1
p1 = arange(5, 1, -1)
print "(A)", center(' Polynomials ', 40, '*')
print "(A)", 'Coefficients:'//str(p1)
print "(A)", center('Evaluations', 40, '-')
p1 = arange(5, 1, -1)
print "(A)", "P(-1)= "//str(polyval(p1, -1._dp)) ! P(-1)= 3
print "(A)", "P(0) = "//str(polyval(p1, 0._dp)) ! P(0) = 1
print "(A)", "P(1) = "//str(polyval(p1, 1._dp)) ! P(1) = 15
! Polynomial evaluated in an array:
print "(A)", "P([-1,0,1]) = "//str(polyval(p1, [-1._dp, 0._dp, 1._dp]))
! P([-1,0,1]) = [ 3, 1, 15]
print "(A)", center('Derivatives', 40, '-')
print "(A)", "Coefficients of P^(1)(x) = "//str(polyder(p1, 1))
! Coefficients of P^(1)(x) = [ 20, 12, 6, 2]
print "(A)", "Coefficients of P^(2)(x) = "//str(polyder(p1, 2))
! Coefficients of P^(2)(x) = [ 60, 24, 6]
print "(A)", "Coefficients of P^(3)(x) = "//str(polyder(p1, 3))
! Coefficients of P^(3)(x) = [120, 24]
print "(A)", "P'([-1,0,1]) = "//str(polyval(polyder(p1, 1), [-1._dp, 0._dp, 1._dp]))
! P'([-1,0,1]) = [-12, 2, 40]
print "(A)", center('Integration', 40, '-')
print "(A)", "Coefficients of first antiderivative (cte=0) ="//str(polyint(p1, 1))
! Coefficients of first antiderivative (cte=0) =[ 1, 1, 1, 1, 1, 0]
print "(A)", "Coefficients of first antiderivative (cte=3) ="//str(polyint(p1, 1, 3._dp))
! Coefficients of first antiderivative (cte=3) =[ 1, 1, 1, 1, 1, 3]
print "(A)", "Coefficients of double antiderivative (ctes=0)="//str(polyint(p1, 2))
! Coefficients of double antiderivative (ctes=0)=[ 0.1666666666667, 0.2, 0.25, 0.3333333333333, 0.5, 0, 0]
print "(A)", "(\int P(x) dx)([-1,0,1]) = "//str(polyval(polyint(p1, 1), [-1._dp, 0._dp, 1._dp]))
! (\int P(x) dx)([-1,0,1]) = [ -1, 0, 5]
end program ex_polynomial

Function/Subroutine Documentation

◆ bisect_pol()

subroutine, public bisect_pol ( real(dp), intent(in) x0,
real(dp), intent(inout) dx,
real(dp), dimension(:), intent(in) p,
real(dp), intent(in) toler,
real(dp), intent(out) x )

bisect_pol Classical bisection method for root finding on polynomials

Parameters
[in]x0Initial value
[in,out]dxrange. It will probe in the range (x0-dx, x0+dx). On return it will have an estimation of error
[in]pArray with coefficients of polynomial
[in]tolerTolerance in the root determination
[out]xValue of the root

◆ polyder()

real(dp) function, dimension(:), allocatable, public polyder ( real(dp), dimension(:), intent(in) p,
integer, intent(in), optional m )

polyder Computes the derivative of a polynomial. Returns an array with the coefficients

Parameters
[in]pArray of coefficients, from highest degree to constant term
[in]mOrder of derivation
Returns
Derivative of polynomial

Examples:

print "(A)", "Coefficients of P^(1)(x) = "//str(polyder(p1, 1))
! Coefficients of P^(1)(x) = [ 20, 12, 6, 2]
print "(A)", "Coefficients of P^(2)(x) = "//str(polyder(p1, 2))
! Coefficients of P^(2)(x) = [ 60, 24, 6]
print "(A)", "Coefficients of P^(3)(x) = "//str(polyder(p1, 3))
! Coefficients of P^(3)(x) = [120, 24]
print "(A)", "P'([-1,0,1]) = "//str(polyval(polyder(p1, 1), [-1._dp, 0._dp, 1._dp]))
! P'([-1,0,1]) = [-12, 2, 40]

References basic::print_msg(), and basic::zero.

Referenced by csplevder::cspl_interpdev(), csplevder::cspl_interpdev_tab(), and csplines::csplder().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ polyint()

real(dp) function, dimension(:), allocatable, public polyint ( real(dp), dimension(:), intent(in) p,
integer, intent(in), optional m,
real(dp), intent(in), optional k )

polyint Computes m-esima antiderivative

Parameters
[in]pArray of coefficients, from highest degree to constant term
[in]mNumber of times that p must be integrated
[in]kAdditive Constant
Returns
Antiderivative polynomial

Examples:

print "(A)", "Coefficients of first antiderivative (cte=0) ="//str(polyint(p1, 1))
! Coefficients of first antiderivative (cte=0) =[ 1, 1, 1, 1, 1, 0]
print "(A)", "Coefficients of first antiderivative (cte=3) ="//str(polyint(p1, 1, 3._dp))
! Coefficients of first antiderivative (cte=3) =[ 1, 1, 1, 1, 1, 3]
print "(A)", "Coefficients of double antiderivative (ctes=0)="//str(polyint(p1, 2))
! Coefficients of double antiderivative (ctes=0)=[ 0.1666666666667, 0.2, 0.25, 0.3333333333333, 0.5, 0, 0]
print "(A)", "(\int P(x) dx)([-1,0,1]) = "//str(polyval(polyint(p1, 1), [-1._dp, 0._dp, 1._dp]))
! (\int P(x) dx)([-1,0,1]) = [ -1, 0, 5]

References basic::print_msg(), and basic::zero.

Referenced by csplines::csplantider().

Here is the call graph for this function:
Here is the caller graph for this function: