create_result_array Module Function

module function create_result_array(this, array_shape) result(result_ptr)

Helper function to safely create result arrays with proper initialization

Arguments

Type IntentOptional Attributes Name
class(array_type), intent(in) :: this
integer, intent(in), optional, dimension(:) :: array_shape

Return Value type(array_type), pointer


Source Code

  module function create_result_array(this, array_shape) result(result_ptr)
    !! Helper function to safely create result arrays with proper initialization
    implicit none
    class(array_type), intent(in) :: this
    integer, dimension(:), intent(in), optional :: array_shape
    type(array_type), pointer :: result_ptr

    allocate(result_ptr)

    if(present(array_shape))then
       call result_ptr%allocate(array_shape=array_shape)
    else
       if(allocated(this%shape))then
          call result_ptr%allocate(array_shape=[this%shape, &
               size(this%val,2)])
       else
          call result_ptr%allocate(array_shape=shape(this%val))
       end if
    end if

    ! Initialise autodiff fields
    result_ptr%requires_grad = .false.
    result_ptr%is_scalar = this%is_scalar
    result_ptr%is_sample_dependent = this%is_sample_dependent
    result_ptr%is_forward = this%is_forward
    result_ptr%operation = 'none'
    result_ptr%owns_gradient = .false.
    result_ptr%owns_left_operand = .false.
    result_ptr%owns_right_operand = .false.
    result_ptr%left_operand => null()
    result_ptr%right_operand => null()
    result_ptr%get_partial_left => null()
    result_ptr%get_partial_right => null()
    result_ptr%get_partial_left_val => null()
    result_ptr%get_partial_right_val => null()
    result_ptr%get_partial_left_val_sum => null()
    result_ptr%get_partial_right_val_sum => null()
    result_ptr%is_temporary = .true.
    result_ptr%fix_pointer = .false.
  end function create_result_array