Matrices Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
Matrix Definition: An array of numbers in m rows and n colums is called an mxn matrix A square matrix of order n, is an (nxn) matrix.
Advertisements

Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.
Maths for Computer Graphics
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 12 System of Linear Equations.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
Introduction to Programming (in C++) Vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
MATRICES. Matrices A matrix is a rectangular array of objects (usually numbers) arranged in m horizontal rows and n vertical columns. A matrix with m.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
A matrix having a single row is called a row matrix. e.g.,
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
Gaussian elimination Jordi Cortadella Department of Computer Science.
Sequences Jordi Cortadella Department of Computer Science.
Matrices. A matrix, A, is a rectangular collection of numbers. A matrix with “m” rows and “n” columns is said to have order m x n. Each entry, or element,
Jordi Cortadella Dept. of Computer Science, UPC
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Two Dimensional Arrays
Polynomials Jordi Cortadella Department of Computer Science.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Sudoku Jordi Cortadella Department of Computer Science.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
Matrices Matrices A matrix (say MAY-trix) is a rectan- gular array of objects (usually numbers). An m  n (“m by n”) matrix has exactly m horizontal.
2009/9 1 Matrices(§3.8)  A matrix is a rectangular array of objects (usually numbers).  An m  n (“m by n”) matrix has exactly m horizontal rows, and.
ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Sorting Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Numerical algorithms Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Module #9: Matrices Rosen 5 th ed., §2.7 Now we are moving on to matrices, section 7.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Matrices and Determinants
Af2. The Queen Mother says “ Please be quite and let Patrick give the lecture. Thank you.”
Vectors Jordi Cortadella Department of Computer Science.
CS 450: COMPUTER GRAPHICS TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE.
Parameter passing Jordi Cortadella Department of Computer Science.
Combinatorial problems Jordi Cortadella Department of Computer Science.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
Prof. Amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays.
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.
Matrices Introduction.
CSE 504 Discrete Mathematics & Foundations of Computer Science
2-D Array.
Discrete Structures – CNS2300
Reasoning with invariants
CSCE 210 Data Structures and Algorithms
For loops, nested loops and scopes
L5 matrix.
Combinatorial problems
Rosen 5th ed., §2.7 ~18 slides, ~1 lecture
Jordi Cortadella Department of Computer Science
Lecture 10 Arrays.
Jordi Cortadella Department of Computer Science
CSE 541 – Numerical Methods
Matrices Introduction.
CS100: Discrete structures
Determinant of a Matrix
Search algorithms for vectors
Jordi Cortadella Department of Computer Science
Discrete Mathematics and its Applications
Slides by: Diana Inkpen and Alan Williams
Rosen 5th ed., §2.7 ~18 slides, ~1 lecture
Arrays and Matrices Prof. Abdul Hameed.
Applied Discrete Mathematics Week 4: Functions
Matrices and Determinants
Presentation transcript:

Matrices Jordi Cortadella Department of Computer Science

Matrices A matrix can be considered a two-dimensional vector, i.e. a vector of vectors. Introduction to Programming© Dept. CS, UPC2 // Declaration of a matrix with 3 rows and 4 columns vector > my_matrix(3,vector (4)); // A more elegant declaration typedef vector Row; // One row of the matrix typedef vector Matrix; // Matrix: a vector of rows Matrix my_matrix(3,Row(4)); // The same matrix as above my_matrix:

Matrices A matrix can be considered as a 2-dimensional vector, i.e., a vector of vectors. Introduction to Programming© Dept. CS, UPC3 my_matrix[0][2] my_matrix[1][3] my_matrix[2] my_matrix:

n-dimensional vectors Vectors with any number of dimensions can be declared: Introduction to Programming© Dept. CS, UPC4 typedef vector Dim1; typedef vector Dim2; typedef vector Dim3; typedef vector Matrix4D; Matrix4D my_matrix(5,Dim3(i+1,Dim2(n,Dim1(9))));

Sum of matrices Introduction to Programming© Dept. CS, UPC5

