Week 11 - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Week 11

Description:

Allocatable (deferred-shape) arrays. Automatic arrays ... Allocatable arrays ... Allocatable arrays. Steps: Array is specified in a type declaration statement ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 33
Provided by: dursunza
Category:
Tags: allocatable | week

less

Transcript and Presenter's Notes

Title: Week 11


1
Week 11
  • Array processing
  • and
  • Matrix manipulation

2
Matrices and 2-d arrays
  • In mathematics, a matrix is a two-dimensional
    rectangular array of elements (that obeys to
    certain rules!)

A
Row number
Column number
3
F extends the concept of a one-dimensional
array in a natural manner, by means of the
dimension attribute.   Thus, to define a
two-dimensional array that could hold the
elements of the matrix A, we would write
  real, dimension ( 3, 4 ) A  
numbers of rows numbers of columns      
4
Matrices and 2-d arrays
  • Similarly, a vector is (given a basis) a
    one-dimensional array of elements (that obeys to
    certain rules!)

5
2-d arrays
  • dimension attributereal, dimension(3, 4)
    alogical, dimension(10,4) b, c, d
  • You can refer to a particular element of a 2-d
    array by specifing the row and column numbers
  • a(2,3)b(9,3)

6
Intrinsic functions
  • matmul(array, array)dot_product(array,
    array)transpose(array)maxval(array,dim,mask)m
    axloc(array,dim) minval(array,dim,mask)
    minloc(array,dim) product(array,dim,mask)su
    m(array,dim,mask)

7
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 ! Set initial value for vector_c
vector_c (/1, 2/)! Set initial value for
matrix_a matrix_a(1,1) 1 ! matrix_a is the
matrix matrix_a(1,2) 2 matrix_a(1,3) 3
! 1 2 3 matrix_a(2,1) 2
! 2 3 4 matrix_a(2,2) 3
matrix_a(2,3) 4 ! Set matrix_b as the
transpose of matrix_a matrix_b
transpose(matrix_a) ! Calculate matrix products
matrix_ab matmul(matrix_a, matrix_b)
vector_bc matmul(matrix_b, vector_c) end
program vectors_and_matrices
8
Some basic array concepts...
  • Ranknumber of its dimensions
  • Examplesreal, dimension(8) ainteger,
    dimension(3, 100, 5) blogical,
    dimension(2,5,3,100,5) c

9
Some basic array concepts...
  • Extentnumber of elements in each dimension
  • Examplesreal, dimension(8) areal,
    dimension(-43) binteger, dimension(3,-5050,
    4044) cinteger, dimension(02, 101, -13)
    d

10
Some basic array concepts...
  • Sizetotal number of elements
  • Examplesreal, dimension(8) ainteger,
    dimension(3, 100, 5) blogical,
    dimension(2,5,4,10,5) c

8
1500
2000
11
Some basic array concepts...
  • Shape
  • Rank
  • Extent in each dimension
  • Shape of an array is representable as a rank-one
    integer array whose elements are the extents
  • Examplelogical, dimension(2,-33,4,09,5)
    cshape_c (/ 2,7,4,10,5 /)

12
Some basic array concepts...
  • Array element orderfirst index of the element
    specification is varying most rapidly,
  • Exampleinteger, dimension(3,4) a a(1,1),
    a(2,1), a(3,1)
  • a(1,2), a(2,2), a(3,2)
  • ...

13
Array constructors for rank-n arrays
  • An array constructor always creates a rank-one
    array of values
  • Solution?
  • reshape function
  • reshape((/ (i, i1,6) /), (/ 2, 3 /))
  • 1 2 3
  • 4 5 6

14
Example The implied-do loop given below
provides the following matrix
integer i, j   real, save, dimension ( 2,2
) a reshape ( ( / (10i j, i
1,2 ), j 1, 2) / ), (/ 2, 2 /) )
  • 12
  • 21 22

