ARRAY PROCESSING - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

ARRAY PROCESSING

Description:

integer, dimension (3,10,2) :: b ... real, dimension (10,20,30) :: a ... whose first element is -1, whose (i 1)th element is i (for i=1,...48) and whose ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 36
Provided by: nuzhet
Category:

less

Transcript and Presenter's Notes

Title: ARRAY PROCESSING


1
ARRAY PROCESSING MATRIX MANIPULATION
  • Ellis Philips, 1998, p 359-406

2
Matrices and two-dimensional arrays
  • The rank-one arrays represent the vectors.
  • The rank-n arrays represent the matrices.
  • Matrices and two dimensional arrays
  • real, dimension (3,4) A
  • row3, column4

3
  • Example
  • a(3,4)2.a(3,4)1.0 ! Doubles a(3,4) and adds
    1 to it.
  • do i1,4 ! Replace row 1 of a by row 3 of a.
  • a(1,i)a(3,i) ! Row 3 is unaltered.
  • end do
  • do i1,3 ! Replace column 2 of a by column 1
  • a(i,2)a(i,1) ! of a. column 1 is unaltered.
  • end do

4
  • Intrinsic functions for use with vectors and
    matrices
  • matmul Matrix product of two matrices, or a
    matrix and a vector
  • dot_product Scalar (dot) product of two vectors
  • transpose Transpose of a matrix
  • maxval Maximum value of all elements of an
    array
  • maxloc The location in an array where the
    maximum value first occurs
  • minval Minimum value of all elements of an array
  • minloc The location in an array where the
    minimum value first occurs
  • product Products of all elements of an array
  • sum Sum of all elements of an array

5
An example of matrix and vector multiplication
  • program vectors_and_matrices
  • integer, dimension(2,3) matrix_a
  • integer, dimension(3,2) matrix_b
  • integer, dimension(2,2) matrix_ab
  • integer, dimension(2) vector_c
  • integer, dimension(3) vector_bc
  • vector_c(/1,2/)
  • matrix_a(1,1)1
  • matrix_a(1,2)2
  • matrix_a(1,3)3
  • matrix_a(2,1)2
  • matrix_a(2,2)3
  • matrix_a(2,3)4
  • matrix_btranspose(matrix_a)
  • matrix_abmatmul(matrix_a,matrix_b)
  • vector_bcmatmul(matrix_b,vector_c)
  • end program vectors_and_matrices

6
Basic array concepts for arrays having more than
one dimension
  • An array may have from one to seven dimensions.
  • A vector is stored in a rank_one array and a
    matrix in
  • a rank_two array.
  • The extent of a dimension is the number of
    permissible
  • index values for that dimension.
  • real, dimension(8) a
  • integer, dimension(3,10,2) b
  • a is an eight-element rank_one real array
  • b is a 3x10x2 rank_three integer array.

7
  • The size of an array is the total number of
    elements it contains. It is equal to the product
    of the elements of all its dimensions.
  • integer, dimension (3,10,2) b
  • The array b has extent 3 for its first dimension,
    extent 10 for its second dimension, and extent 2
    for its third dimension. Therefore, its size is
    3x10x2 or 60.
  • The shape of an array is determined by the number
    of its dimensions and the extent along each
    dimension. It is representable as a rank-one
    array.
  • real, dimension (10,20,30) a
  • Its shape is representable as the rank_one _array
    whose elements are in order 10, 20 and 30.

8
  • The array declarations for rank_n arrays may
    specify lower and upper
  • bounds for one or more of their dimensions.
  • real, dimension (1118) a
  • integer, dimension (57, -101, 2) b
  • When we specify the lower and upper index bounds
    for a dimension,
  • the extent for that dimension is one plus the
    difference between
  • the upper and lower index bounds.
  • real, dimension (110) a
  • real, dimension (2029) b
  • A rank-one array with the index bounds 1 and 10
    has the same shape
  • as another rank-one array with index bounds 20
    and 29.

9
  • The elements of an array form a sequence known as
  • the array element order.
  • real, dimension(4,3) arr
  • arr(1,1), arr(2,1),arr(3,1),arr(4,1),
  • arr(1,2),arr(2,2),arr(3,2),arr(4,2),
  • arr(1,3),arr(2,3),arr(3,3),arr(4,3)
  • F stores arrays by columns.
  • Array constructors for rank_n arrays
  • Array constructs define array_valued constants in
    the form of
  • rank_one arrays. The reshape intrinsic function
    can be
  • used to change this into any specified shape for
    assignment
  • to an array whose rank is greater than one.

10
  • (/value_list/)
  • For a rank_one array,
  • (/-1,(i,i1,48),1/)
  • defines an array of size 50 whose first element
    is -1, whose (i1)th element is i (for i1,48)
    and whose 50th element is 1.
  • For a rank_two array,
  • reshape(/1,2,3,4,5,6/),(/2,3/)
  • takes the rank_one real array whose elements are
    1.0,2.0,3.0,4.0,5.0,6.0 and produces a result the
    2x3 real array whose elements are


11
An improved example of matrix and vector
multiplication
  • program vectors_and_matrices
  • integer, dimension(2,3) matrix_a
  • integer, dimension(3,2) matrix_b
  • integer, dimension(2,2) matrix_ab
  • integer, dimension(2) vector_c
  • integer, dimension(3) vector_bc
  • vector_c(/1,2/)
  • matrix_areshape(/1,2,2,3,3,4/),(/2,3/)
  • ! a has the values 1,2,3
  • ! 2,3,4
  • matrix_btranspose(matrix_a)
  • ! matrix b is now the matrix 1,2
  • ! 2,3
  • ! 3,4
  • matrix_abmatmul(matrix_a,matrix_b)
  • ! matrix_ab is now the matrix 14,20
  • ! 20,29
  • vector_bcmatmul(matrix_b,vector_c)
  • ! Vector_bs is now the vector 5,8,11

12
Input and output with arrays
  • A list of individual array elements
  • a(1,1), a(2,2)
  • The input or output of an entire array
  • integer, dimension (2,3) a
  • print ,a
  • Output a(1,1),a(2,1),a(1,2),a(2,2),a(1,3),a(2,3)

13
The four classes of arrays
  • The classes of arrays
  • 1) Explicit_shape arrays
  • 2) Assumed_shape arrays
  • 3) Automatic arrays (A sub-class of
    explicit-shape arrays)
  • 4) Allocatable arrays (Deferred_shape arrays)
  • 1) Explicit_shape arrays are arrays whose index
    bounds for
  • each dimension are specified when the array is
    declared in
  • a type declaration statement.
  • lower_bound upper_bound
  • or
  • upper_bound

14
  • 2) Assumed_shape arrays may only be dummy
    arguments of
  • a procedure. They can not occur in a main
    program.
  • dimension (list of assumed_shape specifiers)
  • lower_bound
  • or
  • If the lower bound is omitted, it is taken to be
    1.
  • function assumed_shape (a,b) result (r)
  • integer, dimension (,), intent (in) a
  • real, dimension (5,,), intent (inout) b
  • .
  • end function assumed_shape
  • a is of rank_2 with the lower index bounds for
    both subscripts being 1.
  • b is of rank_3 with the lower index bound for the
    first subscript being 5,
  • and the lower index bounds for the other
    subscripts being 1.

15
  • The intrinsic functions for the rank_n arrays
  • size
  • lbound
  • ubound
  • The size has two arguments, the second of which
    is optional. The first argument is the name of
    the array and the second argument dim is an
    integer which, if present, must lie in the range
    1? dim ? rank, where rank is the rank of the
    array. If dim is not present, size returns the
    size of the whole array. If dim is present, size
    returns the extent of the array for the specified
    dimension
  • The function lbound also has two arguments of
    which the second is optional. The first argument
    is the name of the array, the second argument
    specifies the dimension. If the second argument,
    dim, is present, the lbound returns the lower
    index bound of the specified dimension in the
    form of an integer. If dim is not present, the
    result of function reference is a rank-one array
    containing all the lower index bounds.
  • The third function, ubound is similar to lbound,
    but returns the upper bound(s) of its first
    argument.

16
3) Automatic arrays
  • An automatic array is a special type of
    explicit-shape array which can only
  • be declared in a procedure, which is not a dummy
    argument, and which
  • has at least one index bound that is not
    constant. The space for
  • the elements of an automatic array is created
    dynamically when
  • the procedure is entered and is removed upon exit
    from the procedure.
  • subroutine abc (x)
  • real, dimension (), intent (inout) x !
    Assumed shape
  • real, dimension (size(x)) e ! Automatic
  • real, dimension (size(x), size(x)) f !
    Automatic
  • real, dimension (10) g ! Explicit shape
  • .
  • .
  • end subroutine abc

17
4) Allocatable arrays
  • These provide more flexibility than automatic
    arrays, because
  • the allocation and deallocation of space for
    their elements are completely
  • under user control. They can be defined in main
    programs, procedures or
  • modules.
  • An allocatable array is an array whose rank is
    declared initially, but none of
  • its extents, and which is subsequently allocated
    with bounds specified
  • dynamically during execution.
  • Using allocatable arrays consists of three steps
  • 1)The allocatable array is specified in a type
    declaration statement.
  • 2) Space is dynamically allocated for its
    elements in a separate allocation
  • statement after which the array may be used in
    the normal way.
  • 3) After the array has been used, and is no
    longer required, the space for
  • the elements is deallocated by a deallocation
    statement.

18
  • real, allocatable, dimension(,,)
    allocatable_array
  • allocate (list of array specifications,
    statstatus_variable)
  • or
  • allocate(list of array_specifications)
  • Example
  • allocate (arr_1(20), arr_2(1030,-1010),
    arr_3(20,3050,5))
  • statstatus_variable element of the allocate
    statement enables
  • the processor to report on the success.
  • deallocate(list of currently allocated_arrays,
    statstatus_variable)
  • deallocate(list of currently_allocated arrays)

19
Example Using allocate and deallocate statements
to enable an allocatable array to have its size
repeatedly changed
  • real, allocatable, dimension() varying_array
  • integer i,n,alloc_error, dealloc_error
  • .
  • read , n
  • do i1,n
  • allocate (varying_array(-i i),
    statalloc_error)
  • If(alloc_error / 0) then
  • print ,Insufficient space to allocate array,
    when i,i
  • stop
  • end if
  • !calculate using varying_array
  • deallocate (varying_array,statdealloc_error)
  • if(dealloc_error / 0) then
  • print , Unexpected deallocation error
  • stop
  • endif
  • end do
  • This code fragment first allocates varying_array
    to be a rank-one array with index bounds of -1
    and 1 and performs calculations and deallocates.
    Next, it allocates varying_array again but this
    time with index bounds of -2 and 2, performs
    calculations and deallocates. This cycle is
    repeated n times.

20
  • An allocatable array which is no longer required
    should,
  • therefore, always be deallocated before an exit
    is made
  • from the procedure in which it was allocated. If
    it will be
  • required again, and its current values need to be
    preserved,
  • then it should be declared with the save
    attribute.
  • character (len50), allocatable, dimension (),
    save name

21
The greater control provided by allocatable
arrays can be used to write programs with more
capacity than is possible with automatic arrays.
  • subroutine space(n)
  • integer, intent(in) n
  • real, allocatable, dimension (,) a,b
  • allocate (a(2n,6n)) ! Allocate space for a
  • !calculate using a
  • deallocate (a) ! Free space used by a
  • allocate(b(3n,4n)) ! Allocate space for b
  • ! calculate using b
  • deallocate (b) ! Free space used by b
  • end subroutine space
  • The subroutine uses 12n2 elements for a during
    execution of the first
  • part, and then releases this space, then uses
    12n2 elements for b during
  • the execution of the second part.
  • The maximum space required for two arrays is that
    required for 12n2 real
  • numbers

22
The subroutine space was rewritten to use
automatic arrays instead of allocatable arrays.
  • subroutine space(n)
  • integer, intent(in) n
  • real, dimension (2n,6n) a
  • real, dimension(3n,4n) b
  • !calculate using a
  • .
  • !calculate using b
  • end subroutine space
  • The subroutine begins execution 12n2 elements are
    allocated for a and
  • 12n2 elements are allocated for b. A total of
    24n2 elements are created
  • and are not released until exit from the
    subroutine.

23
The main restrictions on allocatable arrays
  • An allocatable arrays can not be dummy arguments
    of
  • a procedure.
  • This means that the allocatable arrays defined in
    a main program or procedure must be allocated and
    deallocated in the main program or procedure in
    which they were initially defined.
  • If the type declarations for an allocatable array
    is placed in a module then the array can be
    allocated and deallocated by any procedure in the
    module or by any main program or procedure using
    the module.
  • To prevent the status of an allocted allocatable
    array from becoming undefined when a procedure
    that allocated it is exited, the array can be
    given the save attribute.

24
  • The result of a function can not be an
    allocatable array.
  • Allocatable arrays cannot be used in a derived
    type
  • definition.

25
Whole-array operations
  • The whole-array processing capability is
    complemented by a
  • number of intrinsic functions designed for
    manipulating
  • arrays.
  • real, dimension (10) p,q
  • real, dimension (1019) r
  • pqr
  • do i1,10
  • p(i)q(i)r(i9)
  • end do

26
  • Rules for working with whole arrays
  • Two arrays are conformable if they have the same
    shape.
  • A scalar, including a constant is conformable
    with any array.
  • All intrinsic operations are defined between the
    conformable
  • arrays.

27
  • character (len7), dimension(3,4)
    string_1,string_2
  • character(len14), dimension(3,4) long_string
  • long_stringstring_1/string_2

28
  • Example An array-valued function to calculate
    the outer
  • product of two vectors
  • function outer_product(x,y) result(outer)
  • real, dimension (), intent(in) x,y
  • real, dimension(size(x,1),size(y,1)) outer
  • integer i,j
  • do i1,size(x,1)
  • do j1,size(y,1)
  • outer(i,j)x(i)y(j)
  • end do
  • end do
  • end function outer_product

29
  • Examples for intrinsic procedures designed for
    use in array
  • processing
  • Dot product of two rank_one arrays
  • dot_product (vector_a, vector_b)
  • Matrix multiplication
  • matmul (matrix_a, matrix_b)
  • See Figure 13.13 in page 395
  • By using the array intrinsics, the resulting code
    will
  • almost be cleaner and more compact than code
    written
  • without them. It will also require less effort.

30
Masked array assignment
  • The where construct allows a finer degree of
    control over
  • the assignment of one array to another, by use of
    mask which
  • determines whether the assignment of a particular
    element should take
  • place or alternatively which of two alternative
    values should be assigned
  • to each element. This concept is called masked
    array assignment.
  • where (mask-expression)
  • array_assignment_statements
  • end where
  • where (arrlt0.0)
  • arr-arr
  • end where
  • where(arrgt0.0)
  • z_arrsqrt(arr)
  • end where

31
  • where(mask_expression)
  • array_assignment_statements
  • elsewhere
  • array_assignment_statements
  • end where
  • where(array / 0.0)
  • array1.0/array
  • elsewhere
  • array1.0
  • end where

32
  • One very important point to emphasize is that,
    despite its
  • synthetic similarity to the if construct, the
    where construct
  • is not a sequential construct.
  • The mask is always an array which is conformable
    with
  • the array, or arrays, which appear on the
    left-hand side of
  • the assignment statement, or statements, in the
    construct,
  • and the effect is as if all the array elements
    were assigned
  • simultaneously, with the mask either preventing
    some of
  • the assignments taking place, or causing
    different ones to
  • take place.

33
Sub-arrays
  • An array section is a sub-array defined by
    specifying a subset
  • of the elements of another array.
  • real, dimension (3,4) arr_3
  • arr_3(2,)
  • is a rank-one real array whose elements are
    arr_3(2,1), arr_3(2,2),arr_3(2,3) and arr_3(2,4).
    It is the second row of arr_3.
  • arr_3(,3)
  • is a rank-one real array whose elements are
    arr_3(1,3), arr_3(2,3) and arr_3(3,3). It is the
    third column of arr_3.
  • arr_3 (2,34)
  • is a rank-one array whose elements are arr_3
    (2,3) and arr_3 (2,4)

34
  • If the array arr_2 is declared as,
  • integer, dimension (29, -21) arr_2
  • then arr_2(45,-10) is a rank-two integer array
    containing the elements

35
IN-CLASS PROBLEM SESSION - 10
  • Objective Learning array processing and matrix
    manipulation
  • Assignment Write a program to calculate the
    product of an mxn matrix
  • (a) with an nxk matrix (b).
  • a 3x3
  • b 3x1
Write a Comment
User Comments (0)
About PowerShow.com