Anthony Stone said:
> I would like to arrange for the scalar variables AXX, AXY, etc. to refer to
> the elements of a 3 * 3 array A. In Fortran 77 I can do this using
> EQUIVALENCE, and this is efficient (I believe):
>
> EQUIVALENCE (AXX, A(1,1)), (AXY, A(1,2)), ...
Should be reasonably efficient...
> I can do this in Fortran 90 too, but it is deprecated. It appears that I am
Actually it is "obsolescent" - i.e. "becoming obsolete" - supposedly because
there are (usually) better ways of doing it.
> supposed to do
>
> DOUBLE PRECISION, DIMENSION (3,3), TARGET :: A
> DOUBLE PRECISION, POINTER :: AXX, AXY, ...
>
> and then
>
> AXX => A(1,1)
> AXY => A(1,2)
> ...
>
> My questions are:
>
> (a) Is this the right way to do it? If not, what should I do?
Probably not. See below.
> (b) Is it efficient? I refer to AXX etc many times.
Probably not efficient, unless all the usage is localised and the compiler can
thus undo your pointerisation.
> (c) Is there a way to set this up without having to execute run-time code to
> associate the pointers with the appropriate variables? For example, can I do
>
> DOUBLE PRECISION, POINTER :: AXX => A(1,1), AXY => A(1,2), ...
No, you cannot do this.
> I want a fixed association, not one that might in principle change. From this
> point of view the EQUIVALENCE statement is simple and straightforward, and the
> use of pointers seems an unnecessary complication.
I agree completely.
Why do you want them as an array? Is it merely to group them or do you do
operations on the array as a whole?
If just to group them (e.g. to pass as a single actual argument) you could
make a derived type, e.g.
TYPE a_type
DOUBLE PRECISION xx,xy,xz,yx,yy,yz,zx,zy,zz
END TYPE
TYPE(a_type) a
And then refer to A%XX and A%XY instead of AXX and AXY.
This might also be useful to do in terms of packaging your type up together
with its operations, should you want to do that.
If you want to do operations on the array as an array, you could
declare PARAMETER names to access your array elements, e.g.
INTEGER,PARAMETER :: x = 1, y = 2, z = 3
And instead of AXX and AXY do A(X,X) and A(X,Y)
[This method is not new to F90; you could already do it in F77]
But if you really really want both to have an array and to refer to elements
of it with scalar names, just keep on using an EQUIVALENCE.
[Personally I would not advocate using EQUIVALENCE in the first place, but if
you already have a working program using it, and its use is not going to get
in the way of any changes you have in mind, why change it?]
Cheers,
--
...........................Malcolm Cohen, NAG Ltd., Oxford, U.K.
([log in to unmask])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|