Skip to content

Instantly share code, notes, and snippets.

@ivan-pi
Created December 19, 2019 18:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ivan-pi/f4b4741d7ed54ceff787c85d6ba22a5a to your computer and use it in GitHub Desktop.
Save ivan-pi/f4b4741d7ed54ceff787c85d6ba22a5a to your computer and use it in GitHub Desktop.
Linspace in Fortran
module linspace_mod
use iso_fortran_env, only: dp => real64
implicit none
contains
!>
! Return evenly spaced numbers over a specified interval.
!
! Returns `num` evenly spaced samples, calculated over the interval `[start, stop]`.
!
! Ported from the numpy routine.
!
! Author: Ivan Pribec
!
function linspace(start,end,num,endpoint,step) result(samples)
! PARAMETERS
real(dp), intent(in) :: start
!! The starting value of the sequence.
real(dp), intent(in) :: end
!! The end value of the sequence, unless `endpoint` is set to `.false.`.
!! In that case, the sequence consists of all but the last of `num + 1`
!! evenly spaced samples, so that `end` is excluded. Note that the
!! step size changes when `endpoint` is `.false.`.
integer, intent(in), optional :: num
!! Number of samples to generate. Default value is 50.
logical, intent(in), optional :: endpoint
!! If `.true.`, `end` is the last sample. Otherwise, it is not included. Default is `.true.`.
real(dp), intent(out), optional :: step
!! If present, `step` is the size of spacing between samples.
! RETURNS
real(dp), allocatable :: samples(:)
!! There are `num` equally spaced samples in the closed interval `[start, stop]` or
!! the half-open interval `[start, stop)` (depending on whether `endpoint` is `.true.` or `.false.`).
integer :: num_, i
logical :: endpoint_
real(dp) :: step_
num_ = 50
if (present(num)) num_ = num
endpoint_ = .true.
if (present(endpoint)) endpoint_ = endpoint
! find step size
if (endpoint_) then
step_ = (end - start)/real(num_-1,dp)
else
step_ = (end - start)/real(num_,dp)
end if
if (present(step)) step = step_
allocate(samples(num_))
do i = 1, num_
samples(i) = start + (i-1)*step_
end do
end function linspace
end module
program linspace_test
use iso_fortran_env, only: dp => real64
use linspace_mod
implicit none
print *, linspace(0.0_dp,1.0_dp,11)
end program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment