270 likes | 472 Views
GG313 Lecture 12. Matrix Operations Sept 29, 2005. Matrix Addition. Requirement: same order Characteristic: element-by-element addition Notation: A + B = C. Eqn 3.20. 3 2 C=A+B = 0 4 1 -1. 1 1 A = 1 1
E N D
GG313 Lecture 12 Matrix Operations Sept 29, 2005
Matrix Addition Requirement: same order Characteristic: element-by-element addition Notation: A+B=C Eqn 3.20 3 2 C=A+B = 0 4 1 -1 1 1 A = 1 1 1 1 2 1 B = -1 3 0 -2
Commutative: A+B=B+A Associative: A+B+C= A+(B+C) You can add a scalar to a matrix: 1 1 A = 1 1 B = A + 4 = 1 1 5 5 5 5 5 5 You can also multiply a scalar by a matrix: 2 4 A = 4 6 B = A * 2 = 1 3 4 8 8 12 2 6
Dot Product Also known as: scalar product or inner product Requirements: row vector and column vector of the same order (dimensions) Characteristics: Result is a scalar Uses: Big in physics. The dot product between a force and a velocity yields the power being given to a body Notation:a • b = d Eqn. 3.25
In physics, we multiply the projection of one vector onto the other vector by the length of the second vector: a•b=uaub+vavb+wawb=|a||b|cos() |a|=sqrt(ua2+va2+wa2)=length of a If a and b are orthogonal to eachother, then cos() =0 and a•b=0 If a and b are parallel to eachother, then cos() =1 and a•b= |a||b|= product of the lengths of the vectors
Squaring a vector is simply taking the dot product of the vector and its transpose: For a row vector: a2 = a • aT For a column vector:a2 = aT • a Matrix Multiplication We can extend the idea of the dot product to matrices: Requirement: There must be the same number of columns in the first matrix as rows in the second. Characteristics: The resulting matrix has the same number of rows as the first matrix and same number of columns as the second.
Notation: C(m,n)=A(m,p)• B(p,n) elements of C are: In Matlab, matrix multiplication is: C = A*B
The order of multiplication is important: NOT Commutative: A • B ≠ B • A Associative: (A • B) • C= A • (B • C) On the previous page, we have an order A(5,3) matrix times a B(3,4) matrix. B•A is not even possible. If the matrices are square, or if AT has the same order as B, or BT the same order as A, then both products, A•B and B•A can be formed. The transpose of a matrix product is equal to the product of the transpose of the matrices in reverse order: A • B • C = CT • BT • AT
Multiplication by the identity matrix (Matlab: eye(p,p) ) leaves the original matrix unchanged - just as multiplying any regular number by “1” leaves that number unchanged. A(n,p) • I(p,p) = I • A = A For example:
Scaling: We can scale a coordinate system by multiplying by a diagonal matrix of scale weights:
On the previous slide we pre-multiplied by the scaling matrix. If we post-multiply instead,then each column gets multiplied by the same scaling factor: Note that we had to use a diagonal order 5x5 matrix.
Efficiency considerations: When dealing with large matrices, it is possible to save a considerable amount of calculating by being careful how multiplies are arranged. While there may be no difference in the result mathematically, if the number of calculations can be significantly reduced, time can be reduced and precision can be improved.
Note that while (A•B)•C = A•(B•C), these two expressions can have very different numbers of multiplications. D(m,n) = A(m,p)• B(p,n) has m*n*p multiplications E(m,q) = D(m,n) • C(n,q) has m*n*q multiplications So E=(A•B)•C requires m*n(p+q) multiplications Similarly, F(p,q)=B(p,n)•C(n,q)has n*p*q multiplications E(m,q)= A(m,p)• F(p,q) has m*p*q multiplications So E=A•(B•C) requires p*q*(m+n) multiplications. If p*q >> m*n, then A•(B•C) will require far more multiplications than (A•B)•C.
Fore example, say m=5, n=2, p=25, and q=100. Then one way requires 25*100*(5+2)=17,500 multiplications, and the other requires 5*2*(25+100)=1,250. As execution time and “digital noise”, or round off error, increase with every multiplication, it makes sense to perform the calculation in a particular way. In class exercises. 1) We have three vectors A, B, and C with one end at the origin and the other at xi,yi,zi. A(x,y,z)=(22,7,100), B=(-32,1,48), and C=(3,-11,33). Using Matlab matrices, transform the vectors into another coordinate system where r=3x and s=5y, t=2.5z.
We set up a matrix with the x-values in the first row, y in the second, and z in the third: >> A=[22 -32 3;7 1 -11;100 48 33] A = 22 -32 3 7 1 -11 100 48 33 Now set up a diagonal matrix containing the transformations: >> D=[3 0 0;0 5 0; 0 0 2.5] D = 3.0 0 0 0 5.0 0 0 0 2.5
Now multiply: >> D*A ans = 66.0 -96.0 9.0 35.0 5.0 -55.0 250.0 120.0 82.5 This is a simple transformation; others, like changing from rectangular to spherical coordinates utilize more terms in the transformation matrix.
DETERMINANT: A property of a matrix very useful in the solution of simultaneous equations. Requirement: Square matrices only Notation: det A, |A|, or ||A|| Calculation: As the matrix size grows, Matlab comes in handy, det(A).
Determinant Characteristics: • • Switching two rows or columns changes the sign. • • Scalars can be factored out of rows and columns. • • Multiplication of a row or column by a scalar multiplies the determinant by that scalar. • • The determinant of a diagonal matrix equals its trace.
Consider the determinant of a 3x3 matrix. It can be re-written: And the determinant can be written: |A|=a11(a22a33-a23a32)-a12(a21a33-a23a31)+a13(a21a32-a22a31) A singular matrix is a square matrix with determinant equal to 0. This happens when any row or column is equal to zero or Any row or column is a linear combination of any other rows or columns
The degree of clustering about the principal diagonal is a property of a matrix that affects the determinant. If clustering is high, then the determinant is larger. The rank of a matrix is the number of linearly independent vectors (row or column) that it contains. The rank of a product of two matrices must be less than or equal to the smallest rank of the two matrices being multiplied. Linear independence here means that the two vectors being considered are NOT scalar multiples of each other.
Matrix division (Matrix inverse) In normal math, we can easily solve the equation below for c. y= cx, c = y/x = y•x-1. X•x-1=1 We can generate inverse matrices to do the same operation in matrix algebra. In matrix algebra the inverse matrix is A-1 , where AA-1=I, the identity matrix. The inverse of a matrix exists only if its determinant is non-zero.
The inverse of a matrix is easy to calculate for 2x2 matrices, but anything larger is difficult to even explain. For a 2x2 matrix: Eqn. 3.63 For example, To check:
Using matrices to calculate normal scores - as in Chapter 2. We can use matrix notation to calculate the mean and z-values of vectors. An important concept is the unit vector that consists of ones in a row (unit row vector) or column (unit column vector). Consider the data matrix A consisting of three columns of data samples: We can calculate the mean of each column in A simply by multiplying A by J3T and dividing by 3:
In our example: We now have the mean values, but we want the normal scores (z-values),
We do the subtraction in the numerator by: D= - (1/n) JA where J is the nxm unit matrix (all entries=1). The standard deviation is not linear, and needs to be calculated separately, then entered into a diagonal matrix: The normal scores are then: