From: Richard Maine on
monir <monirg(a)mondenet.com> wrote:

> 1) An array is declared as:
> ...Real X(10,20)
> and intentionally only a few elements are initialized in the program.

That's somewhat unusual. Do you actualy mean it when you say
"initialized" or are you using the term slopily? In particular, are you
doing this with a DATA statement? That's the only way I know to
initialize some elements of an array. Any other way you migt be talking
about is not initialization.

> 2) Can one correctly assume that the other elements of the array X are
> set to zero by the compiler (g95) ??
> OR
> Are these elements initially undefined ??

They are undefined, the same as anything else that isn't initialized.

> 3) In some cases I get zero value for these elements, while in other
> procedures I get exceptionally large values, and occasionally NaN.

The whole point of undefined is that you don't know what is there. It is
not valid to even look to see. If you do so, different things can happen
with different compilers. It doesn't even need to be consistent.

> 4) If the answer to the subject question is NO, and if it is not
> redundant and/or the problem is somewhere else, then one remedy would
> be to give zero initial value to the entire array via a DATA
> statement, something like:
> ...Data ((X(i,j),i=1,10), j=1,20) /200*0.0/
> or simply by writing the name of the array:
> ...Data X /200*0.0/
>
> But if the array X is declared as X(m,n), then how would the value
> list of the Data statement look like ??
> ...Data X /m*n*0.0/ !syntax error
> ...Data X /(m*n)*0.0/ !syntax error

I suggest looking at a textbook for to find the syntax of the data
statement. That's the kind of thing textbooks are for. I don't feel like
dragging one over right now to check for you. I will tell you that the
syntax for the DATA statement is unusual. Don't try to just guess it, or
you'll get it wrong. It is *NOT* obvious and it is not the same as that
of anything else. You actually have to look it up. Doing so will show
you that the above two forms won't work. You won't find anything that
allows any variant of m*n as a repeat factor. You could probably do a
somewhat awkward workaround by using a separate parameter statement to
define an appropriate named constant for the purpose.

However, I recommend instead using an initializer in the type
declaration statement as in

real :: x(m,n) = 0.0

That's much more straightforward than working around the oddities in the
syntax of the DATA statement. There are a few things that the odd syntax
of DATA allows that can't be conveniently done in other ways. (That
business about initializing isolated elements is the main one). If it
weren't for those things, I'd not use it at all.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: Terence on
On Mar 7, 5:33 pm, monir <mon...(a)mondenet.com> wrote:
> Hello;
>
> 1) An array is declared as:
> ...Real X(10,20)
> and intentionally only a few elements are initialized in the program.
>
> 2) Can one correctly assume that the other elements of the array X are
> set to zero by the compiler (g95) ??
> OR
> Are these elements initially undefined ??
>
> 3) In some cases I get zero value for these elements, while in other
> procedures I get exceptionally large values, and occasionally NaN.
>
> 4) If the answer to the subject question is NO, and if it is not
> redundant and/or the problem is somewhere else, then one remedy would
> be to give zero initial value to the entire array via a DATA
> statement, something like:
> ...Data ((X(i,j),i=1,10), j=1,20) /200*0.0/
> or simply by writing the name of the array:
> ...Data X /200*0.0/
>
> But if the array X is declared as X(m,n), then how would the value
> list of the Data statement look like ??
> ...Data X /m*n*0.0/    !syntax error
> ...Data X /(m*n)*0.0/  !syntax error
>
> Thank you.
> Monir

My answer is: initialize X to zero (which will be economic in code and
time)
Then in the main program start by setting the subsection of of the X
matrix to the value(s) you want, using a DO loop (which will also be
efficient). And if this code is to be used recursively, you need a
local switch to do this once only.
From: monir on
On Mar 7, 6:44 pm, Terence <tbwri...(a)cantv.net> wrote:
> On Mar 7, 5:33 pm, monir <mon...(a)mondenet.com> wrote:
>

