1MatricesMike [email protected] – September 5, 2014Oregon State UniversityComputer GraphicsOregon State UniversityMatricesA matrix is a 2D array of numbers, arranged in rows that go across and columns that go down:12344 columnsA column:mjb – September 5, 2014Oregon State UniversityComputer Graphics56 7 891011123 rowsMatrix sizes are termed “#rows x #columns”, so this is a 3x4 matrixMatrix TransposeA matrix transpose is formed by interchanging the rows and columns:106295187654321Tmjb – September 5, 2014Oregon State UniversityComputer Graphics1284117312111098765This is a 3x4 matrix This is a 4x3 matrixSquare MatricesA square matrix has the same number of rows and columns1233 columnsmjb – September 5, 2014Oregon State UniversityComputer Graphics34567893 rowsThis is a 3x3 matrixRow and Column MatricesA matrix can have a single row (a “row matrix”) or just a single column (a “column matrix”)”12345mjb – September 5, 2014Oregon State UniversityComputer Graphics123This is a 3x1 matrix56This is a 1x3 matrixSometimes these are called row and column vectors, but that overloads the word “vector” and we won’t do it…Matrix MultiplicationThe basic operation of matrix multiplication is to pair-wise multiply a single row by a single column1234****mjb – September 5, 2014Oregon State UniversityComputer GraphicsABC12356*→32→4*1 + 5*2 + 6*31x3 3x1 1x12Matrix MultiplicationTwo matrices, A and B, can be multiplied if the number of columns in A equals the number of rows in B. The result is a matrix that has the same number of rows as A and the same number of columns as B.* Amjb – September 5, 2014Oregon State UniversityComputer Graphics I x KABCK x JI x JMatrix Multiplication in SoftwareHere’s how to remember how to do it:1. C = A * B2. [ I x J ] = [ I x K ] * [ K x J ]mjb – September 5, 2014Oregon State UniversityComputer GraphicsI x K K x JI x J=C[ i ][ j ] = A[ i ][ k ] * B[ k ][ j ] ;Matrix Multiplication in Softwarefor( int i = 0; i < numArows; i++ ){for( int j = 0; j < numBcols; j++ ){mjb – September 5, 2014Oregon State UniversityComputer GraphicsC[ i ][ j ] = 0.;for( int k = 0; k < numAcols; k++ ){C[ i ][ j ] += A[ i ][ k ] * B[ k ][ j ];}}}Note: numAcols must == numBrows !Matrix Multiplication in SoftwareNote that:C[ i ][ j ] = 0.;mjb – September 5, 2014Oregon State UniversityComputer Graphicsfor( int k = 0; k < numAcols; k++ ){C[ i ][ j ] += A[ i ][ k ] * B[ k ][ j ];}Is like saying:C[ i ][ j ] = A[i][0] * B[0][j] + A[i][1] * B[1][j] + A[i][2] * B[2][j] + A[i][3] * B[3][j] ;Matrix Multiplication where B and C are Column Matricesfor( int i = 0; i < numArows; i++ ){C[ i ] = 0.;for( int k = 0; k < numAcols; k++ ){C[ i ] += A[ i ][ k ]*B[ k ];mjb – September 5, 2014Oregon State UniversityComputer GraphicsC[ i ] += A[ i ][ k ] B[ k ];}}To help you remember this, think of the “C[ i ]“ lines as:C[ i ][ 0 ] = 0.;…C[ i ][ 0 ] += A[ i ][ k ] * B[ k ][ 0 ];A Special MatrixConsider the matrix * column situation below:000xzy xyz xyzyx zCAABCA ABCAA B mjb – September 5, 2014Oregon State UniversityComputer Graphicszyx z (,, )yzzyzxxzxyyxCABABABABABABThis gives:Which you hopefully recognize as the Cross Product AxB3DeterminantsThe determinant is important in graphics applications. It represents sort of a “scale factor”, when the matrix is used to represent a transformation.The determinant of a 2x2 matrix is easy:mjb – September 5, 2014Oregon State UniversityComputer GraphicsdetABADBCCDDeterminantsdetABCDEFGH IEF DF DE The determinant of a 3x3 matrix is done in terms of its component 2x2 sub-matrices:mjb – September 5, 2014Oregon State UniversityComputer Graphicsdet det detEF DF DEABCHIGIGH ()()( )AEI FH B DI FG C DH EG InversesThe matrix inverse is also important in graphics applications because it represents the undoing of the original transformation matrix. It is also useful in solving systems of simultaneous equations.The inverse of a 2x2 matrix is the transpose of the cofactor matrix divided by the determinant:mjb – September 5, 2014Oregon State UniversityComputer Graphics11ABDBCD C AAD BC Inverses1ABCDEFGH IThe determinant of a 3x3 matrix is done in terms of its component 2x2 sub-matrices:det det detdet det detEF DF DEHI GI GHBC AC ABHI GI GHBC AC AB mjb – September 5, 2014Oregon State UniversityComputer GraphicsThe determinant of 4x4 and larger matrices can be done in a similar way, but usually isn’t. Gauss Elimination is more efficient.det det detdetBC AC ABEF DF DEABCDEFGH
View Full Document