Array Intrinsic Functions - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Array Intrinsic Functions

Description:

MINVAL (x) ! The minimum value of all elements of x. MINLOC (x) ! The index ... do i = starti, endi [,increment] block. end do. Do loops can be named. sum = 0.0 ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 24
Provided by: cass76
Category:

less

Transcript and Presenter's Notes

Title: Array Intrinsic Functions


1
Array Intrinsic Functions
SIZE (x) ! The size of x SHAPE (x) ! The shape
of x LBOUND (x,n) ! The nth lower bound of
x UBOUND (x,n) ! The nth upper bound of
x MINVAL (x) ! The minimum value of all elements
of x MINLOC (x) ! The index of the
minimum MAXVAL (x) ! The maximum value of all
elements of x MAXLOC (x) ! The location of the
maximum
2
Array Intrinsic Functions
SUM (x,n) ! The sum of all elements of
x Product (x,n) ! The product of all elements
of x TRANSPOSE (x) ! The transpose of
x DOT_PRODUCT (x, y) ! The dot product of x and
y MATMUL (x, y) ! The matrix multiplication of x
and y
and many more
3
Simple Input/Output
Terminal input and output
real a, b(10), c(2,3) integer i, j
read (,) a
lt--gt read (,) b(1), b(2),, b(10)
read (,) b
lt--gt read (,) c(1,1), c(2,1),
c(1,2), c(2,2),
c(1,3), c(2,3)
read (,) c
lt--gt read (,) c(1,3), c(2,3)
read (,) c(, 3)
4
Simple Input/Output
Terminal input and output
real a, b(10), c(2,3) integer i, j
read (,) a
lt--gt read (,) b(1), b(2),, b(10)
read (,) b
lt--gt read (,) b(1), b(2),, b(5)
read (,) b(15)
lt--gt read (,) b(1), b(2),, b(i)
read (,) i, b(1i)
5
Input/Output
Reading and writing to disk write(20,) a, b
!writes to file fort.20 ! use units numbers gt
6 read(20,) c Opening a file for
input/output open(unit12, filemyfile) write(1
2,) a, b read(12,) a, b close(12) The format
in reading/writing can also be specified write(1
2, (10f8.3, i5)) b, i ! prints 10 floats with 3
digits and 8 ! characters wide, and 1 integer
5 wide
6
Sequences and Conditionals
  • Simple algorithms are just sequences
  • A simple algorithm for controlling the
    temperature could be
  • Read the temperature
  • Turn on the heat
  • Whereas it probably should have been
  • Read the temperature
  • If Temperature is below minimum
  • 2.1 Then turn on the heat
  • 2.2 Else turn off the heat

7
Repeated Instructions
The previous program handled only one value
  • A more flexible one would be
  • Start the program
  • Reserve memory for data
  • Repeat this until end of input
  • 3.1 Read the temperature
  • 3.2 IF. Then.. Else
  • 4. End of Program

8
Control Structures
  • Control constructs
  • 1. do loop
  • 2. if clause
  • 3. case construct
  • 4. go to statement

9
Some Comments on Constructs
the general form is a block construct key
words at beginning and end of each block
Start the program Reserve memory for data Do this
until end of input Read the value of hours,
Convert to seconds Write out the result End
Do End of Program
10
do Construct
Purpose do statement gives the ability to
iterate Form do i starti, endi
,increment block end do
Example sum 0.0 do i 1, n
sum sum a(i) end do
Do loops can be named sum
0.0 Summation do i 1, n
sum sum a(i)
end do Summation
11
do Construct (contd)
do-loops dont need to be bound Use exit
statement to exit loop sum 0.0 do sum
sum 1.0 if (sum .gt. 100.0) exit end do
You can also exit from a bounded do loop
sum 0.0 do i 1,
n sum sum a(i)
if (sum .gt. 100.0) exit
end do
12
do Construct (contd)
naming the loops might have been helpful sum
0.0 i_loop do i 1, n j_loop do j 1, m
sum sum a(j,i) end do j_loop end do
i_loop
do loops can be nested sum 0.0 do i 1,
n do j 1, m sum sum a(j,i) end
do end do
13
do Construct (contd)
Use cycle statement to skip execution of rest
of present iteration sum 0.0 do i 1, n
do j 1, m if (i j) cycle !dont
need elements i j sum sum a(j,i)
end do ! j loop end do !i loop
14
if Statement and Construct
if statement tests logical expression and
executes single statement Form if (scalar
logical expression) action-stmt Example
if (a .gt. b) xa
15
if Statement and Construct
if construct allows the execution more than
one statement, or the execution of alternative
statements depending on alternative conditions.
Form if (scalar logical expression)
then block end if Example if (a .gt. b)
then !exchange a and b cp a a
b bcp endif
16
if Construct (contd)
if then else endif Form if ( scalar
logical expr ) then block1 else block2 endif
17
if Construct (contd)
if then elseif then elseif then else
endif Form if ( scalar logical expr )
then block1 else if ( scalar logical expr )
then block2 else block3 endif
18
Nested if Construct
if constructs can be nested
Example Calculate the square root of x/y
if (y .ne. 0.0) then ! Square root of
x/y if (x/y .ge. 0.0) then sr
sqrt(x/y) else write(,)
Argument is negative endif else
write(,) You are dividing by zero! endif
19
Example Bisection Method
Consider a cubic function
Find the root of f(x) that is accurate within a
specified tolerance value
REPEAT Set x3 (x1 x2) /2. IF f(x3)
f(x1) lt 0. Set x2 x3 ELSE Set x1
x3 ENDIF UNTIL (x1 - x2) lt Tol OR f(x3) 0
20
case Construct
The case construct is very similar to the if
construct. For the case construct only one
expression is evaluated for testing
select case (case-expr) case (1st
case-selector) things that need to
be done for case 1 case (2nd
case-selector) things that need to
be done for case 2 .
case default end select name
case-expression must be a scalar and can be
integer, logical, or character case-selector
must agree with the type of the case-expression
21
case Construct
Example
select case (color) !color is of type
character case (red)
write(,) STOP case (green)
write(,) GO case (yellow)
write(,) Be Careful case default
write(,) Something is wrong. Check
the traffic light end select name
case default selects all the other possible
values of case-expression that are not included
in the other selectors of the construct
22
case Construct
If the case-expression is of type integer, we can
have case (1, 2, 5, 79, 12) ! selects the
values 1, 2, 5, 7, 8 , 9, 12 case (-1)
! all values below 0 case (0)
! only 0 case(1)
! all values above zero
23
goto Statement
Statement go to label Purpose execution of
program jumps to statement label label
Example x y 3.0 goto 2 x x
2.0 2 write(,) x Note A goto statement
must never branch into a block construct, but it
can be used to exit from a block construct.
Write a Comment
User Comments (0)
About PowerShow.com