Syntax Description Remarks and examples Also see



Similar documents
Introduction to Matrix Algebra

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

6. Cholesky factorization

8 Square matrices continued: Determinants

Abstract: We describe the beautiful LU factorization of a square matrix (or how to write Gaussian elimination in terms of matrix multiplication).

Notes on Determinant

Rotation Matrices and Homogeneous Transformations

Elementary Matrices and The LU Factorization

7. LU factorization. factor-solve method. LU factorization. solving Ax = b with A nonsingular. the inverse of a nonsingular matrix

Solution of Linear Systems

DERIVATIVES AS MATRICES; CHAIN RULE

Direct Methods for Solving Linear Systems. Matrix Factorization

Typical Linear Equation Set and Corresponding Matrices

Using row reduction to calculate the inverse and the determinant of a square matrix

Similar matrices and Jordan form

7 Gaussian Elimination and LU Factorization

18.06 Problem Set 4 Solution Due Wednesday, 11 March 2009 at 4 pm in Total: 175 points.

Lecture 5: Singular Value Decomposition SVD (1)

Linear Algebra: Determinants, Inverses, Rank

Operation Count; Numerical Linear Algebra

Here are some examples of combining elements and the operations used:

Vector Math Computer Graphics Scott D. Anderson

Question 2: How do you solve a matrix equation using the matrix inverse?

Factorization Theorems

[1] Diagonal factorization

K80TTQ1EP-??,VO.L,XU0H5BY,_71ZVPKOE678_X,N2Y-8HI4VS,,6Z28DDW5N7ADY013

Orthogonal Projections

SOLVING LINEAR SYSTEMS

5. Orthogonal matrices

8.2. Solution by Inverse Matrix Method. Introduction. Prerequisites. Learning Outcomes

MATHEMATICS FOR ENGINEERS BASIC MATRIX THEORY TUTORIAL 2

Solving Systems of Linear Equations

Linear Algebra Review. Vectors

v w is orthogonal to both v and w. the three vectors v, w and v w form a right-handed set of vectors.

Matrix Algebra in R A Minimal Introduction

Solving Linear Systems, Continued and The Inverse of a Matrix

Matrix Multiplication

LINEAR ALGEBRA. September 23, 2010

3 Orthogonal Vectors and Matrices

Similarity and Diagonalization. Similar Matrices

13 MATH FACTS a = The elements of a vector have a graphical interpretation, which is particularly easy to see in two or three dimensions.

Continued Fractions and the Euclidean Algorithm

CS3220 Lecture Notes: QR factorization and orthogonal transformations

Math 1050 Khan Academy Extra Credit Algebra Assignment

Chapter 17. Orthogonal Matrices and Symmetries of Space

Linear Algebra Notes for Marsden and Tromba Vector Calculus

DETERMINANTS TERRY A. LORING

MATH APPLIED MATRIX THEORY

MAT 200, Midterm Exam Solution. a. (5 points) Compute the determinant of the matrix A =

Solving simultaneous equations using the inverse matrix

5.5. Solving linear systems by the elimination method

Review Jeopardy. Blue vs. Orange. Review Jeopardy

Row Echelon Form and Reduced Row Echelon Form

Orthogonal Diagonalization of Symmetric Matrices

Linear Algebra and TI 89

Notes on Matrix Calculus

CURVE FITTING LEAST SQUARES APPROXIMATION

Linear Programming. March 14, 2014

Origins of the Unusual Space Shuttle Quaternion Definition

Linear Algebra Notes

December 4, 2013 MATH 171 BASIC LINEAR ALGEBRA B. KITCHENS

Name: Section Registered In:

Matrix Differentiation

The Characteristic Polynomial

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

Au = = = 3u. Aw = = = 2w. so the action of A on u and w is very easy to picture: it simply amounts to a stretching by 3 and 2, respectively.

1.2 Solving a System of Linear Equations

1 Solving LPs: The Simplex Algorithm of George Dantzig

Data Mining: Algorithms and Applications Matrix Math Review

Lecture L3 - Vectors, Matrices and Coordinate Transformations

1.5 SOLUTION SETS OF LINEAR SYSTEMS

Activity 1: Using base ten blocks to model operations on decimals

3.2 Matrix Multiplication

Solution to Homework 2

Content. Chapter 4 Functions Basic concepts on real functions 62. Credits 11

Section 5.3. Section 5.3. u m ] l jj. = l jj u j + + l mj u m. v j = [ u 1 u j. l mj

Eigenvalues and Eigenvectors

Section Inner Products and Norms

Lecture notes on linear algebra

160 CHAPTER 4. VECTOR SPACES

Linear Codes. Chapter Basics

Applied Linear Algebra I Review page 1

Physics 235 Chapter 1. Chapter 1 Matrices, Vectors, and Vector Calculus

Linear Programming Problems

MULTIPLE-OBJECTIVE DECISION MAKING TECHNIQUE Analytical Hierarchy Process

More than you wanted to know about quadratic forms

1 Introduction to Matrices

Matrices 2. Solving Square Systems of Linear Equations; Inverse Matrices

A =

Math 115A HW4 Solutions University of California, Los Angeles. 5 2i 6 + 4i. (5 2i)7i (6 + 4i)( 3 + i) = 35i + 14 ( 22 6i) = i.

LU Factorization Method to Solve Linear Programming Problem

3. Mathematical Induction

Base Conversion written by Cathy Saxton

A Concrete Introduction. to the Abstract Concepts. of Integers and Algebra using Algebra Tiles

To give it a definition, an implicit function of x and y is simply any relationship that takes the form:

A linear combination is a sum of scalars times quantities. Such expressions arise quite frequently and have the form

x1 x 2 x 3 y 1 y 2 y 3 x 1 y 2 x 2 y 1 0.

Math 550 Notes. Chapter 7. Jesse Crawford. Department of Mathematics Tarleton State University. Fall 2010

Transcription:

Title stata.com permutation An aside on permutation matrices and vectors Syntax Description Remarks and examples Also see Syntax Permutation matrix Permutation vector Action notation notation permute rows B = P*A B = A[p,.] permute columns B = A*P B = A[.,p] unpermute rows B = P A B = A ; B[p,.] = A or B = A[invorder(p),.] unpermute columns B = A*P B = A ; B[,.p] = A or B = A[., invorder(p)] A permutation matrix is an n n matrix that is a row (or column) permutation of the identity matrix. A permutation vector is a 1 n or n 1 vector of the integers 1 through n. The following permutation matrix and permutation vector are equivalent: Either can be used to permute the rows of and to permute the columns of P = 0 1 0 0 0 1 p = 2 3 1 0 0 1 A = a b c d e f g h to produce e f g h i j k l i j k l a b c d m n o p q r P = to produce s t u v w x n o m q r p t u s w x v 1

2 permutation An aside on permutation matrices and vectors Description Permutation matrices are a special kind of orthogonal matrix that, via multiplication, reorder the rows or columns of another matrix. Permutation matrices cast the reordering in terms of multiplication. Permutation vectors also reorder the rows or columns of another matrix, but they do it via subscripting. This alternative method of achieving the same end is, computerwise, more efficient, in that it uses less memory and less computer time. The relationship between the two is shown below. Remarks and examples stata.com Remarks are presented under the following headings: Permutation matrices Permutation matrices How permutation matrices arise Permutation vectors A permutation matrix is a square matrix whose rows are a permutation of the identity matrix. The following are the full set of all 2 2 permutation matrices: [ ] 1 0 (1) 0 1 [ ] 0 1 (2) 1 0 Let P be an n n permutation matrix. If n m matrix A is premultiplied by P, the result is to reorder its rows. For example, P 0 1 0 0 0 1 1 0 0 A = PA 1 2 3 4 5 6 = 4 5 6 7 8 9 7 8 9 1 2 3 Above, we illustrated the reordering using square matrix A, but A did not have to be square. If m n matrix B is postmultiplied by P, the result is to reorder its columns. We illustrate using square matrix A again: (3) A 1 2 3 4 5 6 7 8 9 P = AP 0 1 0 0 0 1 = 3 1 2 6 4 5 1 0 0 9 7 8 (4) Say that we reorder the rows of A by forming PA. Obviously, we can unreorder the rows by forming P 1 PA. Because permutation matrices are orthogonal, their inverses are equal to their transpose. Thus the inverse of the permutation matrix (0,1,0\0,0,1\1,0,0) we have been using is (0,0,1\1,0,0\0,1,0). For instance, taking our results from (3)

permutation An aside on permutation matrices and vectors 3 P 0 0 1 1 0 0 0 1 0 PA = A 4 5 6 7 8 9 = 1 2 3 4 5 6 1 2 3 7 8 9 (3 ) Allow us to summarize: 1. A permutation matrix P is a square matrix whose rows are a permutation of the identity matrix. 2. PA = a row permutation of A. 3. AP = a column permutation of A. 4. The inverse permutation is given by P. 5. P PA = A. 6. APP = A. How permutation matrices arise Some of Mata s matrix functions implicitly permute the rows (or columns) of a matrix. For instance, the LU decomposition of matrix A is defined as A = LU where L is lower triangular and U is upper triangular. For any matrix A, one can solve for L and U, and Mata has a function that will do that (see [M-5] lud( )). However, Mata s function does not solve the problem as stated. Instead, it solves P A = LU where P is a permutation matrix that Mata makes up! Just to be clear; Mata s function solves for L and U, but for a row permutation of A, not A itself, although the function does tell you what permutation it chose (the function returns L, U, and P). The function permutes the rows because, that way, it can produce a more accurate answer. You will sometimes read that a function engages in pivoting. What that means is that, rather than solving the problem for the matrix as given, it solves the problem for a permutation of the original matrix, and the function chooses the permutation in a way to minimize numerical roundoff error. Functions that do this invariably return the permutation matrix along with the other results, because you are going to need it. For instance, one use of LU decomposition is to calculate inverses. If A = LU then A 1 = U 1 L 1. Calculating the inverses of triangular matrices is an easy problem, so one recipe for calculating inverses is 1. decompose A into L and U, 2. calculate U 1, 3. calculate L 1, and 4. multiply the results together.

4 permutation An aside on permutation matrices and vectors That would be the solution except that the LU decomposition function does not decompose A into L and U; it decomposes P A, although the function does tell us P. Thus we can write, P A = LU A = PLU (remember P 1 = P) A 1 = U 1 L 1 P Thus the solution to our problem is to use the U and L just as we planned calculate U 1 L 1 and then make a column permutation of that, which we can do by postmultiplying by P. There is, however, a detail that we have yet to reveal to you: Mata s LU decomposition function does not return P, the permutation matrix. It instead returns p, a permutation vector equivalent to P, and so the last step forming the column permutation by postmultiplying by P is done differently. That is the subject of the next section. Permutation vectors are more efficient than permutation matrices, but you are going to discover that they are not as mathematically transparent. Thus when working with a function that returns a permutation vector when working with a function that permutes the rows or columns think in terms of permutation matrices and then translate back into permutation vectors. Permutation vectors Permutation vectors are used with Mata s subscripting operator, so before explaining permutation vectors, let s understand subscripting. Not surprisingly, Mata allows subscripting. Given matrix A = 1 2 3 4 5 6 7 8 9 Mata understands that A[2,3] = 6 Mata also understands that if one or the other subscript is specified as. (missing value), the entire column or row is to be selected: A[.,3] = 3 6 9 A[2,.] = [ 4 5 6 ] Mata also understands that if a vector is specified for either subscript A[ 2 3,.] = 4 5 6 7 8 9 2 4 5 6

permutation An aside on permutation matrices and vectors 5 In Mata, we would actually write the above as A[(2\3\2),.], and Mata would understand that we want the matrix made up of rows 2, 3, and 2 of A, all columns. Similarly, we can request all rows, columns 2, 3, and 2: A[.,(2, 3, 2)] = 2 3 2 5 6 5 8 9 8 In the above, we wrote (2,3,2) as a row vector because it seems more logical that way, but we could just as well have written A[.,(2\3\2)]. In subscripting, Mata does not care whether the vectors are rows or columns. In any case, we can use a vector of indices inside Mata s subscripts to select rows and columns of a matrix, and that means we can permute them. All that is required is that the vector we specify contain a permutation of the integers 1 through n because, otherwise, we would repeat some rows or columns and omit others. A permutation vector is an n 1 or 1 n vector containing a permutation of the integers 1 through n. For example, the permutation vector equivalent to the permutation matrix P = 0 1 0 0 0 1 1 0 0 is p = 2 3 1 p can be used with subscripting to permute the rows of A A[p,.] = 4 5 6 7 8 9 1 2 3 and similarly, A[.,p] would permute the columns. Also subscripting can be used on the left-hand side of the equal-sign assignment operator. So far, we have assumed that the subscripts are on the right-hand side of the assignment operator, such as B = A[p,.] We have learned that if p = (2\3\1) (or p = (2,3,1)), the result is to copy the second row of A to the first row of B, the third row of A to the second row of B, and the first row of A to the third row of B. Coding B[p,.] = A does the inverse: it copies the first row of A to the second row of B, the second row of A to the third row of B, and the third row of A to the first row of B. B[p,.]=A really is the inverse of C=A[p,.] in that, if we code C = A[p,.]

6 permutation An aside on permutation matrices and vectors B will be equal to C, and if we code C[p,.] = A B = C[p,.] B will also be equal to C. There is, however, one pitfall that you must watch for when using subscripts on the left-hand side: the matrix on the left-hand side must already exist and it must be of the appropriate (here same) dimension. Thus when performing the inverse copy, it is common to code B = C The first line is not unnecessary; it is what ensures that B exists and is of the proper dimension, although we could just as well code B = J(rows(C), cols(c),.) The first construction is preferred because it ensures that B is of the same type as C. If you really like the second form, you should code B = J(rows(C), cols(c), missingof(c)) Going back to the preferred code B = C some programmers combine it into one statement: (B=C)[p,.] = C Also Mata provides an invorder(p) (see [M-5] invorder( )) that will return an inverted p appropriate for use on the right-hand side, so you can also code B = C[invorder(p),.] Also see [M-5] invorder( ) Permutation vector manipulation [M-1] intro Introduction and advice