Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

fortran - Subroutine with array element as argument

In my program subroutine stlstp passing work(2,1) as parameter to stlfts(...) subroutine. work(2,1) will be double value at that index, but how subroutine converting it as single dimension array x(n)?

When I print x value in stlfts(...) subroutine, it is printing elements of n size, example:

 STLFTS....X,,,   0.0000000000000000        1.4964418382246345E-317   1.48978578
58438612E-317   1.4964339331743010E-317   1.4964418382246345E-317   4.3367611401 ....

My understanding is, except 1st value, all other values in this x are garbage, but not sure how this assignment will will effect original reference? can someone please help me with this?

subroutine stlstp(y,n,np,ns,nt,nl,isdeg,itdeg,ildeg,nsjump,ntjump,nljump,ni,userw,rw,season,trend,work)

!     implicit none
! Arg
      integer n,np,ns,nt,nl,isdeg,itdeg,ildeg,nsjump,ntjump,nljump,ni
      logical userw
      double precision y(n),rw(n),season(n),trend(n),work(20,5)
! Var
      integer i,j

     do 80 j = 1,1
         do 1 i = 1,n
              work(i,1) = y(i)-trend(i)
 1       continue
         call stlss(y,n,np,ns,isdeg,nsjump,userw,rw,season)

         PRINT *, 'WORK 1,2 ....',work(1,2)
         PRINT *, 'WORK ....',work
         call stlfts(work(1,2),n+2*np,np,work(1,3),work(1,1))

Now stlfts subroutine code is:

  subroutine stlfts(x,n,np,trend,work)
    integer n, np
    double precision x(n), trend(n), work(n)
    PRINT *, 'STLFTS....X,,,',x 
    call stlma(x,    n,      np, trend)
  end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Thanks, to IanH for the direction to the standard (Fortran 2008 12.5.2.11(1)), the answer was wrong in the statement about the standard conformance:

An actual argument represents an element sequence if it is an array expression, an array element designator, a default character scalar, or a scalar of type character with the C character kind (15.2.2). If the actual argument is an array expression, the element sequence consists of the elements in array element order. If the actual argument is an array element designator, the element sequence consists of that array element and each element that follows it in array element order.

This means this praxis is standard conforming and this is a legal way how to pass a part of an array starting from some element.

The situation can be explained to C programmers as the subroutine stlstp passing the address of the actual point in the original array.

The subroutine stlfts then interprets the address as an address of a part of the original array of length n. It will be the first n elements in the column major order starting from the element (1,2). This way it gets access to other elements of the original array WORK and the result will not be garbage.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...