An introduction to arrays. Introduction In scientific and engineering computing, it is very common...

88
An introduction to arrays

Transcript of An introduction to arrays. Introduction In scientific and engineering computing, it is very common...

Page 1: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

An introduction to arrays

Page 2: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

IntroductionIn scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such as vectors and matrices.

 There is a common requirement in many applications to repeat the same sequence of operations on successive sets of data.

 In order to handle both of these requirements, F provides extensive facilities for grouping a set of items of the same type into an array which can be operated on

 Either as an object in its own right

Or by reference to each of its individual elements.

Page 3: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

The array concept

1 2 3 4 5 6

A

Page 4: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

The array concept

A set with n (=6) object, named A In mathematical terms, we can call A, a

vector And we refer to its elements as

A1, A2, A3, …

Page 5: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

The array concept

In F, we call such an ordered set of related variables an array

Individual items within an arrayarray elements

A(1), A(2), A(3), …, A(n)

Page 6: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Array ConceptIn all the programs we have used one name to refer to one location in the computer’s memory.

 It is possible to refer a complete set of data obtained for example from repeating statements, etc.

 To do this is to define a group called “array” with locations in the memory so that its all elements are identified by an index or subscript of integer type.

 An array element is designated by the name of the array along with a parenthesized list of subscript expressions.

Page 7: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

The array concept

Subscripts can be:x(10)

y(i+4)

z(3*i+max(i, j, k))

x(int(y(i)*z(j)+x(k)))

x(1)=x(2)+x(4)+1

print*,x(5)

Page 8: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

The array concept

A

1

2

3

4

-1 0 1 2 3

A20

A31

Page 9: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Array declarations

type, dimension(subscript bounds) :: list_of_array_names

type, dimension(n:m) :: variable_name_list

real, dimension(0:4) :: gravity, pressure

integer, dimension(1:100) :: scores

logical, dimension(-1, 3) :: table

An integer showing the array subscript lower bound

An integer showing the array subcript upper bound

Type can be integer, real, complex, logical, character

Page 10: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Examples for Shape Arrays

real, dimension ( 0 : 49 ) : : z

! the array “z” has 50 elements

 

real, dimension ( 11 : 60 ) : : a, b, c

! a,b,c has 50 elements

 

real, dimension ( -20 : -1 ) : : x

! the array “x” has 20 elements

 

real, dimension ( -9 : 10 ) : : y

! the array “y” has 20 elements

Page 11: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Array declarations real, dimension ( 2,3,4,3,4,5,2) : : a

Number of permissible subscripts:rank

Number of elements in a particular dimension:extent

Total number of elements of an array:size

Shape of an array is determined by its rank and the extent of each dimension

1………….…7 Up to 7 dimensions

2*3*4*3*4*5*2=

Page 12: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Array constants and initial values

Since an array consists of a number of array elements, it is necessary to provide values for each of these elements by means of an “ array constructor “.

 In its simplest form, an array constructor consists of a list of values enclosed between special delimiters :

   the first of which consists of the 2 characters : ( /

   the second of the 2 characters : / )

 

(/ value_1, value_2, ……………………., value_n /)

Page 13: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Examples

integer, dimension ( 10 ) : : arr

 

arr = (/ 3, 5, 7, 3, 27, 8, 12, 31, 4, 22 /)

 

 arr ( 1 ) = 3

 arr ( 5 ) = 27

! the value 27 is storen in the 5th location of the array “arr”

 arr ( 7 ) = 12

arr ( 10 ) = 22

Page 14: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Array constructors

(/ value_1, value_2, … /) arr = (/ 123, 234, 567, 987 /)

Regular patterns are common:implied do(/ value_list, implied_do_control /)

arr = (/ (i, i = 1, 10) /) arr = (/ -1, (0, i = 1, 48), 1 /) arr = (/ (i*4, i = 1, 10) /)

4,8,12,16….

Page 15: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Initialization

You can declare and initialize an array at the same time:

integer,dimension(50)::my_array=(/(0,i=1,50)/)

Page 16: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Input and output

Array elements treated as scalar variables

Array name may appear: whole array Subarrays can be referred too

EXAMPLE :

integer, dimension ( 5 ) : : value

read *, value

read *, value(3)

Page 17: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

real, dimension(5) :: p, qinteger, dimension(4) :: rprint *, p, q(3), rread *, p(3), r(1), qprint *, p, q (3), q (4), rprint *, q (2)

! displays the value in the 2nd location of the array “q”

print *, p! displays all the values storen in the array “p”

Examples

Page 18: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Using arrays and array elements...  The use of array variables within a loop , therefore, greatly increases the power and flexibility of a program. F enables an array to be treated as a single object in its own right, in much the same way as a scalar object. Assignment of an array constant to an array variable will be performed as is seen below :   array_name = (/ list_of_values /) array_name = (/ value_1, value_2, ….., value_n /) An array element can be used anywhere that a scalar variable can be useda(2) = t(5) - t(3)*q(2) a(i) = t(j) - f(k)

Page 19: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Using arrays and array elements...

An array can be treated as a single object– 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

two conformable objects

Page 20: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Using arrays and array elements...

.real, dimension(20) :: a, b, c..a = b*c

do i = 1, 20 a(i) = b(i)*c(i)end do

Arrays having the same number of elements may be applied to arrays and simple expressions. In this case, operation applied to an array are carried out element wise.

Page 21: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

integer, dimension ( 4 ) : : a, b

integer, dimension ( 0 : 3 ) : : c

integer, dimension ( 6 : 9 ) : : d

 

a = (/ 1, 2, 3, 4 /)

b = (/ 5, 6, 7, 8 /)

c = (/ -1, 3, -5, 7 /)

c(0) c(1) c(2) c(3)

a = a + b ! will result a = (/ 6, 8, 10, 12 /)

d = 2 * abs ( c ) + 1 ! will result d = (/ 3, 7, 11, 15 /)

d(6) d(7) d(8) d(9)

Example

Page 22: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Intrinsic procedures with arrays

Elemental intrinsic procedures with arrays

array_1 = sin(array_2)

arr_max = max(100.0, a, b, c, d, e)

Some special intrinsic functions:maxval(arr)

maxloc(arr)

minval(arr)

minloc(arr)

size(arr)

sum(arr)

Page 23: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Sub-arrays

Array sections can be extracted from a parent array in a rectangular grid usinf subscript triplet notation or using vector subscript notation

Subscript triplet:subscript_1 : subscript_2 : stride

Similar to the do – loops, a subscript triplet defines an ordered set of subscripts

beginning with subscript_1,     ending with subscript_2 and

 considering a seperation of stride between the consecutive subscripts. The value of stride must not be “zero”.

Page 24: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Sub-arrays

Subscript triplet:subscript_1 : subscript_2 : stride

Simpler forms:subscript_1 : subscript_2

subscript_1 :

subscript_1 : : stride

: subscript_2

: subscript_2 : stride

: : stride

:

Page 25: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Examplereal, dimension ( 10 ) : : arr

 

arr ( 1 : 10 ) ! rank-one array containing all the elements of arr.

arr ( : ) ! rank-one array containing all the elements of arr.

arr ( 3 : 5 ) ! rank-one array containing the elements arr (3), arr(4), arr(5).

