Re: how to use 1D array as a multidimensional array
From: Richard Maine (nospam_at_see.signature)
Date: 06/28/04
- Next message: Richard Maine: "Re: Problems with intel fortran compiler version 8"
- Previous message: James Van Buskirk: "Re: how to use 1D array as a multidimensional array"
- In reply to: Kamaraju Kusumanchi: "how to use 1D array as a multidimensional array"
- Next in thread: Kamaraju Kusumanchi: "Re: how to use 1D array as a multidimensional array"
- Reply: Kamaraju Kusumanchi: "Re: how to use 1D array as a multidimensional array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 28 Jun 2004 09:35:53 -0700
Kamaraju Kusumanchi <kk288@cornell.edu> writes:
> real, dimension(100) :: A
> Now I want to access this array as a "2D equivalent"
> like B(i,j) where B is a 10x10 array...
Do you mean those declarations literally? Or were you
just trying to simplify the explanation? It matters.
In particular, what matters is whether the array in question
is declared with fixed dimensions.
If the array size is actually fixed, as it is in your example,
then *BY FAR* the easiest way is to use equivalence, as in
real, dimension(100) :: A
real, dimension(10,10) :: B
equivalence (a,b)
This is basically f77 (well, for that matter, earlier, as this
predates even f66), other than the trivia that your example
happens to use f90 declaration syntax. Nothing of substance
new here.
Now if your array is dynamically sized, then things are much
more complicated. Equivalence is fundamentaly about static
memory layout; you can't use it with dynamically sized things
(no matter what dynamic sizing mechanism you use - equivalence
won't work with any of them).
For dynamically-sized things, see the trick that Jugoslav
mentioned. I *THINK* that is standard-conforming (but some
of the details of pointers and dummy arguments are quite tricky,
so I'm not sure at all. I'd ask James Buskirk, who seems far
more up on such things than I am. I always did have trouble
reading that particular part of the standard; takes me hours
to pour over it...and then I still discover that I missed some
detail that makes my answer wrong.) But if the size is static,
you don't need the complications and side issues of this method.
Note that f2003 provides a simpler way to achieve this for
dynamically-sized arrays. You can allocate a 1-D array and
then point at it with a pointer of different rank; there is
new syntax in the pointer assignment statement to allow this.
It is only allowed if the allocation is 1-D; you can't do it the
other way around because of possible complications with the
stride. (I'm mildly advocating for a concept of sequence pointers
in f2003+; it would, as a side effect, allow you to do things the
other way around also.) But these things are for the future,
f2003 not yet being here in actual compilers.
-- Richard Maine | Good judgment comes from experience; email: my first.last at org.domain | experience comes from bad judgment. org: nasa, domain: gov | -- Mark Twain
- Next message: Richard Maine: "Re: Problems with intel fortran compiler version 8"
- Previous message: James Van Buskirk: "Re: how to use 1D array as a multidimensional array"
- In reply to: Kamaraju Kusumanchi: "how to use 1D array as a multidimensional array"
- Next in thread: Kamaraju Kusumanchi: "Re: how to use 1D array as a multidimensional array"
- Reply: Kamaraju Kusumanchi: "Re: how to use 1D array as a multidimensional array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|