Quote:

> >Hello. Suppose I have declared a two-dimensional array, call it M,

> >like this:

> > parameter(maxn=10000,maxp=50)

> > double precision M(maxn,maxp)

> >Then, I read a data file (into M) which has n rows and p columns.

> >After reading the data file, I know the values of the integers n and

> >p. Here it is assumed that n <= maxn, and p <= maxp. In particular,

> >we are likely to have n < maxn and p < maxp.

> >But, now I want to pass the n * p (sub)matrix to a subroutine (e.g.,

> >least-squares routine [LAPACK]). That is, I don't want to send the

> >10000 by 50 matrix to the subroutine. How can I do this?

> >Intuitively, I'd like to be able to declare a new matrix *after*

> >obtaining knowledge of n and p, but I don't think this is possible.

> ---Use adjustable dimensions makes this easy:

> PROGRAM MAIN

> . . . .

> DOUBLE PRECISION M(maxn,maxp)

> . . .

> read *, n, p

> <<do NOT use array M>>

> call intermediate (M, N, P)

> end

> subroutine intermediate (M, N, P) << Array M now appears as an N x P >>

> DOUBLE PRECISION M(N,P)

> <<create array M of size M, N (e.g. read it in) >>

> READ *, M

> CALL LAPACK (M, N, P)

> end

> subroutine LAPACK (M, N, P)

> DOUBLE PRECISION M (N, P)

> <<use array M of size N x P >>

> END

> By using an intermediate subroutine, the array M appears to be

> of the desired size in subroutines INTERMEDIATE and LAPACK.

> Dynamic arrays is one area in which PL/I has been particularly good.

> In this problem, a working space of 500,000 times 8 = 4Mb of storage

> would be needed, even for the smallest pf problems.

> In PL/I, only the amount of storage actually needed would be

> used.

> Fortran 90 offers a comparable facility.

The suggested solution is, in my opinion, about the worst way to solve this

"problem"; it is what I call "cheat mode", and full of pitfalls. I get the

impression that ++ robin believes that the whole maxn*maxp matrix is somehow

passed to subroutines. In fact, the address of M(1,1) is passed, along with

information from which the subroutine can find where any given element M(i,j)

is. The only clean and safe way to do this is to pass both the set (maxn, maxp)

AND the actually used dimensions, (n,p):

call SUB (M, maxn, maxp, n, p)

...

subroutine SUB (M, maxn, maxp, n, p)

integer maxn, maxp, n, p

double precision M(maxn, maxp)

etc. This gets tedious if you are passing several matrices, each of which

must be assumed to have its own maximum and actual dimensions. Even better

would be switching to F90 and using modules.