Sie sind auf Seite 1von 22

1

Fortran 90 Overview
J.E. Akin, Copyright 1998

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.

List of Tables
1 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

LIST OF TABLES
43 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3 20 21 21 21 22 22 22

LIST OF TABLES

Language M ATLAB C F90 F77

Syntax
% comment (to end of line) /*comment*/ ! comment (to end of line) * comment (to end of line)

Location anywhere anywhere anywhere column 1

Table 1: Comment syntax.

Storage byte integer single precision double precision complex Boolean argument pointer structure

M ATLAB a

C++
char int float double
b

F90
character:: integer:: real:: real*8:: complex:: logical:: parameter:: pointer:: type::

F77
character integer real double precision complex logical parameter

bool * struct

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.

Table 2: Intrinsic data types of variables.

Description addition subtractionc multiplication division exponentiation remainder increment decrement parentheses (expression grouping)

M ATLAB a + * and .* / and ./ and .

()

C++ + * / powd % ++ -()

Fortranb + * / **

()

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 .

Table 3: Arithmetic operators.

LIST OF TABLES
Description 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 == = < <= > >= & !
xor

5 C++ == != < <= > >= ! && || == != F90 == /= < <= > >=
.NOT. .AND. .OR. .XOR. .EQV. .NEQV.

F77
.EQ. .NE. .LT. .LE. .GT. .GE. .NOT. .AND. .OR. .XOR. .EQV. .NEQV.

== =

Table 4: Relational operators (arithmetic and logical). M ATLAB Operators


() + -

C++ Operators
() [] -> . ! ++ -- + - * & (type) sizeof * / % + -b << >> < <= > =>

F90 Operatorsa
() **

F77 Operators
() **

* / + -b < <= > >= == =

* / + -b // == /= < <= > >= .NOT. .AND. .OR. .EQV. .NEQV.

& | =

== != && || | ?: = += -= *= /= %= &= = |= <<= >>= ,

* / + -b // .EQ. .NE. .LT. .LE. .GT. .GE. .NOT. .AND. .OR. .EQV. .NEQV.

a User-dened b These

unary (binary) operators have the highest (lowest) precedence in F90. 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 Default

F90
B:E:I B: :E :

M ATLAB
B:I:E B: :E :

B E

Full range

Use Array subscript ranges Character positions in a string Loop control Array element generation

F90 yes yes no no

M ATLAB yes yes yes yes

Table 6: Colon Operator Syntax and its Applications.

LIST OF TABLES

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
a Differ

M ATLAB
exp(x) log(x) log10(x) sqrt(x) x.r abs(x) ceil(x) floor(x) rem(x,y) 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)

C++
exp(x) log(x) log10(x) sqrt(x) pow(x,r) fabs(x) ceil(x) floor(x) fmod(x,y)

F90
exp(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)

F77
exp(x) log(x) log10(x) sqrt(x) x**r abs(x)

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)

cos(x) sin(x) tan(x) acos(x) asin(x) atan(x) atan2(x,y) cosh(x) sinh(x) tanh(x)

for

is used to calculate the arc


 tangent 
 of computes the arc tangent of in the range   .
b atan2(x,y)

in the range





  . The one-argument function atan(x)

Table 7: Mathematical functions.

LIST OF TABLES

Description Conditionally execute statements Loop a specic number of times Loop an indenite number of times 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

C++

F90
if end if do k=1,n end do do while end do exit cycle stop return where else elseif elsewhere

F77
if end if do # k=1,n # continue

M ATLAB
if end for k=1:n end while end break

if

for k=1:n

while


go to go to stop return

break continue error() return

error return if else elseif else elseif if end

else else if

else elseif


switch

select case end select


if end if

Table 8: Flow Control Statements.

Loop Indexed loop

M ATLAB
for index=matrix statements end

for (init;test;inc) statements

C++

Fortran
do index=b,e,i statements end do

Pre-test loop

while test statements end

while (test) statements

Post-test loop

do

do while (test) statements end do do statements if (test) exit end do

statements while (test)

Table 9: Basic loop constructs.

8 M ATLAB
if l expression true group end

LIST OF TABLES
Fortran
IF (l expression) THEN true group END IF

if (l expression) true group;

C++

IF (l expression) true statement

if (l expression) true statement;

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 ATLAB
if l expression1 true group A if l expression2 true group B end true group C end statement group D

