Programmers Guide to F - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

Programmers Guide to F

Description:

Declaring and Using Arrays in F ... In F, arrays are declared as follows: real,dimension(1:9) :: x, y ... Write a statement that declares values to be an array of 100 ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 47
Provided by: lecturenot2
Category:

less

Transcript and Presenter's Notes

Title: Programmers Guide to F


1
Programmers Guide to F
Chapter 4 Arrays Version
2.0 Last update Dec 4, 2006
2
Arrays
  • In every day life, as you know, a list is a
    sequence of values.
  • Usually, the values that are in a list represent
    data that have the same data type.
  • In F, a collection of values of the same data
    type is called an array.
  • The same operation or the same sequence of
    operations may be performed on every element in
    an array.

3
Declaring and Using Arrays in F
Suppose that a company has a list of 8262 credit
cards that are reported lost Account number of
1st lost credit card 2718281Account number
of 2nd lost credit card 7389056Account
number of 3rd lost credit card
1098612Account number of 4th lost credit card
5459815Account number of 5th lost credit
card 1484131 ... Account number of 8262nd
lost credit card 1383596 Unless we use an
array, each account number must be assigned to a
variable with a different name.
4
Subscripts
It is possible to use variables with the 8262
names lost_card_1 lost_card_2 lost_card_3 los
t_card_8262 However, this is impractical. Instea
d, we declare a single array lost_ card that will
house all the integer account values for lost
cards lost_card (1) lost_card (2) lost_card
(3)? lost_card (8262)
5
Subscripts (Cont.)?
  • The numbers in parentheses specify the location
    of
  • an item within the array.
  • These numbers are called subscripts, a name
  • borrowed from mathematics.
  • It is customary to read the expression x(3) as
  • "x sub 3".
  • Note that this is also how we would read x3 in
  • mathematical notation.

6
Based on the official text bookProgrammers
Guide to Fby Walter Brainerd, et al.
  • BIL 106E
  • Introduction to Programming in F
  • Lecture Notes
  • Chapter 4, version 1.8
  • Last Update Nov 27, 2006

7
Subscripts (Cont.)?
The subscript of an array variable may itself be
a variable, or an even more complicated
expression. i 12 print , lost_card
(i)? produce exactly the same output as the
single statement print , lost_card (12)?
8
Subscripts (Cont.)?
The entire list of account numbers of lost credit
cards can be printed by the following
subroutine. subroutine print_lost_cards(lost_card)
? integer, dimension (), intent
(in)lost_card !The above will be explained
later.integer i do i 1, 8262 print ,
lost_card (i)end do end subroutine
print_lost_cards ! Instead of 8262 may use size
(lost_card). ! do i 1, size(lost_card)?
9
Array Declarations
  • The name of an array must obey the same rules as
  • an ordinary variable name.
  • Each array must be declared in the declaration
    section of
  • program, module, or procedure.
  • In F, arrays are declared as follows
  • real,dimension(19) x, y
    logical,dimension(-99100, 05) tuncay
  • real,dimension(3)a
  • real,dimension(3, -15)b
  • x and y are 1-D (one-dimensional) arrays of 9
    real values.
  • tuncay is a 2-D array of 1200 logical values.
  • a is a 1-D array of 3 real values.
  • b is a 2-D array of 21 elements, which have
    real values.

10
Array Declarations (Summary)?
  • real, dimension(3,2) s
  • s is a 2-D array with 3 elements in its first
    dimension and 2
  • elements in its second dimension. That is, s is
    a 3x2 matrix.
  • It has 3 rows and 2 columns.
  • The subscripts of s are as follows
  • s(1,1) s(1,2)?
  • s(2,1) s(2,2)?
  • s(3,1) s(3,2)?
  • real, dimension(02,89) r
  • r is a 2-D array which is conformable to s.
  • The subscripts of s are as follows
  • r(0,8) r(0,9)?
  • r(1,8) r(1,9)?
  • r(2,8) r(2,9)?

