1 / 15

Data Structure (Part III)

Data Structure (Part III). Chapter 2 – Arrays. 2.4.4 Matrix Multiplication. j. i. We have to find all the elements in column j. Scan all the elements in smArray ? Exhausted! We can compute the transpose of b . This puts all column elements in consecutive order. currRowBegin. R: 0

zita
Download Presentation

Data Structure (Part III)

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. Data Structure (Part III) Chapter 2 – Arrays

  2. 2.4.4 Matrix Multiplication j i • We have to find all the elements in column j. • Scan all the elements in smArray? Exhausted! • We can compute the transpose of b. • This puts all column elements in consecutive order.

  3. currRowBegin R: 0 C: 0 R: 0 C: 4 R: 1 C: 1 R: 2 C: 1 R: 5 C: 0 R: 2 C: 5 R: 3 C: 2 R: 3 C: 0 15 22 -15 11 3 -6 -6 3 -15 91 28 91 15 22 11 28 R: 0 C: 0 R: 0 C: 3 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 6 C: -1 R: 5 C: 2 currColB 15 22 -15 11 3 -6 91 28 currRowA currRowIndex currColIndex R: 0 C: 0 R: 0 C: 3 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 5 C: 2 To get (0, 0) To get inner product Line 32: …bXpose.smArray[currColIndex].row != currColB Program 2.14 dummy Sum = 0 Sum = 225 d: 225 (1)

  4. currRowBegin R: 0 C: 0 R: 0 C: 4 R: 1 C: 1 R: 5 C: 0 R: 2 C: 1 R: 2 C: 5 R: 3 C: 2 R: 3 C: 0 15 22 -15 11 3 -6 -6 22 15 91 -15 11 3 28 91 28 R: 0 C: 0 R: 0 C: 3 R: 0 C: 5 R: 1 C: 1 R: 1 C: 1 R: 1 C: 2 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 5 C: 2 R: 6 C: -1 currColB currColB 15 22 -15 11 11 3 3 -6 91 28 currRowA currRowIndex currColIndex currColIndex R: 0 C: 0 R: 0 C: 3 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 5 C: 2 Line 53: while (smArray[currRowIndex].row == currRowA) Line 54: currRowIndex++; Line 51: else currColIndex++; Leave the while-loop (Line 20) Line 32: …bXpose.smArray[currColIndex].row != currColB dummy Sum = 0 Sum = -225 d: 225 -420 330 -225 (2)

  5. currRowBegin R: 0 C: 0 R: 5 C: 0 R: 3 C: 0 R: 2 C: 5 R: 2 C: 1 R: 3 C: 2 R: 1 C: 1 R: 0 C: 4 15 22 -15 11 3 -6 3 15 28 91 91 -6 11 -15 22 28 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 5 C: 2 R: 6 C: -1 currColB 11 3 -6 91 28 currRowA currColIndex currRowIndex R: 0 C: 0 R: 0 C: 3 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 5 C: 2 Line 28: while (bXpose.smArray[currColIndex].row == currColB) Line 29: currColIndex++; Line 30: currColB = bXpose.smArray[curColIndex].row; Line 22: if (smArray[currRowIndex].row != currRowA) Line 26: currRowIndex = currBeginRow; dummy Sum = 0 Sum = 1365 d: 225 -420 330 -225 121 33 -18 1365 (3)

  6. 2.5 Representation of Arrays • Multidimensional arrays are usually implemented by one-dimensional array. • can be retrieve efficiently. • Consider an array that is declared a[u1][u2],…,[un] • The number of elements required in the 1D array is

  7. 0 1 2 3 4 a[0][0][0][0] a[0][0][0][1] a[0][0][1][0] a[0][0][1][1] a[0][1][0][0] Representation of Arrays • Example • a[2][3][2][2] • The total number of elements: • 2*3*2*2 = 24 • One of the common ways to represent an array is in row major order.

  8. Representation of Arrays • Row major order a [2] [2] [3] [2] a 0 1 0 1 0 1 0 1 2 0 1 2 0 1 2 0 1 2 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 How to find the position in the 1D array for a[i1][i2][i3][i4]?

  9. a[0] a[1] a[2] …… a[i] … a[u1-1] Array element: … α+i …… α+2 α … α+1 α+u1-1 address: Row Representation of 1D Array • Suppose a[u1] is a 1D array with length of u1 and α is the address of a[0]. • The array can be represented in sequential memory. • The address of a[i] is • α+ i

  10. Row Representation of 2D Array col 0 col 1 col 2 col u2-1 a[0][0] a[0][1] a[0][2] a[0][u2-1] • Suppose a[u1] [u2] is a 2D array with u1 rows and u2 colums. • α is the address of a[0][0]. row 0 a[1][0] a[1][1] a[1][2] a[1][u2-1] row 1 a[u1-1][0] a[u1-1][1] a[u1-1][2] a[u1-1][u2-1] row u1 a[i][j] The address of a[i][j] isα+i*u2+ j. u2 elements u2 row 0 row 1 row i row u1-1 α i*u2 elements α+ i*u2

  11. Column Representation of 2D Array col 0 col 1 col 2 col u2-1 a[0][1] a[0][2] a[0][u2-1] a[0][0] • Column major representation row 0 a[1][0] a[1][1] a[1][2] a[1][u2-1] row 1 a[u1-1][0] a[u1-1][1] a[u1-1][2] a[u1-1][u2-1] row u1 a[i][j] The address of a[i][j] isα+j*u1+ i. u1 elements u1 col 0 col 1 col j col u2-1 α j*u1 elements α+ j*u1

  12. Row Representation of 3D Array • Suppose a[u1][u2] [u3] is a 3D array. • α is the address of a[0][0]. This array can be interpreted as u1 2D arrays of dimensional u2*u3. u2 u1 u3

  13. Row Representation of 3D Array • Sequential row major representation of a 3D array. Each 2D array is represented as mentioned previously. iu1u2 elements α a(0, u2, u3) a(1, u2, u3) a(i, u2, u3) a(u1-1, u2, u3) The address of a[i][j][k] isα+i*u2u3+ j*u2+k.

  14. Formula for n-Dimensional Array • Suppose a[u1][u2] [u3],…,[un] is a n-dimensional array. • α is the address of a[0][0],…,[0]. • The address of a[i1][i2],…,[in] is:

  15. Exercise • Let A be a 2D array. Suppose the address of A(1, 1) is 644, and A(3, 3) is 676. What is the address of A(14, 14)? Assume A has m rows and n columns. Let the address of A(0, 0) is α. Therefore, α + 1*n + 1 = 644 α + 3*n + 3 = 676 Obtain α = 628, n = 15. Thus, the address of A(14, 14) is α + 14*n + 14 = 852

More Related