Fortran
IF (l expression1) THEN true group A IF (l expression2) THEN true group B END IF true group C END IF statement group D

if (l expression1) true group A if (l expression2)

C++

true group B

true group C statement group D

Table 11: Nested IF Constructs. M ATLAB


if l expression true group A else false group B end

Fortran
IF (l expression) THEN true group A ELSE false group B END IF

if (l expression) true group A else false group B

C++

Table 12: Logical IF-ELSE Constructs. M ATLAB


if l expression1 true group A elseif l expression2 true group B elseif l expression3 true group C else default group D end

Fortran
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

if (l expression1) true group A else if (l expression2) true group B else if (l expression3) true group C else default group D

C++

Table 13: Logical IF-ELSE-IF Constructs.

LIST OF TABLES

F90
SELECT CASE (expression) CASE (value 1) group 1 CASE (value 2) group 2 . . . CASE (value n) group n CASE DEFAULT default group END SELECT

switch (expression) case value 1 : group 1 break; case value 2 : group 2 break; . . . case value n : group n break; default: default group break;

C++

Table 14: Case Selection Constructs.

F90 Named IF
name: IF (logical 1) THEN true group A ELSE IF (logical 2) THEN true group B ELSE default group C ENDIF name

F90Named SELECT
name: SELECT CASE (expression) CASE (value 1) group 1 CASE (value 2) group 2 CASE DEFAULT default group END SELECT name

Table 15: F90 Optional Logic Block Names.

Fortran
DO 1 ... DO 2 ... ... IF (disaster) THEN GO TO 3 END IF ... 2 END DO 1 END DO 3 next statement

C++
for (...) for (...) ... if (disaster) go to error ...

error:

Table 16: GO TO Break-out of Nested Loops. This situation can be an exception to the general recommendation to avoid GO TO statements.

10

LIST OF TABLES

F77
DO 1 I = 1,N ... IF (skip condition) THEN GO TO 1 ELSE false group END IF 1 continue

F90
DO I = 1,N ... IF (skip condition) THEN CYCLE ! to next I ELSE false group END IF END DO

for (i=1; i<n; i++) if (skip condition) continue; // to next else if false group end

C++

Table 17: Skip a Single Loop Cycle.

F77
DO 1 I = 1,N IF (exit condition) THEN GO TO 2 ELSE false group END IF 1 CONTINUE 2 next statement

F90
DO I = 1,N IF (exit condition) THEN EXIT ! this do ELSE false group END IF END DO next statement

for (i=1; i<n; i++) if (exit condition) break;// out of loop else if false group end

C++

next statement

Table 18: Abort a Single Loop.

main: DO ! forever test: DO k=1,k max third: DO m=m max,m min,-1 IF (test condition) THEN CYCLE test ! loop on k END IF END DO third ! loop on m fourth: DO n=n min,n max,2 IF (main condition) THEN EXIT main ! forever loop END DO fourth ! on n END DO test ! over k END DO main next statement

Table 19: F90 DOs Named for Control.

LIST OF TABLES

11

M ATLAB
initialize test while l expression true group change test end

C++
initialize test while (l expression) true group change test

F77
initialize test # continue IF (l expression) THEN true group change test go to # END IF

F90
initialize test do while (l expression) true group change test end do

Table 20: Looping While a Condition is True.

Function Type program

M ATLABa

statements main(argc,char **argv) [y1...yn]=f(a1,...,am) [end of file] statements y = f(a1,I,am);

C++

Fortran
program main type y type a1,...,type am statements y = f(a1,...,am) call s(a1,...,am) end program subroutine s(a1,...,am) type a1,...,type am statements end

subroutine

void f (type a1,...,type am) statements

function

function [r1...rn] =f(a1,...,am) statements

type f (type a1,...,type am) statements


function f(a1,...,am) type f type a1,...,type am statements end

a Every

function or program in M ATLAB must be in separate les.

Table 21: Function denitions. In each case, the function being dened is named f and is called with m arguments a1,...,am.

12 One-Input, One-Result Procedures M ATLAB F90 C++


function out = name (in) function name (in) ! name = out function name (in) result (out) name (in, out)

LIST OF TABLES

Multiple-Input, Multiple-Result Procedures M ATLAB F90 C++


function [inout, out2] = name (in1, in2, inout) subroutine name (in1, in2, inout, out2) name(in1, in2, inout, out2)