How are the elements of a matrix visited? By rows For every row i For every column j Visit Matrix[i][j] By columns For every column j For every row i Visit Matrix[i][j] Introduction to Programming© Dept. CS, UPC6 i j i j

Sum of matrices (by rows) typedef vector > Matrix; // Pre: A and B are non-empty matrices with the same size // Returns A+B (sum of matrices) Matrix matrix_sum(const Matrix& A, const Matrix& B) { int nrows = A.size(); int ncols = A[0].size(); Matrix C(nrows, vector (ncols)); for (int i = 0; i < nrows; ++i) { for (int j = 0; j < ncols; ++j) { C[i][j] = A[i][j] + B[i][j]; } return C; } Introduction to Programming© Dept. CS, UPC7

Sum of matrices (by columns) typedef vector > Matrix; // Pre: A and B are non-empty matrices with the same size // Returns A+B (sum of matrices) Matrix matrix_sum(const Matrix& A, const Matrix& B) { int nrows = A.size(); int ncols = A[0].size(); Matrix C(nrows, vector (ncols)); for (int j = 0; j < ncols; ++j) { for (int i = 0; i < nrows; ++i) { C[i][j] = A[i][j] + B[i][j]; } return C; } Introduction to Programming© Dept. CS, UPC8

Transpose a matrix Design a procedure that transposes a square matrix in place: void Transpose (Matrix& A); Observation: we need to swap the upper with the lower triangular matrix. The diagonal remains intact. Introduction to Programming© Dept. CS, UPC

Transpose a matrix // Interchanges two values void swap(int& a, int& b) { int c = a; a = b; b = c; } // Pre: A is a square matrix // Post: A contains the transpose of the input matrix void Transpose(Matrix& A) { int n = A.size(); for (int i = 0; i < n - 1; ++i) { for (int j = i + 1; j < n; ++j) { swap(A[i][j], A[j][i]); } } } Introduction to Programming© Dept. CS, UPC10

Is a matrix symmetric? Design a procedure that indicates whether a matrix is symmetric: bool is_symmetric(const Matrix& A); Observation: we only need to compare the upper with the lower triangular matrix. Introduction to Programming© Dept. CS, UPC symmetric not symmetric

Is a matrix symmetric? // Pre: A is a square matrix // Returns true if A is symmetric, and false otherwise bool is_symmetric(const Matrix& A) { int n = A.size(); for (int i = 0; i < n – 1; ++i) { for (int j = i + 1; j < n; ++j) { if (A[i][j] != A[j][i]) return false; } return true; } Introduction to Programming© Dept. CS, UPC12

Search in a matrix Design a procedure that finds a value in a matrix. If the value belongs to the matrix, the procedure will return the location (i, j) at which the value has been found. Otherwise, it will return (-1, -1). Introduction to Programming© Dept. CS, UPC13 // Pre: A is a non-empty matrix // Post: i and j define the location of a cell that // contains the value x in A. In case x is not // in A, then i = j = -1 void search(const Matrix& A, int x, int& i, int& j);

Search in a matrix // Pre: A is a non-empty matrix // Post: i and j define the location of a cell that contains the // value x in A. In case x is not in A, then i = j = -1 void search(const Matrix& A, int x, int& i, int& j) { int nrows = A.size(); int ncols = A[0].size(); for (i = 0; i < nrows; ++i) { for (j = 0; j < ncols; ++j) { if (A[i][j] == x) return; } i = -1; j = -1; } Introduction to Programming© Dept. CS, UPC14

Search in a sorted matrix A sorted matrix A is one in which A[i][j]  A[i][j+1] A[i][j]  A[i+1][j] Introduction to Programming© Dept. CS, UPC

Search in a sorted matrix Example: let us find 10 in the matrix. We look at the lower left corner of the matrix. Since 13 > 10, the value cannot be found in the last row. Introduction to Programming© Dept. CS, UPC

Search in a sorted matrix We look again at the lower left corner of the remaining matrix. Since 11 > 10, the value cannot be found in the row. Introduction to Programming© Dept. CS, UPC

