42 Intrinsic data types of variables.. Table 2: Intrinsic data types of variables.. Global Variable Declaration MATLAB globallist of variables savetypetype tag :: list of variables Acces
Trang 1F ortran 90 O verview
J.E Akin, Copyright 1998
This 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 significant improvements over F77 and matches or exceeds newersoftware capabilities found in C++ and Matlab for dynamic memory management, user defined datastructures, matrix operations, operator definition 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
Trang 21 Comment syntax 4
2 Intrinsic data types of variables 4
3 Arithmetic operators 4
4 Relational operators (arithmetic and logical) 5
5 Precedence pecking order 5
6 Colon Operator Syntax and its Applications 5
7 Mathematical functions 6
8 Flow Control Statements 7
9 Basic loop constructs 7
10 IFConstructs 8
11 NestedIFConstructs 8
12 LogicalIF-ELSEConstructs 8
13 LogicalIF-ELSE-IFConstructs 8
14 Case Selection Constructs 9
15 F90 Optional Logic Block Names 9
16 GO TOBreak-out of Nested Loops 9
17 Skip a Single Loop Cycle 10
18 Abort a Single Loop 10
19 F90DOs Named for Control 10
20 Looping While a Condition is True 11
21 Function definitions 11
22 Arguments and return values of subprograms 12
23 Defining 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 Defining New Types of Data Structure 14
30 Nested Data Structure Definitions 14
31 Declaring, initializing, and assigning components of user-defined datatypes 14
32 F90 Derived Type Component Interpretation 15
33 Definition of pointers and accessing their targets 15
34 Nullifing a Pointer to Break Association with Target 15
35 Special Array Characters 15
36 Array Operations in Programming Constructs 16
37 Equivalent Fortran90 and MATLABIntrinsic Functions 17
38 Truncating Numbers 18
39 F90WHEREConstructs 18
40 F90 Array Operators with Logic Mask Control 19
41 Array initialization constructs 20
42 Array initialization constructs 20
3
Trang 343 Elementary matrix computational routines 20
44 Dynamic allocation of arrays and pointers 21
45 Automatic memory management of local scope arrays 21
46 F90 Single Inheritance Form 21
47 F90 Selective Single Inheritance Form 22
48 F90 Single Inheritance Form, with Local Renaming 22
49 F90 Multiple Selective Inheritance with Renaming 22
Trang 4Language Syntax Location
MATLAB % comment (to end of line) anywhere
Table 1: Comment syntax.
aM 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.
bThere is no specific data type for a complex variable in C++; they must be created by the programmer.
Table 2: Intrinsic data types of variables.
Description M ATLABa C++ Fortranb
aWhen 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; figure out the difference between ‘ * ’ and ‘ * ’ Note that since matrix and scalar addition coincide, no ‘ + ’ operator exists (same holds for subtraction).
bFortran 90 allows the user to change operators and to define new operator symbols.
cIn 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.
Trang 5Greater or equal >= >= >= GE.
Logical AND & && AND .AND
Logical not equivalent ˜= != NEQV .NEQV
Table 4: Relational operators (arithmetic and logical).
aUser-defined unary (binary) operators have the highest (lowest) precedence in F90.
bThese are binary operators representing addition and subtraction Unary operators+and-have higher precedence.
Table 5: Precedence pecking order.
B= Beginning,E= Ending,I= Increment
Array element generation no yes
Table 6: Colon Operator Syntax and its Applications.
Trang 6Description M ATLAB C++ F90 F77
raise to power (
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)
arc tangentb atan2(x,y) atan2(x,y) atan2(x,y) atan2(x,y)
hyperbolic arc cosine acosh(x)
hyperbolic arc sine asinh(x)
hyperbolic arctan atanh(x)
Trang 7Description C++ F90 F77 M ATLAB
Loop a specific number of times for k=1:n do k=1,n do # k=1,n for k=1:n
Conditional case selections switch
Table 8: Flow Control Statements.
Indexed loop for index=matrix
Table 9: Basic loop constructs.
Trang 8state-Table 10:IFConstructs The quantityl expressionmeans a logical expression having a value that
is eitherTRUEofFALSE The termtrue statementortrue groupmeans that the statement or group
of statements, respectively, are executed if the conditional in theifstatement evaluates toTRUE
Trang 9Table 14: Case Selection Constructs.
END SELECT name
Table 15: F90 Optional Logic Block Names.
1 END DO
3 next statement
for ( )for ( )
if (disaster)
go to error
Trang 10IF (skip tion) THEN
condi-CYCLE ! to next IELSE
false group
END IFEND DO
for (i=1; i<n; i++)
if (skip condition)continue; // to nextelse if
condi-EXIT ! this doELSE
false group
END IFEND DO
next statement
for (i=1; i<n; i++)
if (exit condition)break;// out of loopelse if
IF (test condition) THENCYCLE test ! loop on kEND IF
END DO third ! loop on m
IF (main condition) THENEXIT main ! forever loopEND DO fourth ! on n
END DO test ! over kEND DO main
next statement
Table 19: F90DOs Named for Control
Trang 11M ATLAB C++
initialize test
true groupchange testend
initialize test
true group change test
go to #END IF
initialize test
true group change test
statements
y = f(a1, ,am)call s(a1, ,am)end program
type a1, ,type am
statements
end
aEvery function or program in M ATLAB must be in separate files.
Table 21: Function definitions In each case, the function being defined is namedfand is called withm
argumentsa1, ,am
Trang 12One-Input, One-Result Procedures
MATLAB function out = name (in)
function name (in) result (out)
Multiple-Input, Multiple-Result Procedures
MATLAB function [inout, out2] = name (in1, in2, inout)
Table 22: Arguments and return values of subprograms.
Global Variable Declaration
MATLAB globallist of variables
savetype(type tag) :: list of variables
Access to Global Variables
MATLAB globallist of variables
F90 useset name,onlysubset of variables
useset name2 list of variables
Table 23: Defining and referring to global variables.
Number of bits in integer sizeof bit size
Transfer bits to integer transfer
Table 24: Bit Function Intrinsics.
Trang 130 NUL 1 SOH 2 STX 3 ETX 4 EOT 5 ENQ 6 ACK 7 BEL
Table 25: The ACSII Character Set.
aOptional arguments not shown.
Table 26: F90 Character Functions.
Action ASCII Character F90 Input a C++ Input
a“Ctrl-” denotes control action That is, simultaneous pressing of theCONTROLkey and the letter following.
Table 27: How to type non-printing characters.
Trang 14C, C++ Variable.component.sub component
Table 28: Referencing Structure Components.
C, C++ struct data tag
;
Table 29: Defining New Types of Data Structure.
C, C++ struct data tag
;
Table 30: Nested Data Structure Definitions.
C, C++ struct data tag variable list; /* Definition */
; /* tion */
Table 31: Declaring, initializing, and assigning components of user-defined datatypes.
Trang 15INTEGER, PARAMETER :: j max = 6
element ofderived
element ofderived
Table 33: Definition of pointers and accessing their targets.
C, C++ pointer name = NULL
Table 34: Nullifing a Pointer to Break Association with Target.
Table 35: Special Array Characters.
Trang 17Table 37: Equivalent Fortran90 and MATLABIntrinsic Functions.
The following KEY symbols are utilized to denote the TYPE of the
in-trinsic 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 MATLABalso have very similar array operations
and colon operators
X
I,V I=MAXLOC(X) [y,i]=max(x) Location(s) of maximum array element
I,V I=MINLOC(X) [y,i]=min(x) Location(s) of minimum array element
call RANDOM SEED rand(’seed’) Initialize random number generator
R RESHAPE(X, (/ I, I2 /)) reshape(x, i, i2) Reshape array X into I I2 array
R SIGN(0.5,X)-SIGN(0.5,-X) sign(x) Signum, normalized sign, –1, 0, or 1
R,Z SQRT(R Z) sqrt(r z) Square root, of real or complex number
(continued)
Trang 18Type Fortran90 M ATLAB Brief Description
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 0–07–000406–4.
MATLAB real(fix) fix real(round) round floor ceil
Table 38: Truncating Numbers.
ELSEWHERE
END WHERE
Table 39: F90WHEREConstructs
Trang 19Function Description Opt Example
all Find if all values are true, for a fixed
(false, true, true)
fixed dimension
(1, 2)
value given by mask
of mask
(0, 7, 3)
dimen-sion, controlled by mask
product(B, DIM = 1, T)(2, 12, 30)
sum Sum all elements, for fixed dimension,
controlled by mask
sum(B, DIM = 2, T)(9, 12)
con-trolled by mask L with elements from the
Trang 20MATLAB C++ F90Pre-allocate
end
% better wayA=12*ones(1,100)
for (j=0; j<100; j++)A[j]=12;
A=12
Pre-allocate
two-dimensional
array
aC++ has a starting subscript of 0, but the argument in the allocation statement is the array’s size.
Table 41: Array initialization constructs.
;
A(1,:)=(/1,7,-2/)A(2,:)=(/3,4,6/)
aOptional in M ATLAB , but improves efficiency.
Table 42: Array initialization constructs.
Addition
C=A+B for (i=0; i<10; i++)
for (j=0; j<10; j++)C[i][j]=A[i][j]+B[i][j];
for (k=0; k<10; k++)C[i][j] += A[i][k]*B[k][j];
aNeither C++ nor F90 have matrix inverse functions as part of their language definitions 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 defined operation, must be used.
Table 43: Elementary matrix computational routines.
Trang 21C++ int* point, vector, matrix
delete matrix
delete vectordelete point
allocate (point)
deallocate (matrix)
deallocate (vector)deallocate (point)
Table 44: Dynamic allocation of arrays and pointers.
IMPLICIT NONEINTEGER, INTENT (IN) :: M,N
Table 45: Automatic memory management of local scope arrays.
use base class name
! new attribute declarations, if any
.contains
! new member definitions
Table 46: F90 Single Inheritance Form.
Trang 22module derived class nameuse base class name, only: list of entities
! new attribute declarations, if any
.contains
! new member definitions
Table 47: F90 Selective Single Inheritance Form.
use base class name, local name = base entity name
! new attribute declarations, if any
.contains
! new member definitions
Table 48: F90 Single Inheritance Form, with Local Renaming.
use base1 class nameuse base2 class nameuse base3 class name, only: list of entitiesuse base4 class name, local name = base entity name
! new attribute declarations, if any
.contains
! new member definitions
Table 49: F90 Multiple Selective Inheritance with Renaming.
... data-page="18">Type Fortran9 0 M ATLAB Brief Description
For more detailed descriptions and example uses of these intrinsic functions see Adams, J.C., et al., Fortran 90 Handbook,...
Table 38: Truncating Numbers.
ELSEWHERE
END WHERE
Table 39: F90WHEREConstructs
Trang 19Trang 20MATLAB C++ F90Pre-allocate
end
% better wayA=12*ones(1,100)
for (j=0; j<100; j++)A[j]=12;