arr ( : 9 ! rank-one array containing the elements arr (1), arr(2),…., arr(9).

arr ( : : 4 ) ! rank-one array containing the elements arr (1), arr(5),arr(9).

Page 26: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Exampleinteger, dimension ( 10 ) : : a

integer, dimension ( 5 ) : : b, i

integer : : j

 

a = (/ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 /)

i = (/ 6, 5, 3, 9, 1 /)

  b = a ( i ) ! will result b = ( 66, 55, 33, 99, 11 /)

a ( 2 : 10 : 2 ) ! will result a = ( 22, 44, 66, 88, 110 /)

  a ( 1 : 10 : 2 ) = (/ j ** 2, ( j = 1, 5 ) /)

! will result a = ( 1, 4, 9, 16, 25 /)

a(1) a(3) a(5) a(7) a(9)

Page 27: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Type of printing sub-arrayswork = ( / 3, 7, 2 /)print *, work (1), work (2), work (3)! orprint *, work ( 1 : 3 )! orprint *, work ( : : 1 )! orinteger : : ido i = 1, 3, 1print *, work (i)end do! or another exampleprint *, sum ( work (1: 3) )! or another exampleprint *, sum ( work (1: 3 : 2) )

Page 28: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Type of INPUT of sub-arraysinteger, dimension ( 10 ) : : a

integer, dimension ( 3 ) : : b .

b = a ( 4 : 6 ) ! b (1 ) = a (4)

! b (2 ) = a (5)

! b (3 ) = a (6)

a ( 1 : 3 ) = 0 ! a(1) = a(2) = a(3) = 0

a ( 1 : : 2 ) = 1 ! a(1) = a(3) =…….= a(9) = 1

a ( 1 : : 2 ) = a ( 2 : : 2 ) + 1

» ! a(1) = a(2) +1

! a(3) = a(4) +1

! a(5) = a(6) +1

! etc.

Page 29: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

PROBLEM : Write a function that takes 2 real arrays as its arguments returns an array in which each element is the maximum of the 2 corresponding elements in the input array

 function max_array ( array_1, array_2 ) result (maximum_array)

 ! this function returns the maximum of 2 arrays on an element-by- element basis

 ! dummy arguments

real, dimension ( : ) , intent (in) : : array_1, array_2

! result variable

real, dimension ( size (array_1) ) : : maximum_array

! use the intrinsic function “max” to compare elements

maximum_array = max ( array_1, array_2)

! or use a do-loop instead of the intrinsic function max to compare elements

! do i = 1, size (array_2)

! maximum_array ( i ) = max ( array_1 ( i ), array_2 ( i ) )

! end do

end function max_array

Page 30: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

program normal_probabilityinteger, parameter :: ndim = 100real, parameter :: pi = 3.1415927, two_pi = 2.0*pireal, dimension(ndim) :: phi_x, xreal :: del_x integer :: i, n , stepstep = 5! use do constructi = 1x(i) = -3.0del_x = 0.2

do if (x(i) > 3.0 + del_x) thenexitend ifphi_x(i) = exp(-x(i)**2/2.0)/sqrt(two_pi)i = i + 1x(i) = x(i-1) + del_xend do n = i-1do i = 1 , nwrite(unit=*,fmt="(1x,i3,2x,f8.5,2x,f8.5)") i,x(i),phi_x(i)end dodo i = 1 , n , stepprint "(5f8.5)", phi_x(i:i+step-1)end doend program normal_probability

Page 31: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

program vector_manipulationUse matris1real, dimension(3) :: a, b, creal :: dot_proa = (/1., -1., 0./)b =(/2., 3., 5./)dot_pro = dots_product(a,b)call vector_product(a,b,c)print *, "the dot product of two vectors is:",dot_proprint *, "the vector product of two vectors is:",cend program vector_manipulation

module matris1Public::dots_product, vector_productcontains

function dots_product(a,b) result(result)real, dimension(3),intent(in):: a, b

result = a(1)*b(1) + a(2)*b(2) + a(3)*b(3)end function dots_productsubroutine vector_product(a,b,c)real, dimension(3),intent(in):: a, breal, dimension(3),intent(out) :: c

c(1) = a(2)*b(3) - a(3)*b(2) c(2) = a(3)*b(1) - a(1)*b(3) c(3) = a(1)*b(2) - a(2)*b(1)

end subroutine vector_product

Page 32: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

!Write a program to order from min to the max value of a vector.program orderingreal,dimension(10)::ainteger::i,jreal::tempa=(/6.3,2.4,7.6,8.0,2.4,10.5,12.2,13.1,3.1,1.2/)minval1=minval(a)do i=1,9 do j=i+1,10 if (a(j)<a(i))then temp=a(j) a(j)=a(i) a(i)=temp endif enddoenddoprint*,a end program ordering

Page 33: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Arrays and procedures

Page 34: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Example

main program unit: real, dimension ( b : 40 ) : : a

subprogram: real, dimension ( d : ) : : x

x ( d ) ----------------------a ( b )

x ( d + 1 ) ----------------------a ( b + 1 )

x ( d + 2 ) ----------------------a ( b + 2 )

x ( d + 3 ) ----------------------a ( b + 3 )

.

.

Page 35: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Examplemain program unit:

real, dimension ( 10 : 40 ) : : a, b

.

call array_example_l (a,b)

subroutine array_example_1 ( dummy_array_1, dummy_array_2)

real, intent (inout), dimension : : dummy_array_1, dummy_array_2

.

.

subprogram:

Page 36: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Exampleinteger , dimension ( 4 ) : : section = (/ 5, 1, 2, 3 /)

real , dimension ( 9 ) : : a

call array_example_3 ( a ( 4 : 8 : 2 ), a ( section ) )

dummy_array_1 = (/ a (4), a (6), a (8) /) 

dummy_array_2 = (/ a (5), a (1), a (2), a (3) /) 

dummy_argument_2 (4) dummy_argument_2 (3)

dummy_argument_2 (2)

dummy_argument_2 (1)

Page 37: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

ExamplePROBLEM : Write a subroutine that will sort a set of names into alphabetic order.

İnitial order 7 1 8 4 6 3 5 2

After 1st exc. 1 7 8 4 6 3 5 2

After 2nd exc. 1 2 8 4 6 3 5 7

After 3rd exc. 1 2 3 4 6 8 5 7

After 4th exc. 1 2 3 4 6 8 5 7

After 5th exc. 1 2 3 4 5 8 6 7

After 6th exc. 1 2 3 4 5 6 8 7

After 7th exc. 1 2 3 4 5 6 7 8

 

Page 38: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Array-valued functionsAs is well known, arrays can be passed to procedures as arguments and a subroutine can return information by means of an array.

 

It is convenient for a function to return its result in the form of an array of values rather than as a single scalar value ------------array-valued function, as can be seen below :

 

  function name ( ……………………..) result (arr)

real, dimension ( dim ) : : arr

.

.

end function name

Page 39: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Array-valued functions

A function can return an array Such a function is called an

array-valued function

function name(…..) result(arr)real, dimension(dim) :: arr...

end function nameExplicit-shape

Page 40: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

ExampleConsider a trivial subroutine that simply adds 2 arrays together, returning the result through a third dummy argument array.

subroutine trivial_fun ( x, y ) result (sumxy)

 

real, dimension ( : ) , intent (in) : : x, y

real, dimension ( size (x) ) : : sumxy

 

end subroutine trivial_fun

Page 41: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Matrices and 2-d arrays

In mathematics, a matrix is a two-dimensional rectangular array of elements (that obeys to certain rules!)

[A1,1 A1,2 A1,3 A1,4

A2,1 A2,2 A2,3 A2,4

A3,1 A3,2 A3,3 A3,4

A4,1 A4,2 A4,3 A2,1]A=

Row number Column number

Page 42: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

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

 

 

 

Page 43: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Matrices and 2-d arrays

Similarly, a vector is (given a basis) a one-dimensional array of elements (that obeys to certain rules!)

[A1 A 2 A 3 A 4 ]

[A1

A2

A3

A4]

Page 44: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

2-d arrays

dimension attribute:real, 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)

Page 45: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Intrinsic functions

matmul(array, array)dot_product(array, array)transpose(array)maxval(array[,dim,mask])maxloc(array[,dim]) minval(array[,dim,mask]) minloc(array[,dim]) product(array[,dim,mask])sum(array[,dim,mask])

Page 46: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

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) do i=1,2 print*,matrix_ab(i,1:2) end doend program vectors_and_matrices

Page 47: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Some basic array concepts...

Ranknumber of its dimensions

Examples:real, dimension(8) :: ainteger, dimension(3, 100, 5) :: blogical, dimension(2,5,3,100,5) :: c

1

35

Page 48: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Some basic array concepts...

Extentnumber of elements in each dimension

Examples:real, dimension(8) :: areal, dimension(-4:3) :: b

integer, dimension(3,-50:50,40:44) :: cinteger, dimension(0:2, 101, -1:3) :: d

Page 49: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Some basic array concepts...

Sizetotal number of elements

Examples:real, dimension(8) :: a

integer, dimension(3, 100, 5) :: b

logical, dimension(2,5,4,10,5) :: c

8

1500

2000

Page 50: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

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,-3:3,4,0:9,5) :: c

shape_c = (/ 2,7,4,10,5 /)

Page 51: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

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) ...

Page 52: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Array constructors for rank-n arrays