11
Array Declarations (Cont.)?
  • An array of character strings may be declared
    as
  • character(len 8),dimension(017)char_list
  • The variable char_list is an array of 18
    character strings,
  • each with length 8.
  • When char_list is used as a dummy argument, its
    length
  • must be and its subscript must be .
  • character(len),dimension(),
    intent(in out)char_list
  • This is because and are wild cards
    (joker in Turkish)
  • that give the dummy argument flexibility.
  • Dummy arguments, afterall, are used in place of
    actual
  • arguments of different lengths and sizes.

12
The shape of an array
  • The shape of an array is a list consisting of the
    number of
  • elements in each dimension. Eg If a1 is
    declared as follows
  • real, dimension(2,3) a1
  • !then
  • shape(a1) is (/2,3/)
  • a1 has 2 elements in its first dimension and 3
    elements
  • in its second dimension. It has 2 rows and 3
    columns.

13
The shape of an array
  • In F, an array can have at most 7 dimensions.
  • Eg
  • real, dimension(2,2,2,2,2,3,2) a2, a3
  • Conformable arrays If two arrays have the same
    shape,
  • they are said to CONFORM to each other.
  • a2 a2 a3
  • is a legal F statement because a3 is
    conformable to a2.
  • Note that a scalar is considered to be
    conformable to all arrays.
  • Therefore,
  • a2 5.99
  • is a legal F statement, which makes all the
    elements of a2
  • equal to 5.99.
  • Furthermore,
  • a3 10a3 a2 0.4
  • is also a legal F statement.

14
The size of an array
  • The size of an array is a the total number of
  • elements in the array. Eg
  • real,dimension(2,3) a1
  • So, size(a1) is 6.
  • Likewise, size(a2) 2222232 192
  • because recall that
  • real,dimension(2,2,2,2,2,3,2)a2
  • Note that size and shape are intrinsic functions
    in F.

15
Array Constructors
  • An array constructor is a sequence of scalar
    values defined along
  • one dimension only.
  • An array constructor is a list of values
    separated by
  • commas and delimited by "(/" and "/)".
  • There are 3 possible forms for array constructor
    values
  • A scalar expression as in
  • x(14) (/ 1.2, 3.5, 1.1, 1.5 /)?
  • An implied do loop as in
  • x(14) (/ (sqrt(real(i)),i1,4)/)?
  • An array expression as in
  • x(14) (/ a1(2,23),a1(1,132) /)?
  • The expression above has two array sections on
    the right hand
  • side, which will be explained later.

16
Array Constructors (Cont.)?
  • The rank of an array constructor is always one
    however, the
  • reshape intrinsic function can be used to define
    rank-2 to rank-7
  • arrays from a given array constructor.
  • For example,
  • reshape ((/ 1, 2, 3, 4, 5, 6 /),(/ 2, 3 /))?
  • is the 2x3 array

17
Array Sections
It is possible to refer to a selected portion of
an array, called an array section. The syntactic
form of a subscript triplet notation
is expression expression
expression where each set of brackets encloses
an optional item, and each expression must
produce a scalar integer value. The first
expression gives a lower bound, the second an
upper bound, and the third a stride. See the
next slide, for example.
18
Array Sections (cont.)?
Here are some examples integer,dimension(06),pa
rameter v (/ 3,9,0,-2,2,8,-1
/)? c v(25)? is the same as c
(/0,-2,2,8/)? real, dimension(2,3) a1 a1(1,)
(/ 5, 0, 8 /)? a1(2,) (/ 2, 1, 4 /)? x (14)
(/ a1(2,23),a1(1,132) /)? is the same
as x(14) (/ 1,4,5,8 /)?
19
Array Sections (cont.)?
Another way of selecting a section of an array is
to use a vector subscript. A vector subscript
is an integer array expression of rank
one. Ordinary subscripts, triplets, and vector
subscripts may be mixed in selecting an array
section from a parent array. An array section
may be empty.
20
Array Sections (cont.)?
To give an example using both the triplet
notation and a vector subscript, suppose that d,e
are declared as integer, dimension(5) d,
e d(/ 3, 5, 6,-5, 0 /)? e(/
2, 1,-3, 8,-4 /)? Then d(152) e(/
4,1,3 /) would do the following
assignments d(1) e(4)?
d(3) e(1)? d(5) e(3) therefore
d (/ 8, 5, 2, -5, -3 /)

