slice_left_array Module Function

module function slice_left_array(a, b, dim) result(c)

Left trim an autodiff array

Arguments

Type IntentOptional Attributes Name
class(array_type), intent(in), target :: a
integer, intent(in) :: b
integer, intent(in), optional :: dim

Return Value type(array_type), pointer


Source Code

  module function slice_left_array(a, b, dim) result(c)
    !! Left trim an autodiff array
    implicit none
    class(array_type), intent(in), target :: a
    integer, intent(in) :: b
    integer, intent(in), optional :: dim
    type(array_type), pointer :: c

    integer :: s
    integer :: dim_

    if(present(dim)) then
       dim_ = dim
    else
       dim_ = 1
    end if

    ! left trim 1D array by using shape to swap dimensions
    if(dim_.eq.1)then
       c => a%create_result(array_shape = [b, size(a%val,2)])
       do concurrent(s=1:size(a%val,2))
          c%val( :, s) = a%val( 1:b, s)
       end do
    else
       c => a%create_result(array_shape = [size(a%val,1), b])
       do concurrent(s=1:size(a%val,1))
          c%val( s, : ) = a%val( s, 1:b)
       end do
    end if
    c%indices = [ dim_, b ]

    if(a%requires_grad) then
       c%requires_grad = .true.
       c%is_forward = a%is_forward
       c%operation = 'slice_left'
       c%left_operand => a
       c%owns_left_operand = a%is_temporary
    end if
  end function slice_left_array