|
[Sponsors] |
Re: which is a better way to loop in fortran? |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
June 24, 2006, 22:56 |
Re: which is a better way to loop in fortran?
|
#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 |
|
June 25, 2006, 10:40 |
Re: which is a better way to loop in fortran?
|
#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. |
|
June 25, 2006, 15:12 |
Re: which is a better way to loop in fortran?
|
#3 |
Guest
Posts: n/a
|
Now that you've gone this far in simplifying you can go one step further and actually assign multi(i) = c*i, where c is your conditional constant (2, 10, etc.). This is an O(N) process. Now you can write the code as
Do j=1,10; Do i=1,10; a(i,j)=multi(i)*j; enddo; enddo Adrin Gharakhani |
|
June 25, 2006, 16:19 |
Re: which is a better way to loop in fortran?
|
#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( END DO |
|
|
|
Similar Threads | ||||
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 |