1) Couple of points:
First: I failed to mention in the OP that array X(10,20) is a local
variable
Second: Item 1 of the OP should read:
" ... and intentionally only a few nonzero elements are assigned in
the subprogram to generate a sparse matrix."
I hope the above clarifications would answer many of your concerns.

2) Back to my question, and based on your collective replies, it is
reasonable to conclude:
a) do not rely on the compiler to set the initial values of the real
variables to zero

b) the preferred remedy in my case is to use the (F90) array
assignment:
....Real X(10,20)
....X = 0.0
or
....Real :: X(10,20) = 0.0

c) in situations where a DATA statement is appropriate for zero
initialization, X is a local variable, and m & n are parameter
constants, one may use:
....Real X(m,n)
....Integer, parameter :: mn = m*n
....Data X /mn*0.0/

d) if X, m, n are dummy arguments, then one can't use Data statement,
but maybe:
....Real :: X(m,n) = 0.0

e) if X is a local variable and m & n are dummy arguments, then
obviously one can't even declare:
....Real X(m,n)

Thank you ALL; James, Glen, Robin, Gordon, Richard, Terence for your
insights and helpful comments.

Regards.
Monir
From: Richard Maine on
I think you got the other points right, but...

monir <monirg(a)mondenet.com> wrote:

> d) if X, m, n are dummy arguments, then one can't use Data statement,
> but maybe:
> ...Real :: X(m,n) = 0.0

No. If X is a dummy argument, then you can't initialize it at all. That
isn't a matter of syntax, but is more fundamental than that. Fiddling
with different statement forms won't help. The closest you could come
would be default initialization for an intent(out) dummy argument of
derived type, but that's pretty far from your case.

You can use an assignment statement, as in

x = 0.0

but that isn't initialization, as defined by Fortran. It is very
important to understand the distinction. In particular, initialization
happens once, at the first call (or you can think of it that way). If
you want the zeroing to happen on every call, then you don't want
initialization.

> e) if X is a local variable and m & n are dummy arguments, then
> obviously one can't even declare:
> ...Real X(m,n)

Some things that are "obvious" are wrong. :-) Including this.

Yes, you can do that. It is called an automatic array; it is
automatically allocated to the appropriate shape on each call to the
subroutine; it is then deallocated on return.

This feature was new to the standard as of f90, but was a moderately
common extension in later f77 compilers as well.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: robin on
"monir" <monirg(a)mondenet.com> wrote in message news:d2e296f5-92c8-4f23-b6cd-6b574d6d254c(a)f8g2000yqn.googlegroups.com...
On Mar 7, 6:44 pm, Terence <tbwri...(a)cantv.net> wrote:
> On Mar 7, 5:33 pm, monir <mon...(a)mondenet.com> wrote:

>1) Couple of points:
>First: I failed to mention in the OP that array X(10,20) is a local
>variable
>Second: Item 1 of the OP should read:
>" ... and intentionally only a few nonzero elements are assigned in
>the subprogram to generate a sparse matrix."

You haven't generated anything at all until you set all the
remaining values to zero.
Only then will you have a sparse matrix.

>2) Back to my question, and based on your collective replies, it is
>reasonable to conclude:

>a) do not rely on the compiler to set the initial values of the real
>variables to zero

I think that all the responders have answered this unequivocally.
So why have you ignored that advice?
and have come back with the same question?

And it's not just REAL variables.
It applies to ALL variables, including INTEGER, COMPLEX, etc.

>b) the preferred remedy in my case is to use the (F90) array
>assignment:
>...Real X(10,20)
>...X = 0.0
>or
>...Real :: X(10,20) = 0.0

>c) in situations where a DATA statement is appropriate for zero
>initialization, X is a local variable, and m & n are parameter
>constants, one may use:
>...Real X(m,n)
>...Integer, parameter :: mn = m*n
>...Data X /mn*0.0/

Best to avoid a DATA statement for such a trivial task.

>d) if X, m, n are dummy arguments, then one can't use Data statement,
>but maybe:
>...Real :: X(m,n) = 0.0

That's right.

>e) if X is a local variable and m & n are dummy arguments, then
>obviously one can't even declare:
>...Real X(m,n)

Yes you can.