CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   Re: which is a better way to loop in fortran? (https://www.cfd-online.com/Forums/main/11706-re-better-way-loop-fortran.html)

zonexo June 24, 2006 21:56

Re: which is a better way to loop in fortran?
 
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

HelpfulSoul June 25, 2006 09:40

Re: which is a better way to loop in fortran?
 
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.

Adrin Gharakhani June 25, 2006 14:12

Re: which is a better way to loop in fortran?
 
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

HelpfulSoul June 25, 2006 15:19

Re: which is a better way to loop in fortran?
 
and the multiplication can be eliminate completely with

a(:,1)=multi(:)

DO j=2,10 a(:,j)=a(:,j-1)+multi(:) END DO


All times are GMT -4. The time now is 05:26.