Array processing and Matrix manipulation

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Introduction to arrays
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
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.
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 10 Modules and programming with subroutines.
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.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Module 6 Matrices & Applications Chapter 26 Matrices and Applications I.
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.
Fortran: Specification Statements Session Six ICoCSIS.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Matlab tutorial course Lesson 2: Arrays and data types
Fortran: Program Units and Procedures Session Four ICoCSIS.
Multi-Dimensional Arrays
Week 8. Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3.
Week 8 Improving on building blocks Recursive procedures.
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.
Fortran 90, 95, and Fortran 90 ● Generalities: format changes, portable numerics ● Arrays: syntax, classes of arrays, dynamic storage ● Structures.
Module and Data Sharing. Programming in the Large Software, in general, is large having multiple units Multiple units designed and developed independently.
Week 3 Let's review! Fundamental data types List-directed input/output.
CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables.
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.
An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.
FORTRAN 90+ Yetmen Wang Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT.
Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007
Introduction to Programming Lecture # 43. Math Library Complex number Matrix Quadratic equation and their solution …………….…
Arrays Chapter 7.
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
MATHEMATICS Matrix Algebra
Programming For Nuclear Engineers Lecture 6 Arrays
MULTI-DIMENSIONAL ARRAY
Ch 13: ARRAY PROCESSING AND MATRIX MULTIPLICATION
Numeric Arrays Numeric Arrays Chapter 4.
2. Understanding VB Variables
JavaScript: Functions.
Two Dimensional Arrays
LINEAR MODELS AND MATRIX ALGEBRA
Arrays An Array is an ordered collection of variables
Multiple Dimension Arrays
VBScript Session 2 Dani Vainstein.
7 Arrays.
Midterm Review Programming in Fortran
Matlab tutorial course
By Deniz Savas, CiCS, Shef. Univ., 2015
Vectors and Matrices I.
CSCI N207 Data Analysis Using Spreadsheet
Constructors and destructors
JavaScript Arrays.
D2 Indices and matrices Matrix representation in computer systems:
Arrays Chapter 7.
Multidimensional array
VBScript Session 2 Dani Vainstein.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CIS16 Application Development and Programming using Visual Basic.net
7 Arrays.
mdul-cntns-sub-cmmnts2.f90
Multi-Dimensional Arrays
C++ Array 1.
Array processing and Matrix manipulation
Vector Spaces RANK © 2012 Pearson Education, Inc..
Presentation transcript:

Array processing and Matrix manipulation Week 11 Array processing and Matrix manipulation

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

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

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

2-d arrays dimension attribute: real, dimension(3, 4) :: a logical, 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)

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

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

Some basic array concepts... Rank number of its dimensions Examples: real, dimension(8) :: a integer, dimension(3, 100, 5) :: b logical, dimension(2,5,3,100,5) :: c

Some basic array concepts... Extent number of elements in each dimension Examples: real, dimension(8) :: a real, dimension(-4:3) :: b integer, dimension(3,-50:50,40:44) :: c integer, dimension(0:2, 101, -1:3) :: d

Some basic array concepts... Size total 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

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 Example logical, dimension(2,-3:3,4,0:9,5) :: c shape_c = (/ 2,7,4,10,5 /)

Some basic array concepts... Array element order first index of the element specification is varying most rapidly, … Example integer, dimension(3,4) :: a a(1,1), a(2,1), a(3,1) a(1,2), a(2,2), a(3,2) ...

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

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 /) ) 12 21 22

Array I/O Could be handled in three different ways As a list of individual array elements: a(1,1), a(4,3), … As the complete array: real, dimension(2,4) :: a print *, a a(1,1), a(2,1), a(1,2), a(2,2), ... As an array section

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

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

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

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

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

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

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

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

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

Allocatable arrays Deallocation deallocate(list_of_currently_allocated_arrays, [stat=status variable]) deallocate(my_array, personel_list, big_table)

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

Whole-array operations Relational operators follow the same rules: Example:

Masked array assignment where (mask_expression) array_assignment_statements end where where (my_array > 0) my_array = exp(my_array) end where

Masked array assignment where (mask_expression) array_assignment_statements elsewhere array_assignment_statements end where where (my_array > 0) my_array = exp(my_array) elsewhere my_array = 0.0 end where

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

Sub-arrays integer, dimension(-2:2, 0:4) :: tablo 2 3 4 5 6 4 5 6 7 8 1 2 3 4 5 tablo(-1:2, 1:3) 6 7 8 9 6 4 4 6 6 8 Rank-2

Sub-arrays integer, dimension(-2:2, 0:4) :: tablo 2 3 4 5 6 4 5 6 7 8 1 2 3 4 5 tablo(2, 1:3) 6 7 8 9 6 4 4 6 6 8 Rank-1