15
Array I/O
Could be handled in three different ways
  • As a list of individual array elementsa(1,1),
    a(4,3),
  • As the complete arrayreal, dimension(2,4)
    aprint , aa(1,1), a(2,1), a(1,2), a(2,2), ...
  • As an array section

16
The four classes of arrays
  • Explicit-shape arrays
  • Assumed-shape arrays
  • Allocatable (deferred-shape) arrays
  • Automatic arrays

17
Explicit-shape arrays
  • Arrays whose index bounds for each dimension are
    specified when the array is declared in a type
    declaration statementreal, dimension(35, 026,
    3) a

18
Assumed-shape arrays
  • Only assumed shape arrays can be procedure dummy
    arguments
  • Extents of such arrays is defined implicitly when
    an actual array is associated with an
    assumed-shape dummy argument
  • subroutine example(a,b) integer,
    dimension(,), intent(in) a integer,
    dimension(), intent(out) b

19
Automatic arrays
  • A special type of explicit-shape array
  • It can only be declared in a procedure
  • It is not a dummy argument
  • At least one index bound 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

20
Automatic arrays
  • subroutine abc(x)
  • ! Dummy arguments
  • real, dimension(), intent(inout) x
  • ! Assumed shape
  • ! Local variables
  • real, dimension(size(x)) e
    !Automatic
  • real, dimension(size(x), size(x)) f
    !Automatic
  • real, dimension(10)
    !Explicit shape
  • .
  • .
  • end subroutine abc

21
Allocatable arrays
  • Allocation and deallocation of space for their
    elements is completely under user control
  • Flexible can be defined in main programs,
    procedures and modules

22
Allocatable arrays
  • Steps
  • Array is specified in a type declaration
    statement
  • Space is dynamically allocated for its elements
    in a separate allocation statement
  • The arrays is used (like any other array!)
  • Space for the elements is deallocated by a
    deallocation statement

23
Allocatable arrays
  • Declaration
  • type, allocatable, dimension(,,)
    allocatable array
  • real, allocatable, dimension(,,) my_array
  • type(person), allocatable, dimension()
    personel_list
  • integer, allocatable, dimension(,) big_table

24
Allocatable arrays
  • Allocation
  • allocate(list_of_array_specs, statstatus_variabl
    e)
  • allocate(my_array(110,-23,515),
    personel_list(110))
  • allocate(big_table(050000,1100000), statcheck)
  • if (check / 0) then
  • print , "No space for big_table"
  • stop
  • end if

25
Allocatable arrays
  • Deallocation
  • deallocate(list_of_currently_allocated_arrays,
  • statstatus variable)
  • deallocate(my_array, personel_list, big_table)

26
Whole-array operations
  • Whole array operations can be used with
    conformable arrays of any rank
  • 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
    conformable arrays

27
Whole-array operations
  • Relational operators follow the same rules
  • Example

28
Masked array assignment
  • where (mask_expression) array_assignment_statemen
    tsend where
  • where (my_array gt 0) my_array
    exp(my_array)end where

29
Masked array assignment
  • where (mask_expression) array_assignment_statemen
    tselsewhere array_assignment_statementsend
    where
  • where (my_array gt 0) my_array
    exp(my_array)elsewhere my_array 0.0end where

30
Sub-arrays
  • Array sections can be extracted from a parent
    array of any rank
  • Using subscript triplet notation first_index,
    last_index, stride
  • Resulting array section is itself an array

31
Sub-arrays
  • integer, dimension(-22, 04) tablo2 3 4
    5 64 5 6 7 81 2 3 4 5 tablo(-12,
    13)6 7 8 9 64 4 6 6 8

Rank-2
32
Sub-arrays
  • integer, dimension(-22, 04) tablo2 3 4
    5 64 5 6 7 81 2 3 4 5 tablo(2,
    13)6 7 8 9 64 4 6 6 8

Rank-1
Write a Comment
User Comments (0)
About PowerShow.com