!///////////////////////////////////////////////////////////////////
!//
!// linspace.f90
!//
!// A subroutine initiealizing an array with linear spaced values
!// from a start value to an end value with the number of elements
!// as the last argument
!//
!///////////////////////////////////////////////////////////////////

  SUBROUTINE linspace(V,s_value,e_value)
    IMPLICIT NONE
    !// Arguments
    !// The array recieving the linear spaced values
    REAL(KIND=8),DIMENSION(:),INTENT(INOUT)    :: V
    !// The start value of the array
    REAL(KIND=8),INTENT(IN)                    :: s_value
    !// The end value fo the array
    REAL(KIND=8),INTENT(IN)                    :: e_value
    !// Local variables
    !// The number of elements in the array
    INTEGER                                    :: n_elem
    !// The step value
    REAL                                       :: delta
    !// An index variable
    INTEGER                                    :: i
    
    !// Get the number of elements in the array
    n_elem = SIZE(V)
    
    !// Calculate the step value taking the end value
    !// divided by one less than the number of elements
    !// in the array
    delta = e_value/FLOAT((n_elem-1))
    !// Do we clunt up or down
    IF(s_value < e_value) THEN
      !// We count up
      V(1) = s_value
      V(n_elem) = e_value
      DO i = 2,n_elem-1
        V(i) = V(i-1) + delta
      END DO
    ELSE
      V(1) = e_value
      V(n_elem) = s_value
      DO i = 2,n_elem-1
        V(i) = V(i-1) - delta
      END DO
    END IF
  END SUBROUTINE linspace
  
