For Programmers: Free Programming Magazines  


Home > Archive > Fortran > July 2004 > Initializing Multi-dimensional array in Fortran 90 Module









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Initializing Multi-dimensional array in Fortran 90 Module
Tom Gederberg

2004-07-14, 4:00 pm

In Fortran 90 Module, you can initialize an single dimensioned array
by doing, for example:

MODULE TEST
REAL :: VECTOR X = (/ 1., 2., 3. /)
END MODULE TEST

however, I have not figured out how to do this for a multi-dimensional
array. For example, the following does not work:

MODULE TEST
REAL :: I_MATRIX(3,3) = (/ 1., 0., 0., 0., 1., 0., 0., 0., 1. /)
END MODULE TEST

The compiler complains that the shape of the initialization expression
does not match that of variable "I_MATRIX".

I've tried doing the following, but this does not work also:

MODULE TEST
REAL :: I_MATRIX(3,3) = (/ 1., 0., 0. /, / 0., 1., 0. /, /0., 0.,
1. /)
END MODULE TEST

Any help would be appreciated. Thanks.
Helge Avlesen

2004-07-14, 4:00 pm

thomas.k.gederberg@boeing.com (Tom Gederberg) writes:

| MODULE TEST
| REAL :: I_MATRIX(3,3) = (/ 1., 0., 0., 0., 1., 0., 0., 0., 1. /)
| END MODULE TEST

try the RESHAPE function

MODULE TEST
REAL :: I_MATRIX(3,3) = &
RESHAPE( (/ 1., 0., 0., 0., 1., 0., 0., 0., 1. /), (/3,3/) )
END MODULE TEST

Helge
David Frank

2004-07-14, 4:00 pm


"Tom Gederberg" <thomas.k.gederberg@boeing.com> wrote in message
news:fb67793f.0407140705.60671177@posting.google.com...
> In Fortran 90 Module, you can initialize an single dimensioned array
> by doing, for example:
>
> MODULE TEST
> REAL :: VECTOR X = (/ 1., 2., 3. /)
> END MODULE TEST
>
> however, I have not figured out how to do this for a multi-dimensional
> array. For example, the following does not work:
>
> MODULE TEST
> REAL :: I_MATRIX(3,3) = (/ 1., 0., 0., 0., 1., 0., 0., 0., 1. /)
> END MODULE TEST
>


A good compiler shud accept your statement above as is, ie. CVF does.


Steve Lionel

2004-07-14, 4:00 pm

On 14 Jul 2004 08:05:24 -0700, thomas.k.gederberg@boeing.com (Tom Gederberg)
wrote:

>I've tried doing the following, but this does not work also:
>
>MODULE TEST
> REAL :: I_MATRIX(3,3) = (/ 1., 0., 0. /, / 0., 1., 0. /, /0., 0.,
>1. /)
>END MODULE TEST


You don't quite have the nested array syntax right. What you want is this:

REAL :: I_MATRIX(3,3) = (/(/ 1., 0., 0. /), (/ 0., 1., 0. /), &
(/0., 0.,1. /)/)

RESHAPE works too. Some compilers may accept the shape mismatch as an
extension, but I don't recommend relying on that.


Steve Lionel
Software Products Division
Intel Corporation
Nashua, NH

User communities for Intel Software Development Products
http://softwareforums.intel.com/
Intel Fortran Support
http://developer.intel.com/software/products/support/
Richard Maine

2004-07-14, 4:00 pm

Steve Lionel <Steve.Lionel@intel.com> writes:

> You don't quite have the nested array syntax right. What you want is this:
>
> REAL :: I_MATRIX(3,3) = (/(/ 1., 0., 0. /), (/ 0., 1., 0. /), &
> (/0., 0.,1. /)/)
>
> RESHAPE works too. Some compilers may accept the shape mismatch as an
> extension, but I don't recommend relying on that.


Alas, the above nested array syntax relies on the *SAME* extension.
All array constructors have rank 1; the above syntax is not an
exception. The inner arrays just get expanded to a list of their
elements - their particular shape information doesn't carry through.

It is unfortunate that there isn't a way to write a constructor for
rank >1. RESHAPE is ugly and error-prone for this application IMO,
but it is the only way I know how to do it in this context.

