dot_product函数case a"语法错误"在fortran

时间:2016-03-15 17:08:25

标签: fortran

我是fortran的新手,现在需要在fortran中生成选定行和列的点生成。代码就像(不完整):

    module parameters
implicit none
   REAL, PARAMETER        :: b = 0.99, d = 0.025, a = 0.36
   REAL, PARAMETER        :: klb = 0.01, inc = 0.025, kub = 45.0
   INTEGER, PARAMETER   :: length_grid_k = (kub-klb)/inc + 1
   INTEGER, PARAMETER   :: length_z = 2
   REAL , PARAMETER       :: toler   = 1.e-4             ! Numerical tolerance
   !REAL,DIMENSION(:,:), ALLOCATABLE :: P         ! Trasition matrix of technology process
   !REAL,DIMENSION(:,:), ALLOCATABLE :: y


end module

! ============================================================================================
module global
USE parameters
implicit none ! change from determinstic case: value fun (value and value_new )is a k*z matrix now
   REAL         :: Kgrid(length_grid_k), value(length_grid_k,length_z), g_k(length_grid_k,length_z)
   REAL                 :: vtmp(length_grid_k,length_grid_k), value_new(length_grid_k,length_z)
end module

! ============================================================================================

PROGRAM  HW2Stochastic
   REAL             :: total, etime, dist
   REAL, DIMENSION(2)       :: elapsed
   REAL, DIMENSION(2,2)   :: P
   REAL, DIMENSION(2)   :: y ! all declarations have to precede all executable statements
   P(1,:) = (/0.977, 0.023/)
   P(2,:) = (/0.074, 0.926/)
   y      = (/1.25, 0.2/)


   call solution

   total=etime(elapsed)


    PRINT*,'--------------------------------------------------'
    PRINT*,'total time elpased =',total
    PRINT*,'--------------------------------------------------'


END PROGRAM HW2Stochastic

! ============================================================================================
subroutine solution
USE parameters
USE global

   IMPLICIT  NONE

   INTEGER :: iter, index_k, index_z, index_kp
   REAL    :: diff, k, kp, c , z, y

   INTEGER :: i = 1, l , m


   do while (i<=length_grid_k)   !do loop for assigning capital grid K
     Kgrid(i) = klb + (i-1)*inc
     !write(*,*) i, Kgrid(i)
     i = i + 1
   end do


   iter = 1
   diff = 1000.d0
   ! value = 0.*Kgrid       !Initial Value guess
   do l = 1, length_z ! Intial value guess in stochastic case, a matrix of zeros
      do m = 1, length_grid_k
        value (l,m) = 0
      end do
   end do    

    do while (diff>= toler)

!------------------
! loop over k(t)
!------------------

        do index_k = 1, length_grid_k               ! Capital grid
             k = Kgrid(index_k)
       vtmp(index_k,:) = -1.0e-16 ! initial matrix used to store value fun


!----------------------------
!  (new) loop over z(t)
!----------------------------  

        do index_z = 1, length_z     
             z = y(index_z)

!-------------------
! loop over k(t+1)
!-------------------    
                         do index_kp = 1, length_grid_k
                            kp = Kgrid(index_kp)
                            c = z*k**a+(1.-d)*k-kp ! add technology shock

                                 if (c>0.) then  
                            vtmp(index_k,index_kp) = log(c)+b*dot_product(P(index_z,:),value(:,index_kp)) ! potential bug, 

                            endif

                          end do

                  value_new(index_k,index_z) = MAXVAL(vtmp(index_k,:)) !Determines the maximum value of the elements in an array value
            g_k(index_k,index_z) = Kgrid(MAXLOC(vtmp(index_k,:),1)) !Collect policy function (but no use in this program)

        end do 
    end do                             ! returns the location of the first element in array having the maximum value (in this case, of each row)

        diff  = maxval(abs(value_new-value))/ABS(value_new(length_grid_k,length_z)) ! different from determinstic 
        value = value_new


        print*, 'Iteration =',iter,'sup_norm =',diff
                iter = iter+1

    enddo

    print *, ' '
    print *, 'Successfully converged with sup_norm ', diff
        !print *, g_k

    !CALL vcDrawCurve@(d, Kgrid, g_k, length_grid_k)


        open (UNIT=1,FILE='valuefun',STATUS='replace')
    do index_k = 1, length_grid_k
     do index_z = 1, length_z
            WRITE(UNIT=1,FMT=*) value(index_k,index_z)
     end do
  end do   
    close (UNIT=1)

end subroutine

因此,您可以看到P是一个(lengh_z,length_z)矩阵,而value是一个(length_z,length_grid_k)矩阵。 但是,使用gfortran 4.6.3,我收到以下错误消息:

test.f90:101.84:

                   vtmp(index_k,index_kp) = log(c)+b*dot_product(P(index_z,:),v

我的fortran编译器的版本是gcc-fortran-4.7.2-1-mingw32-bin                                                                                1
    错误:参数列表中的语法错误(1)

enter image description here

1 个答案:

答案 0 :(得分:0)

现在您已经向我们展示了您的代码以及编译器正在抱怨该问题的确切含义。不知何故,您的编译器只能看到发现错误的行的一部分,特别是部分:

               vtmp(index_k,index_kp) = log(c)+b*dot_product(P(index_z,:),v

这不是语法上有效的片段,因此是错误。

很难说为什么编译器只读取该行的前80个字符,因为您还没有向我们展示如何编译它。但是一个解决方案是对压痕不太热心,并将该行左移16个字符。

另一个解决方案是告诉编译器期望更长的行 - 它的文档会告诉你如何。

我确定这是一个重复的问题,但我现在没有时间寻找原件。

并且,正如已经指出的那样,粗略的语法错误不是代码中唯一的错误,它只是提示错误消息的错误。