MOM6
polynomial_functions.F90
1 !> Polynomial functions
3 
4 ! This file is part of MOM6. See LICENSE.md for the license.
5 
6 implicit none ; private
7 
8 public :: evaluation_polynomial, integration_polynomial, first_derivative_polynomial
9 
10 contains
11 
12 !> Pointwise evaluation of a polynomial at x
13 !!
14 !! The polynomial is defined by the coefficients contained in the
15 !! array of the same name, as follows: C(1) + C(2)x + C(3)x^2 + C(4)x^3 + ...
16 !! where C refers to the array 'coeff'.
17 !! The number of coefficients is given by ncoef and x
18 !! is the coordinate where the polynomial is to be evaluated.
19 real function evaluation_polynomial( coeff, ncoef, x )
20  real, dimension(:), intent(in) :: coeff !< The coefficients of the polynomial
21  integer, intent(in) :: ncoef !< The number of polynomial coefficients
22  real, intent(in) :: x !< The position at which to evaluate the polynomial
23  ! Local variables
24  integer :: k
25  real :: f ! value of polynomial at x
26 
27  f = 0.0
28  do k = 1,ncoef
29  f = f + coeff(k) * ( x**(k-1) )
30  enddo
31 
32  evaluation_polynomial = f
33 
34 end function evaluation_polynomial
35 
36 !> Calculates the first derivative of a polynomial evaluated at a point x
37 !!
38 !! The polynomial is defined by the coefficients contained in the
39 !! array of the same name, as follows: C(1) + C(2)x + C(3)x^2 + C(4)x^3 + ...
40 !! where C refers to the array 'coeff'.
41 !! The number of coefficients is given by ncoef and x
42 !! is the coordinate where the polynomial's derivative is to be evaluated.
43 real function first_derivative_polynomial( coeff, ncoef, x )
44  real, dimension(:), intent(in) :: coeff !< The coefficients of the polynomial
45  integer, intent(in) :: ncoef !< The number of polynomial coefficients
46  real, intent(in) :: x !< The position at which to evaluate the derivative
47  ! Local variables
48  integer :: k
49  real :: f ! value of polynomial at x
50 
51  f = 0.0
52  do k = 2,ncoef
53  f = f + real(k-1)*coeff(k) * ( x**(k-2) )
54  enddo
55 
56  first_derivative_polynomial = f
57 
58 end function first_derivative_polynomial
59 
60 !> Exact integration of polynomial of degree npoly
61 !!
62 !! The array of coefficients (Coeff) must be of size npoly+1.
63 real function integration_polynomial( xi0, xi1, Coeff, npoly )
64  real, intent(in) :: xi0 !< The lower bound of the integral
65  real, intent(in) :: xi1 !< The lower bound of the integral
66  real, dimension(:), intent(in) :: coeff !< The coefficients of the polynomial
67  integer, intent(in) :: npoly !< The degree of the polynomial
68  ! Local variables
69  integer :: k
70  real :: integral
71 
72  integral = 0.0
73 
74  do k = 1,npoly+1
75  integral = integral + coeff(k) * (xi1**k - xi0**k) / real(k)
76  enddo
77 !
78 !One non-answer-changing way of unrolling the above is:
79 ! k=1
80 ! integral = integral + Coeff(k) * (xi1**k - xi0**k) / real(k)
81 ! if (npoly>=1) then
82 ! k=2
83 ! integral = integral + Coeff(k) * (xi1**k - xi0**k) / real(k)
84 ! endif
85 ! if (npoly>=2) then
86 ! k=3
87 ! integral = integral + Coeff(k) * (xi1**k - xi0**k) / real(k)
88 ! endif
89 ! if (npoly>=3) then
90 ! k=4
91 ! integral = integral + Coeff(k) * (xi1**k - xi0**k) / real(k)
92 ! endif
93 ! if (npoly>=4) then
94 ! k=5
95 ! integral = integral + Coeff(k) * (xi1**k - xi0**k) / real(k)
96 ! endif
97 !
98  integration_polynomial = integral
99 
100 end function integration_polynomial
101 
102 !> \namespace polynomial_functions
103 !!
104 !! Date of creation: 2008.06.12
105 !! L. White
106 !!
107 !! This module contains routines that handle polynomials.
108 
109 end module polynomial_functions
polynomial_functions
Polynomial functions.
Definition: polynomial_functions.F90:2