|
[Sponsors] |
Re: which is a better way to loop in fortran? |
![]() |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
![]() |
![]() |
#1 |
Guest
Posts: n/a
|
Tks for your suggestion Tom, but what if i need to multiply by 2 for some values of i, but 10 for some other values. it's something like this:
integer multi(10) for i=1,2,7,9, multi(i)=1 do j=1,10 do i=1,10 if (multi(i)=1) then a(i,j)=2*i*j else a(i,j)=10*i*j end do end do of cos, values of i/j are very small. but in reality, i,j can be 100 or 1000. in this case, will changing the innner loop to a(i,j)=(1-multi(i))*10*i*j + multi(i)*2*i*j be better? i can't think of a better way. for others a(i,j)=10*i*j. Normally, i would use a logical variable multi(10), where multi(1)=.true. for the 1st case, .false. for 2nd case. Here's just an example, but in |
|
![]() |
![]() |
![]() |
#2 |
Guest
Posts: n/a
|
I would have thought that holding the coefficients 2 and 10 in the multi() array and simply using
DO i=1,10 DO j=1,10 a(i,j)=multi(i)*i*j END DO END DO would be better as it avoids the branch statements of your first bit if code and avoids the extra arithmetic of your second bit of code. |
|
![]() |
![]() |
![]() |
#3 |
Guest
Posts: n/a
|
Now that you've gone this far in simplifying
![]() Do j=1,10; Do i=1,10; a(i,j)=multi(i)*j; enddo; enddo Adrin Gharakhani |
|
![]() |
![]() |
![]() |
#4 |
Guest
Posts: n/a
|
and the multiplication can be eliminate completely with
a(:,1)=multi( ![]() DO j=2,10 a(:,j)=a(:,j-1)+multi( ![]() |
|
![]() |
![]() |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
[Gmsh] Problem with Gmsh | nishant_hull | OpenFOAM Meshing & Mesh Conversion | 23 | August 5, 2015 03:09 |
[CAD formats] my stl surface is seen as just a line | rcastilla | OpenFOAM Meshing & Mesh Conversion | 2 | January 6, 2010 02:30 |
NACA0012 geometry/design software needed | Franny | Main CFD Forum | 13 | July 7, 2007 16:57 |
which is a better way to loop in fortran? | zonexo | Main CFD Forum | 7 | June 23, 2006 05:47 |
Big loop problem of fortran | Wen Long | Main CFD Forum | 9 | April 1, 2004 13:38 |