You could use a DATA statement, which has its own set of "issues" IMO.
One of those "issues" is that shape doesn't matter; DATA statements
operate only on the element sequence. This means that you get no
shape checking, which I regard as a negative. But the negative is
also one of its positives... when the shape checking happens to be
getting in the way.

--
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
bv

2004-07-14, 8:57 pm

Helge Avlesen wrote:
>
> REAL :: I_MATRIX(3,3) = &
> RESHAPE( (/ 1., 0., 0., 0., 1., 0., 0., 0., 1. /), (/3,3/) )


Horror, not everybody's into a fortran freak show; how about suggesting
something toxic free and more amenable to numeric trench work,

real matrix(3,3) /1,0,0, 0,1,0, 0,0,1/

James Giles

2004-07-14, 8:57 pm

bv wrote:
> Helge Avlesen wrote:
>
> Horror, not everybody's into a fortran freak show; how about suggesting
> something toxic free and more amenable to numeric trench work,
>
> real matrix(3,3) /1,0,0, 0,1,0, 0,0,1/


I don't like bv's solution: slashes make terrible delimiters.
Fortran's rule concerning rank conformance could be relaxed
when array constructors are used and get about the same
result:

real :: I_MATRIX(3,3) = [1., 0., 0., 0., 1., 0., 0., 0., 1.]

(Square brackets as specified by F2003.) All this would take
would be the compatible extension permitting this form. It's
up to the committee. The rule would be that the length of the
constructor had to be the same as the SIZE of the array, and
the data would be assigned (initialized) in array element order
as defined by the standard.

--
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


James Van Buskirk

2004-07-14, 8:57 pm

"James Giles" <jamesgiles@worldnet.att.net> wrote in message
news:CdjJc.254441$Gx4.87838@bgtnsc04-news.ops.worldnet.att.net...

> real :: I_MATRIX(3,3) = [1., 0., 0., 0., 1., 0., 0., 0., 1.]


The problem I have with this kind of initialization (which
could in fact be done similarly with a DATA statement) is
that you end up looking at code that sometimes has matrices
embedded in it and sometimes their transposes. Maybe I am
just being nonadaptable, but I seem to encounter problems
in telling which matrices are being presented in transpose
form and which in direct form. As a consequence I prefer
to make a blessing out of the requirement to use RESHAPE as
follows:

real, parameter :: I_MATRIX(3,3) = reshape((/ &
2, 3, 5, &
7, 11, 13, &
17, 19, 23/), &
shape = (/3,3/), order = (/2,1/))

In this way the matrix looks in code like I think
about it internally, not like its transpose. There
are many situations I am familiar with where it's
hard enough to determine mathematically whether you
want a matrix or its transpose and it complicates
the problem for me further when you can't tell a
matrix from its transpose in the code.

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


James Giles

2004-07-15, 3:58 am

James Van Buskirk wrote:
> "James Giles" <jamesgiles@worldnet.att.net> wrote in message
> news:CdjJc.254441$Gx4.87838@bgtnsc04-news.ops.worldnet.att.net...
>
>
> The problem I have with this kind of initialization (which
> could in fact be done similarly with a DATA statement) is
> that you end up looking at code that sometimes has matrices
> embedded in it and sometimes their transposes. Maybe I am
> just being nonadaptable, but I seem to encounter problems
> in telling which matrices are being presented in transpose
> form and which in direct form. As a consequence I prefer
> to make a blessing out of the requirement to use RESHAPE as
> follows:
>
> real, parameter :: I_MATRIX(3,3) = reshape((/ &
> 2, 3, 5, &
> 7, 11, 13, &
> 17, 19, 23/), &
> shape = (/3,3/), order = (/2,1/))


Well, I'm not arguing that the existing form be disallowed
or anthing. The issue is complicated because array constructing
itself has some complexity. But, if the objective is to simplify
the most common case, the form I gave is certainly better than
the DATA statement imitation. Possibly some additional thought
could come up with an even better solution. My point is that
mere adoption of some existing extension that wasn't carefully
thought out is certainly not a good idea.

--
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


r08n

2004-07-15, 8:57 am

Richard Maine <nospam@see.signature> wrote in message news:<m1pt6yqxdd.fsf@macfortran.local>...

> RESHAPE is ugly and error-prone for this application IMO,


Why? I had the same problem (initializing 2D array) and I used RESHAPE, because
I couldn't find other (better) solution. What errors can I expect from this?
Richard Maine