21
Array Assignment
  • Array Assignment is permitted under two
    circumstances
  • a) when the array expression on the right has
    exactly the same
  • shape as the array on the left
  • or
  • b) when the expression on the right is a
    scalar.
  • When a scalar is assigned to an array, the value
    of the scalar is
  • assigned to every element of the array
  • k(35,2) 10.2 sets the elements
  • k(3,2)?
  • k(4,2)
  • k(5,2) to 10.2

22
The where construct
  • The where construct may be used to assign values
    only to
  • those elements of an array where a logical
    condition is true.
  • The elsewhere statement within a where construct
    permits
  • array assignments to be done where the logical
    expression is
  • false.
  • integer, dimension(5) a,b,c
  • ! Note that a, b and c do conform to each other.
  • ! Otherwise, the where construct would give
  • ! a compiler error.
  • c (/ 0, 2, 6, 5, 0 /)?
  • b (/ 0, 0, 4, 6, 9 /)?
  • where (c / 0) ! c/0 is a logical array.
  • a b / c
  • elsewhere
  • a 0 ! The elements of a are set to 0
  • ! where they have not been set to b/c.
  • c 1 ! The zeros of c are set to 1 here.
  • end where

23
The where construct (cont.)?
! Here is how the where construct works. c (/
0, 2, 6, 5, 0 /)? b (/ 0, 0, 4, 6, 9 /)? where
(c / 0) a b / c ! a (/ ?, 0, 0, 1,
? /)? elsewhere a 0 ! a (/ 0,
0, 0, 1, 0 /)? c 1 ! c (/ 1,
2, 6, 5, 1 /)? end where
24
Dynamic Arrays
  • Dynamic Storage Allocation means that storage may
    be allocated or deallocated for variables
    during the execution of the program (i.e., run
    time) and NOT during compile time.
  • It is not unusual that the size of an array be
    unknown during compile time and that it will be
    known LATER, during run time.
  • With Dynamic Storage Allocation, the program can
    wait until it knows during run time exactly
    what size of an array is needed and then allocate
    only that much space.

25
Dynamic Arrays (cont.)?
The allocatable attribute indicates that the
array is to be allocated dynamically. During
execution, the system must be able to create an
array of correct size after reading the value of
the variable number_of_lost_ cards. The
deallocate statement may be used to free the
allocated storage.
26
Dynamic Arrays (cont.)?
program cards integer, dimension (), allocatable
lost_cardinteger number_of_lost_cardsinteg
er allocation_status ... print , Please
enter number_of_lost_cardsread ,
number_of_lost_cardsallocate(lost_card(number_of_
lost_cards), stat allocation_status)? if
(allocation_status gt 0) then print ,
"Allocation error" stopend if ! Read the
account numbers of lost cards ! When finished,
deallocate(lost_card). ... end program cards
27
Intrinsic Operators Functions Applied to Arrays
  • All of the intrinsic operators and many of the
    intrinsic
  • functions may be applied to arrays, operating
    independently
  • on each element of the array.
  • Eg The intrinsic operator / seen in the
    assignment
  • statement below
  • a(3,25) a(3,25)/9.35
  • divides each element of a(3,25) by the
    real scalar
  • constant 9.35.