Table 22: Arguments and return values of subprograms. Global Variable Declaration global list of variables common /set name/ list of variables module set name
save type (type tag) :: list of variables end module set name extern list of variables

M ATLAB F77 F90

C++

Access to Global Variables M ATLAB F77 F90 C++


global list of variables common /set name/ list of variables use set name, only subset of variables use set name2 list of variables extern list of variables

Table 23: Dening and referring to global variables. Action Bitwise AND Bitwise exclusive OR Bitwise exclusive OR Circular bit shift Clear bit Combination of bits Extract bit Logical complement Number of bits in integer Set bit Shift bit left Shift bit right Test on or off Transfer bits to integer C++
&

F90
iand ieor ior ishftc ibclr mvbits ibits not bit size ibset ishft ishft btest transfer

sizeof

Table 24: Bit Function Intrinsics.

LIST OF TABLES
0 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 NUL 1 BS 9 DLE 17 CAN 25 SP 33 ( 41 0 49 8 57 @ 65 H 73 P 81 X 89 97 h 105 p 113 x 121 SOH 2 HT 10 DC1 18 EM 26 ! 34 ) 42 1 50 9 58 A 66 I 74 Q 82 Y 90 a 98 i 106 q 114 y 122 STX 3 NL 11 DC2 19 SUB 27 " 35 * 43 2 51 : 59 B 67 J 75 R 83 Z 91 b 99 j 107 r 115 z 123 ETX VT DC3 ESC # + 3 ; C K S [ c k s { 4 12 20 28 36 44 52 60 68 76 84 92 100 108 116 124 EOT NP DC4 FS $ , 4 < D L T \ d l t | 5 13 21 29 37 45 53 61 69 77 85 93 101 109 117 125 ENQ CR NAK GS % 5 = E M U ] e m u } 6 14 22 30 38 46 54 62 70 78 86 94 102 110 118 126 ACK SO SYN RS & . 6 > F N V f n v 7 15 23 31 39 47 55 63 71 79 87 95 103 111 119 127 BEL SI ETB US / 7 ? G O W _ g o w DEL

13

Table 25: The ACSII Character Set.


ACHAR (I) ADJUSTL (STRING) ADJUSTR (STRING) CHAR (I) IACHAR (C) ICHAR (C) INDEX (STRING, SUBSTRING)a LEN (STRING) LEN TRIM (STRING) LGE (STRING A, STRING B) LGT (STRING A, STRING B) LLE (STRING A, STRING B) LLT (STRING A, STRING B) REPEAT (STRING, NCOPIES) SCAN (STRING, SET)a TRIM (STRING) VERIFY (STRING, SET)a STRING A//STRING B

Character number I in ASCII collating set Adjust left Adjust right Character I in processor collating set Position of C in ASCII collating set Position of C in processor collating set Starting position of a substring Length of a character entity Length without trailing blanks Lexically greater than or equal Lexically greater than Lexically less than or equal Lexically less than Repeated concatenation Scan a string for a character in a set Remove trailing blank characters Verify the set of characters in a string Concatenate two strings

a Optional

arguments not shown.

Table 26: F90 Character Functions. Action Alert (Bell) Backspace Carriage Return End of Transmission Form Feed Horizontal Tab New Line Vertical Tab
a Ctrl-

ASCII Character 7 8 13 4 12 9 10 11

F90 Inputa Ctrl-G Ctrl-H Ctrl-M Ctrl-D Ctrl-L Ctrl-I Ctrl-J Ctrl-K

C++ Input a b r Ctrl-D f t n v


denotes control action. That is, simultaneous pressing of the CONTROL key and the letter following.

Table 27: How to type non-printing characters.

14

LIST OF TABLES

C, C++ F90

Variable.component.sub component Variable%component%sub component

Table 28: Referencing Structure Components.

C, C++

struct data tag intrinsic type 1 component names; intrinsic type 2 component names; ;

F90

type data tag intrinsic type 1 :: component names; intrinsic type 2 :: component names; end type data tag

Table 29: Dening New Types of Data Structure.

C, C++

struct data tag intrinsic type 1 component names; struct tag 2 component names; ;

F90

type data tag intrinsic type :: component names; type (tag 2) :: component names; end type data tag

Table 30: Nested Data Structure Denitions.

C, C++

struct data tag variable list; /* Definition */ struct data tag variable = component values ; /* Initialization */ variable.component.sub component = value; /* Assignment */ type (data tag) :: variable list ! Definition variable = data tag (component values) ! Initialization variable%component%sub component = value ! Assignment

