1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Introduction to arrays
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Need for Arrays Exercise Read the IDs and the grades for all ICS 101 students. Compute and print the average of the students. Print the grades and IDs.
Fortran: Array Features Session Five ICoCSIS. Outline 1.Zero-sized Array 2.Assumed-shaped Array 3.Automatic Objects 4.Allocation of Data 5.Elemental Operations.
Arrays Array Terminology Declarations Visualisation of Arrays Array Conformance Array Element Ordering Array Syntax Whole Array Expressions Array Sections.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 ARRAYS. 2 Array subscript expressions  Each subscript in an array element designator is an expression of integer type. It need not be a constant.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
Chapter 8 Arrays and Strings
JavaScript, Third Edition
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Pointers Applications
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
VB .NET Programming Fundamentals
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Matrix Definition A Matrix is an ordered set of numbers, variables or parameters. An example of a matrix can be represented by: The matrix is an ordered.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Structured Data Types. Date Types We have seen various data types –Integer, Real, Character, Logical All these types define data values of different kinds.
Objectives You should be able to describe: Data Types
Matlab tutorial course Lesson 2: Arrays and data types
Systems of Linear Equation and Matrices
Multi-Dimensional Arrays
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Chapter 8 Arrays and Strings
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables.
4.4 Identify and Inverse Matrices Algebra 2. Learning Target I can find and use inverse matrix.
An introduction to arrays WEEK 7 Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values,
Arrays Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such as vectors and matrices.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Fortran: Control Structures Session Three ICoCSIS.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Programming For Nuclear Engineers Lecture 6 Arrays
Array processing and Matrix manipulation
JavaScript: Functions.
Midterm Review Programming in Fortran
Vectorized Code, Logical Indexing
C Operators, Operands, Expressions & Statements
Matlab Basics.
Array processing and Matrix manipulation
Presentation transcript:

1 Week 12 Arrays, vectors, matrices and cubes

Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an array element designator is an expression of integer type. It need not be a constant. The value of each subscript expression must be within the declared subscript range for the corresponding dimension: –Each subscript value must be less than or equal to the upper bound specified for the corresponding array dimension. –Each subscript value must be greater than or equal to the specified Lower bound; or, if no Lower bound is explicitly specified, it must be greater than or equal to 1 (the default Lower bound). Example for Array Element Names integer, parameter :: EXTENT = 20 integer, parameter :: K1 = 1, L3 = 3, M1 = 1, I2 = 2, & J1 = 1, L0 = 0, L16 = 16, I4 = 4, M2 = 2 real :: X0 real, dimension(EXTENT) :: X1 real, dimension(EXTENT, EXTENT) :: X2

Introduction to Scientific & Engineering Computing 3 Array element names real, dimension(EXTENT, EXTENT, EXTENT) :: X3 read (unit = *, fmt = *) X1(6) write (unit = *, fmt = *) X1(I4+2), X1(3*M2) read (unit =*, fmt = *) X3(L3-1, 2*M1, 13*I2-7) write (unit =*, fmt = *) X3(2, 2, 19) read (unit =*, fmt = *) X2(3*J1-1, 3*L0+L16) write (unit =*, fmt = *) X2(2, 16) read (unit =*, fmt = *) X1(3) read (unit =*, fmt = *) X2(K1, L0+2) X1(1) = X1(3) + L16 X1(I4) = X0 = X1(6) + X2(1,2) write (unit =*, fmt = *) X1(K1), X1(4), X0

Introduction to Scientific & Engineering Computing 4 Vectors, matrices and cubes One-dimensional and two-dimensional arrays may be interpreted in special ways according to the rules of linear algebra. An array of rank 1 is a vector, an array of rank 2 is a matrix and an array of rank 3 may be viewed as a stack of cubes forming a large block (parallelepiped). Examples: One-dimensional array; vector: integer, dimension(3) :: A Two-dimensional array; integer, dimension(3,3) :: B A(1) A(2) A(3) B(1,1) B(1,2) B(1,3) B(2,1) B(2,2) B(2,3) B(3,1) B(3,2) B(3,3)

