Multidimensional arrays

  • Follow


Dear All

Can Fortran 9x handle multidimensional arrays (i.e. tensors)? For
clarification, at run time I decide that I need an array accessed by a
subscript list of k elements. Can this be done naturally by simply
specifying a subscript list?

Many thanks,
Miggs
0
Reply miggs_ectomorph (2) 1/8/2004 5:30:42 PM

Miggs:  There were several postings here on that topic. A runnable example
file is at:
http://ftp.cac.psu.edu/pub/ger/fortran/hdk/VectorSubscripts.f90

Skip Knoble, Penn State

On 8 Jan 2004 09:30:42 -0800, miggs_ectomorph@yahoo.com (Ectomorph) wrote:

-|Dear All
-|
-|Can Fortran 9x handle multidimensional arrays (i.e. tensors)? For
-|clarification, at run time I decide that I need an array accessed by a
-|subscript list of k elements. Can this be done naturally by simply
-|specifying a subscript list?
-|
-|Many thanks,
-|Miggs


   Herman D. (Skip) Knoble, Research Associate
   (a computing professional for 38 years)
   Email: SkipKnobleLESS@SPAMpsu.edu
   Web: http://www.personal.psu.edu/hdk
   Penn State Information Technology Services
    Academic Services and Emerging Technologies
     Graduate Education and Research Services
   Penn State University
     214C Computer Building
     University Park, PA 16802-21013
   Phone:+1 814 865-0818   Fax:+1 814 863-7049
0
Reply SkipKnobleLESS (57) 1/8/2004 6:13:03 PM


Herman D. Knoble <SkipKnobleLESS@SPAMpsu.edu> wrote in message news:<v97rvvg97qhsmrrsd6hjbeb9iatgbvpccc@4ax.com>...
> Miggs:  There were several postings here on that topic. A runnable example
> file is at:
> http://ftp.cac.psu.edu/pub/ger/fortran/hdk/VectorSubscripts.f90
> 
> Skip Knoble, Penn State
> 
> On 8 Jan 2004 09:30:42 -0800, miggs_ectomorph@yahoo.com (Ectomorph) wrote:
> 
> -|Dear All
> -|
> -|Can Fortran 9x handle multidimensional arrays (i.e. tensors)? For
> -|clarification, at run time I decide that I need an array accessed by a
> -|subscript list of k elements. Can this be done naturally by simply
> -|specifying a subscript list?
> -|
> -|Many thanks,
> -|Miggs

Dear Skip

Thank you for your reply. I anticipated that this might be difficult
to describe! Unless I have misread the very informative example that
you kindly posted, it appears that you actually specify at compile
time the number of dimensions (i.e. the rank) of the tensor. I need
something slightly more complex: the number of dimensions can only be
known at run time.

new_excitation(i1,i2) = i1 * 10 + i2

The new_excitation variable appears just to be of two-dimensional
(i.e. matrix) type, which can be accessed with the natural syntax
(x,y).

c(b)

from the second example might fit the bill perhaps? In this case, each
element of b would, I think, have to be a vector. In this case, b is
really an array of vectors, and the vectors are the subscripts for
each dimension for accessing the multidimensional tensor array. What I
think you have demonstrated to me is a neat method for using a vector
or list of indices to implicitly reference a number of elements in 2D
arrays.

Another way of stating it is that I wish to get access to a 3D array,
say:
z=1
   x=1 2 3
y=1  1 2 3
y=2  4 5 6
y=3  7 8 9

z=2
   x=1  2  3
y=1 11 12 13
y=2 14 15 16
y=3 17 18 19

Naturally I could just specify a 3D array at compile time and then
access with (x,y,z) but unfortunately I do not know whether the array
will be 1D, 2D, 3D etc. prior to running the code. C is just awful for
expressing such situations, what I am trying to discover is whether
Fortran is any better at it?

Once again, many thanks.
Miggs
0
Reply miggs_ectomorph (2) 1/9/2004 1:37:55 AM

Ectomorph wrote:

(snip)

> Thank you for your reply. I anticipated that this might be difficult
> to describe! Unless I have misread the very informative example that
> you kindly posted, it appears that you actually specify at compile
> time the number of dimensions (i.e. the rank) of the tensor. I need
> something slightly more complex: the number of dimensions can only be
> known at run time.
> 
> new_excitation(i1,i2) = i1 * 10 + i2
> 
> The new_excitation variable appears just to be of two-dimensional
> (i.e. matrix) type, which can be accessed with the natural syntax
> (x,y).

(big snip)

> Naturally I could just specify a 3D array at compile time and then
> access with (x,y,z) but unfortunately I do not know whether the array
> will be 1D, 2D, 3D etc. prior to running the code. C is just awful for
> expressing such situations, what I am trying to discover is whether
> Fortran is any better at it?

I think C would do better than Fortran, though natural syntax is
still not obvious to me.  If you don't know how many dimensions
it has, you can't use natural syntax.

In C, you can make a (pointer to)**n, that is, any depth of pointer
to a variable that you need.  If cast to the appropriate type, a
variable can be dereferenced using C's array syntax.

More obvious to me, though, is that you might have the subscripts
in an array, and need to reference an element of the array that
way.  The offset calculation into a single dimension array isn't
so hard to do at that point.

-- glen

0
Reply gah (12259) 1/9/2004 6:35:17 AM

Miggs:   Sorry that I misunderstood your initial question.

Apologies also that I still don't understand your notation in defining the
problem. What array operations on this multidimensional array will you
be doing?  E.g., C=A+B where A, B, C are either scalars or of the
same rank.  

To have a program that can at run-time  handle 1, 2, or 3D arrays, there
might be some possibilites:

1) By coding three subprograms to handle the three cases; someone more
creative than myself may be able to illustrate this using Derived Types
and possibly the Shape function and/or linked lists. 

To a Fortran compiler, all arrays are (1D) vectors; the 2D and 3D
subscripting that we do notationally is translated to vector subscripts
(address offsets) based on the 2D or 3D subscripts and the first 1 or 2 
(respectively) shape (Dimension)  parameters.  This translation is not 
that complex and may be enable one to do the same thing using Derived 
Types. Again, someone more creative than  I may be able to code up a 
running example.

2)  By (gasp!) using an interpretive language other than Fortran. For
example APL (J): http://www.jsoftware.com/  Since there are no
Dimension statements in APL, a multidimensional array can
be created on the fly so to speak using the Rho operator on a
string of data.

Good luck with it.
Skip

On 8 Jan 2004 17:37:55 -0800, miggs_ectomorph@yahoo.com (Ectomorph) wrote:

-|Herman D. Knoble <SkipKnobleLESS@SPAMpsu.edu> wrote in message
news:<v97rvvg97qhsmrrsd6hjbeb9iatgbvpccc@4ax.com>...
-|> Miggs:  There were several postings here on that topic. A runnable example
-|> file is at:
-|> http://ftp.cac.psu.edu/pub/ger/fortran/hdk/VectorSubscripts.f90
-|> 
-|> Skip Knoble, Penn State
-|> 
-|> On 8 Jan 2004 09:30:42 -0800, miggs_ectomorph@yahoo.com (Ectomorph) wrote:
-|> 
-|> -|Dear All
-|> -|
-|> -|Can Fortran 9x handle multidimensional arrays (i.e. tensors)? For
-|> -|clarification, at run time I decide that I need an array accessed by a
-|> -|subscript list of k elements. Can this be done naturally by simply
-|> -|specifying a subscript list?
-|> -|
-|> -|Many thanks,
-|> -|Miggs
-|
-|Dear Skip
-|
-|Thank you for your reply. I anticipated that this might be difficult
-|to describe! Unless I have misread the very informative example that
-|you kindly posted, it appears that you actually specify at compile
-|time the number of dimensions (i.e. the rank) of the tensor. I need
-|something slightly more complex: the number of dimensions can only be
-|known at run time.
-|
-|new_excitation(i1,i2) = i1 * 10 + i2
-|
-|The new_excitation variable appears just to be of two-dimensional
-|(i.e. matrix) type, which can be accessed with the natural syntax
-|(x,y).
-|
-|c(b)
-|
-|from the second example might fit the bill perhaps? In this case, each
-|element of b would, I think, have to be a vector. In this case, b is
-|really an array of vectors, and the vectors are the subscripts for
-|each dimension for accessing the multidimensional tensor array. What I
-|think you have demonstrated to me is a neat method for using a vector
-|or list of indices to implicitly reference a number of elements in 2D
-|arrays.
-|
-|Another way of stating it is that I wish to get access to a 3D array,
-|say:
-|z=1
-|   x=1 2 3
-|y=1  1 2 3
-|y=2  4 5 6
-|y=3  7 8 9
-|
-|z=2
-|   x=1  2  3
-|y=1 11 12 13
-|y=2 14 15 16
-|y=3 17 18 19
-|
-|Naturally I could just specify a 3D array at compile time and then
-|access with (x,y,z) but unfortunately I do not know whether the array
-|will be 1D, 2D, 3D etc. prior to running the code. C is just awful for
-|expressing such situations, what I am trying to discover is whether
-|Fortran is any better at it?
-|
-|Once again, many thanks.
-|Miggs


   Herman D. (Skip) Knoble, Research Associate
   (a computing professional for 38 years)
   Email: SkipKnobleLESS@SPAMpsu.edu
   Web: http://www.personal.psu.edu/hdk
   Penn State Information Technology Services
    Academic Services and Emerging Technologies
     Graduate Education and Research Services
   Penn State University
     214C Computer Building
     University Park, PA 16802-21013
   Phone:+1 814 865-0818   Fax:+1 814 863-7049
