Beruflich Dokumente
Kultur Dokumente
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
Syntax
% comment (to end of line) /*comment*/ ! comment (to end of line) * comment (to end of line)
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.
Description addition subtractionc multiplication division exponentiation remainder increment decrement parentheses (expression grouping)
()
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 .
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.
== =
C++ Operators
() [] -> . ! ++ -- + - * & (type) sizeof * / % + -b << >> < <= > =>
F90 Operatorsa
() **
F77 Operators
() **
& | =
* / + -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.
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
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
in the range
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
else else if
else elseif
switch
if end if
M ATLAB
for index=matrix statements end
C++
Fortran
do index=b,e,i statements end do
Pre-test loop
Post-test loop
do
8 M ATLAB
if l expression true group end
LIST OF TABLES
Fortran
IF (l expression) THEN true group END IF
C++
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
C++
true group B
Fortran
IF (l expression) THEN true group A ELSE false group B END IF
C++
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++
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++
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
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++
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
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
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
M ATLABa
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
function
a Every
Table 21: Function denitions. In each case, the function being dened is named f and is called with m arguments a1,...,am.
LIST OF TABLES
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
C++
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
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
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
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
denotes control action. That is, simultaneous pressing of the CONTROL key and the letter following.
14
LIST OF TABLES
C, C++ F90
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
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
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
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
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
pointer name = NULL nullify (list of pointer names) pointer name = NULL()
Purpose Form subscripts Separates subscripts & elements Generates elements & subscripts Separate commands Forms arrays Continue to new line Indicate comment Suppress printing
M ATLAB () , : ; [] ... % ;
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
&
and
$ % * , / + &*
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
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
nint round
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
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)
(1, 2)
maxloc(A, A
(2, 3) (2, 4, 6)
9) 0)
m b v b
minloc(A, A
3)
(2, 2)
minval(B, DIM = 2)
(1, 2)
pack(A, B
(0, 7, 3)
4)
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)
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
a Optional
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.
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)
22
LIST OF TABLES
use base class name, only: list of entities ! new attribute declarations, if any ...
contains
use base class name, local name = base entity name ! new attribute declarations, if any ...
contains
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