2004-07-15, 3:59 pm

s8ngsu3@yahoo.com (r08n) writes:

> Richard Maine <nospam@see.signature> wrote in message news:<m1pt6yqxdd.fsf@macfortran.local>...
>
>
> Why? I had the same problem (initializing 2D array) and I used RESHAPE, because
> I couldn't find other (better) solution. What errors can I expect from this?


The "ugly" part is, of course, my subjective judgement; I can't say much more
about that.

The main thing I think is error prone is that columns are delimitted
by nothing other than by the count of elements. I don't like having
to count - reminds me of Hollerith. This gets worse as the array gets
larger. If I have a 6 by 9 array, I don't think of it as just an
undifferentiated sequence of 42 elements, so I don't like writing it
that way. I'd like the compiler to be able to notice if I accidentally
put only 5 elements in one column.

And then there is the fact that you have to write it "transposed".

--
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
Ian Bush

2004-07-15, 3:59 pm

Richard Maine wrote:
>
> The main thing I think is error prone is that columns are delimitted
> by nothing other than by the count of elements. I don't like having
> to count - reminds me of Hollerith. This gets worse as the array gets
> larger. If I have a 6 by 9 array, I don't think of it as just an
> undifferentiated sequence of 42 elements, so I don't like writing it
> that way. I'd like the compiler to be able to notice if I accidentally
> put only 5 elements in one column.
>
> And then there is the fact that you have to write it "transposed".
>


So allow TRANSPOSE in initialization expressions ? :> )

( Yes, I know, only works for the 2D case )

Ian

Richard Edgar

2004-07-15, 3:59 pm

Richard Maine wrote:
> If I have a 6 by 9 array, I don't think of it as just an
> undifferentiated sequence of 42 elements


*cough*

How about an undifferentiated sequence of 54 elements? :-)

Richard
J. F. Cornwall

2004-07-15, 3:59 pm

Richard Edgar wrote:
> Richard Maine wrote:
>
>
>
> *cough*
>
> How about an undifferentiated sequence of 54 elements? :-)
>
> Richard


Which makes the point quite nicely about it being error-prone...

Jim C

Toon Moene

2004-07-15, 3:59 pm

Richard Maine wrote:

> If I have a 6 by 9 array, I don't think of it as just an
> undifferentiated sequence of 42 elements, so I don't like writing it
> that way.


Well, if you have problems with determining that 6 * 9 = 63 then RESHAPE
is the least of your woes.

--
Toon Moene - mailto:toon@moene.indiv.nluug.nl - phoneto: +31 346 214290
Saturnushof 14, 3738 XG Maartensdijk, The Netherlands
Maintainer, GNU Fortran 77: http://gcc.gnu.org/onlinedocs/g77_news.html
A maintainer of GNU Fortran 95: http://gcc.gnu.org/fortran/
Richard Maine

2004-07-15, 3:59 pm

Richard Edgar <rge21@astro.su.se> writes:

> Richard Maine wrote:
>
> *cough*
>
> How about an undifferentiated sequence of 54 elements? :-)


I wondered whether anyone would bite. :-)

Obviously not a Hitchhiker's Guide fan. That isn't a requirement for
programming in Fortran? :-)

(Ok. To avoid leaving you completely puzzled, the first book in the
Hitchhiker's Guide "trilogy" ends by revealing that the answer to
life, the universe, and everything is 42.... if one only knew what
the question was. The second book ends by revealing that the question
is "what is 6 times 9?". The third book explains the missing 12.)

--
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
Richard Edgar

2004-07-15, 3:59 pm

Richard Maine wrote:
> Richard Edgar <rge21@astro.su.se> writes:
>
>
>
> Obviously not a Hitchhiker's Guide fan.


Not _enough_ of a fan. The 42 had registered, but I wasn't working in
base 13 at the time.... ;-)

Richard
Jugoslav Dujic

2004-07-15, 3:59 pm

Richard Maine wrote:
| Richard Edgar <rge21@astro.su.se> writes:
| Obviously not a Hitchhiker's Guide fan. That isn't a requirement for
| programming in Fortran? :-)

I thought so too...

| (Ok. To avoid leaving you completely puzzled, the first book in the
| Hitchhiker's Guide "trilogy" ends by revealing that the answer to
| life, the universe, and everything is 42.... if one only knew what
| the question was. The second book ends by revealing that the question
| is "what is 6 times 9?". The third book explains the missing 12.)