28
maxloc and minloc intrinsic functions
The intrinsic function maxloc returns a list of
integers giving the position of the largest
element of an array. There is also an intrinsic
function, minloc, whose value is the list of
subscripts of the smallest element of an
array. maxloc(a) is (/3,3/) minloc(a)
is (/3,2/)
29
The random_number intrinsic subroutine
Let x be a real scalar variable. real x A
scalar is just a number as opposed to an array,
which could be a list of scalars (among other
things). When you call
random_number(x)? this intrinsic subroutine
returns a pseudo-random real number x where 0.0
x lt 1.0 We say pseudo-random instead of
random here because computer programs can never
actually produce numbers that are 100 random.
30
The random_number intrinsic subroutine (cont.)?
Now, let B be a real array variable. real,
dimension(1000) B When you call
random_number(B)? it returns a real array B,
which is an array of 1000 pseudo-random real
scalars B(i)? where 0.0 B(i) lt 1.0 for i
1, 2, 3, ..., 1000
31
Exercise 1
Incorporating arrays into subroutine
random_int (see page 124) write a program that
determines by simulation the probability of
getting 8 or 12 when two fair dice are rolled.
The name of the new subroutine should be
random_int_array.
32
module random_int_......... public
random_int........ contains subroutine
random_int_.......(VALUES, low, high)?
integer, dimension (...), intent (out) VALUES
integer, intent (in) ...., high real,
dimension (size (........)) B call
random_number(B) VALUES int ((high - low
1) B low) end subroutine random_int_array
end module random_int_module program ....... !
This program calculates the !probability of
! getting an 8 or a 12 when two fair !dice are
rolled. use random_int_....... integer,
parameter number_of_rolls ..... integer,
dimension(number_of_rolls) dice, die_1, die_2
integer wins call random_int_array(die_1, 1,
6) call ............................. dice
die_1 ....... wins count((dice
...).or.(..........)) print , The answer
is,.....(wins) / .......... end program
simulation1
33
module random_int_module public
random_int_array contains subroutine
random_int_array(VALUES, low, high)? integer,
dimension (), intent (out) VALUES
integer, intent (in) low, high real,
dimension (size (VALUES)) B call
random_number(B) VALUES int ((high - low
1) B low) end subroutine random_int_array
end module random_int_module program
simulation1 !This program calculates the
!probability of ! getting an 8 or a 12 when two
fair !dice are rolled. use random_int_module
integer, parameter number_of_rolls
1000 integer, dimension(number_of_rolls) dice,
die_1, die_2 integer wins call
random_int_array(die_1, 1, 6) call
random_int_array(die_2, 1, 6) dice die_1
die_2 wins count((dice 8).or.(dice12))
print , The answer is,real(wins) /
number_of_rolls end program simulation1
34
module random_int_module public
random_int_array contains subroutine
random_int_array(VALUES, low, high)? integer,
dimension (), intent (out) VALUES
integer, intent (in) low, high real,
dimension (size (VALUES)) B call
random_number(B) VALUES int ((high - low
1) B low) end subroutine random_int_array
end module random_int_module program
YAZI_TURA use random_int_module integer,
parameter number_of_rolls 1000 integer,
dimension(40,number_of_rolls) sonuc integer
wins, i do i1, 40 call random_int_array(sonuc
(i,), 1, 2)!YAZI1, TURA2 end do wins count
(count((sonuc 1)24)) print , The answer
is,real(wins) / number_of_rolls end program
YAZI_TURA
35
Element Renumbering
The elements in an expression no longer have the
same subscripts as the corresponding elements in
the arrays that they originate from. They are
renumbered with 1 as the lower bound in each
dimension. Eg c(4)10 c(9)11 !After the
assignment of b c(49)? !b(1) is 10
and b(6) is 11.
36
Element Renumbering (cont.)?
  • The following is a difficult example on the same
    topic
  • integer,dimension(06),parameter
  • v (/ 3,9,0,-2,2,8,-1 /)?
  • Note that maxloc(v) is (/2/) even though v(1)
  • is the maximum element of v.
  • Also, maxloc(v(26)) is (/4/)
  • because the largest entry of v(26), which is
    8,
  • appears in the fourth position of v(26).