Introduction to Scientific & Engineering Computing 5 Vectors, matrices and cubes Three-dimensional array; Integer, dimension(3,3,3) :: C C(1,1,1) C(1,1,2) C(1,1,3) C(1,2,1) C(1,2,2) C(1,2,3) C(1,3,1) C(1,3,2) C(1,3,3) C(2,1,1) C(2,1,2) C(2,1,3) C(2,2,1) C(2,2,2) C(2,2,3) C(2,3,1) C(2,3,2) C(2,3,3) C(3,1,1) C(3,1,2) C(3,1,3) C(3,2,1) C(3,2,2) C(3,2,3) C(3,3,1) C(3,3,2) C(3,3,3)

Introduction to Scientific & Engineering Computing 6 Whole-array operations An array is a variable; thus, the array name (without a subscript) represents all elements of the array. For instance, an array name in an input or output list causes input or output of all the array elements. There is a rich set of operations on whole arrays; most scalar operations are extended to whole-array operands. A whole-array operation is applied to all elements of the array. A whole-array, denoted by the array name without a subscript, is an array variable. F language permits an array variable in most contexts where a scalar variable is permitted. Assignment is permitted between whole arrays of the same shape: 1) The shape of an array is determined by its rank (number of dimension) and by its extent (number of elements) along each dimension. 2) Two arrays have the same shape if they have the same rank and if their extents agree along each dimension. The subscript ranges (upper and lower subscript bounds) are not required to agree.

Introduction to Scientific & Engineering Computing 7 Whole-array operations Whole-array assignment real, dimension(5, 7) :: A, B real, dimension(0 : 4, 0 : 6) :: C integer, dimension(5, 7) :: I read(unit = *, fmt = *) B C = B I = C ! Elementwise type coercion, with truncation, occurs. A = I write (unit = *, fmt = *) A

Introduction to Scientific & Engineering Computing 8 Arithmetic and relational operations on whole arrays real, dimension(5, 7) :: A, B, C logical, dimension(5, 7) :: T real, dimension(20) :: V, V_Squared read (unit = *, fmt = *) B, C, V A = B + C T = B > C C = A * B V_Squared = V * V write (unit = *, fmt = *) T, C, V, V_Squared