....but, frankly, I don't recall the 6*9 and missing 12 stuff at all...
(but then, I didn't like much the last two books of the trilogy) -- did
you just made it up or is it me having "senior moments"? (in my thirties) :-).
As I recall, the Earth itself was the computer that should have provided
the question, but the Vogons destroyed it in order to make an intergalactic
superhighway (project later abandoned).

--
Jugoslav
___________
www.geocities.com/jdujic

Please reply to the newsgroup.
You can find my real e-mail on my home page above.

James Giles

2004-07-15, 3:59 pm

Richard Maine wrote:
....
> The main thing I think is error prone is that columns are delimitted
> by nothing other than by the count of elements. I don't like having
> to count - reminds me of Hollerith. This gets worse as the array gets
> larger. If I have a 6 by 9 array, I don't think of it as just an
> undifferentiated sequence of 42 elements, so I don't like writing it
> that way. I'd like the compiler to be able to notice if I accidentally
> put only 5 elements in one column.


I think the usual first thought of the naive user might be an aid
to the design. Most people (that I run into) think that the
constructor for a 6 by 9 array would have nine sub-constructors
each with six elements:

REAL :: Arr(6,9) = [ [1.0,2.0,3.0,4.0,5.0,6.0], ...]

I was never clear why the committee chose that the constructor
to be considered rank-1 regardless of its form (I know, don't ask
the committee why). Some new form of the constructor with
this property retained could be worth pursuing.

> And then there is the fact that you have to write it "transposed".


I learned computing first. Array element order is more natural
to me than matrix order anyway.

--
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


Richard Maine

2004-07-15, 3:59 pm

"Jugoslav Dujic" <jdujic@yahoo.com> writes:

> Richard Maine wrote:


> | Obviously not a Hitchhiker's Guide fan.


> ...but, frankly, I don't recall the 6*9 and missing 12 stuff at all...
> did you just made it up or is it me having "senior moments"? (in my thirties) :-).


I didn't just make it up....but its been quite a while since I read the
books. I won't swear that it isn't a result of my own senior moments,
for which I have at least a little more excuse than you.

--
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
Richard Maine

2004-07-15, 3:59 pm

"James Giles" <jamesgiles@worldnet.att.net> writes:

> I was never clear why the committee chose that the constructor
> to be considered rank-1 regardless of its form (I know, don't ask
> the committee why).


In this case, I really have no clue as to why, so I can't even
give much of a guess. Perhaps just that they had trouble comming
up with a good simple yet general syntax for the higher rank cases?
That's a question from me, not an answer.

> Some new form of the constructor with
> this property retained could be worth pursuing.


Agree. Of course, "worth pursuing" doesn't necessarily translate into
"I'd like the result and think it a good idea to do", but I will
go along with the "worth pursuing" part.

--
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
J. F. Cornwall

2004-07-15, 3:59 pm

Richard Maine wrote:

> Richard Edgar <rge21@astro.su.se> writes:
>
>
>
>
> I wondered whether anyone would bite. :-)
>
> Obviously not a Hitchhiker's Guide fan. That isn't a requirement for
> programming in Fortran? :-)
>
> (Ok. To avoid leaving you completely puzzled, the first book in the
> Hitchhiker's Guide "trilogy" ends by revealing that the answer to
> life, the universe, and everything is 42.... if one only knew what
> the question was. The second book ends by revealing that the question
> is "what is 6 times 9?". The third book explains the missing 12.)
>


Actually, I had wondered if that was your intention. Flipped a virtual
coin on which way to respond, picked the wrong side. :-/

Jim

glen herrmannsfeldt

2004-07-15, 9:00 pm

James Van Buskirk wrote:
> "James Giles" <jamesgiles@worldnet.att.net> wrote in message
> news:CdjJc.254441$Gx4.87838@bgtnsc04-news.ops.worldnet.att.net...
>
>
>
>
> The problem I have with this kind of initialization (which
> could in fact be done similarly with a DATA statement) is
> that you end up looking at code that sometimes has matrices
> embedded in it and sometimes their transposes. Maybe I am
> just being nonadaptable, but I seem to encounter problems
> in telling which matrices are being presented in transpose
> form and which in direct form. As a consequence I prefer
> to make a blessing out of the requirement to use RESHAPE as
> follows:


> real, parameter :: I_MATRIX(3,3) = reshape((/ &
> 2, 3, 5, &
> 7, 11, 13, &
> 17, 19, 23/), &
> shape = (/3,3/), order = (/2,1/))


> In this way the matrix looks in code like I think
> about it internally, not like its transpose. There
> are many situations I am familiar with where it's
> hard enough to determine mathematically whether you
> want a matrix or its transpose and it complicates
> the problem for me further when you can't tell a
> matrix from its transpose in the code.


Considering that Fortran uses the convention where the
first subscript varies fastest, and most of mathematics uses
(row, column) for subscript notation of arrays, there are two
choices when implementing matrix operations.

1) Reverse the order of the subscripts from the mathematical
notation, following column major order.

2) Keep the subscript ordering, though the arrangement in
memory does not match the order used in mathematical
descriptions.

I believe 1) is my choice, though I am not sure which is
more common. In either case, the loops of the program
should be arranges so that the leftmost subscript is
described by the innermost loops, (that is, varies fastest).

-- glen


John Harper

2004-07-15, 9:00 pm

In article <m1wu15nwp9.fsf@macfortran.local>,
Richard Maine <nospam@see.signature> wrote:
>
> ... I don't like having to count - reminds me of Hollerith. This gets
> worse as the array gets larger. If I have a 6 by 9 array, I don't
> think of it as just an undifferentiated sequence of 42 elements


That, like the related error of referring to the (n+1)th element of
an array declared with n, is a special case of the Baby Worm problem:
Once upon a time, Father Worm, Mother Worm and Baby Worm were wriggling
across a field and came to a concrete wall. Father Worm went ahead
tunnelling under it, followed by Mother Worm, followed by Baby Worm.
When they came out the other side Baby Worm looked around and said
"Look, Mummy, there's four of us now!" Explain that observation, given
that no more baby worms had been born on the way.
Answer: Baby Worm was very young and hadn't learnt to count properly yet.

John Harper, School of Mathematical and Computing Sciences,
Victoria University, PO Box 600, Wellington, New Zealand
e-mail john.harper@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
bv

2004-07-15, 9:00 pm

James Van Buskirk wrote:
>
> to make a blessing out of the requirement to use RESHAPE as
>
> real, parameter :: I_MATRIX(3,3) = reshape((/ &
> 2, 3, 5, &
> 7, 11, 13, &
> 17, 19, 23/), &
> shape = (/3,3/), order = (/2,1/))
>
> In this way the matrix looks in code like I think...


This "blessing" still looks like a devil's curse; once again, plain
vanilla does it,

parameter (nr = 3, nc = 4)
real a(nr,nc)
data ((a(i,j), j=1,nc), i=1,nr) /
& .40558, -.40558, .40558, -.40558,
& .81915, -.81915, -.81915, .81915,
& -.40558, -.40558, -.40558, -.40558/

and when JPL calls what the fifth wheel y-axis inertia is, you'll
immediately know it's April's fools day.

James Van Buskirk

2004-07-16, 3:57 am

"bv" <bvoh@Xsdynamix.com> wrote in message
news:40F70596.505B02C4@Xsdynamix.com...

> James Van Buskirk wrote:


[color=darkred]
> real a(nr,nc)
> data ((a(i,j), j=1,nc), i=1,nr) /


Of course, if you notice, I made I_MATRIX a named constant so
the initialization with DATA would not be possible.

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


meek@skyway.usask.ca

2004-07-16, 3:58 pm

In a previous article, bv <bvoh@Xsdynamix.com> wrote:
>James Van Buskirk wrote:
>
>This "blessing" still looks like a devil's curse; once again, plain
>vanilla does it,
>
> parameter (nr = 3, nc = 4)
> real a(nr,nc)
> data ((a(i,j), j=1,nc), i=1,nr) /
> & .40558, -.40558, .40558, -.40558,
> & .81915, -.81915, -.81915, .81915,
> & -.40558, -.40558, -.40558, -.40558/
>
>and when JPL calls what the fifth wheel y-axis inertia is, you'll
>immediately know it's April's fools day.
>

But it doesn't look complicated like C++, or whatever the
present name is, it looks like a non-expert could figure it out.
Why would anyone want that!

Chris
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com