Search in a sorted matrix Since 9 < 10, the value cannot be found in the column. Introduction to Programming© Dept. CS, UPC

Search in a sorted matrix Since 11 > 10, the value cannot be found in the row. Introduction to Programming© Dept. CS, UPC

Search in a sorted matrix Since 7 < 10, the value cannot be found in the column. Introduction to Programming© Dept. CS, UPC

Search in a sorted matrix The element has been found! Introduction to Programming© Dept. CS, UPC

Search in a sorted matrix Invariant: if the element is in the matrix, then it is located in the sub-matrix [0…i, j…ncols-1] Introduction to Programming© Dept. CS, UPC22 j i

Search in a sorted matrix // Pre: A is non-empty and sorted by rows and columns in ascending order // Post: i and j define the location of a cell that contains the value // x in A. In case x is not in A, then i=j=-1 void search(const Matrix& A, int x, int& i, int& j) { int nrows = A.size(); int ncols = A[0].size(); i = nrows - 1; j = 0; // Invariant: x can only be found in A[0..i,j..ncols-1] while (i >= 0 and j < ncols) { if (A[i][j] < x) j = j + 1; else if (A[i][j] > x) i = i – 1; else return; } i = -1; j = -1; } Introduction to Programming© Dept. CS, UPC23

Search in a sorted matrix What is the largest number of iterations of a search algorithm in a matrix? The search algorithm in a sorted matrix cannot start in all of the corners of the matrix. Which corners are suitable? Introduction to Programming© Dept. CS, UPC24 Unsorted matrixnrows × ncols Sorted matrixnrows + ncols

Matrix multiplication Design a function that returns the multiplication of two matrices. Introduction to Programming© Dept. CS, UPC25 // Pre: A is a non-empty n×m matrix, // B is a non-empty m×p matrix // Returns A×B (an n×p matrix) Matrix multiply(const Matrix& A, const Matrix& B); × =

Matrix multiplication // Pre: A is a non-empty n×m matrix, B is a non-empty m×p matrix // Returns A×B (an n×p matrix) Matrix multiply(const Matrix& A, const Matrix& B) { int n = A.size(); int m = A[0].size(); int p = B[0].size(); Matrix C(n, vector (p)); for (int i = 0; i < n; ++i) { for (int j = 0; j < p; ++j) { int sum = 0; for (int k = 0; k < m; ++k) { sum = sum + A[i][k]B[k][j]; } C[i][j] = sum; } } return C; } Introduction to Programming© Dept. CS, UPC26

Matrix multiplication // Pre: A is a non-empty n×m matrix, B is a non-empty m×p matrix // Returns A×B (an n×p matrix) Matrix multiply(const Matrix& A, const Matrix& B) { int n = A.size(); int m = A[0].size(); int p = B[0].size(); Matrix C(n, vector (p, 0)); for (int i = 0; i < n; ++i) { for (int j = 0; j < p; ++j) { for (int k = 0; k < m; ++k) { C[i][j] += A[i][k]B[k][j]; } } } return C; } Introduction to Programming© Dept. CS, UPC27 Initialized to zero AccumulationAccumulation The loops can be in any order

Matrix multiplication // Pre: A is a non-empty n×m matrix, B is a non-empty m×p matrix // Returns A×B (an n×p matrix) Matrix multiply(const Matrix& A, const Matrix& B) { int n = A.size(); int m = A[0].size(); int p = B[0].size(); Matrix C(n, vector (p, 0)); for (int j = 0; j < p; ++j) { for (int k = 0; k < m; ++k) { for (int i = 0; i < n; ++i) { C[i][j] += A[i][k]B[k][j]; } } } return C; } Introduction to Programming© Dept. CS, UPC28

Summary Matrices can be represented as vectors of vectors. N-dimensional matrices can be represented as vectors of vectors of vectors … Recommendations: – Use indices i, j, k, …, consistently to refer to rows and columns of the matrices. – Use const reference parameters (const Matrix&) whenever possible to avoid costly copies of large matrices. Introduction to Programming© Dept. CS, UPC29