Fortran 90 Overview - Tables

Download Fortran 90 Overview - Tables

Post on 13-Nov-2014

1.475 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

<p>1</p> <p>Fortran 90 OverviewJ.E. Akin, Copyright 1998</p> <p>This overview of Fortran 90 (F90) features is presented as a series of tables that illustrate the syntax and abilities of F90. Frequently comparisons are made to similar features in the C++ and F77 languages and to the Matlab environment. These tables show that F90 has signicant improvements over F77 and matches or exceeds newer software capabilities found in C++ and Matlab for dynamic memory management, user dened data structures, matrix operations, operator denition and overloading, intrinsics for vector and parallel processors and the basic requirements for object-oriented programming. They are intended to serve as a condensed quick reference guide for programming in F90 and for understanding programs developed by others.</p> <p>List of Tables1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 Comment syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Intrinsic data types of variables . . . . . . . . . . . . . . . . . . . . . . . . Arithmetic operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Relational operators (arithmetic and logical) . . . . . . . . . . . . . . . . . Precedence pecking order . . . . . . . . . . . . . . . . . . . . . . . . . . . Colon Operator Syntax and its Applications . . . . . . . . . . . . . . . . . Mathematical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . Flow Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . Basic loop constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . IF Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nested IF Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Logical IF-ELSE Constructs . . . . . . . . . . . . . . . . . . . . . . . . . Logical IF-ELSE-IF Constructs . . . . . . . . . . . . . . . . . . . . . . . Case Selection Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . F90 Optional Logic Block Names . . . . . . . . . . . . . . . . . . . . . . GO TO Break-out of Nested Loops . . . . . . . . . . . . . . . . . . . . . . Skip a Single Loop Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . Abort a Single Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . F90 DOs Named for Control . . . . . . . . . . . . . . . . . . . . . . . . . Looping While a Condition is True . . . . . . . . . . . . . . . . . . . . . . Function denitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arguments and return values of subprograms . . . . . . . . . . . . . . . . Dening and referring to global variables . . . . . . . . . . . . . . . . . . Bit Function Intrinsics . . . . . . . . . . . . . . . . . . . . . . . . . . . . The ACSII Character Set . . . . . . . . . . . . . . . . . . . . . . . . . . . F90 Character Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . How to type non-printing characters . . . . . . . . . . . . . . . . . . . . . Referencing Structure Components . . . . . . . . . . . . . . . . . . . . . . Dening New Types of Data Structure . . . . . . . . . . . . . . . . . . . . Nested Data Structure Denitions . . . . . . . . . . . . . . . . . . . . . . Declaring, initializing, and assigning components of user-dened datatypes F90 Derived Type Component Interpretation . . . . . . . . . . . . . . . . Denition of pointers and accessing their targets . . . . . . . . . . . . . . . Nulling a Pointer to Break Association with Target . . . . . . . . . . . . . Special Array Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . Array Operations in Programming Constructs . . . . . . . . . . . . . . . . Equivalent Fortran90 and M ATLAB Intrinsic Functions . . . . . . . . . . . Truncating Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . F90 WHERE Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . F90 Array Operators with Logic Mask Control . . . . . . . . . . . . . . . Array initialization constructs . . . . . . . . . . . . . . . . . . . . . . . . . Array initialization constructs . . . . . . . . . . . . . . . . . . . . . . . . . 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 4 4 5 5 5 6 7 7 8 8 8 8 9 9 9 10 10 10 11 11 12 12 12 13 13 13 14 14 14 14 15 15 15 15 16 17 18 18 19 20 20</p> <p>LIST OF TABLES43 44 45 46 47 48 49 Elementary matrix computational routines . . . . . . . Dynamic allocation of arrays and pointers . . . . . . . Automatic memory management of local scope arrays . F90 Single Inheritance Form . . . . . . . . . . . . . . F90 Selective Single Inheritance Form . . . . . . . . . F90 Single Inheritance Form, with Local Renaming . . F90 Multiple Selective Inheritance with Renaming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .</p> <p>3 20 21 21 21 22 22 22</p> <p>4</p> <p>LIST OF TABLES</p> <p>Language M ATLAB C F90 F77</p> <p>Syntax% comment (to end of line) /*comment*/ ! comment (to end of line) * comment (to end of line)</p> <p>Location anywhere anywhere anywhere column 1</p> <p>Table 1: Comment syntax.</p> <p>Storage byte integer single precision double precision complex Boolean argument pointer structure</p> <p>M ATLAB a</p> <p>C++char int float doubleb</p> <p>F90character:: integer:: real:: real*8:: complex:: logical:: parameter:: pointer:: type::</p> <p>F77character integer real double precision complex logical parameter</p> <p>bool * struct</p> <p>a M ATLAB 4 requires no variable type declaration; the only two distinct types in M ATLAB are strings and reals (which include complex). 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.</p> <p>Table 2: Intrinsic data types of variables.</p> <p>Description addition subtractionc multiplication division exponentiation remainder increment decrement parentheses (expression grouping)</p> <p>M ATLAB a + * and .* / and ./ and .</p> <p>()</p> <p>C++ + * / powd % ++ -()</p> <p>Fortranb + * / **</p> <p>()</p> <p>a When doing arithmetic operations on matrices in M ATLAB , a period (.) must be put before the operator if scalar arithmetic is desired. Otherwise, M ATLAB assumes matrix operations; gure out the difference between * and .*. Note that since matrix and 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 .</p> <p>Table 3: Arithmetic operators.</p> <p>LIST OF TABLESDescription Equal to Not equal to Less than Less or equal Greater than Greater or equal Logical NOT Logical AND Logical inclusive OR Logical exclusive OR Logical equivalent Logical not equivalent M ATLAB == = &lt; &gt;= &amp; !xor</p> <p>5 C++ == != &lt; &gt;= ! &amp;&amp; || == != F90 == /= &lt; &gt;=.NOT. .AND. .OR. .XOR. .EQV. .NEQV.</p> <p>F77.EQ. .NE. .LT. .LE. .GT. .GE. .NOT. .AND. .OR. .XOR. .EQV. .NEQV.</p> <p>== =</p> <p>Table 4: Relational operators (arithmetic and logical). M ATLAB Operators() + -</p> <p>C++ Operators() [] -&gt; . ! ++ -- + - * &amp; (type) sizeof * / % + -b &gt; &lt; =&gt;</p> <p>F90 Operatorsa() **</p> <p>F77 Operators() **</p> <p>* / + -b &lt; &gt;= == =</p> <p>* / + -b // == /= &lt; &gt;= .NOT. .AND. .OR. .EQV. .NEQV.</p> <p> &amp; | =</p> <p>== != &amp;&amp; || | ?: = += -= *= /= %= &amp;= = |= = ,</p> <p>* / + -b // .EQ. .NE. .LT. .LE. .GT. .GE. .NOT. .AND. .OR. .EQV. .NEQV.</p> <p>a User-dened b These</p> <p>unary (binary) operators have the highest (lowest) precedence in F90. are binary operators representing addition and subtraction. Unary operators + and - have higher precedence.</p> <p>Table 5: Precedence pecking order.B = Beginning, E = Ending, I = Increment</p> <p>Syntax Default</p> <p>F90B:E:I B: :E :</p> <p>M ATLABB:I:E B: :E :</p> <p>B E</p> <p>Full range</p> <p>Use Array subscript ranges Character positions in a string Loop control Array element generation</p> <p>F90 yes yes no no</p> <p>M ATLAB yes yes yes yes</p> <p>Table 6: Colon Operator Syntax and its Applications.</p> <p>6</p> <p>LIST OF TABLES</p> <p>conj(z) imag(z) fix(x) round(x) cos(x) sin(x) tan(x) acos(x) asin(x) atan(x) atan2(x,y) cosh(x) sinh(x) tanh(x) acosh(x) asinh(x) atanh(x)</p> <p>cos(x) sin(x) tan(x) acos(x) asin(x) atan(x) atan2(x,y) cosh(x) sinh(x) tanh(x)</p> <p>Table 7: Mathematical functions.</p> <p>is used to calculate the arc tangent of computes the arc tangent of in the range .</p> <p>b atan2(x,y)</p> <p>in the range</p> <p>a Differ</p> <p>for</p> <p>.</p> <p>Description exponential natural log base 10 log square root raise to power ( ) absolute value smallest integer x largest integer x division remainder modulo complex conjugate imaginary part drop fraction round number cosine sine tangent arc cosine arc sine arc tangent arc tangentb hyperbolic cosine hyperbolic sine hyperbolic tangent hyperbolic arc cosine hyperbolic arc sine hyperbolic arctan</p> <p>M ATLABexp(x) log(x) log10(x) sqrt(x) x.r abs(x) ceil(x) floor(x) rem(x,y)</p> <p>C++exp(x) log(x) log10(x) sqrt(x) pow(x,r) fabs(x) ceil(x) floor(x) fmod(x,y)</p> <p>F90exp(x) log(x) log10(x) sqrt(x) x**r abs(x) ceiling(x) floor(x) mod(x,y) modulo(x,y)a conjg(z) imag(z) aint(x) nint(x) cos(x) sin(x) tan(x) acos(x) asin(x) atan(x) atan2(x,y) cosh(x) sinh(x) tanh(x)</p> <p>F77exp(x) log(x) log10(x) sqrt(x) x**r abs(x)</p> <p>mod(x,y) conjg(z) aimag(z) aint(x) nint(x) cos(x) sin(x) tan(x) acos(x) asin(x) atan(x) atan2(x,y) cosh(x) sinh(x) tanh(x)</p> <p>. The one-argument function atan(x)</p> <p>LIST OF TABLES</p> <p>7</p> <p>Terminate and exit loop Skip a cycle of loop Display message and abort Return to invoking function Conditional array action Conditional alternate statements Conditional array alternatives Conditional case selections</p> <p>break continue error() return</p> <p>else else if</p> <p>switch</p> <p>Table 8: Flow Control Statements.</p> <p>statements</p> <p>Pre-test loop</p> <p>Post-test loop</p> <p>do</p> <p>statements while (test)</p> <p>Table 9: Basic loop constructs.</p> <p>while test statements end</p> <p>while (test) statements</p> <p>Loop Indexed loop</p> <p>M ATLABfor index=matrix statements end</p> <p>Loop an indenite number of times</p> <p>while</p> <p>Loop a specic number of times</p> <p>for k=1:n</p> <p>Description Conditionally execute statements</p> <p>C++if</p> <p>F90if end if do k=1,n end do do while end do exit cycle stop return where else elseif elsewhere</p> <p>F77if end if do # k=1,n # continue</p> <p>M ATLABif end for k=1:n end while end break</p> <p> go to go to stop return</p> <p>error return if else elseif else elseif if end</p> <p>else elseif</p> <p>select case end select</p> <p> if end if</p> <p>C++for (init;test;inc)</p> <p>Fortrando index=b,e,i statements end do</p> <p>do while (test) statements end do do statements if (test) exit end do</p> <p>8 M ATLABif l expression true group end</p> <p>LIST OF TABLESFortran C++if (l expression) true group;</p> <p>IF (l expression) true statement</p> <p>if (l expression) true statement;</p> <p>Table 10: IF Constructs. The quantity l expression means a logical expression having a value that is either TRUE of FALSE. The term true statement or true group means that the statement or group of statements, respectively, are executed if the conditional in the if statement evaluates to TRUE. M ATLABif l expression1 true group A if l expression2 true group B end true group C end statement group D</p> <p>Fortran</p> <p>true group A if (l expression2) true group B true group C statement group D</p> <p>Table 11: Nested IF Constructs. M ATLABif l expression true group A else false group B end</p> <p>Fortran</p> <p>true group A else false group B</p> <p>Table 12: Logical IF-ELSE Constructs. M ATLABif l expression1 true group A elseif l expression2 true group B elseif l expression3 true group C else default group D end</p> <p>Fortran</p> <p>true group A else if (l expression2) true group B else if (l expression3) true group C else default group D</p> <p>Table 13: Logical IF-ELSE-IF Constructs.</p> <p>IF (l expression1) THEN true group A ELSE IF (l expression2) THEN true group B ELSE IF (l expression3) THEN true group C ELSE default group D END IF</p> <p>IF (l expression) THEN true group A ELSE false group B END IF</p> <p>if (l expression)</p> <p>if (l expression1)</p> <p>IF (l expression1) THEN true group A IF (l expression2) THEN true group B END IF true group C END IF statement group D</p> <p>if (l expression1)</p> <p>IF (l expression) THEN true group END IF</p> <p>C++</p> <p>C++</p> <p>C++</p> <p>LIST OF TABLES</p> <p>9</p> <p>F90</p> <p>C++switch (expression)</p> <p>Table 14: Case Selection Constructs.</p> <p>F90 Named IFname: IF (logical 1) THEN true group A ELSE IF (logical 2) THEN true group B ELSE default group C ENDIF name</p> <p>name: SELECT CASE (expression) CASE (value 1) group 1 CASE (value 2) group 2 CASE DEFAULT default group END SELECT name</p> <p>Table 15: F90 Optional Logic Block Names.</p> <p>error:</p> <p>Table 16: GO TO Break-out of Nested Loops. This situation can be an exception to the general recommendation to avoid GO TO statements.</p> <p>DO 1 ... DO 2 ... ... IF (disaster) THEN GO TO 3 END IF ... 2 END DO 1 END DO 3 next statement</p> <p>for (...) for (...) ... if (disaster) go to error ...</p> <p>Fortran</p> <p>SELECT CASE (expression) CASE (value 1) group 1 CASE (value 2) group 2 . . . CASE (value n) group n CASE DEFAULT default group END SELECT</p> <p>case value 1 : group 1 break; case value 2 : group 2 break; . . . case value n : group n break; default: default group break;</p> <p>F90Named SELECT</p> <p>C++</p> <p>10</p> <p>LIST OF TABLES</p> <p>F77DO 1 I = 1,N ... IF (skip condition) THEN GO TO 1 ELSE false group END IF 1 continue</p> <p>F90</p> <p>C++for (i=1; i</p>