Arrays.

Slides:



Advertisements
Similar presentations
Linear Transformations and Matrices Jim Van Verth Lars M. Bishop
Advertisements

Chapter 6 Data Structures
Arrays and Matrices CSE, POSTECH. 2 2 Introduction Data is often available in tabular form Tabular data is often represented in arrays Matrix is an example.
Gauss – Jordan Elimination Method: Example 2 Solve the following system of linear equations using the Gauss - Jordan elimination method Slide 1.
Matrices CSE, POSTECH.
Arrays.
Arrays. 1D Array Representation In C 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
Section 1.7 Diagonal, Triangular, and Symmetric Matrices.
1.5 Elementary Matrices and a Method for Finding
1.7 Diagonal, Triangular, and Symmetric Matrices.
Autar Kaw Benjamin Rigsby Transforming Numerical Methods Education for STEM Undergraduates.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Psychology 202b Advanced Psychological Statistics, II January 18, 2011.
Matrix table of values
Chapter 1: Matrices Definition 1: A matrix is a rectangular array of numbers arranged in horizontal rows and vertical columns. EXAMPLE:
1.7 Diagonal, Triangular, and Symmetric Matrices 1.
Table of Contents Solving Systems of Linear Equations - Gaussian Elimination The method of solving a linear system of equations by Gaussian Elimination.
Arrays and Matrices 황승원 Fall 2010 CSE, POSTECH. 2 2 Any real-life matrix you know?
SNU IDB Lab. Ch8. Arrays and Matrices © copyright 2006 SNU IDB Lab.
Equivalence Relations
Sparse Matrices Matrix  table of values. Sparse Matrices Matrix  table of values Row 2 Column 4 4 x 5 matrix.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Arrays.
LEARNING OUTCOMES At the end of this topic, student should be able to :  D efination of matrix  Identify the different types of matrices such as rectangular,
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
Chapter 1 Section 1.6 Algebraic Properties of Matrix Operations.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
Introduction Types of Matrices Operations
Arrays. 1D Array Representation In C++ 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is.
MATRICES A rectangular arrangement of elements is called matrix. Types of matrices: Null matrix: A matrix whose all elements are zero is called a null.
Vocab: Matrices – Determinants and Inverses (Intro) Terms: Identity matrix – square matrix with ‘1’s in the main diagonal and zeros in every other entry.
MTH108 Business Math I Lecture 20.
Array in C# Array in C# RIHS Arshad Khan
nhaa/imk/sem /eqt101/rk12/32
Matrices.
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
2-D Array.
1.5 Matricies.
Java Array Object Chuen-Liang Chen Department of Computer Science
Arrays.
Linear Algebraic Equations and Matrices
L9Matrix and linear equation
MULTI-DIMENSIONAL ARRAY
Chapter 7: Array.
Chapter 8: Lesson 8.1 Matrices & Systems of Equations
Reminder: Array Representation In C++
بردارها و ماتريس ها ساختمان داده ها
Reminder: Array Representation In C++
Matrix Algebra - Overview
Declaration, assignment & accessing
Determinants.
Graph Operations And Representation
CSE 541 – Numerical Methods
MSIS 655 Advanced Business Applications Programming
RECORD. RECORD COLLABORATE: Discuss: Is the statement below correct? Try a 2x2 example.
Determinant of a Matrix
Multidimensional array
Arrays.
Arrays Week 2.
8-1 Introduction a) Plane Stress y
Dr Tripty Singh Arrays.
Multi-Dimensional Arrays
Arrays.
Laboratory in Oceanography: Data and Methods
C++ Array 1.
Reminder: Array Representation In C++
Arrays and Matrices Prof. Abdul Hameed.
ENERGY 211 / CME 211 Lecture 11 October 15, 2008.
Presentation transcript:

Arrays

1D Array Representation In C++ Memory a b c d start 1-dimensional array x = [a, b, c, d] map into contiguous memory locations location(x[i]) = start + i

Space Overhead space overhead = 4 bytes for start Memory a b c d start space overhead = 4 bytes for start (excludes space needed for the elements of x) What about #bytes/element of array? Excluded in above analysis.

2D Arrays The elements of a 2-dimensional array a declared as: int a[3][4]; may be shown as a table a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]

Rows Of A 2D Array a[0][0] a[0][1] a[0][2] a[0][3] row 0

Columns Of A 2D Array a[0][0] a[0][1] a[0][2] a[0][3]

2D Array Representation In C++ 2-dimensional array x a, b, c, d e, f, g, h i, j, k, l view 2D array as a 1D array of rows x = [row0, row1, row 2] row 0 = [a,b, c, d] row 1 = [e, f, g, h] row 2 = [i, j, k, l] and store as 4 1D arrays

2D Array Representation In C++ b c d e f g h i j k l x[] The 4 separate arrays are x, x[0], x[1], and x[2]. 4 separate 1-dimensional arrays