An array constructor always creates a rank-one array of values

Solution? reshape functionreshape((/ (i, i=1,6) /), (/ 2, 3 /))

1 2 3

4 5 6

Page 53: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Example : The implied-do loop given below provides the following matrix

integer : : i, j

 

real, save, dimension ( 2,2 ) : : a = &

reshape ( ( / (10*i + j, i = 1,2 ), j = 1, 2) / ), (/ 2, 2 /) )

1 12

21 22

Page 54: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Matrix a is given as follows. Write a program calculate Matrix b as transpose of matrix a and matrix n as multiplication of matrices a and b.

2 4

1 5

a =

Page 55: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

program mat_cal

integer,dimension(2,2)::a,b,n

integer::i

a(1,1)=2

a(1,2)=4

a(2,1)=1

a(2,2)=5

b=transpose (a)

n=matmul(a,b)

print*,"Martix_a=",a,"Matrix_b=",b,"Matrix_n=",n

do i=1,2

print "(3(2i3,a))",a(i,1:2)," ",b(i,1:2)," ",n(i,1:2)," "

end do

endprogram mat_cal

Page 56: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Matrix a and b are given as follows. Write a program calculate and print Matrix a, b, c and d. (Write same program using reshape function also).

-1 2 2

4 3 1

1 1 1

1 1 1

1 1 1

1 1 1

a=

b=

e=transpose(a)

c=matmul(e,b)

d=a+c

Page 57: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

program mat1

integer,dimension(3,3)::a,b,c,d,e

integer::i,j

a(1,1)=-1

a(1,2)=2

a(1,3)=2

a(2,1)=4

a(2,2)=3

a(2,3)=1

a(1,3)=2

a(3,1)=1

a(3,2)=1

a(3,3)=1

do i=1,3

do j=1,3

b(i,j)=1

e=transpose(a)

c=matmul(e,b)

d=a+c

end do

end do

print*,"a=",a," b=",b," c=",c," d=",d

end program mat1

print*," "

do i=1,3

print "(4(3i3,a))",a(i,1:3)," ",b(i,1:3)," ",c(i,1:3)," ",d(i,1:3)

end do

Page 58: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

program mat2integer,dimension(3,3)::a,b,c,d,einteger::i,ja=reshape((/-1,4,1,2,3,1,2,1,1/),(/3,3/))do i=1,3do j=1,3b(i,j)=1e=transpose(a)c=matmul(e,b)d=a+cend doend doprint*," "do i=1,3print "(4(3i3,a))",a(i,1:3)," ",b(i,1:3)," ",c(i,1:3)," ",d(i,1:3)end doend program mat2

Page 59: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Write down a program that calculates each rows and columns and print the elements of given matrix using do loop.

Page 60: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

program mat3integer,dimension(5,5)::ainteger::i,jdo i=1,5do j=1,5if (i==j) thena(i,j)=i**2elsea(i,j)=(i+j)-1end ifend doend dodo i=1,5print"(3(5i4))",a(i,1:5)end doend program mat3

Page 61: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Array I/O

As a list of individual array elements:a(1,1), a(4,3), …

As the complete array:real, dimension(2,4) :: aprint *, a

a(1,1), a(2,1), a(1,2), a(2,2), ...

As an array section

Could be handled in three different ways

Page 62: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

The four classes of arrays

Explicit-shape arrays Assumed-shape arrays Allocatable (deferred-shape) arrays Automatic arrays

Page 63: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Explicit-shape arrays

Arrays whose index bounds for each dimension are specified when the array is declared in a type declaration statement

real, dimension(35, 0:26, 3) :: a

Page 64: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

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

Page 65: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

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

Page 66: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

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

Page 67: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Allocatable arrays

Allocation and deallocation of space for their elements us completely under user control

Flexible: can be defined in main programs, procedures and modules

Page 68: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Allocatable arrays

Steps:– Array is specified in a type declaration

statement– Space is diynamically 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

Page 69: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Allocatable arrays

Declaration

type, allocatable, dimension(:,:,:) :: allocatable array

real, allocatable, dimension(:,:,:) :: my_array

type(person), allocatable, dimension(:) :: personel_list

integer, allocatable, dimension(:,:) :: big_table

Page 70: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Allocatable arrays

Allocation

allocate(list_of_array_specs, [stat=status_variable])

allocate(my_array(1:10,-2:3,5:15), personel_list(1:10))

allocate(big_table(0:50000,1:100000), stat=check)

if (check /= 0) then

print *, "No space for big_table"

stop

end if

Page 71: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Allocatable arrays

Deallocation

deallocate(list_of_currently_allocated_arrays,

[stat=status variable])

deallocate(my_array, personel_list, big_table)

Page 72: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

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

Page 73: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Whole-array operations

Relational operators follow the same rules:

Example:

A=[1 23 4] B=[−1 −5

6 2 ]A>B

[. true . . true .. false . . true .]

Page 74: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Masked array assignment

where (mask_expression)array_assignment_statements

end where where (my_array > 0)

my_array = exp(my_array)end where

Page 75: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Masked array assignment

where (mask_expression)array_assignment_statements

elsewherearray_assignment_statements

end where where (my_array > 0)

my_array = exp(my_array)elsewhere

my_array = 0.0end where

Page 76: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

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

Page 77: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Sub-arrays

integer, dimension(-2:2, 0:4) :: tablo

2 3 4 5 64 5 6 7 81 2 3 4 5 tablo(-1:2, 1:3)6 7 8 9 64 4 6 6 8

Rank-2

Page 78: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

integer, dimension(-2:2, 0:4) :: tablo

2 3 4 5 64 5 6 7 81 2 3 4 5 tablo(2, 1:3)6 7 8 9 64 4 6 6 8

Sub-arrays

Rank-1

Page 79: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

The pressure inside a can of carbonated drink is given by the expression0.0015xT2+0.0042xT+1.352 atmwhere ToC is the temperature of the drink. When the pressure exceeds 3.2atm, the can will explode. Write a program to print the pressure inside the can for the temperature rising in one degree steps from 25oC until the can explodes.

Page 80: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Write a program which produces a table of sin x and cos x for angles x from 0° to 90° in steps of 15°.

Page 81: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.
Page 82: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

program home10integer,dimension(5,5)::A,B,C,Dinteger::i,jA=reshape((/1,2,3,4,5,2,4,4,5,6,3,4,9,6,7,4,5,6,16,8,5,6,7,8,25/),(/5,5/))do i=1,5do j=1,5if (i==j)thenB(i,j)=7else B(i,j)=0end ifend doend do

Page 83: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

C=A+BD=5*A-3*Bprint*," Matrix A"," ","Matrix B"do i=1,5print"(2(5i4,a))",A(i,1:5)," ",B(i,1:5)end doprint*," "print*," Matrix C"," ","Matrix D"do i=1,5print"(2(5i4,a))",C(i,1:5)," ",D(i,1:5)end doend program home10

Page 84: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

Write down a program that writes the results of the multiplication table as following.

Page 85: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

program multi_square!-------------------------------------------------------------!This program shows the multipication square.!-------------------------------------------------------------!variable declerationsinteger::i,jinteger,dimension(12)::xprint*," 1 2 3 4 5 6 7 8 9 10 11 12"print*," x"!make the calculationsdo i=1,12do j=1,12x(j)=i*jend do !print the outputsprint "(i6,12i4)",i,x(1:12)end doend program multi_square

Page 86: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.
Page 87: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

program mat2integer,dimension(3,3)::a,b,c,d,einteger::i,ja=reshape((/-1,4,1,2,3,1,2,1,1/),(/3,3/))do i=1,3do j=1,3b(i,j)=1e=transpose(a)c=matmul(e,b)d=a+cend doend doprint*," "do i=1,3print "(4(3i3,a))",a(i,1:3)," ",b(i,1:3)," ",c(i,1:3)," ",d(i,1:3)end doend program mat2

Page 88: An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.

write a program finds the numbers which have integer square root of between 1 and 1000. Example:n=1 square=1n=4 square=2n=9 square=3