F90

Table 31: Declaring, initializing, and assigning components of user-dened datatypes.

LIST OF TABLES

15

INTEGER, PARAMETER :: j max = 6 TYPE meaning demo INTEGER, PARAMETER :: k max = 9, word = 15 CHARACTER (LEN = word) :: name(k max) END TYPE meaning demo TYPE (meaning demo) derived(j max)

Construct
derived derived(j) derived(j)%name derived%name(k) derived(j)%name(k)

Interpretation All components of all deriveds elements All components of element of derived All k max components of name within element of derived Component k of the name array for all elements of derived Component k of the name array of element of derived

Table 32: F90 Derived Type Component Interpretation.

C++ Declaration Target Examples


type tag *pointer name; &target name char *cp, c; int *ip, i; float *fp, f; cp = & c; ip = & i; fp = & f;

F90
type (type tag), pointer :: pointer name type (type tag), target :: character, pointer :: cp integer, pointer :: ip real, pointer :: fp cp = c ip = i fp = f target name

Table 33: Denition of pointers and accessing their targets.

C, C++ F90 F95

pointer name = NULL nullify (list of pointer names) pointer name = NULL()

Table 34: Nulling a Pointer to Break Association with Target.

Purpose Form subscripts Separates subscripts & elements Generates elements & subscripts Separate commands Forms arrays Continue to new line Indicate comment Suppress printing

F90 () , : ; (/ /) & ! default

M ATLAB () , : ; [] ... % ;

Table 35: Special Array Characters.

16

Equation

     
       
  

     


  








Fortran90 Operator

Matlab Operator

Original Sizes

Result Size

and and

and and

and and

Description Scalar plus scalar Element plus scalar Element plus element Scalar times scalar Element times scalar Element times element Scalar divide scalar Scalar divide element Element divide element Scalar power scalar Element power scalar and
    $ %   " #   ! 
)     * *   *  . $ % + +,  '


(
'

.


and
&

Element power element Matrix transpose Matrix times matrix


)  *  *  $ % , + $ %  0 *   " 1 0 , " ! 

and
$  % * , / +   &*

Vector dot vector

and and

Table 36: Array Operations in Programming Constructs. Lower case letters denote scalars or scalar elements of arrays. Matlab arrays are allowed a maximum of two subscripts while Fortran allows seven. Upper case letters denote matrices or scalar elements of matrices.

LIST OF TABLES

LIST OF TABLES
Table 37: Equivalent Fortran90 and M ATLAB Intrinsic Functions. The following KEY symbols are utilized to denote the TYPE of the intrinsic function, or subroutine, and its arguments: A-complex, integer, or real; I-integer; L-logical; M-mask (logical); R-real; X-real; Y-real; V-vector (rank 1 array); and Z-complex. Optional arguments are not shown. Fortran90 and M ATLAB also have very similar array operations and colon operators. Type A R R R L R L R R R I Z Z R R I R,L R R,Z I R I R R R I,V R I,V R I A call call R R I,V R R R,Z R I R,Z R Fortran90 ABS(A) ACOS(X) AIMAG(Z) AINT(X) ALL(M) ANINT(X) ANY(M) ASIN(X) ATAN(X) ATAN2(Y,X) CEILING(X) CMPLX(X,Y) CONJG(Z) COS(R Z) COSH(X) COUNT(M) DOT PRODUCT(X,Y) EPSILON(X) EXP(R Z) FLOOR(X) HUGE(X) INT(A) LOG(R Z) LOG10(X) MATMUL(X,Y) I=MAXLOC(X) Y=MAXVAL(X) I=MINLOC(X) Y=MINVAL(X) NINT(X) PRODUCT(A) RANDOM NUMBER(X) RANDOM SEED REAL (A) RESHAPE(X, (/ I, I2 /)) SHAPE(X) SIGN(X,Y) SIGN(0.5,X)-SIGN(0.5,-X) SIN(R Z) SINH(X) SIZE(X) SQRT(R Z) SUM(X) M ATLAB abs(a) acos(x) imag(z) real(x(x)) all(m) real(round(x)) any(m) asin(x) atan(x) atan2(y,x) ceil(x) (x+yi) conj(z) cos(r z) cosh(x) sum(m==1) x y eps exp(r z) oor realmax x(a) log(r z) log10(x) x y [y,i]=max(x) y=max(x) [y,i]=min(x) y=min(x) round(x) prod(a) x=rand rand(seed) real(a) reshape(x, i, i2) size(x)

17

sign(x) sin(r z) sinh(x) length(x) sqrt(r z) sum(x) (continued)

Brief Description Absolute value of A. Arc cosine function of real X. Imaginary part of complex number. Truncate X to a real whole number. True if all mask elements, M, are true. Real whole number nearest to X. True if any mask element, M, is true. Arcsine function of real X. Arctangent function of real X. Arctangent for complex number(X, Y). Least integer = real X. Convert real(s) to complex type. Conjugate of complex number Z. Cosine of real or complex argument. Hyperbolic cosine function of real X. Number of true mask, M, elements. Dot product of vectors X and Y. Number, like X, . Exponential of real or complex number. Greatest integer X. Largest number like X. Convert A to integer type. Logarithm of real or complex number. Base 10 logarithm function of real X. Conformable matrix multiplication, X*Y. Location(s) of maximum array element. Value of maximum array element. Location(s) of minimum array element. Value of minimum array element. Integer nearest to real X. Product of array elements. Pseudo-random numbers in . Initialize random number generator. Convert A to real type. Reshape array X into I I2 array. Array (or scalar) shape vector. Absolute value of X times sign of Y. Signum, normalized sign, 1, 0, or 1. Sine of real or complex number. Hyperbolic sine function of real X. Total number of elements in array X. Square root, of real or complex number. Sum of array elements.

18

LIST OF TABLES

Type Fortran90 M ATLAB Brief Description R TAN(X) tan(x) Tangent function of real X. R TANH(X) tanh(x) Hyperbolic tangent function of real X. R TINY(X) realmin Smallest positive number like X. R TRANSPOSE(X) x Matrix transpose of any type matrix. R X=1 x=ones(length(x)) Set all elements to 1. R X=0 x=zero(length(x)) Set all elements to 0. For more detailed descriptions and example uses of these intrinsic functions see Adams, J.C., et al., Fortran 90 Handbook, McGraw-Hill, New York, 1992, ISBN 0070004064.

C++ F90 M ATLAB Argument 2.000 1.999 1.500 1.499 1.000 0.999 0.500 0.499 0.000 0.499 0.500 0.999 1.000 1.499 1.500 1.999 2.000

aint real (x)

int int fix

anint real (round)

nint round

floor floor floor

ceil ceiling ceil

2.0 1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 2.0

2 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 2

Value of Result 2.0 2 2.0 2 2.0 2 1.0 1 1.0 1 1.0 1 1.0 1 0.0 0 0.0 0 0.0 0 1.0 1 1.0 1 1.0 1 1.0 1 2.0 2 2.0 2 2.0 2

2 2 2 2 1 1 1 1 0 0 0 0 1 1 1 1 2

2 1 1 1 1 0 0 0 0 1 1 1 1 2 2 2 2

Table 38: Truncating Numbers.


WHERE (logical array expression) true array assignments ELSEWHERE false array assignments END WHERE WHERE (logical array expression) true array assignment

Table 39: F90 WHERE Constructs.

LIST OF TABLES

19

Function
all any count maxloc maxval merge

Description Find if all values are true, for a xed dimension. Find if any value is true, for a xed dimension. Count number of true elements for a xed dimension. Locate rst element with maximum value given by mask. Max element, for xed dimension, given by mask. Pick true array, A, or false array, B, according to mask, L. Locate rst element with minimum value given by mask. Min element, for xed dimension, given by mask. Pack array, A, into a vector under control of mask. Product of all elements, for xed dimension, controlled by mask. Sum all elements, for xed dimension, controlled by mask.

Opt d d d m b

Example
all(B = A, DIM = 1)

(true, false, false)


any (B 2, DIM = 1)

(false, true, true)


count(A = B, DIM = 2)

(1, 2)
maxloc(A, A

(2, 3) (2, 4, 6)

9) 0)

maxval (B, DIM=1, B merge(A, B, L)

minloc minval pack product

m b v b

minloc(A, A

3)

(2, 2)
minval(B, DIM = 2)

(1, 2)
pack(A, B

(0, 7, 3)

4)

product(B) ; (720) product(B, DIM = 1, T)

(2, 12, 30)


sum

sum(B) ;(21) sum(B, DIM = 2, T)

unpack

Table 40: F90 Array Operators with Logic Mask Control. and denote true and false, respectively. Optional arguments: b -- DIM & MASK, d -- DIM, m -- MASK, v -- VECTOR and DIM = 1 implies for any rows, DIM = 2 for any columns, and DIM = 3 for any plane.


            
     
(9, 12) Replace the true locations in array B controlled by mask L with elements from the vector U.
unpack(U, L, B)

20 M ATLAB Pre-allocate linear array Initialize to a constant value of 12 Pre-allocate two-dimensional array
a C++

LIST OF TABLES
C++
int A[100];
a

F90
integer A(100) A=12

A(100)=0 for j=1:100 % slow A(j)=12 end % better way A=12*ones(1,100) A=ones(10,10)

for (j=0; j<100; j++) A[j]=12;

int A[10][10];

integer A(10,10)

has a starting subscript of 0, but the argument in the allocation statement is the arrays size.

Table 41: Array initialization constructs. Action Dene size Enter rows M ATLAB
A=zeros(2,3) A=[1,7,-2; 3, 4, 6];
a

C++
int A[2][3]; int A[2][3]= 1,7,2 , 3,4,6 ;

F90

integer,dimension(2,3)::A A(1,:)=(/1,7,-2/) A(2,:)=(/3,4,6/)

a Optional

in M ATLAB , but improves efciency.

Table 42: Array initialization constructs.

Addition

  

M ATLAB
C=A+B

Multiplication

C=A*B

Scalar multiplication

C=a*B

Matrix inverse


 

for (i=0; i<10; i++) for (j=0; j<10; j++) C[i][j]=A[i][j]+B[i][j]; for (i=0; i<10; i++) for (j=0; j<10; j++) C[i][j] = 0; for (k=0; k<10; k++) C[i][j] += A[i][k]*B[k][j]; for (i=0; i<10; i++) for (j=0; j < 10; j++) C[i][j] = a*B[i][j];
C++
a

F90
C=A+B

C=matmul(A,B)

C=a*B

B=inv(A)

B=inv(A)a

a Neither C++ nor F90 have matrix inverse functions as part of their language denitions nor as part of standard collections of mathematical functions (like those listed in Table 7). Instead, a special function, usually drawn from a library of numerical functions, or a user dened operation, must be used.

Table 43: Elementary matrix computational routines.

LIST OF TABLES
C++
int* point, vector, matrix ... point = new type tag

21

vector = new type tag [space 1] if (vector == 0) error process matrix = new type tag [space 1 * space 2] ... delete matrix ... delete vector delete point

F90

type tag, pointer, allocatable :: point type tag, allocatable :: vector (:), matrix (:,:) ... allocate (point) allocate (vector (space 1), STAT = my int) if (my int /= 0) error process allocate (matrix (space 1, space 2)) ... deallocate (matrix) if (associated (point, target name)) pointer action... if (allocated (matrix)) matrix action... ... deallocate (vector) deallocate (point)

Table 44: Dynamic allocation of arrays and pointers.


SUBROUTINE AUTO ARRAYS (M,N, OTHER) USE GLOBAL CONSTANTS ! FOR INTEGER K IMPLICIT NONE INTEGER, INTENT (IN) :: M,N type tag, INTENT (OUT) :: OTHER (M,N) ! dummy array ! Automatic array allocations type tag :: FROM USE (K) type tag :: FROM ARG (M) type tag :: FROM MIX (K,N) ... ! Automatic deallocation at end of scope END SUBROUTINE AUTO ARRAYS

Table 45: Automatic memory management of local scope arrays.


module derived class name

use base class name ! new attribute declarations, if any ...


contains

! new member denitions ... end module derived class name

Table 46: F90 Single Inheritance Form.

22

LIST OF TABLES

module derived class name

use base class name, only: list of entities ! new attribute declarations, if any ...
contains

! new member denitions ... end module derived class name

Table 47: F90 Selective Single Inheritance Form.

module derived class name

use base class name, local name = base entity name ! new attribute declarations, if any ...
contains

! new member denitions ... end module derived class name

Table 48: F90 Single Inheritance Form, with Local Renaming.

module derived class name

use base1 class name use base2 class name use base3 class name, only: list of entities use base4 class name, local name = base entity name ! new attribute declarations, if any ...
contains

! new member denitions ... end module derived class name

Table 49: F90 Multiple Selective Inheritance with Renaming.

Das könnte Ihnen auch gefallen