Saturday, 15 September 2012

How can I declare a local 'matrix' inside a subroutine in FORTRAN? -



How can I declare a local 'matrix' inside a subroutine in FORTRAN? -

i writing subroutine inverse matrix. input matrix (n n), output matrix inva. within subroutine, want declare temporary matrix "temp" dimension (n 2n), declaration caused weird problem. after line (that emphasize below) , declare integer numbers , j , initialise them = 0,j = 0. debug these values of , j, shows = 1572472 ! weird!!!!! if remove line of code " real, dimension (m, m * 2) :: temp " fine. can explain me why ?

thanks in advance. (i programmer in .net , learning fortran - drives me crazy!)

program weird implicit none real, dimension (2,2)::b real, dimension (2,2) ::b_inversed b(1,1) = 0.6 b(1,2) = 0.8 b(2,1) = -0.8 b(2,2) = 0.6 phone call inversematrix(b,b_inversed) contains subroutine inversematrix(a, inva) implicit none real, intent(in), dimension (:,:) :: real, intent(out), dimension (size(a,1),size(a,2)) :: inva real, dimension (size(a,1),2*size(a,2)) :: temp <------this line causes problems integer:: i,j = 0 !<------- j = 0 !<-------debug line stops here, showing = 3734648 !very weird!!!!! inva(1,1) =0.0 inva(1,2) =0.0 inva(2,1) =0.0 inva(2,2) =0.0 end subroutine end programme

this straight forwards fortran code, why didn't right value of 'i' ?

since fortran arrays carry info own sizes improve approach start subroutine bit more this:

subroutine inversematrix(a, inva) ! include next line within scoping unit implicit none real, intent(in), dimension (:,:) :: ! no need tell compilers dims real, intent(out), dimension (size(a,1),size(a,2)) :: inva real, dimension (size(a,1),2*size(a,2)) :: temp

to reply question, can't see why code doesn't work , why variable i doesn't maintain value assign it. wonder whether connected interface of subroutine. in modern fortran it's thought ensure, when technically feasible, compiler generates necessary routine interface. 1 way accomplish set routines modules , use-associate them, write code basic structure

program ! declarations ! executable statements contains subroutine ... end subroutine end programme

rather

subroutine ... end subroutine programme ! declarations ! executable statements end programme

but i'm guessing.

use implicit none , ensure compiler generates routine interfaces; these may not cure immediate problem they're general guidelines , save lot of pain in future.

fortran

No comments:

Post a Comment