Best way to initialize 2D matrix in place

• From: "Nasser M. Abbasi" <nma@xxxxxxxxx>
• Date: Thu, 12 Jul 2012 01:45:05 -0500

I am trying to come up with the best method to declare
and initialize a 2D matrix in Fortran. But I want
to do that in one place. i.e. Not declare it, then do
the assignment later.

After a bit of experimentation, I found this method,
and wanted to ask the experts if there is an
improvement to it that can be made.

This initializes a 2 by 3 matrix (2 rows, 3 columns)

-------------------
program foo5
implicit none

INTEGER, dimension(2,3)::A = &
transpose(reshape([2,3,4 , &
4,0,7], [3,2]))
print *,shape(A)

end program foo5
-----------------------------

gfortran -std=f2003 -Wextra -Wall -pedantic -Wsurprising -Wconversion foo5.f90
./a.out
2 3

I wanted the initialization to show as the matrix
would appear in the text book.

The above does that. I do not like that I had
to use [3,2] for SHAPE in there, while the matrix A
is actually a (2,3). But found no way around this,
since I need to do a transpose, I had to do this. If
I do not do transpose, the matrix will be stored inside
as

2 4 0
3 4 7

instead of what I want, which is, what I typed

2 3 4
4 0 7

Again, the rules of the game: all must be done in
one place. Which means, one must use reshape() and
shape() as above, right? I did not see another way
so far. I am using gfortran 4.6

thanks,
--Nasser
.

Relevant Pages

• Re: Best way to initialize 2D matrix in place
... and initialize a 2D matrix in Fortran. ... end program foo5 ... I do not do transpose, the matrix will be stored inside ... Don't do this often, but look the optional order argument to the reshape function, ...
(comp.lang.fortran)
• Re: Setting pointer to null!
... I think this debugging fetaure emerged originally ... developer forgets to initialize a variable and it accidentally ... to declare a bunch that would be "initialized" in relatively distant code. ... because you prevent the compiler from initializing them ...
(microsoft.public.vc.language)
• Re: "no variable or argument declarations are necessary."
... the compilor will allert you ... If I forget to declare several variables in C, ... >> forget to initialize several variables in Python, ... > runtime error per "forgot to initialize". ...
(comp.lang.python)
• Re: Replicating results
... gradient calculation used in a large optimization routine. ... It will make sure that if you do something like declare a variable called "x1" and use a variable called "xl" the compiler will tell you that xl was undeclared. ... Make sure that you initialize all of your variables, ...
(comp.lang.fortran)
• Re: question on scope of a variable
... G'day "Chip Orange", ... If I have a std code module and declare at the top of it, ... >to be able to initialize it, or have access to it's value if it's ... >While this compiles for me, I'm getting a runtime error that indicates the ...
(microsoft.public.word.vba.general)