suggestion for new intrinsic function: CONFORM



When passing assumed shape arrays to procedures, typically the first
thing I do is check that the dimension are what I expect, in
particular that certain pairs of arguments conform, for example

subroutine foo(imat,y,z)
integer, intent(in) :: imat(:,:) ! (n1,n2)
real, intent(in) :: x(:,:) ! (n1,n2)
real, intent(out) :: z(:,:) ! (n1,n2)
if (size(imat,1) /= size(x,1) .or. size(imat,2) /= size(x,2) .or.
size(x,1) /= size(z,1) .or. size(x,2) /= size(z,2)) then
stop "unexpected dimenions in foo"
end if
! some code
end subroutine foo

(There is of course no requirement in Fortran 90 or 95 that the
dimensions of array arguments passed to procedures have any
relationship to each other -- it just typically makes sense for my
codes).

It would be convenient to have a CONFORM intrinsic function such that

conform(x,y)

would be .true. if x and y had the same shape. Arrays x and y could be
of different (intrinsic) types and would have to have the same rank
(that would be checked at compile time).

Then the if statement above could be written

if (conform(imat,y) .and. conform(imat,z)) then

which is shorter and more expressive. Usually I am checking the
conformance of 1, 2, or 3D arrays, and I can write an overloaded
CONFORM myself, but I think the need to check that arrays conform is
so common that it be convenient to have it in the language.

One way to avoid having to check that arrays conform is to use arrays
of derived types:

type foo_dt
integer :: i
real :: y,z
end type foo_dt

subroutine foo(z)
type(foo_dt) :: z(:,:)
end subroutine foo

but this is not always convenient.

.



Relevant Pages

  • Re: gfortran & adjustable array: most values remain zero
    ... Indeed I expected that adjustable arrays would work in 2D the same ... adjustable arrays of more than one dimension should be generally avoided ... the actual and dummy arrays don't ...
    (comp.lang.fortran)
  • Re: Pros and cons of assumed-shape arrays
    ... dimension relations. ... In most subroutines with several arrays as ... arguments almost all of the arrays have similar shapes. ... is loop invariant and will be done once in the loop preamble ...
    (comp.lang.fortran)
  • Re: Program Fails When Parameter Fixed Constants are Changed (F77) ??
    ... Dimension xbys, rbys ... calculated integer numerical constant ... You can only dimension arrays passed as arguments with nPlns, ... You could always join the 21st century and follow Ron Shepard's suggestion, but you'll have to follow it carefully. ...
    (comp.lang.fortran)
  • Two dimensional arrays "C question"
    ... My question has to do with two dimensional arrays. ... int main ... So we see that the compiler can automatically calculate the first dimension. ...
    (microsoft.public.vc.language)
  • Re: CheckedListbox
    ... Dim MyInt(,) - Verschlechterung? ... Das Problem ist ich kann immer nur die letzte Dimension verändern, ... Man kann überhaupt keine Dimension eines Arrays verändern. ... Falls du auf Redim Preserve anspielst: ...
    (microsoft.public.de.german.entwickler.dotnet.vb)