25.06.2013 Views

Il Linguaggio Fortran 90/95

Il Linguaggio Fortran 90/95

Il Linguaggio Fortran 90/95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3.4 Operazioni globali su array 133<br />

• Soluzione elemento×elemento:<br />

REAL a(20), b(20), c(20)<br />

...<br />

DO i=1,20<br />

a(i) = 0.<br />

END DO<br />

...<br />

DO i=1,20<br />

a(i) = a(i)/3.1 + b(i)*SQRT(c(i))<br />

END DO<br />

• Soluzione globale:<br />

REAL, DIMENSION(20) :: a, b, c<br />

...<br />

a = 0.<br />

...<br />

a = a/3.1 + b*SQRT(c)<br />

Si noti che, nell’esempio in esame, la funzione intrinseca SQRT opera su ciascun elemento<br />

dell’array c.<br />

L’utilità di lavorare con array in forma globale è resa tanto più evidente quanto maggiore<br />

è il rango e meno ”concordi” sono i limiti delle estensioni degli array operandi. Ad esempio,<br />

dichiarati tre array di rango quattro al modo seguente:<br />

REAL, DIMENSION(10,10,21,21) :: x<br />

REAL, DIMENSION(0:9,0:9,-10:10,-10:10) :: y<br />

REAL, DIMENSION(11:20,-9:0,0:20,-20:0) :: z<br />

la semplice istruzione:<br />

x = y+z<br />

è perfettamente equivalente al seguente insieme di cicli DO innestati:<br />

DO i=1,10<br />

DO j=1,10<br />

DO k=1,21<br />

DO l=1,21<br />

x(i,j,k,l) = y(i-1,j-1,k-11,l-11) + z(i+10,j-10,k-1,l-21)<br />

END DO<br />

END DO<br />

END DO<br />

END DO

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!