Fortran 90 overview

Download Fortran 90 overview

Post on 02-Jul-2015

269 views

Category:

Technology

1 download

Embed Size (px)

TRANSCRIPT

<ul><li> 1. 1Fortran 90 OverviewJ.E. Akin, Copyright 1998This overview of Fortran 90 (F90) features is presented as a series of tables that illustrate the syntaxand abilities of F90. Frequently comparisons are made to similar features in the C++ and F77 languagesand to the Matlab environment.These tables show that F90 has signicant improvements over F77 and matches or exceeds newersoftware capabilities found in C++ and Matlab for dynamic memory management, user dened datastructures, matrix operations, operator denition and overloading, intrinsics for vector and parallel pro-cessors and the basic requirements for object-oriented programming.They are intended to serve as a condensed quick reference guide for programming in F90 and forunderstanding programs developed by others.</li></ul><p> 2. List of Tables 1Comment syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . .4 2Intrinsic data types of variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 3Arithmetic operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . .4 4Relational operators (arithmetic and logical) . . . . . . . . . . . . . . . . . . . . . . . .5 5Precedence pecking order . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . .5 6Colon Operator Syntax and its Applications . . . . . . . . . . . . . . . . .. . . . . . .5 7Mathematical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . .6 8Flow Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 9Basic loop constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 10 IF Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 11 Nested IF Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . .8 12 Logical IF-ELSE Constructs . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . .8 13 Logical IF-ELSE-IF Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 14 Case Selection Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 15 F90 Optional Logic Block Names . . . . . . . . . . . . . . . . . . . . . .. . . . . . .9 16 GO TO Break-out of Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 17 Skip a Single Loop Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . 10 18 Abort a Single Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 19 F90 DOs Named for Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 20 Looping While a Condition is True . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 21 Function denitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 22 Arguments and return values of subprograms . . . . . . . . . . . . . . . .. . . . . . . 12 23 Dening and referring to global variables . . . . . . . . . . . . . . . . . . . . . . . . . 12 24 Bit Function Intrinsics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 25 The ACSII Character Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 26 F90 Character Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 27 How to type non-printing characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 28 Referencing Structure Components . . . . . . . . . . . . . . . . . . . . . .. . . . . . . 14 29 Dening New Types of Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 30 Nested Data Structure Denitions . . . . . . . . . . . . . . . . . . . . . .. . . . . . . 14 31 Declaring, initializing, and assigning components of user-dened datatypes. . . . . . . 14 32 F90 Derived Type Component Interpretation . . . . . . . . . . . . . . . . . . . . . . . 15 33 Denition of pointers and accessing their targets . . . . . . . . . . . . . . . . . . . . . . 15 34 Nulling a Pointer to Break Association with Target . . . . . . . . . . . . . . . . . . . . 15 35 Special Array Characters . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . 15 36 Array Operations in Programming Constructs . . . . . . . . . . . . . . . .. . . . . . . 16 37 Equivalent Fortran90 and M ATLAB Intrinsic Functions . . . . . . . . . . .. . . . . . . 17 38 Truncating Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . 18 39 F90 WHERE Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . 18 40 F90 Array Operators with Logic Mask Control . . . . . . . . . . . . . . . . . . . . . . 19 41 Array initialization constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 42 Array initialization constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 3. LIST OF TABLES343 Elementary matrix computational routines . . . . . . .. . . . . . . . . . . . . . . . . . 2044 Dynamic allocation of arrays and pointers . . . . . . . . . . . . . . . . . . . . . . . . . 2145 Automatic memory management of local scope arrays . . . . . . . . . . . . . . . . . . . 2146 F90 Single Inheritance Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2147 F90 Selective Single Inheritance Form . . . . . . . . . . . . . . . . . . . . . . . . . . . 2248 F90 Single Inheritance Form, with Local Renaming . .. . . . . . . . . . . . . . . . . . 2249 F90 Multiple Selective Inheritance with Renaming . .. . . . . . . . . . . . . . . . . . 22 4. 4LIST OF TABLESLanguage SyntaxLocationM ATLAB% comment (to end of line)anywhereC/*comment*/ anywhereF90! comment (to end of line)anywhereF77* comment (to end of line)column 1Table 1: Comment syntax.Storage M ATLAB a C++ F90F77byte charcharacter::characterintegerint integer::integersingle precision float real:: realdouble precision doublereal*8:: double precisionbcomplexcomplex::complexBooleanboollogical::logicalargument parameter::parameterpointer* pointer::structurestructtype:: a M ATLAB 4 requires no variable type declaration; the only two distinct types in M ATLAB are strings and reals (which includecomplex). Booleans are just 0s and 1s treated as reals. M ATLAB 5 allows the user to select more types. b There is no specic data type for a complex variable in C++; they must be created by the programmer. Table 2: Intrinsic data types of variables. Description M ATLAB aC++ Fortranbaddition ++ +subtractionc -- -multiplication * and .* * *division / and ./ / /exponentiation and . powd**remainder %increment ++decrement --parentheses (expres- () ()()sion grouping)a When doing arithmetic operations on matrices in M ATLAB , a period (.) must be put before the operator if scalar arithmeticis desired. Otherwise, M ATLAB assumes matrix operations; gure out the difference between * and .*. Note that since matrixand scalar addition coincide, no .+ operator exists (same holds for subtraction).b Fortran 90 allows the user to change operators and to dene new operator symbols.c In all languages the minus sign is used for negation (i.e., changing sign). d In C++ the exponentiation is calculated by function pow .Table 3: Arithmetic operators. 5. LIST OF TABLES5 DescriptionM ATLAB C++ F90F77 Equal to== ====.EQ. Not equal to= !=/=.NE.Less than =&gt;=.GE. Logical NOT!.NOT. .NOT.Logical AND &amp; &amp;&amp; .AND..AND. Logical inclusive OR!|| .OR. .OR. Logical exclusive ORxor .XOR. .XOR.Logical equivalent== ==.EQV. .EQV. Logical not equivalent = !=.NEQV..NEQV. Table 4: Relational operators (arithmetic and logical).M ATLABC++ OperatorsF90 OperatorsaF77 OperatorsOperators() () [] -&gt; .() ()+ -! ++ -- + ** ** - * &amp; (type) sizeof* /* / % * /* /+ -b + -b+ -b + -b&lt; &gt;=&gt; // //== =&lt; =&gt; == /= &lt; .EQ. .NE. &gt;= .LT. .LE..GT. .GE.== != .NOT..NOT.&amp;&amp;&amp;.AND..AND.|||.OR. .OR.=| .EQV. .NEQV. .EQV. .NEQV. ?: = += -= *= /= %= &amp;= = |= = ,a User-denedunary (binary) operators have the highest (lowest) precedence in F90.b These are binary operators representing addition and subtraction. Unary operators + and - have higher precedence. Table 5: Precedence pecking order. B = Beginning, E = Ending, I = Increment Syntax F90M ATLABUse F90M ATLABDefaultB:E:I B:I:E Array subscript ranges yes yes Character positions in a stringyes yesB B:B: Loop controlno yesE :E:EFull range : : Array element generationno yes Table 6: Colon Operator Syntax and its Applications. 6. 6LIST OF TABLES DescriptionM ATLAB C++F90F77exponentialexp(x)exp(x)exp(x)exp(x)natural loglog(x)log(x)log(x)log(x)base 10 loglog10(x)log10(x)log10(x)log10(x)square rootsqrt(x) sqrt(x) sqrt(x) sqrt(x)raise to power ( ) x.rpow(x,r)x**rx**rabsolute value abs(x)fabs(x) abs(x)abs(x)smallest integer x ceil(x) ceil(x) ceiling(x)largest integer x floor(x)floor(x)floor(x)division remainder rem(x,y)fmod(x,y) mod(x,y) mod(x,y)modulo modulo(x,y)acomplex conjugateconj(z) conjg(z)conjg(z)imaginary part imag(z) imag(z) aimag(z)drop fractionfix(x)aint(x) aint(x)round number round(x)nint(x) nint(x)cosine cos(x)cos(x)cos(x)cos(x)sine sin(x)sin(x)sin(x)sin(x)tangenttan(x)tan(x)tan(x)tan(x)arc cosine acos(x) acos(x) acos(x) acos(x)arc sine asin(x) asin(x) asin(x) asin(x)arc tangentatan(x) atan(x) atan(x) atan(x)arc tangentb atan2(x,y)atan2(x,y)atan2(x,y)atan2(x,y)hyperbolic cosinecosh(x) cosh(x) cosh(x) cosh(x)hyperbolic sinesinh(x) sinh(x) sinh(x) sinh(x)hyperbolic tangent tanh(x) tanh(x) tanh(x) tanh(x)hyperbolic arc cosineacosh(x)hyperbolic arc sineasinh(x)hyperbolic arctanatanh(x) a Differ for. b atan2(x,y)is used to calculate the arc tangent ofin the range . The one-argument function atan(x) computes the arc tangent of in the range.Table 7: Mathematical functions. 7. LIST OF TABLES 7DescriptionC++ F90F77 M ATLAB Conditionally execute statements ifif if if end if end if end Loop a specic number of timesfor k=1:n do k=1,ndo # k=1,nfor k=1:n end do # continue end Loop an indenite number of timeswhiledo while while end do end Terminate and exit loopbreakexitgo to break Skip a cycle of loop continuecyclego to Display message and abort error() stopstoperror Return to invoking function return returnreturn return Conditional array action where if Conditional alternate statements else else elseelse else ifelseif elseifelseif Conditional array alternatives elsewhere else elseif Conditional case selections switch select caseif if end selectend if endTable 8: Flow Control Statements.Loop M ATLABC++Fortran Indexed loop for index=matrix for (init;test;inc) do index=b,e,istatementsstatementsend statements end do Pre-test loopwhile test while (test) do while (test)statements statementsstatementsendend do Post-test loopdo dostatements statementswhile (test)if (test) exitend doTable 9: Basic loop constructs. 8. 8 LIST OF TABLESM ATLAB Fortran C++if l expression IF (l expression) THENif (l expression) true grouptrue groupend END IFtrue group;IF (l expression) true statementif (l expression) true state-ment;Table 10: IF Constructs. The quantity l expression means a logical expression having a value thatis either TRUE of FALSE. The term true statement or true group means that the statement or groupof statements, respectively, are executed if the conditional in the if statement evaluates to TRUE.M ATLABFortranC++if l expression1 IF (l expression1) THEN if (l expression1) true group A true group A if l expression2 IF (l expression2) THEN true group A true group B true group Bif (l expression2) endEND IF true group C true group Ctrue group BendEND IFstatement group Dstatement group Dtrue group C statement group D Table 11: Nested IF Constructs.M ATLABFortranC++if l expressionIF (l expression) THENif (l expression) true group A true group Aelse ELSE true group A false group Bfalse group BendEND IFelsefalse group BTable 12: Logical IF-ELSE Constructs.M ATLABFortranC++if l expression1 IF (l expression1) THENif (l expression1) true group A true group Aelseif l expression2 ELSE IF (l expression2) THEN true group A true group B true group Belseif l expression3 ELSE IF (l expression3) THEN else if (l expression2) true group C true group Celse ELSE true group B default group Ddefault group DendEND IF else if (l expression3)true group Celsedefault group DTable 13: Logical IF-ELSE-IF Constructs. 9. LIST OF TABLES 9F90 C++ SELECT CASE (expression) switch (expression)CASE (value 1)group 1 case value 1 :CASE (value 2) group 1group 2break; . .case value 2 : . group 2CASE (value n) break;group n.CASE DEFAULT . .default group case value n : END SELECTgroup n break;default: default groupbreak; Table 14: Case Selection Constructs.F90 Named IF F90Named SELECT name: IF (logical 1) THENname: SELECT CASE (expression)true group A CASE (value 1) ELSE IF (logical 2) THENgroup 1true group B CASE (value 2) ELSEgroup 2default group CCASE DEFAULT ENDIF namedefault group END SELECT name Table 15: F90 Optional Logic Block Names. Fortran C++ DO 1 ...DO 2 ...for (...) for (...) ......IF (disaster) THEN if (disaster) GO TO 3go to errorEND IF ...... 2 END DO 1 END DO error: 3 next statementTable 16: GO TO Break-out of Nested Loops. This situation can be an exception to the general recom-mendation to avoid GO TO statements. 10. 10LIST OF TABLESF77 F90 C++ DO 1 I = 1,N DO I = 1,Nfor (i=1; in; i++) ......IF (skip condi-IF (skip condi- if (skip condition) tion) THEN tion) THENcontinue; // to nextGO TO 1CYCLE ! to next I else ifELSE ELSE false groupfalse groupfalse group endEND IF END IF 1 continue END DOTable 17: Skip a Single Loop Cycle.F77 F90 C++ DO 1 I = 1,N DO I = 1,Nfor (i=1; in; i++) IF (exit condi-IF (exit condi- tion) THEN tion) THEN if (exit condition)GO TO 2EXIT ! this do break;// out of loopELSE ELSEelse iffalse groupfalse groupfalse groupEND IF END IFend 1 CONTINUE END DO 2 next statement next statementnext statementTable 18: Abort a Single Loop.main: DO ! forever test: DO k=1,k max third: DO m=m max,m min,-1IF (test condition) THENCYCLE test ! loop on kEND IF END DO third ! loop on m fourth: DO n=n min,n max,2IF (main condition) THENEXIT main ! forever loop END DO fourth ! on n END DO test ! over kEND DO mainnext statement Table 19: F90 DOs Named for Control. 11. LIST OF TABLES 11 M ATLAB C++ initialize testinitialize test while l expression while (l expression) true groupchange testtrue group end change test F77F90 initialize testinitialize test # continue do while (l expression)IF (l expression) THEN true grouptrue group change testchange test end dogo to #END IF Table 20: Looping While a Condition is True. FunctionM ATLABa C++ FortranType program statements main(argc,char **argv) program main [y1...yn]=f(a1,...,am) type y [end of file]statementstype a1,...,type amy = f(a1,I,am);statementsy = f(a1,...,am)call s(a1,...,am)end program subroutinevoid f subroutine s(a1,...,am) (type a1,...,type am) type a1,...,type amstatements statements end functionfunction [r1...rn]type f (type a1,...,type am) function f(a1,...,am) =f(a1,...,am)type f statements statements type a1,...,type amstatementsenda Every function or program in M ATLAB must be in separat...</p>

Recommended

View more >