Skip to content
Prev 308679 / 398503 Next

Trouble returning 2D array into R from Fortran

Your attachments did not make it through to the official R-help mailing list.
Nabble R is not R-help.
I downloaded the files from the links to Nabble.
See further down for my comments.
On 22-10-2012, at 14:24, paulfjbrowne wrote:

            
Butthe code for the geta subroutine is missing.
I commented out the if branch using the geta subroutine.
So all calculations are for dtau=0 and du0=0.
I do not understand what you mean by this.
Your fortran routine declares a Nx2 matrix i.e. a matrix with 2 columns.

The code

	do i=1,k
	   call xypos_parallax(year,ra,dec,ti(i),t0,tE,alpha,u0,piee,pien,y1,y2)
	   y(i,1) = y1
	   y(i,2) = y2
	end do

fills the columns in row i sequentially from row 1 to row k.
Fortran stores arrays column-wise.

In the R function calling your Fortran you declare the output matrix as a matrix with 2 columns.
R also stores matrix in column order.

So you are getting exactly what you have programmed.

Running your code as follows for a short input vector, this is the result (with geta disabled):
[,1]         [,2]
 [1,] -0.118969682 -0.203707575
 [2,] -0.103819120 -0.153139480
 [3,] -0.088668558 -0.102571385
 [4,] -0.073517996 -0.052003290
 [5,] -0.058367434 -0.001435194
 [6,] -0.043216872  0.049132901
 [7,] -0.028066310  0.099700996
 [8,] -0.012915748  0.150269091
 [9,]  0.002234814  0.200837187
[10,]  0.017385376  0.251405282

Would you by any chance mean transposed output like this
[,1]       [,2]        [,3]        [,4]         [,5]        [,6]
[1,] -0.1189697 -0.1038191 -0.08866856 -0.07351800 -0.058367434 -0.04321687
[2,] -0.2037076 -0.1531395 -0.10257138 -0.05200329 -0.001435194  0.04913290
            [,7]        [,8]        [,9]      [,10]
[1,] -0.02806631 -0.01291575 0.002234814 0.01738538
[2,]  0.09970100  0.15026909 0.200837187 0.25140528
As I said above the output is not being stored incorrectly.

Berend