Space Overhead space overhead = overhead for 4 1D arrays = 4 * 4 bytes g h i j k l x[] space overhead = overhead for 4 1D arrays = 4 * 4 bytes = 16 bytes = (number of rows + 1) x 4 bytes

Array Representation In C++ b c d e f g h i j k l x[] This representation is called the array-of-arrays representation. Requires contiguous memory of size 3, 4, 4, and 4 for the 4 1D arrays. 1 memory block of size number of rows and number of rows blocks of size number of columns

Row-Major Mapping Example 3 x 4 array: a b c d e f g h i j k l Convert into 1D array y by collecting elements by rows. Within a row elements are collected from left to right. Rows are collected from top to bottom. We get y[] = {a, b, c, d, e, f, g, h, i, j, k, l} Used in Pascal and Fortran for example. row 0 row 1 row 2 … row i

Locating Element x[i][j] row 0 row 1 row 2 … row i c 2c 3c ic assume x has r rows and c columns each row has c elements i rows to the left of row i so ic elements to the left of x[i][0] so x[i][j] is mapped to position ic + j of the 1D array

Space Overhead 4 bytes for start of 1D array + row 0 row 1 row 2 … row i 4 bytes for start of 1D array + 4 bytes for c (number of columns) = 8 bytes Although, for our example the difference in space overhead between the two representation methods is small, 20 bytes, the space overhead for the row-major scheme is 8 bytes regardless of the number of rows in the array. In the array of arrays scheme, the space overhead is 4 * (r+1) bytes. So the overhead for an array with 10000 rows Is 40004 bytes when the array of array scheme is used and only 8 bytes when the row-major scheme is used.

Need contiguous memory of size rc. Disadvantage Need contiguous memory of size rc.

Column-Major Mapping a b c d e f g h i j k l Convert into 1D array y by collecting elements by columns. Within a column elements are collected from top to bottom. Columns are collected from left to right. We get y = {a, e, i, b, f, j, c, g, k, d, h, l}

Matrix Table of values. Has rows and columns, but numbering begins at 1 rather than 0. a b c d row 1 e f g h row 2 i j k l row 3 Use notation x(i,j) rather than x[i][j]. May use a 2D array to represent a matrix.

Shortcomings Of Using A 2D Array For A Matrix Indexes are off by 1. C++ arrays do not support matrix operations such as add, transpose, multiply, and so on. Suppose that x and y are 2D arrays. Can’t do x + y, x –y, x * y, etc. in C++. Develop a class matrix for object-oriented support of all matrix operations. See text. The class Matrix uses the row-major representation of a matrix.

An n x n matrix in which all nonzero terms are on the diagonal. Diagonal Matrix An n x n matrix in which all nonzero terms are on the diagonal. The text considers several kinds of special matrices—diagonal, tridiagonal, lower triangular, upper triangular, and symmetric. Applications are given in the text and exercises.

Diagonal Matrix 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 x(i,j) is on diagonal iff i = j number of diagonal elements in an n x n matrix is n non diagonal elements are zero store diagonal only vs n2 whole

Lower Triangular Matrix An n x n matrix in which all nonzero terms are either on or below the diagonal. 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 x(i,j) is part of lower triangle iff i >= j. number of elements in lower triangle is 1 + 2 + … + n = n(n+1)/2. store only the lower triangle

Array Of Arrays Representation 1 2 3 4 5 6 x[] 7 8 9 l0 Use an irregular 2-D array … length of rows is not required to be the same.

Creating And Using An Irregular Array // declare a two-dimensional array variable // and allocate the desired number of rows int ** irregularArray = new int*[numberOfRows]; // now allocate space for elements in each row for (int i = 0; i < numberOfRows; i++) irregularArray[i] = new int [length[i]]; // use the array like any regular array irregularArray[2][3] = 5; irregularArray[4][6] = irregularArray[2][3]+2; irregularArray[1][1] += 3;

Map Lower Triangular Array Into A 1D Array Use row-major order, but omit terms that are not part of the lower triangle. For the matrix 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 we get 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 If the 1D array that is used to store the lower triangle is y, then following the above mapping y[i] = i +1 . y[] = {1, 2, 3, 4, …, 10}

Index Of Element [i][j] r 1 r2 r3 … row i 1 3 6 Order is: row 1, row 2, row 3, … Row i is preceded by rows 1, 2, …, i-1 Size of row i is i. Number of elements that precede row i is 1 + 2 + 3 + … + i-1 = i(i-1)/2 So element (i,j) is at position i(i-1)/2 + j -1 of the 1D array. When using the irregular array mapping of a lower triangular matrix we are able to use conventional Java syntax to access elements, but for large n, the space overhead is quite a bit more than when the row-major mapping into a 1D array is used. The row-major mapping requires you to develop a formula to access elements.