Introduction to Scientific & Engineering Computing 9 Elemental intrinsic functions n Many intrinsic functions (see Appendix A, Meissner`s book), notably including the mathematical functions, are classified as elemental. An elemental intrinsic function accepts either a scalar or an array as its argument. When the argument is an array, the function performs its operation element wise, applying the scalar operation to every array element and producing a result array of the same shape. Example: real, dimension(5, 7) :: A, B, C logical, dimension(5, 7) :: T. A = max(B,C) T=sin(A) > B

Introduction to Scientific & Engineering Computing 10 Array sections with increment (stride) designator An array section designator may include a third expression, analogous to the third control parameter of an indexed do construct. This increment (in this context, often called the stride) gives the spacing between those elements of the underlying parent array that are to be selected for the array section: real, dimension(8) :: A real, dimension(18) :: V A = V(1: 15: 2) By analogy to an indexed do with explicit increment, we see that the values assigned to A are those of V(1), V(3), …, V(15). A negative Increment value reverses the normal array element sequence: A = V(9: 2: -1) the eight elements are V(9), V(8), V(7),…, V(2)

Introduction to Scientific & Engineering Computing 11 Array sections with increment (stride) designator real, dimension (18) :: V V(2:9) V(1: 15: 2) V(2: 17: 5)

Introduction to Scientific & Engineering Computing 12 Array sections with increment (stride) designator real, dimension(5, 7) :: M M(1: 4, 1: 4) real, dimension(3, 5) :: E real, dimension(2, 3) :: F F = E (1 : 3 : 2, 1 : 5 : 2)

Introduction to Scientific & Engineering Computing 13 Array sections of reduced rank n Array section designators may be combined with single subscripts to designate an array object of lower rank V( : 5) = M(2, 1: 5) Here, the elements M(2,1) through M(2,5) form a one-dimensional array section that is assigned to the first five elements of V

Introduction to Scientific & Engineering Computing 14 Intrinsic and array inquiry functions  Intrinsic functions perform many operations that would otherwise have to be programmed with loops. A selection of these functions is listed on pages on Meissner’s book.  For example:  transpose(matrix) forms the transpose of a matrix (rank-2 array) of any type.  ubound(array [, dim]) returns the upper bound [in the specified dimension], or a vector of upper bounds.  mask argument in sum, product, any, all, count, maxloc, and minloc is a logical array with the same shape as array, and the function is applied to elements where mask is true.

Introduction to Scientific & Engineering Computing 15 Sum and product (sum & dim) n The intrinsic functions sum and product compute the sum or product of all elements of an array or array section of any numerical type. n Sum of selected elements (optional argument mask). mask is an array of the same shape as the first argument array or it is a scalar, and its type is logical. When mask is present, sum and product operate only on the array elements selected by the mask. The conditional sum  xi > 0.0 x i is denoted by the expression sum (X, mask = (X > 0.0)) The mask locates the positive elements of the vector X. The value of this expression is 0.7 if X is (0.0, -0.1, 0.2, 0.3, 0.2, -0.1, 0.0). n The optional argument dim(dimension) simplifies computation of the sums or products of all rows or columns of a matrix, or along any one dimension of an array of higher rank. The result array has one less dimension than Matrix; the dimension designated by dim is deleted, or rather it is reduced to a scalar. If the rank of Matrix is 2, sum(Matrix, dim =1) produces a vector containing the column sums, and sum(Matrix, dim=2) produces a vector containing the row sums.

Introduction to Scientific & Engineering Computing 16 Sum and product (sum & dim) Example: Let A be the following 3 by 3 matrix: The function sum with two different dim argument values returns the vector of column sums (7, 8, 10) and the vector of row sums (6, 15, 4). n The sum of products is denoted by the expression sum( product (A, dim = 1) ) The argument dim = 1 means that the product is to be computed down each column of A. The result of product is the vector (8, 10, 18), whose sum is 36. n The product of sums is denoted by the expression product( sum (A, dim =2 ) ) The argument dim = 2 means that the sum is to be computed along each row of A. The result of sum is the vector (6, 15, 4), whose product is 360.

Introduction to Scientific & Engineering Computing 17 The where construct n A where construct may be used to assign values to arrays depending on the value of a logical array expression. where construct has the following form: where (logical-array-expr) array-var 1 = array-expr 1 or where (logical-array-expr) array-var 1 = array-expr 1. array-var m = array-expr m elsewhere array-var m+1 = array-expr m+1. array-var n = array-expr n end where where each array-var m has the same size as the value of logical-array-expr, in the second form, the elsewhere part is optional.

Introduction to Scientific & Engineering Computing 18 The where construct n The logical-array-expr is evaluated element by element, and whenever the expression is true (false), the value of the corresponding element of each array-expr i (in the elsewhere part) is assigned to the corresponding element of array-var i. In the first form, all other elements are left unchanged. n For example: Assume that A and B are declared as integer, dimension(5) :: A = (/ 0, 2, 5, 0, 10 /) real, dimension(5) :: B the where construct is where (A > 0) B = 1.0 / real (A) elsewhere B = -1.0 end where construct assigns to B the sequence -1.0, 0.5, 0.2, -1.0, 0.1.

Introduction to Scientific & Engineering Computing 19 Array intrinsics for linear algebra applications n The intrinsic functions transpose, dot_product, and matmul perform essential operations of linear algebra. real, dimension(3, 5) :: A real, dimension(5, 3) :: B. B = transpose(A) dot_product forms the sum of products of corresponding elements of two vector, which must have the same length. matmul forms the product of a p by r matrix (or a vector of length r) and an r by s matrix (or a vector of length r); the result is a p by s matrix. The arguments must not both be vectors. For example; real, dimension(5) :: A, C real, dimension(5, 3) :: X C = matmul(A, X)

Introduction to Scientific & Engineering Computing 20 Array constructors and array- valued constants n An array constructor consists of a List of expressions and implied do loops, enclosed by the symbols (/ and /): (/ List /) The expressions in the List may be scalar expressions or array expressions, and all must have the same type and kind. Let`s see the array-valued constants in the following. The first two of the following array constructors form constant vectors of real type, while the third is a constant vector of integer type. ( / 3.2, 4.01, 6.4 /) ( / 4.5, 4.5 /) ( / 3, 2 /) The implied do loop form that is permitted in an array constructor is so called because of its resemblance to the control statement of an indexed do construct:

Introduction to Scientific & Engineering Computing 21 Array constructors and array- valued constants n (Sublist, Index = Initial value, Limit [, Increment]) The Sublist consists of a sequence of items of any form that is permitted in the List The Index, the Initial value, and the optional Increment are subject to the same rules that apply to indexed do constructs. Example: (/ (I, I = 1, N) /) (/ ((0.0, J=1, I-1), 1.0, (0.0, J = I+1, N), I = 1, N) /) It must be kept in mind that the rank of an array constructor is always 1. An array constructor always creates an array of rank 1, but the intrinsic function reshape, which changes the shape of an array, can be applied to an array constructor to create an array of higher rank. The result from reshape is an array whose rank is the number of elements in the shape vector and whose shape is given by the shape vector.

Introduction to Scientific & Engineering Computing 22 Array constructors and array- valued constants For example, the following expression constructs an N by N identity matrix: reshape( ( / ((0.0, J=1, I-1), 1.0, (0.0, J = I+1, N), I = 1, N) / ),( / N, N / ) An array constructor on the right side of an assignment statement (or in the definition of a named constant) must agree in shape with the array on the left. A(1: 7 : 2) = (/ 11, 6, 14, 5 /) result is: A(1)=11, A(3)=6, A(5)=14, A(7)=5

Introduction to Scientific & Engineering Computing 23 Array allocation n Allocatable arrays and allocated pointer target arrays are deferred- shape arrays. Allocatable arrays are declared in the usual way, except that the keyword allocatable appears in the list of attributes and that the dimension attribute is only specified. Colons in the array shape specification determine the rank (number of dimension) of the allocatable array, but the Subscript bounds are omitted from the declaration. The Subscript bounds remain to be established at the time the array is actually allocated: real, dimension( :, : ), allocatable :: Alimony real, dimension( : ), allocatable :: Alpenglow real, dimension( :, :, : ), allocatable :: Alabaster At various points in the program, allocate statement can then be executed to create the arrays: allocate(Alimony(5, 1000)) allocate(Alpenglow(0 : Range), Alabaster(5, 1000, -17 : -6) )

Introduction to Scientific & Engineering Computing 24 Array allocation Storage space that has been provided for the array can be released by execution of a deallocate statement: deallocate(Alimony, Alabaster) An array that has been deallocated may be reallocated with different Subscript bounds, but the rank cannot be changed. It is an error to allocate an array that is currently allocated or to deallocate an array that is not currently deallocated. The intrinsic function allocated may be used to determine whether or not an array is currently allocated. At the end of execution of the allocate statement, the designated Integer variable will be set to zero if the allocation is successful and to a positive value otherwise.

Introduction to Scientific & Engineering Computing 25 Exercises n do I = 1, 3 do J=1, 3 B(I,J) = I + J end do RESULT:

Introduction to Scientific & Engineering Computing 26 n do I =1, 3 do J = 1, 3 if ( I < J) then A(I,J) = -1 else if (I == J) then A(I,J)= 0 else A(I,J) = 1 end if end do Exercises