1MatricesMike [email protected] State Universitymjb – September 5, 2014Oregon State UniversityComputer GraphicsMatricesA matrix is a 2D array of numbers, arranged in rows that go across and columns that go down:A column:123456 7 84 columns3 rowsmjb – September 5, 2014Oregon State UniversityComputer Graphics9101112Matrix sizes are termed “#rows x #columns”, so this is a 3x4 matrix2Matrix TransposeA matrix transpose is formed by interchanging the rows and columns:128411731062951121110987654321Tmjb – September 5, 2014Oregon State UniversityComputer Graphics1284This is a 3x4 matrix This is a 4x3 matrixSquare MatricesA square matrix has the same number of rows and columns1234563 columns3 rowsmjb – September 5, 2014Oregon State UniversityComputer Graphics789This is a 3x3 matrix3Row and Column MatricesA matrix can have a single row (a “row matrix”) or just a single column (a “column matrix”)”123456This is a 1x3 matrixmjb – September 5, 2014Oregon State UniversityComputer GraphicsThis is a 3x1 matrix6Sometimes 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 columnAC123456*→32***→4*1 + 5*2 + 6*3mjb – September 5, 2014Oregon State UniversityComputer GraphicsAB61x3 3x1 1x14Matrix 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. * ABCmjb – September 5, 2014Oregon State UniversityComputer GraphicsI x K K x JI x JMatrix Multiplication in SoftwareHere’s how to remember how to do it:1C=A*B1.C = A B2. [ I x J ] = [ I x K ] * [ K x J ]I x K K x JI x J=mjb – September 5, 2014Oregon State UniversityComputer GraphicsC[ i ][ j ] = A[ i ][ k ] * B[ k ][ j ] ;5Matrix Multiplication in Softwarefor( int i = 0; i < numArows; i++ )(;;){for( int j = 0; j < numBcols; j++ ){C[ i ][ j ] = 0.;for( int k = 0; k < numAcols; k++ ){mjb – September 5, 2014Oregon State UniversityComputer GraphicsC[ i ][ j ] += A[ i ][ k ] * B[ k ][ j ];}}}Note: numAcols must == numBrows !Matrix Multiplication in SoftwareNote that:C[ i ][ j ] = 0.;for( int k = 0; k < numAcols; k++ ){C[ i ][ j ] += A[ i ][ k ] * B[ k ][ j ];}mjb – September 5, 2014Oregon State UniversityComputer Graphics}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] ;6Matrix Multiplication where B and C are Column Matricesfor( int i = 0; i < numArows; i++ ){C[ i ] 0C[ i ] = 0.;for( int k = 0; k < numAcols; k++ ){C[ i ] += A[ i ][ k ] * B[ k ];}}mjb – September 5, 2014Oregon State UniversityComputer GraphicsTo 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:0xzy xCAAB 00yzxyzyx zCA ABCAA B ()CABABABABABABThis gives:mjb – September 5, 2014Oregon State UniversityComputer Graphics(,,)yz zy zx xz xy yxCABABABABABAB Which you hopefully recognize as the Cross Product AxB7DeterminantsThe 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:detABADBCCDmjb – September 5, 2014Oregon State UniversityComputer GraphicsDeterminantsdetABCDEFThe determinant of a 3x3 matrix is done in terms of its component 2x2 sub-matrices:detDEFGH Idet det detEF DF DEABCHIGIGH mjb – September 5, 2014Oregon State UniversityComputer Graphics()()( )AEI FH B DI FG C DH EG 8InversesThe 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 dividedThe inverse of a 2x2 matrix is the transpose of the cofactor matrix divided by the determinant:11ABDBCD C AAD BC mjb – September 5, 2014Oregon State UniversityComputer GraphicsCD C AAD BC Inverses1ABCDEFThe determinant of a 3x3 matrix is done in terms of its component 2x2 sub-matrices:det det detEF DF DEHI GI GH DEFGH Idet det detdet det detBC AC ABHI GI GHBC AC ABEF DF DEABC 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 DEFGH
View Full Document