Title: Mixed Expressions
1Mixed Expressions
Expressions with integer and real constants and
variables
---gt INTEGER and REAL is evaluated as REAL
CAUTION (this can be deceptive)
REAL X 3. INTEGER K 5 X X
K/2
K/2 is still an INTEGER expression
2Type Conversions
Several ways can force a type conversion
---gt Intrinsic functions INT, REAL, and COMPLEX
We can also use appropriate constants
X X K / 2.0
X X (K 0.0) / 2
3Mixed Type Assignment
lt real variable gt lt integer expression gt
---gt RHS is evaluated and than converted to REAL
lt integer variable gt lt real expression gt
---gt RHS is evaluated and than truncated to
INTEGER
4Intrinsic Functions
Intrinsic functions are built-in functions that
you can just use
You dont need to declare them ---- just use them
Examples
X REAL (N) N INT (X) Y SQRT (X) Y SQRT
(1.5 SIN(X)2)
5Intrinsic Numeric Functions
REAL(N) ! Converts its argument n to
REAL INT(X) ! Truncates x to INTEGER AINT(X) !
The result remains REAL NINT(X) ! Converts x to
the nearest INTEGER ANINT(X) ! The result
remains REAL ABS(X) ! The absolute value of
the argument MAX(X,Y,) ! The maximum of its
arguments MIN(X,Y,) ! The minimum of its
arguments MOD(X,Y) ! X modulo Y
And many more
6Intrinsic Mathematical Functions
SQRT(X) ! Square root of X EXP(X) ! e raised to
the power X LOG(X) ! Natural logarithm of
X LOG10(X) ! Base 10 logarithm of X SIN(X) !
Sine of X, where X is in radians COS(X) ! Cosine
of X, where X is in radians TAN(X) ! Tangent of
X, where X is in radians ASIN(X) ! Arc sin of X
in radians ACOS(X) ! Arc cosine of X in
radians ATAN(X) ! Arc tangent of X in radians
And many more
7Logical Type
Can only take two values .TRUE. Or .FALSE.
LOGICAL answer
IF (answer) THEN write(,) The answer is
Yes ENDIF
8Relational Operators
Relations create logical values A is
greater than B
This can be .TRUE. Or it can be .FALSE.
Relational .eq. .ne. .lt. .le.
.gt. .ge. / lt lt
gt gt
Can be used on any other built-in types
Can only be used on INTEGER and REAL
9Logical Operators
.NOT. .NOT. x true if x is false .AND. x .AND.
y true when x and y are true .OR. x .OR. y true
if x and/or y are true .NEQV. x .NEQV. y true if
x is true and y is false or if x is false
and y is true .EQV. x .EQV. y true if both x
and y are true or if both x and y are false
.not. A .or. B .and. C
(.not. A) .or. (B .and. C)
10Logical Operators
logical done, answer, dd done
.true. answer .false. dd .not. answer
.and. Done write(,) dd
----gt T
11Character Type
Typical use character (LEN 20)
string string fortran is fun !up to 20
characters fit here write(,) string
Another Example character (LEN 10)
string string fortran is fun write(,)
string -------gt fortran is
12Alternative Form
Alternatively you can declare a character sting
as character astring20, bstring20,
cstring30 astring fortran is fun write(,)
astring
Dont mix the forms! character (len 20)
astring, bstring, cstring30
13Derived Data Types
Derived data types
type satellite character(len 20)
type_of_obs real altitude,
inclination integer id end type
satellite type(satellite) TIMED timedtype_of_
obs Electron Density timedaltitude
450.0 timedinclination 90.0 timedid
5498
14Arrays
Arrays are one of the most useful data structures
in computational physics!
Examples Vectors, Matrices,
15Array Declaration
The DIMENSION attribute declares arrays
For example
REAL, DIMENSION (0 99) DENSITIES
Other examples
INTEGER, DIMENSION (-99 99) arr1, arr2,
arr3
REAL, DIMENSION (1 100, 1 50 ) amatrix
REAL, DIMENSION (05, 27, -34, -113 )
four_dim
16Array Declaration
Lower bounds of one (1) can be omitted
REAL, DIMENSION (1 100) DENSITIES
REAL, DIMENSION (100) DENSITIES
REAL, DIMENSION (-55, 110) TEMPERATURE
REAL, DIMENSION (-55, 10) TEMPERATURE
Its up to your taste what you like to do!
17Alternative Form
Alternatively you can declare an array as REAL
DENSITIES(099), arr1(-55),
month(112) CHARACTER (LEN20) name(10),
city(10)
Dont mix the forms! real, dimension(099)
densities, arr1(-55),
month(112)
18A very useful Declaration
integer, parameter n 20 ! fixes n once
and for all real b(n), cc(n, n)
! declares a vector and a matrix
If you later want to change the bounds of the
arrays ---gtgt you only need to change the value
of n
19Array Terminology
Arrays are organized in dimensions. ---gt
Fortran 90 allows up to 7 dimensions.
REAL X(0 99), Y(3, 36, 5)
The number of dimensions determines the rank. X
has rank 1 and Y has rank 3
Each dimension has an upper and a lower bound. X
has bounds 099 and Y has bounds 13, 36, and 14
The total number of elements in a dimension - its
extend - is calculated by extend
upper_bound - lower_bound 1 X has extend 100
and Y has extents 3, 4, and 5
20Array Terminology
REAL X(0 99), Y(3, 36, 5)
The size of an array is the product of its
extents. X has size 100 and Y has size 60
The shape of an array is its rank and its
extents. X has shape (100) and Y has shape (3, 4,
5)
Arrays are conformable if they have the same
shape --gt The bounds do not have to be the same!!
Arrays Y(3, 36, 5) and A(46, 14, 1115)
are conformable
21Allocatable Arrays
Not always is the size of an array known at the
start of the program We can declare the size
later using the allocate attribute
real, allocatable, dimension() v1, v2
!declares two vectors real, allocatable,
dimension(,) m1, m2 !declares two
matrices allocate(v1(10))
!allocates memory for vector v1 size
10 allocate(v2(099))
!allocates memory for vector v2 size 100
allocate(m1(3, 6), m2(6, 8)) !allocates
memory for the two matrices .. deallocate (v1,
v2, m1, m2) !free memory once arrays
!are not used anymore
22Allocatable Arrays
integer npoints, i real, allocatable,
dimension() v, !declares a
vectors write(,) How many points would you
like to input? read (,) npoints allocate(v(npo
ints)) !allocates memory for vector v
size npoints do i1,npoints read(,)
v(i) end do deallocate(v)
!free memory once vector is not used anymore