37
Exercise 2
  • Write a statement that declares values to be an
    array of 100
  • real values with subscripts ranging from -100 to
    -1.
  • Use an array constructor to assign the squares of
    the first 100 positive integers to an array of
    integers whose name is squares.
  • Eg squares (5) 25.

38
Searching a List
  • The problem is that of checking a given number
    against a list of
  • numbers.
  • There is more than one way to do this.

39
Sequential Search Strategy
The first and simplest strategy for checking a
given number is simply to search from beginning
to end through the list of numbers, number by
number, until the given number is found in the
list, or until the end of the list is reached
without finding the desired number. This is
called sequential search.
40
Sequential Search (cont.)?
subroutine sequential_search(lost_card,
card_number, found,
sira)? intrinsic size integer, dimension(),
intent(in) lost_card integer, intent(in)
card_number logical, intent(out) found integer
i, sira found .false.do i 1, size
(lost_card)? if (lost_card(i) card_number)
then found .true. sira i
exit end ifend do end subroutine
sequential_search
41
Array Processing
  • Subroutine sequential_search makes a nice example
    for
  • illustrating how individual elements of an
    array can be manipulated
  • sequentially using a conventional sequential
    computer, like a PC.
  • However, in F, it is often better to choose
    operations for processing
  • the array as a whole.
  • In fact, using intrinsic array functions, it is
    possible to do the
  • search using only one statement.
  • foundany(lost_card(1size(lost_card))card_n
    umber)?
  • When parallel processors are used, the statement
    above is
  • performed not sequentially, but in parallel.
    Thus, one may get a speed-up close to the number
    of parallel processors.

42
Array Processing (cont.)?
  • This is correct because the comparison
  • lost_card(1size(lost_card))
    card_number
  • creates an array of logical values with .true.
    in any position
  • where the value of card_number matches a
    number in the array
  • lost_card.
  • You could distribute the array lost_card on
    multiple processors,
  • and the processor that finds a .true. value
    would return the
  • answer immediately.
  • Since that processor has only a small part of
    the whole
  • lost_card array, it is for sure that the result
    will be found at an
  • earlier time than when a sequential PC is used.
  • Note that we shall not go into parallel
    processing any deeper
  • than this.

43
Sorting a List
  • Frequently it is necessary to sort a list of
    numbers
  • (or character strings.)
  • There are many ways to do this.

44
A Simple Sorting Algorithm Its Implementation
in F
One of the simplest algorithms to sort a list (in
increasing order) is to compare every number in
the list with every other number in the list and
swap them if they are out of order.
subroutine simple_sort(a) !Sorts list a in
increasing order.use swap_module real,
dimension(), intent(in out) ainteger i,
j, n n size(a)? do i 1, n1 do j i1,
n if (a(j) lt a(i)) then call
swap(a(i),a(j)) end if end doend
do end subroutine simple_sort
45
Exercise 3
Using arrays, write a simulation program to
determine the percentage of times exactly 5 coins
will be heads and 5 will be tails, if 10 fair
coins are tossed simultaneously.
46
Solution to Exercise 1
module random_int_module public
random_int_array contains subroutine
random_int_array (values, low, high)? integer,
dimension (), intent (out) VALUES
integer, intent (in) low, high real,
dimension (size (VALUES)) B call
random_number(B) VALUES int ((high - low
1) B low) end subroutine random_int_array
end module random_int_module program
simulation1 ! This program calculates the
probability of ! getting an 8
or a 12 when two fair dice are rolled. use
random_int_module integer, parameter
number_of_rolls 1000 integer,
dimension(number_of_rolls) dice, die_1, die_2
integer wins call random_int_array(die_1, 1,
6) call random_int_array(die_2, 1, 6) dice
die_1 die_2 wins count((dice 8).or.(dice
12)) print , The answer is,
real(wins) / number_of_rolls end program
simulation1
Write a Comment
User Comments (0)
About PowerShow.com