0
Reply SkipKnobleLESS (57) 1/9/2004 6:07:56 PM

Hi:

An old trick:

Make a statement function(s), with as many indices (dimensions) as you are
ever likely to want:
INDEX(i,j,k,l,...) = ((j-1)*N2+i)*N1.....

using whatever formula you need for the definition of INDEX.

N1 is the inner dimension of the first index, N2 the second...

Setting N3=N4=...=0 will effectively give you a 2-D array, if that happens
to be the case.
This can be done at run-time.

As long as your array is big enough (can be done with ALLOCATE at run time),
then refer to your array as follows:

ARRAY(index(i,j,k,...))

and you've got run-time indexing with variable (unlimited number of)
dimensions, which I think is what was being asked.

With F95, you should put INDEX into a module and USE it as a function,
rather than a statement function,
but you could add it to each subroutine with an "include" statement if you
want to do it the old way.

Mike

glen herrmannsfeldt wrote:

> Ectomorph wrote:
>
> (snip)
>
> > Thank you for your reply. I anticipated that this might be difficult
> > to describe! Unless I have misread the very informative example that
> > you kindly posted, it appears that you actually specify at compile
> > time the number of dimensions (i.e. the rank) of the tensor. I need
> > something slightly more complex: the number of dimensions can only be
> > known at run time.
> >
> > new_excitation(i1,i2) = i1 * 10 + i2
> >
> > The new_excitation variable appears just to be of two-dimensional
> > (i.e. matrix) type, which can be accessed with the natural syntax
> > (x,y).
>
> (big snip)
>
> > Naturally I could just specify a 3D array at compile time and then
> > access with (x,y,z) but unfortunately I do not know whether the array
> > will be 1D, 2D, 3D etc. prior to running the code. C is just awful for
> > expressing such situations, what I am trying to discover is whether
> > Fortran is any better at it?
>
> I think C would do better than Fortran, though natural syntax is
> still not obvious to me.  If you don't know how many dimensions
> it has, you can't use natural syntax.
>
> In C, you can make a (pointer to)**n, that is, any depth of pointer
> to a variable that you need.  If cast to the appropriate type, a
> variable can be dereferenced using C's array syntax.
>
> More obvious to me, though, is that you might have the subscripts
> in an array, and need to reference an element of the array that
> way.  The offset calculation into a single dimension array isn't
> so hard to do at that point.
>
> -- glen

0
Reply gmtrcs (6) 1/10/2004 10:04:06 PM

5 Replies
35 Views

(page loaded in 0.152 seconds)

Similiar Articles:













7/11/2012 1:17:17 AM


Reply: