merge_scalar Function

private function merge_scalar(tsource, fsource, mask) result(c)

Merge two autodiff arrays based on a mask

Arguments

Type IntentOptional Attributes Name
class(array_type), intent(in), target :: tsource
real(kind=real32), intent(in) :: fsource
logical, intent(in), dimension(:,:) :: mask

Return Value type(array_type), pointer


Source Code

  function merge_scalar(tsource, fsource, mask) result(c)
    !! Merge two autodiff arrays based on a mask
    implicit none
    class(array_type), intent(in), target :: tsource
    real(real32), intent(in) :: fsource
    logical, dimension(:,:), intent(in) :: mask
    type(array_type), pointer :: c

    integer :: i, j

    if(size(tsource%shape) .ne. 1)then
       call stop_program("merge_array: only 1D arrays can be merged")
    end if

    c => tsource%create_result(array_shape=[size(tsource%val,1), size(tsource%val,2)])
    ! merge 1D array by using shape to swap dimensions
    do concurrent(i=1:size(tsource%val,1), j=1:size(tsource%val,2))
       if(mask(i,j)) then
          c%val(i,j) = tsource%val(i,j)
       else
          c%val(i,j) = fsource
       end if
    end do
    c%mask = mask

    c%get_partial_left => get_partial_merge_left
    c%get_partial_left_val => get_partial_merge_left_val
    if(tsource%requires_grad) then
       c%requires_grad = .true.
       c%is_forward = tsource%is_forward
       c%operation = 'merge_scalar'
       c%left_operand => tsource
       c%owns_left_operand = tsource%is_temporary
    end if
  end function merge_scalar