1

R で Fortran プログラムを使用したいのですが、R プログラムを実行するとエラーが発生します。Fortran コードには、2 次元の REAL 変数があります。fortran のテスト コードは次のようになります: test_inside_program.f90:

program testprogram

implicit none
interface
    SUBROUTINE testm(testvar, thelength)
        IMPLICIT NONE
        REAL, INTENT(IN), DIMENSION(:) :: testvar
        INTEGER, INTENT(IN) :: thelength
    END SUBROUTINE testm
end interface

REAL, DIMENSION(:),ALLOCATABLE :: testvar
INTEGER :: i
allocate(testvar(3))
DO i = 1,2
  testvar(i) = i
ENDDO
call testm(testvar, 3)
write(*,*) 'program finished'
end program testprogram




SUBROUTINE testm(testvar,thelength)
    IMPLICIT NONE
    INTEGER, INTENT(IN) :: thelength
    REAL, INTENT(IN), DIMENSION(:) :: testvar

    write(*,*) 'program cont. X'
    write(*,*)' THe testvar 1st variable is', testvar

END SUBROUTINE testm

R からサブルーチン testm を呼び出したいと思います。もちろん次元を維持したいと思います。したがって、R で次のテスト コードを生成しました: test.r

dyn.load("test_inside_program.so")
is.loaded("testm")

dd <- c(5,2,3)
.Fortran("testm",as.single(dd),as.integer(3))

助けていただければ幸いです。私は.soを生成します

R CMD SHLIB test_inside_program.f90
4

2 に答える 2

1

R はそのような配列を渡す方法がわからないため、ここで想定形状配列を宣言することはできません (データへの単なるポインターではなく、次元と一緒に来る必要があります)。

これはうまくいきます:

subroutine testm(testvar,thelength)
   implicit none
   integer, intent(in) :: thelength
   real, intent(in), dimension(thelength) :: testvar

   write(*,*) 'program cont. x'
   write(*,*) 'length=', thelength
   write(*,*) 'testvar=', testvar
end subroutine testm

また、必要に応じて、プログラムで倍精度配列を宣言し、R から "そのまま" 渡すことを検討することもできます (これは既定の数値型です)。整数の場合、R で 3L を​​直接記述することもできます。したがって、single と double の場合、次のようになります。

.Fortran("testm", as.single(dd), 3L)
.Fortran("testm", dd, 3L)
于 2013-09-13T07:52:20.253 に答える
0

よし、問題解決!fortran のサブルーチン:

SUBROUTINE testm(testvar, thelength)
    IMPLICIT NONE
    INTEGER, INTENT(IN) :: thelength
    REAL, INTENT(IN), DIMENSION(thelength) :: testvar
END SUBROUTINE testm

Rで

dd <- c(5.5,2,3.3)
#length(dd)
print("call fortran routine")
.Fortran("testm",as.single(dd),as.integer(3))

なぜ以前は機能しなかったのかわかりません:o

于 2013-09-13T07:49:07.043 に答える