Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.

Similar presentations


Presentation on theme: "Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions."— Presentation transcript:

1 Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions and Matrices

2 Section 4.6Matrices1 Matrix Data about many kinds of problems can often be represented using a rectangular arrangement of values; such an arrangement is called a matrix. A is a matrix with two rows and three columns. The dimensions of the matrix are the number of rows and columns; here A is a 2  3 matrix. Elements of a matrix A are denoted by a ij, where i is the row number of the element in the matrix and j is the column number. In the example matrix A, a 23 = 8 because 8 is the element in row 2, column 3, of A.

3 Section 4.6Matrices2 Example: Matrix The constraints of many problems are represented by the system of linear equations, e.g.: x + y = 70 24x + 14y = 1180 The solution is x = 20, y = 50 (you can easily check that this is a solution). The matrix A is the matrix of coefficients for this system of linear equations.

4 Section 4.6Matrices3 Matrix If X = Y, then x = 3, y = 6, z = 2, and w = 0. We will often be interested in square matrices, in which the number of rows equals the number of columns. If A is an n  n square matrix, then the elements a 11, a 22,..., a nn form the main diagonal of the matrix. If the corresponding elements match when we think of folding the matrix along the main diagonal, then the matrix is symmetric about the main diagonal. In a symmetric matrix, a ij = a ji.

5 Section 4.6Matrices4 Matrix Operations Scalar multiplication calls for multiplying each entry of a matrix by a fixed single number called a scalar. The result is a matrix with the same dimensions as the original matrix. The result of multiplying matrix A: by the scalar r = 3 is:

6 Section 4.6Matrices5 Matrix Operations Addition of two matrices A and B is defined only when A and B have the same dimensions; then it is simply a matter of adding the corresponding elements. Formally, if A and B are both n  m matrices, then C = A + B is an n  m matrix with entries c ij = a ij + b ij :

7 Section 4.6Matrices6 Matrix Operations Subtraction of matrices is defined by A  B = A + (  l)B In a zero matrix, all entries are 0. If we add an n  m zero matrix, denoted by 0, to any n  m matrix A, the result is matrix A. We can symbolize this by the matrix equation: 0 + A = A If A and B are n  m matrices and r and s are scalars, the following matrix equations are true: A + B = B + A (A + B) + C = A + (B + C) r(A + B) = rA + rB (r + s)A = rA + sA r(sA) = (rs)A rA = Ar

8 Section 4.6Matrices7 Matrix Operations Matrix multiplication is computed as A times B and denoted as A  B. Condition required for matrix multiplication: the number of columns in A must equal the number of rows in B. Thus we can compute A  B if A is an n  m matrix and B is an m  p matrix. The result is an n  p matrix. An entry in row i, column j of A  B is obtained by multiplying elements in row i of A by the corresponding elements in column j of B and adding the results. Formally, A  B = C, where

9 Section 4.6Matrices8 Example: Matrix Multiplication To find A  B = C for the following matrices: Similarly, doing the same for the other row, C is:

10 Section 4.6Matrices9 Matrix Multiplication Compute A  B and B  A for the following matrices: Note that even if A and B have dimensions so that both A  B and B  A are defined, A  B need not equal B  A.

11 Section 4.6Matrices10 Matrix Multiplication Where A, B, and C are matrices of appropriate dimensions and r and s are scalars, the following matrix equations are true (the notation A (B  C) is shorthand for A  (B  C)): A (B  C) = (A  B) C A (B + C) = A  B + A  C (A + B) C = A  C + B  C rA  sB = (rs)(A  B) The n  n matrix with 1s along the main diagonal and 0s elsewhere is called the identity matrix, denoted by I. If we multiply I times any nn matrix A, we get A as the result. The equation is: I  A = A  I = A

12 Section 4.6Matrices11 Matrix Multiplication Algorithm ALGORITHM MatrixMultiplication //computes n  p matrix A  B for n  m matrix A, m  p matrix B //stores result in C for i = 1 to n do for j = 1 to p do C[i, j] = 0 for k =1 to m do C[i, j] = C[i, j] + A[i, k] * B[k, j] end for write out product matrix C If A and B are both n  n matrices, then there are  (n 3 ) multiplications and  (n 3 ) additions required. Overall complexity is  (n 3 )


Download ppt "Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions."

Similar presentations


Ads by Google