1 / 42

Week 6

Week 6. Introduction to Scientific & Engineering Computing – Week 6. Repeating parts of your program. Program repetition. In many cases, we have to repeat certain sections of a program Many numerical methods involve repetition/iteration

beverlycruz
Download Presentation

Week 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 6 Introduction toScientific & Engineering Computing – Week 6 Repeating parts of your program

  2. Program repetition • In many cases, we have to repeat certain sections of a program • Many numerical methods involve repetition/iteration • We have to have construct to repeat a group of statements, to end the repetition, or to restart it when certain condition is fulfilled.

  3. do construct do count = initial, final, inc block of statements end do loop count_variable : must be an integer initial_value, final_value : are arbitrary expressions of integer type selected step_size = inc : must be an integer

  4. Examples do statement iteration count do variable values do i = 1,10 10 1,2,3,4,5,6,7,8,9,10 do j = 20, 50, 5 7 20,25,30,35,40,45,50 do p = 7, 19, 4 4 7,11,15,19 do q = 4, 5, 6 1 4 do r = 6, 5, 4 0 (6) do x = -20,20, 6 7 -20,-14,-8,-2,4,10,16 do n = 25, 0, -5 6 25,20,15,10,5,0 do m = 20, -20, -6 7 20,14,8,2,-4,-10,-16

  5. Example do number = 1, 10, 2 print *, number, number **2 end do  OUTPUT produced will display following results : 11 3 9 525 7 49 9 81

  6. Count-controlled do loops docount = initial, final, inc docount = initial, final (inc = 1) do Iteration count: How many times we will go through the loop? max((final-initial+inc)/inc, 0) Integer variable

  7. NESTED DO - LOOPS The body of a do loop may contain another do loop. In this case,the second do loop is said to be “nested” within the first do loop. EXAMPLE : dom = 1, 4 do n = 1, 3 product = m * n print *, m, n, product end do end do OUTPUT 1 1 1 1 2 2 1 3 3 2 1 2 2 2 4 2 3 6 3 1 3 3 2 6 3 3 9 4 1 4 4 2 8 4 3 12

  8. Some flexibility... do . . . if (condition) then exit end if . . . end do .

  9. Some more flexibility... do . . . if (condition) then cycle end if . . . end do .

  10. Example PROBLEM : Suppose that in the temprature-conversion only temprature of 0oc or above values are wanted to convert. do . if ( Celcius < 0.0 ) then print *, “ given temprature must be 0.0 or above” else cycle ! go and convert the given temprature end if . end do

  11. Naming your do constructs Especially in “nested do–loops” it’s very difficult to control the transfer of the program. As mentioned before an “exit“ statement in the example seen below, will transfer control to the first executable statement following the second “ end do ”.On the other hand, there will be occasions when it is required to exit from all of the enclosing loops; or even from more than the immediately enclosing or current loop, but not from all of them.For this reason it is strongly recommended to use “named do– constructs” by preceding the do statement by a name as is seen below : block_name:do . . end doblock_name

  12. Naming your do constructs outer: do . . inner: do . . . end doinner . end doouter

  13. Dealing with exceptional situations: There are occasionally situations in which the statements used before are inconvenient or make programming very difficult.Thus, two additional statements exist to help us in these exceptional situations. These are, STOP:this statement terminates the execution without to need to find a way of reaching the “end” statement of the main program unit. This word “stop” causes execution of the program to be terminated immediately. RETURN : this statement causes a return from a procedure without the need to find a way of reaching the “end” statement of the procedure. This word “return” causes execution of the procedure to be terminated immediately and control transferred back to the program unit which called or referenced the procedure.

  14. Dealing with exceptional situations: • stop statement • simply terminates execution • return statement • causes execution of the procedure to be terminated immediately and control transferred back to the program unit which called or referenced the procedure

  15. Arrays Introduction

  16. Introduction In 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.

  17. 1 2 3 4 5 6 A The array concept

  18. 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 asA1, A2, A3, …

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

  20. Array Concept In 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.

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

  22. -1 0 1 2 3 1 2 3 4 A The array concept A20 A31

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

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

  25. Array declarations • Up to 7 dimensions • 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

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

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

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

  29. Initialization • You can declare and initialize an array at the same time: integer, dimension(50) :: my_array = (/ (0, i = 1, 50) /)

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

  31. Examples real, dimension(5) :: p, qinteger, dimension(4) :: rprint *, p, q(3), rread *, p(3), r(1), q print *, p, q (3), q (4), r print *, q (2) ! displays the value in the 2nd location of the array “q” print *, p ! displays all the values storen in the array “p”

  32. 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 used a(2) = t(5) - t(3)*q(2)a(i) = t(j) - f(k)

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

  34. Using arrays and array elements... Arrays having the same number of elements may be applied to arrays and simple expressions.In this case, operation applied toan array are carried out elementwise. . real, dimension(20) :: a, b, c . . a = b*c do i = 1, 20 a(i) = b(i)*c(i) end do

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

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

  37. Sub-arrays • Array sections can be extracted from a parent array in a rectangular grid usinf subscript triplet notation or using vectorsubscript 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”.

  38. Example real, 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).

  39. Example integer, 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)

  40. Type of printing sub-arrays work = ( / 3, 7, 2 /) print *, work (1), work (2), work (3) ! or print *, work ( 1 : 3 ) ! or print *, work ( : : 1 ) ! or integer : : i do i = 1, 3, 1 print *, work (i) end do ! or another example print *,sum ( work (1: 3) ) ! or another example print *,sum ( work (1: 3 : 2) )

  41. Type of INPUT of sub-arrays integer, 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.

  42. PROBLEM : Write a function thattakes 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

More Related