Dear Guys, I do converting codes from Fortran into R and got stuck in solving LOOPING procedure with R. In FORTRAN, it is (DO and END DO) for looping in the net. In R, it is (FOR with { }). I believe there is something wrong with my coding in R, do hope that you can help me solving following problems. It seems easy, but results are not the same. *************************************************************************** WITH R theta<-c(0.08,0.06,0.09,0) for (i in 1:4){ for (j in 1:4){ a<-1.0 for (k in i:(j-1)){ a<-a*theta[k] } print(a) } -------------------------------------------------------------------------------------- HERE GOES the results ------------------------- numeric(0) [1] 0.08 [1] 0.0048 [1] 0.000432 numeric(0) [1] 0.0048 [1] 0.06 [1] 0.0054 numeric(0) [1] 0.000432 [1] 0.0054 [1] 0.09 numeric(0) [1] 0 [1] 0 [1] 0 ********************************************************************************* IN FORTRAN, the results is totally different. program calculating_a implicit none integer i, j, k double precision a, theta(4) theta(1)=0.06; theta(2)=0.08; theta(3)=0.09; theta(4)=0 do i=1, 4 do j=1, 4 a=1 do k=i,j-1 a=a*theta(k) end do print*, a end do end do end Here goes the results with FOTRAN ------------------------------------------------------------------------------------- 1 0.06 4.79E-003 4.31E-004 1 1 0.08 7.2E-0.03 1 1 1 0.09 1 1 1 1 ----------------------------------------------------------------------------- Thank you! Nam [[alternative HTML version deleted]]
On Aug 30, 2010, at 1:27 AM, Nam Lethanh wrote:> Dear Guys, > > I do converting codes from Fortran into R and got stuck in solving > LOOPING > procedure with R. In FORTRAN, it is (DO and END DO) for looping in > the net. > In R, it is (FOR with { }).Looking at the results (namely the 1's from the fortran code) it appears there are some conditions that cause FORTRAN not to enter the loop whereas in R the inner loop is always entered. The numeric(0)'s occur when you attempt to use a zero as an index. If you have a more specific question about what the indices are at times that surprise, then maybe you ought to put in more debugging code.> > I believe there is something wrong with my coding in R, do hope that > you can > help me solving following problems. > > It seems easy, but results are not the same. > > *************************************************************************** > WITH R > > theta<-c(0.08,0.06,0.09,0) > for (i in 1:4){ > for (j in 1:4){ > a<-1.0 > for (k in i:(j-1)){ > a<-a*theta[k] > } > print(a) > } > -------------------------------------------------------------------------------------- > HERE GOES the results > ------------------------- > numeric(0) > [1] 0.08 > [1] 0.0048 > [1] 0.000432 > numeric(0) > [1] 0.0048 > [1] 0.06 > [1] 0.0054 > numeric(0) > [1] 0.000432 > [1] 0.0054 > [1] 0.09 > numeric(0) > [1] 0 > [1] 0 > [1] 0 > ********************************************************************************* > IN FORTRAN, the results is totally different. > > program calculating_a > implicit none > integer i, j, k > double precision a, theta(4) > theta(1)=0.06; theta(2)=0.08; theta(3)=0.09; theta(4)=0 > do i=1, 4 > do j=1, 4 > a=1 > do k=i,j-1 > a=a*theta(k) > end do > > print*, a > > end do > end do > end > > Here goes the results with FOTRAN > > ------------------------------------------------------------------------------------- > 1 > 0.06 > 4.79E-003 > 4.31E-004 > 1 > 1 > 0.08 > 7.2E-0.03 > 1 > 1 > 1 > 0.09 > 1 > 1 > 1 > 1 > ----------------------------------------------------------------------------- > > > > Thank you! > > > > Nam > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
When j==1 for loops from i down to zero. 5:0 is valid and means c(5,4,3,2,1,0) Hope it helps mario Nam Lethanh wrote:> Dear Guys, > > I do converting codes from Fortran into R and got stuck in solving LOOPING > procedure with R. In FORTRAN, it is (DO and END DO) for looping in the net. > In R, it is (FOR with { }). > > I believe there is something wrong with my coding in R, do hope that you can > help me solving following problems. > > It seems easy, but results are not the same. > > *************************************************************************** > WITH R > > theta<-c(0.08,0.06,0.09,0) > for (i in 1:4){ > for (j in 1:4){ > a<-1.0 > for (k in i:(j-1)){ > a<-a*theta[k] > } > print(a) > } > -------------------------------------------------------------------------------------- > HERE GOES the results > ------------------------- > numeric(0) > [1] 0.08 > [1] 0.0048 > [1] 0.000432 > numeric(0) > [1] 0.0048 > [1] 0.06 > [1] 0.0054 > numeric(0) > [1] 0.000432 > [1] 0.0054 > [1] 0.09 > numeric(0) > [1] 0 > [1] 0 > [1] 0 > ********************************************************************************* > IN FORTRAN, the results is totally different. > > program calculating_a > implicit none > integer i, j, k > double precision a, theta(4) > theta(1)=0.06; theta(2)=0.08; theta(3)=0.09; theta(4)=0 > do i=1, 4 > do j=1, 4 > a=1 > do k=i,j-1 > a=a*theta(k) > end do > > print*, a > > end do > end do > end > > Here goes the results with FOTRAN > > ------------------------------------------------------------------------------------- > 1 > 0.06 > 4.79E-003 > 4.31E-004 > 1 > 1 > 0.08 > 7.2E-0.03 > 1 > 1 > 1 > 0.09 > 1 > 1 > 1 > 1 > ----------------------------------------------------------------------------- > > > > Thank you! > > > > Nam > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Ing. Mario Valle Data Analysis and Visualization Group | http://www.cscs.ch/~mvalle Swiss National Supercomputing Centre (CSCS) | Tel: +41 (91) 610.82.60 v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax: +41 (91) 610.82.82
Hi, I would also like to point out that you flipped the first two the values of theta in the R vs. FORTRAN versions. This fixes part of your differences (and makes it easy to see that the differences occur when j < i, as David and Mario point out). Josh On Sun, Aug 29, 2010 at 10:27 PM, Nam Lethanh <namkyodai at gmail.com> wrote:> Dear Guys, > > I do converting codes from Fortran into R and got stuck in solving LOOPING > procedure with R. In FORTRAN, it is (DO and END DO) for looping in the net. > In R, it is (FOR with { ? }). > > I believe there is something wrong with my coding in R, do hope that you can > help me solving following problems. > > It seems easy, but results are not the same. > > *************************************************************************** > WITH R > > theta<-c(0.08,0.06,0.09,0) > for (i in 1:4){ > for (j in 1:4){ > a<-1.0 > for (k in i:(j-1)){ > a<-a*theta[k] > } > print(a) > } > -------------------------------------------------------------------------------------- > HERE GOES the results > ------------------------- > numeric(0) > [1] 0.08 > [1] 0.0048 > [1] 0.000432 > numeric(0) > [1] 0.0048 > [1] 0.06 > [1] 0.0054 > numeric(0) > [1] 0.000432 > [1] 0.0054 > [1] 0.09 > numeric(0) > [1] 0 > [1] 0 > [1] 0 > ********************************************************************************* > IN FORTRAN, the results is totally different. > > program calculating_a > implicit none > integer i, j, k > double precision a, theta(4) > theta(1)=0.06; theta(2)=0.08; theta(3)=0.09; theta(4)=0 > do i=1, 4 > do j=1, 4 > a=1 > do k=i,j-1 > a=a*theta(k) > end do > > print*, a > > end do > end do > end > > Here goes the results with FOTRAN > > ------------------------------------------------------------------------------------- > 1 > 0.06 > 4.79E-003 > 4.31E-004 > 1 > 1 > 0.08 > 7.2E-0.03 > 1 > 1 > 1 > 0.09 > 1 > 1 > 1 > 1 > ----------------------------------------------------------------------------- > > > > Thank you! > > > > Nam > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
You made a mistake with theta theta<-c(0.08,0.06,0.09,0) This should be (see the fortran) theta<-c(0.06,0.08,0.09,0) The innermost loop (for( k in ...) is better written as while loop to take into account how Fortran handles loops (see the previous replies): k <- i while( k <= j-1 ){ a<-a*theta[k] k <- k + 1 } Berend -- View this message in context: http://r.789695.n4.nabble.com/LOOping-problem-with-R-tp2399596p2399709.html Sent from the R help mailing list archive at Nabble.com.