Re: suggestion for new intrinsic function: CONFORM
- From: "James Giles" <jamesgiles@xxxxxxxxxxxxxxxx>
- Date: Tue, 27 Feb 2007 23:35:15 GMT
Beliavsky wrote:
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
I recommended this about two years ago (as part of a discussion
on generic programming). Except note that the bounds that must
conform are not always the same. Consider if IMAT, X, and Z
must be such that IMAT is the appropriate shape for the result
of matrix multiplying X and Z. Then Size(X,2) must be the
same as Size(Z,1), Size(IMAT,1) must match Size(X,1), and
Size(IMAT,2) must match Size(Z,2). I proposed:
Subroutine foo(imat, X, Z) ! I assume you really meant X
integer, inferred :: i, j, k
Real, intent(in) :: X(^i, ^j), Z(^j, ^k)
Real, intent(out) :: imat(^i, ^k)
The rule was that the ^ meant that the value was to be inferred
from the correponding property of the actual argument and that
all inferrences of the same name must yield the same value.
Since it's part of the procedure's declarations, the requirement
is in the procedure's interface and the compiler can often test
the constraint at compile-time when processing the caller.
Within the subroutine, the values of I, J, and K can otherwise
be used as named constants (whose value is that which was
inferred). So, they can be used to declare the bounds of some
local working arrays, or as loop limits, etc.
--
J. Giles
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
.
- References:
- suggestion for new intrinsic function: CONFORM
- From: Beliavsky
- suggestion for new intrinsic function: CONFORM
- Prev by Date: Re: Matlab -> Fortran
- Next by Date: array-valued function as dummy argument
- Previous by thread: Re: suggestion for new intrinsic function: CONFORM
- Next by thread: Matlab -> Fortran
- Index(es):
Relevant Pages
|
|