Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

same_shape function #403

Open
Open
Labels
topic: utilitiescontainers, strings, files, OS/environment integration, unit testing, assertions, logging, ...
@Beliavsky

Description

Related to a Fortran Discourse thread, I think a same_shape function that takes two array arguments and returns .TRUE. if the arguments have the same shape or if the 2nd argument is not PRESENT would be useful. It would make it easier to check array dimensions upon entering a procedure, including array dimensions of optional arguments. Making it easier could make it more likely that programmers do it. Below is an example for real arrays up to dimension 3, but it should be available for dimension up to say 7 and for all pairs of basic types, since one may want to check that a real and integer array have the same shape.

A rationale for same_shape is that if array y is not PRESENT, the expression

all(shape(x) == shape(y))

is invalid. Instead you could write

same_shape(x,y)

interface same_shape
 module procedure same_shape_real_real_1d, same_shape_real_real_2d, &
 same_shape_real_real_3d
end interface same_shape
pure function same_shape_real_real_1d(x,y) result(tf)
! true if x(:) and y(:) have the same shape or y not PRESENT
real(kind=dp), intent(in) :: x(:)
real(kind=dp), intent(in), optional :: y(:)
logical :: tf
if (present(y)) then
 tf = size(x) == size(y)
else
 tf = .true.
end if
end function same_shape_real_real_1d
!
pure function same_shape_real_real_2d(x,y) result(tf)
! true if x(:,:) and y(:,:) have the same shape or y not PRESENT
real(kind=dp), intent(in) :: x(:,:)
real(kind=dp), intent(in), optional :: y(:,:)
logical :: tf
if (present(y)) then
 tf = all(shape(x) == shape(y))
else
 tf = .true.
end if
end function same_shape_real_real_2d
!
pure function same_shape_real_real_3d(x,y) result(tf)
! true if x(:,:,:) and y(:,:,:) have the same shape or y not PRESENT
real(kind=dp), intent(in) :: x(:,:,:)
real(kind=dp), intent(in), optional :: y(:,:,:)
logical :: tf
if (present(y)) then
 tf = all(shape(x) == shape(y))
else
 tf = .true.
end if
end function same_shape_real_real_3d 

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: utilitiescontainers, strings, files, OS/environment integration, unit testing, assertions, logging, ...

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /