从子程序输出中将n维Fortran数组转换为R? [英] get n dimensional Fortran array from subroutine output into R?
问题描述
子程序测试(d,i,nMCd,DF,X)
整数,intent(in):: d,i,nMCd
双精度,intent(in),dimension(i,nMCd):: DF
双精度,intent(out),dimension(i ):: X
X = DF(:,d)+ DF(:,d)
结束子程序测试
我可以编译它以便R载入并运行它。但不是获得一个数组我得到一个单一的数字。
system(R CMD SHLIB ./Fortran/mytest.f90)
可能意味着你想要第一列的总和。请注意,我只是向X提供了一个空的4元素矢量,并且它的Fortran尺寸与DF相同:
dyn.load(./ Fortran / mytest.so)
input < - data.frame(A = c(11,12),B = c(21,22))
.Fortran(test ,d = as.integer(1),i = nrow(输入),nMCd = ncol(输入),DF = unlist(输入),X = as.numeric(1))
$ c $
我的输出看起来像
$ d
[1] 1
$ i
[1] 2
$ nMCd
NULL
$ DF
A1 A2 B1 B2
11 12 21 22
$ X
[1] 22
这个R版本是:
input [,1] + input [,1]
解决方案我还没有弄清楚这是应该做什么的,因为我不用FORTRAN编程(而且你没有说你期望的语言我确实读过),但这是一个实验,它可以在输入对象的第一列中提供项目的总和,当我看到带有输入的代码时会有所帮助。从
> DF(:,d)+中提取
来发送d 1
DF(:,d)
文件中的源代码:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $精度,intent(in),dimension(i,nMCd):: DF
双精度,intent(out),dimension(i,nMCd):: X(i)
X = DF(:,d)+ DF(:,d)
结束子程序测试
R代码:
pre $ input < - data.frame(A = c(11,12),B = c (21,22))
.Fortran(test,d = as.integer(1),i = nrow(输入),nMCd = ncol(输入),
DF =未列出(输入) ,X =数字(4))
#---结果------
$ d
[1] 1
$ i
[1] 2
$ nMCd
[1] 2
$ DF
A1 A2 B1 B2
11 12 21 22
$ X
[1] 22 24 0 0
更进一步实验,仍然没有任何知识Fortran,尝试在第一行中添加项目:
X = DF(d,:)+ DF(d ,:)
制作:
$ X
[1] 22 42 0 0
I have the following Fortran subroutine:
subroutine test(d, i, nMCd, DF, X)
integer, intent(in) :: d, i, nMCd
double precision, intent(in), dimension(i,nMCd) :: DF
double precision, intent(out), dimension(i) :: X
X = DF(:,d)+DF(:,d)
end subroutine test
I am able to compile it for R load it and run it. But instead of getting an array I'm getting a single number.
system("R CMD SHLIB ./Fortran/mytest.f90")
dyn.load("./Fortran/mytest.so")
input <- data.frame(A=c(11,12), B=c(21, 22))
.Fortran("test", d = as.integer(1), i = nrow(input), nMCd = ncol(input), DF = unlist(input), X = as.numeric(1))
What am I doing wrong?!
My output looks like
$d
[1] 1
$i
[1] 2
$nMCd
NULL
$DF
A1 A2 B1 B2
11 12 21 22
$X
[1] 22
The R version of this is:
input[,1]+input[,1]
I haven't figured out what this was supposed to do because I don't program in FORTRAN (And you didn't say what you expected in a language that I do read) but this is an experiment that delivers the sum of the items in the first columns of the input object, which might make some sense when I look at the code with the inputs. It seems possible that sending 1
for d
to extract from DF(:,d)+ DF(:,d)
might mean you wanted the sums of first columns. Notice that I just supplied an empty 4 element vector to X and made its Fortran dimensions the same as DF:
Source in file:
subroutine test(d, i, nMCd, DF, X)
integer, intent(in) :: d, i, nMCd
double precision, intent(in), dimension(i,nMCd) :: DF
double precision, intent(out), dimension(i,nMCd) :: X(i)
X = DF(:,d)+DF(:,d)
end subroutine test
R code:
input <- data.frame(A=c(11,12), B=c(21, 22))
.Fortran("test", d = as.integer(1), i = nrow(input), nMCd = ncol(input),
DF = unlist(input), X = numeric(4))
#--- result------
$d
[1] 1
$i
[1] 2
$nMCd
[1] 2
$DF
A1 A2 B1 B2
11 12 21 22
$X
[1] 22 24 0 0
Further experiment, still without any knowledge of Fortran, trying to add the items in the first row together:
X = DF(d,:)+DF(d,:)
Produced:
$X
[1] 22 42 0 0
这篇关于从子程序输出中将n维Fortran数组转换为R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!