Lecture+19 - Engineering 101 Engineering 101 Lecture 19...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Engineering 101 Lecture 19 11/8/07 Matrices and Linear Equations Quote of the Day Quote of the Day Honest disagreement is often a good sign of progress. ­ Mahatma Gandhi Project 6 Project 6 I am teh king of teh cipherz! Matrices Matrices One very important data structure for doing engineering and scientific computation is the matrix. A matrix is a two dimensional array. A matrix can be represented as a vector of vectors. 125 241 0 2 -1 Matrices Matrices We can think of labeling each element of the matrix by its row and column A11 A12 A13 A21 A22 A23 A31 A32 A33 Aij = Matrices Matrices The numbers of rows and columns can be different from each other A11 A12 A13 A21 A22 A23 A31 A32 A33 Aij = Matrices and Vectors Matrices and Vectors Matrices and vectors can be multiplied by each other. To do this the number of columns in the matrix must equal the number of elements in the vector. A11 A12 A13 A21 A22 A23 A31 A32 b1 b2 b3 Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. b1 b2 b3 A11b1+A12b2+A1 3b3 A11 A12 A13 A21 A22 A23 A31 A32 = Matrices and Vectors Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. b1 b2 b3 A11b1+A12b2+A1 3b3 A21b1+A22b2+A2 3b3 A11 A12 A13 A21 A22 A23 A31 A32 = Matrices and Vectors Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. b1 b2 b3 A11b1+A12b2+A1 3b3 A21b1+A22b2+A2 3b3 A31b1+A32b2+A3 A11 A12 A13 A21 A22 A23 A31 A32 = Matrices and Vectors Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. b1 b2 b3 A11b1+A12b2+A1 3b3 A21b1+A22b2+A2 3b3 A31b1+A32b2+A3 A11 A12 A13 A21 A22 A23 A31 A32 = Matrices and Vectors Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. 0 7 1 0 -1 0 0 1 3 2 1 4*3 + 0*2 + (-1)*1 4 3 0 1 = 3*3 + 7*2 + 0 *1 0*3 + 1*2 + 0*1 1*3 + 0*2 + 1*1 Matrices and Vectors Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. 0 7 1 0 -1 0 0 1 3 2 1 12 + 0 – 1 4 3 0 1 = 9 + 14 + 0 0+2+0 3+0+1 Matrices and Vectors Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. 0 7 1 0 -1 0 0 1 3 2 1 11 4 3 0 1 = 23 2 4 Matrices and Vectors Matrices and Vectors A matrix times a vector gives another vector. An (M x N) matrix multiplied by a size N vector gives a size M vector 4 0 -1 3 11 3 0 1 7 1 0 0 0 1 2 1 = 23 2 4 1 0 ­1 1­ 2­ 2 ­1 0 7 5 1 3 2 ­5 6 4 Exercise Exercise = ? 3­ 7 ­5 ­6 4­ 5 6 8 2 ­1 ­6 8 1 0 ­1 1­ 2­ 2 ­1 0 7 5 1 3 2 ­5 6 4 Exercise Exercise = ? 3­ 7 ­5 ­6 4­ 5 6 8 2 ­1 ­6 8 Which program performs matrix multiplication? 1. 2. 3. Which program performs matrix multiplication? 1. 2. 3. Which program performs a matrix multiplication? 2. template <class G> vector <G> multiply( vector < vector <G> > A, vector <G> v){ int i, j; vector <G> ans(A.size(),0); for(i=0; i< A.size(); i++) for(j=0; j< A[ i ].size(); j++) ans[ i ] = ans[ i ] + A[ i ][ j ]*v[ j ]; return ans; } A little something extra: A little something extra: Command Line Arguments What if we want to take input from the user right when the program is run? % myProgram inputfile outputfile Solution: Convert your “int main()” into: int main(int argc, char * argv[ ]) { … } int main(int argc, char * argv[ ]) int main(int argc, char * argv[ ]) int argc Specifies the number of arguments that were on the command line There is at least 1 (i.e. the program’s name) char * argv[ ] This is a native array where each element is a C­ string To conveniently use them you must do something like: string comLine; What is the output using the given command line? What is the output using the given command line? iint main(int argc, char * argv[ ]) { nt argc, string command; string int i = 1; int while (i < argc) { while command = argv[i]; command i++; i++; if (command == “All”) if cout << “Are ”; cout else if (command == “Your”) else cout << “Belong ”; cout else if (command == “Base”) else cout << “To Us! ” << endl; cout else else cout << “Make Your Time!”; cout } } % runProgram All Your Base . 1 – Are Belong To Us! 2 – Are Belong To Us! Make Your Time! 3 – Someone set up us the bomb. 4 – For great justice. What is the output using the given command line? What is the output using the given command line? iint main(int argc, char * argv[ ]) { nt argc, string command; string int i = 1; int while (i < argc) { while command = argv[i]; command i++; i++; if (command == “All”) if cout << “Are ”; cout else if (command == “Your”) else cout << “Belong ”; cout else if (command == “Base”) else cout << “To Us! ” << endl; cout else else cout << “Make Your Time!”; cout } } % runProgram All Your Base . 1 – Are Belong To Us! 2 – Are Belong To Us! Make Your Time! 3 – Someone set up us the bomb. 4 – For great justice. Matrices and Linear Equations Matrices and Linear Equations Matrices are so useful because they can be used to represent linear equations: w = 4 a – c x = 3 a + 7 b y = b z = a + c Matrices and Linear Equations Matrices and Linear Equations Matrices are so useful because they can be used to represent linear equations: w = 4 a – c x = 3 a + 7 b y = b z = a + c w x y z = A11 A12 A13 A21 A22 A23 A31 A32 a b c Matrices and Linear Equations Matrices and Linear Equations Matrices are so useful because they can be used to represent linear equations: w = 4 a – c x = 3 a + 7 b y = b z = a + c w x y z 4 0 7 1 0 -1 0 0 1 a b c = 3 0 1 What are they good for? What are they good for? Consider the problem of tomography (CAT scans). A beam of radiation is passed through an object or person. By measuring the attenuation of the beam the density of the slice is determined. Tomography Tomography How can we compute the mass through which each beam passes? First row: 8 ρ 0 = m0 ρ 3 = 1.0 ρ 2 = 0.5 ρ 1 = 0.2 ρ 0 = 0.1 m0 m1 m2 m3 m3 m2 m1 Tomography Tomography How can we compute the mass through which each beam passes? First row: 8 ρ 0 = m0 Second row: 2 ρ 0 + 6 ρ 1 = m1 ρ 3 = 1.0 ρ 2 = 0.5 ρ 1 = 0.2 ρ 0 = 0.1 m0 m1 m2 m3 m3 m2 m1 Tomography Tomography How can we compute the mass through which each beam passes? First row: 8 ρ 0 = m0 Second row: 2 ρ 0 + 6 ρ 1 = m1 Third row: 2 ρ 0 + 2 ρ 1 + 4 ρ 2 = m2 ρ 3 = 1.0 ρ 2 = 0.5 ρ 1 = 0.2 ρ 0 = 0.1 m0 m1 m2 m3 m3 m2 m1 m0 Tomography Tomography How can we compute the mass through which each beam passes? First row: 8 ρ 0 = m0 Second row: 2 ρ 0 + 6 ρ 1 = m1 Third row: 2 ρ 0 + 2 ρ 1 + 4 ρ 2 = m2 Fourth row: 2 ρ 0 + 2 ρ 1 + 2 ρ 2 + 2 ρ 3 = m3 ρ 3 = 1.0 ρ 2 = 0.5 ρ 1 = 0.2 ρ 0 = 0.1 m0 m1 m2 m3 m3 m2 m1 m0 Tomography Tomography How can we compute the mass through which each beam passes? 8 ρ 0 = m0 2 ρ 0 + 6 ρ 1 = m1 2 ρ 0 + 2 ρ 1 + 4 ρ 2 = m2 2 ρ 0 + 2 ρ 1 + 2 ρ 2 + 2 ρ 3 = m3 ρ 3 = 1.0 ρ 2 = 0.5 ρ 1 = 0.2 ρ 0 = 0.1 m0 m1 m2 m3 m3 m2 m1 m0 Tomography Tomography How can we compute the mass through which each beam passes? 8 ρ 0 = m0 2 ρ 0 + 6 ρ 1 = m1 2 ρ 0 + 2 ρ 1 + 4 ρ 2 = m2 2 ρ 0 + 2 ρ 1 + 2 ρ 2 + 2 ρ 3 = m3 0 0 0 ρ 0 ρ 1 ρ2 ρ 3 = 1.0 ρ 2 = 0.5 ρ 1 = 0.2 ρ 0 = 0.1 8 0 0 m0 2 6 0 m1 2 24 m2 = m0 m1 m2 m3 m3 m2 m1 m0 Tomography Tomography 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 ρ 0 ρ 1 ρ2 ρ 3 = m0 m1 m2 m3 This problem is fairly easy to encode: Set up the matrix Read in the densities Print out the masses Tomography Tomography 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 ρ 0 ρ 1 ρ2 ρ 3 = vector <double> rho(4,0), m; vector < vector <double> > A(4,rho); m0 m1 m2 m3 set up identifiers Tomography Tomography 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 ρ 0 ρ 1 ρ2 ρ 3 = vector <double> rho(4,0), m; vector < vector <double> > A(4,rho); for(int i = 0; i < A.size(); i ++) for(int j = 0; j < A[i].size(); j ++) if( i == j ) A[ i ][ j ] = 8 – 2* i; else if ( j < i ) A[ i ][ j ] = 2; m0 m1 m2 m3 set up the matrix Tomography Tomography 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 ρ 0 ρ 1 ρ2 ρ 3 = vector <double> rho(4,0), m; vector < vector <double> > A(4,rho); for(int i = 0; i < A.size(); i ++) for(int j = 0; j < A[i].size(); j ++) if( i == j ) A[ i ][ j ] = 8 – 2* i; else if ( j < i ) A[ i ][ j ] = 2; for(int i = 0; i< rho.size( ); i++){ cout << “Input rho “ << i << “ : “ << endl; cin >> rho[ i ]; } m0 m1 m2 m3 read in the densities Tomography Tomography 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 ρ 0 ρ 1 ρ2 ρ 3 = vector <double> rho(4,0), m; vector < vector <double> > A(4,rho); for(int i = 0; i < A.size(); i ++) for(int j = 0; j < A[i].size(); j ++) if( i == j ) A[ i ][ j ] = 8 – 2* i; else if ( j < i ) A[ i ][ j ] = 2; for(int i = 0; i< rho.size( ); i++){ cout << “Input rho “ << i << “ : “ << endl; cin >> rho[ i ]; } m = multiply(A, rho); m0 m1 m2 m3 calculate the masses Tomography Tomography vector <double> rho(4,0), m; vector < vector <double> > A(4,rho); for(int i = 0; i < A.size(); i ++) for(int j = 0; j < A[i].size(); j ++) if( i == j ) A[ i ][ j ] = 8 – 2* i; else if ( j < i ) A[ i ][ j ] = 2; for(int i = 0; i< rho.size( ); i++){ cout << “Input rho “ << i << “ : “ << endl; cin >> rho[ i ]; } m = multiply(A, rho); // see next slide for this function for (i=0; i< m.size( ); i++ ) cout << m.at( i ) << “ “; 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 ρ 0 ρ 1 ρ2 ρ 3 = m0 m1 m2 m3 print out the masses Recall Matrix Multiply Recall Matrix Multiply template <class G> vector <G> multiply( vector < vector <G> > A, vector <G> v){ int i, j; vector <G> ans(A.size(),0); for(i=0; i< A.size(); i++) for(j=0; j< A[ i ].size(); j++) ans[ i ] = ans[ i ] + A[ i ][ j ]*v[ j ]; return ans; } News Flash! News Flash! One Laptop per Child http://video.on.nytimes.com/?fr_story =6ffd976ed367bacae4171dd4999d36431c84b0f5 Tomography Tomography Of course the problem of finding the mass of a slice is not the problem we are usually interested in solving. We can measure the masses of the slices by tomography. We want to know the density of the rings given the masses of the slices. This is straightforward if the matrix is triangular. The Inverse Problem The Inverse Problem 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 ρ 0 ρ 1 ρ2 ρ 3 = m0 m1 m2 m3 Getting the first density is easy: A00 ρ 0 = m0 ρ 0 = m0 / A00 The Inverse Problem The Inverse Problem 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 ρ 0 ρ 1 ρ2 ρ 3 = m0 m1 m2 m3 Getting the second density is a bit harder: A10 ρ 0 + A11 ρ 1 = m1 ρ 1 = (m1 ­ A10 ρ 0) / A11 The Inverse Problem The Inverse Problem 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 ρ 0 ρ 1 ρ2 ρ 3 = m0 m1 m2 m3 Getting the third density is similar: A20 ρ 0 + A21 ρ 1 + A22 ρ 2 = m2 ρ 2 = (m2 – A20 ρ 0 – A21 ρ 1) / A22 The Inverse Problem The Inverse Problem 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 ρ 0 ρ 1 ρ2 ρ 3 = m0 m1 m2 m3 The final solution: ρ 0 = m0 / A00 ρ 1 = (m1 ­ A10 ρ 0) / A11 ρ 2 = (m2 – A20 ρ 0 – A21 ρ 1) / A22 ρ 3 = (m3 – A30 ρ 0 – A31 ρ 1 – A32 ρ 2) / A33 Which of the functions solves the inverse problem given a matrix A and a vector m? 1. r0 = m0/A00 r1 = (m1–A10*r0)/A11 r2 = (m2–A20*r0–A21*r1)/A22 r3 = (m3–A30*r0–A31*r1–A32*r2)/A33 2. Which of the functions solves the inverse problem given a matrix A and a vector m? 1. r0 = m0/A00 r1 = (m1–A10*r0)/A11 r2 = (m2–A20*r0–A21*r1)/A22 r3 = (m3–A30*r0–A31*r1–A32*r2)/A33 2. template <class T> vector <T> solve( vector < vector <T> > A, vector <T> m){ The Inverse Problem The Inverse Problem template <class T> vector <T> solve( vector < vector <T> > A, vector <T> m){ vector <T> r(A.size( )); T num; The Inverse Problem The Inverse Problem return r; } template <class T> vector <T> solve( vector < vector <T> > A, vector <T> m){ vector <T> r(A.size( )); T num; for(int i=0; i < A.size( ); i++){ num = m [ i ]; The Inverse Problem The Inverse Problem } return r; } template <class T> vector <T> solve( vector < vector <T> > A, vector <T> m){ vector <T> r(A.size( )); T num; for(int i=0; i < A.size( ); i++){ num = m [ i ]; for(int j=0; j < i; j++) num = num – A[ i ][ j ] * r[ j ]; } return r; } The Inverse Problem The Inverse Problem template <class T> vector <T> solve( vector < vector <T> > A, vector <T> m){ vector <T> r(A.size( )); T num; for(int i=0; i < A.size( ); i++){ num = m [ i ]; for(int j=0; j < i; j++) num = num – A[ i ][ j ] * r[ j ]; r[ i ] = num / A[ i ][ i ]; } return r; } The Inverse Problem The Inverse Problem Some Inverse Problems are Easy Some Inverse Problems are Easy To Solve If the matrix is lower triangular (the upper triangle is all zeros and the diagonal elements are all non­zero) the problem is easily solvable The same is true if the matrix is upper triangular But most matrices are neither. Next Lecture Next Lecture MLC – Phase 3 ...
View Full Document

Ask a homework question - tutors are online