Derivative free 1D function minimizer in modern Fortran
A Fortran Package Manager manifest file is included, so that the library and test cases can be compiled with FPM. For example:
fpm build --profile release
fpm test --profile release
By default, the library is built with double precision (real64
) real values. Explicitly specifying the real kind can be done using the following preprocessor flags:
Preprocessor flag | Kind | Number of bytes |
---|---|---|
REAL32 |
real(kind=real32) |
4 |
REAL64 |
real(kind=real64) |
8 |
REAL128 |
real(kind=real128) |
16 |
For example, to build a single precision version of the library, use:
fpm build --profile release --flag "-DREAL32"
To use fmin
within your fpm project, add the following to your fpm.toml
file:
[dependencies]
fmin = { git="https://github.com/jacobwilliams/fmin.git" }
program test
use fmin_module
use iso_fortran_env, only: wp => real64 ! double precision
real(wp) :: xmin, xerr
real(wp),parameter :: ax = -4.0_wp ! lower bound
real(wp),parameter :: bx = 0.0_wp ! upper bound
real(wp),parameter :: tol = 1.0e-8_wp ! tolerance
real(wp),parameter :: pi = acos(-1.0_wp) ! pi
real(wp),parameter :: x = -pi/2.0_wp ! true answer
xmin = fmin(func,ax,bx,tol) ! compute the minimum
xerr = xmin - x ! difference from true value
write(*,*) 'xmin = ', xmin
write(*,*) 'xmin exact = ', x
write(*,*) 'xmin error = ', xerr
contains
function func(x) result(f)
implicit none
real(wp),intent(in) :: x !! indep. variable
real(wp) :: f !! function value `f(x)`
f = sin(x)
end function func
end program test
The output is:
xmin = -1.5707963254967554
xmin exact = -1.5707963267948966
xmin error = 1.2981411501300499E-009
master
branch can be found here. This is generated by processing the source files with FORD.