Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (25.81 MB, 585 trang )

328

Chapter 5 Matrices and Vectors

For example, to use Mathematica to deﬁne m to be the matrix A

a11

a21

a12

enter

a22

the command

m={{a11,a12},{a21,a22}}.

As when using TableForm,

the result of using

MatrixForm is no longer

a list that can be manipulated

using Mathematica

commands. Use

MatrixForm to view a

matrix in traditional

row-and-column form. Do

not attempt to perform

matrix operations on a

MatrixForm object.

The command m=Array[a,{2,2}] produces a result equivalent to this. Once

a matrix A has been entered, it can be viewed in the traditional row-and-column

form using the command MatrixForm[A]. You can quickly construct 2 2

matrices by clicking on the

button from the BasicTypesetting palette, which

is accessed by going to File under the Mathematica menu, followed by Palettes

and then BasicTypesetting.

Alternatively, you can construct matrices of any dimension by going to the

Mathematica menu under Input and selecting Create Table/Matrix/Palette...

5.1 Nested Lists: Introduction to Matrices, Vectors, and Matrix Operations

The resulting pop-up window allows you to create tables, matrices, and palettes.

To create a matrix, select Matrix, enter the number of rows and columns of the

matrix, and select any other options. Pressing the OK button places the desired

matrix at the position of the cursor in the Mathematica notebook.

a11

EXAMPLE 5.1.1: Use Mathematica to deﬁne the matrices a22

a31

b

b12 b13 b14

.

and 11

b21 b22 b23 b24

a12

a22

a32

a13

a23

a33

329

330

Chapter 5 Matrices and Vectors

SOLUTION: In this case, both Table ai,j , i, 1, 3 , j, 1, 3

and

Array[a,{3,3}] produce equivalent results when we deﬁne matrixa

to be the matrix

a11 a12 a13

a22 a22 a23 .

a31 a32 a33

The commands MatrixForm or TableForm are used to display the

results in traditional matrix form.

In[926]:= Clear a, b, matrixa, matrixb

General

spell1

Possible spelling error new symbol name "

matrixb " is similar to existing symbol "

matrixa ".

In[927]:= matrixa

Out[927]=

Table ai,j , i, 1, 3 , j, 1, 3

a1 , 1 , a1 , 2 , a1 , 3 ,

a2 , 1 , a2 , 2 , a2 , 3 , a3 , 1 , a3 , 2 , a3 , 3

In[928]:= MatrixForm matrixa

a1 , 1 a1 , 2 a1 , 3

Out[928]= a2,1 a2,2 a2,3

a3 , 1 a3 , 2 a3 , 3

In[929]:= matrixa

Out[929]=

Array a, 3, 3

a 1, 1 , a 1, 2 , a 1, 3 ,

a 2, 1 , a 2, 2 , a 2, 3 ,

a 3, 1 , a 3, 2 , a 3, 3

In[930]:= MatrixForm matrixa

a 1, 1

a 1, 2

a 1, 3

a 2, 2

a 2, 3

Out[930]= a 2, 1

a 3, 1

a 3, 2

a 3, 3

We may also use Mathematica to deﬁne non-square matrices.

In[931]:= matrixb

Out[931]=

Array b, 2, 4

b 1, 1 , b 1, 2 , b 1, 3 , b 1, 4 ,

b 2, 1 , b 2, 2 , b 2, 3 , b 2, 4

In[932]:= MatrixForm matrixb

b 1, 2

b 1, 3

b 1, 1

Out[932]=

b 2, 2

b 2, 3

b 2, 1

Equivalent results would

Table bi,j , i, 1, 2 , j, 1, 4 .

have

been

b 1, 4

b 2, 4

obtained

by

entering

5.1 Nested Lists: Introduction to Matrices, Vectors, and Matrix Operations

More generally the commands Table[f[i,j],{i,imax},{j,jmax}] and

Array[f,{imax,jmax}] yield nested lists corresponding to the imax jmax

matrix

f 1, 1

f 1, 2

f 1, jmax

f 2, 1

f 2, 2

f 2, jmax

.

f imax, 1

f imax, 2

f imax, jmax

Table[f[i,j],{i,imin,imax,istep},{j,jmin,jmax,jstep}]

the list of lists

returns

{{f[imin,jmin],f[imin,jmin+jstep],...,f[imin,jmax]},

{f[imin+istep,jmin],...,f[imin+istep,jmax]},

...,{f[imax,jmin],...,f[imax,jmax]}}

and the command

Table[f[i,j,k,...],{i,imin,imax,istep},{j,jmin,jmax,jstep},

{k,kmin,kmax,kstep},...]

calculates a nested list; the list associated with i is outermost. If istep is omitted,

the stepsize is one.

EXAMPLE 5.1.2: Deﬁne C to be the 3 4 matrix ci j , where ci j , the

entry in the ith row and jth column of C, is the numerical value of

cos j2 i2 sin i2 j2 .

SOLUTION: After clearing all prior deﬁnitions of c, if any, we deﬁne

c[i,j] to be the numerical value of cos j2 i2 sin i2 j2 and then

use Array to compute the 3 4 matrix matrixc.

In[933]:= Clear c, matrixc

N Cos j2 i2 Sin i2

c i ,j

General

spell

Possible spelling error new symbol name "

matrixc " is similar to existing symbols

matrixa, matrixb .

Out[933]= Cos i2 1. j2 Sin i2 1. j2

In[934]:= matrixc

Out[934]=

j2

Array c, 3, 4

0., 0.139708, 0.143952, 0.494016 ,

0.139708, 0., 0.272011, 0.452789 ,

0.143952, 0.272011, 0., 0.495304

331

332

Chapter 5 Matrices and Vectors

In[935]:= MatrixForm matrixc

0.

0.139708

0.139708

0.

Out[935]=

0.272011

0.143952

EXAMPLE 5.1.3: Deﬁne the matrix I3

1

0

0

0.143952

0.272011

0.

0

1

0

0.494016

0.452789

0.495304

0

0.

1

SOLUTION: The matrix I3 is the 3 3 identity matrix. Generally, the

n n matrix with 1’s on the diagonal and 0’s elsewhere is the n n

identity matrix. The command IdentityMatrix[n] returns the n n

identity matrix.

In[936]:= IdentityMatrix 3

Out[936]=

1, 0, 0 , 0, 1, 0 , 0, 0, 1

The same result is obtained by going to Input under the Mathematica

menu and selecting Create Table/Matrix/Palette... We then check

Matrix, Fill with: 0 and Fill diagonal: 1.

Pressing the OK button inserts the 3

of the cursor.

In[937]:=

Out[937]=

3 identity matrix at the location

1 0 0

0 1 0

0 0 1

1, 0, 0 , 0, 1, 0 , 0, 0, 1

5.1 Nested Lists: Introduction to Matrices, Vectors, and Matrix Operations

In Mathematica, a vector is a list of numbers and, thus, is entered in the same manner as lists. For example, to use Mathematica to deﬁne the row vector vectorv

to be v1 v2 v3 enter vectorv={v1,v2,v3}. Similarly, to deﬁne the column

v1

vector vectorv to be v2 enter vectorv={v1,v2,v3} or vectorv={{v1},

v3

{v2},{v3}}.

For a 2 1 vector, you can use the

button on the Basic Typesetting palette.

Generally, with Mathematica you do not need to distinguish between row and

column vectors: Mathematica performs computations with vectors and matrices

correctly as long as the computations are well-deﬁned.

4

5 , vectorv to be the vector

2

v4 and zerovec to be the vector 0 0 0 0 0 .

EXAMPLE 5.1.4: Deﬁne the vector w

v1

v2

v3

SOLUTION: To deﬁne w, we enter

In[938]:= w

4, 5, 2

Out[938]=

4, 5 , 2

or

In[939]:= w

4 ,

5 , 2

MatrixForm w

4

5

Out[939]=

2

To deﬁne vectorv, we use Array.

In[940]:= vectorv Array v, 4

Out[940]= v 1 , v 2 , v 3 , v 4

Equivalent results would have been obtained

Table vi , i, 1, 4 . To deﬁne zerovec, we use Table.

by

entering

In[941]:= zerovec Table 0, 5

Out[941]= 0, 0, 0, 0, 0

The same result is obtained by going to Input under the Mathematica

menu and selecting Create Table/Matrix/Palette...

333

With Mathematica, you do

not need to distinguish

between row and column

vectors. Provided that

computations are

well-deﬁned, Mathematica

carries them out correctly.

Mathematica warns of any

ambiguities when they

(rarely) occur.

334

Chapter 5 Matrices and Vectors

In[942]:=

0

Out[942]=

0

0

0

0

0, 0, 0, 0, 0

5.1.2 Extracting Elements of Matrices

For the 2 2 matrix m

a1,1 , a1,2 , a2,1 , a2,2 deﬁned earlier, m[[1]] yields the

ﬁrst element of matrix m which is the list a1,1 , a1,2 or the ﬁrst row of m; m[[2,1]]

yields the ﬁrst element of the second element of matrix m which is a2,1 . In general,

if m is an i j matrix, m[[i,j]] or Part[m,i,j] returns the unique element in

the ith row and jth column of m. More speciﬁcally, m[[i,j]] yields the jth part

of the ith part of m; list[[i]] or Part[list,i] yields the ith part of list;

list[[i,j]] or Part[list,i,j] yields the jth part of the ith part of list, and

so on.

10

6

6

5

10

9

(a) Extract the third row of mb. (b) Extract the element in the ﬁrst

and third column of mb. (c) Display mb in traditional matrix form.

EXAMPLE 5.1.5: Deﬁne mb to be the matrix

9

7.

12

row

SOLUTION: We begin by deﬁning mb. mb[[i,j]] yields the (unique)

number in the ith row and jth column of mb. Observe how various

components of mb (rows and elements) can be extracted and how mb

is placed in MatrixForm.

In[943]:= mb

10, 6, 9 , 6, 5, 7 ,

10, 9, 12

In[944]:= MatrixForm

10

6

6

5

Out[944]=

10

9

In[945]:= mb

Out[945]=

In[946]:= mb

Out[946]=

3

10, 9, 12

9

1, 3

mb

9

7

12

5.1 Nested Lists: Introduction to Matrices, Vectors, and Matrix Operations

If m is a matrix, the ith row of m is extracted with m[[i]]. The command

Transpose[m] yields the transpose of the matrix m, the matrix obtained by

interchanging the rows and columns of m. We extract columns of m by computing

Transpose[m] and then using Part to extract rows from the transpose. Namely,

if m is a matrix, Transpose[m][[i]] extracts the ith row from the transpose of

m which is the same as the ith column of m.

EXAMPLE 5.1.6: Extract the second and third columns from A if A

0

2

2

1

1

3 .

2

4

1

SOLUTION: We ﬁrst deﬁne matrixa and then use Transpose to

compute the transpose of matrixa, naming the result ta, and then

displaying ta in MatrixForm.

In[947]:= matrixa

0, 2, 2 ,

2, 4, 1

In[948]:= ta

1, 1, 3 ,

Transpose matrixa

MatrixForm ta

Out[948]=

0

2

2

1

1

3

2

4

1

Next, we extract the second column of matrixa using Transpose

together with Part ([[...]]). Because we have already deﬁned ta

to be the transpose of matrixa, entering ta[[2]] would produce

the same result.

In[949]:= Transpose matrixa

Out[949]=

2

2, 1, 4

To extract the third column, we take advantage of the fact that we have

already deﬁned ta to be the transpose of matrixa. Entering

Transpose[matrixa][[3]] produces the same result.

335

336

Chapter 5 Matrices and Vectors

In[950]:= ta 3

Out[950]= 2, 3, 1

Other commands that can be used to manipulate matrices are included in the

Matrix Manipulation package that is contained in the Linear Algebra folder (or

directory).

After this package has been loaded,

In[951]:= << LinearAlgebra‘MatrixManipulation‘

we can use commands like TakeColumns and TakeRows to extract columns and

rows from a given matrix. For example, entering

In[952]:= TakeColumns matrixa, 2

2

1

Out[952]=

4

//MatrixForm

extracts the second column of the matrix A deﬁned in the previous example and

displays the result in MatrixForm while entering

5.1 Nested Lists: Introduction to Matrices, Vectors, and Matrix Operations

In[953]:= TakeColumns matrixa, 2, 3

MatrixForm

Out[953]=

2

1

4

//

2

3

1

extracts the second and third columns of A and displays the result in MatrixForm.

5.1.3 Basic Computations with Matrices

Mathematica performs all of the usual operations on matrices. Matrix addition

(A B), scalar multiplication (kA), matrix multiplication (when deﬁned) (AB),

and combinations of these operations are all possible. The transpose of A, At ,

is obtained by interchanging the rows and columns of A and is computed with

the command Transpose[A]. If A is a square matrix, the determinant of A is

obtained with Det[A].

If A and B are n n matrices satisfying AB BA I, where I is the n n matrix

with 1’s on the diagonal and 0’s elsewhere (the n n identity matrix), B is called

the inverse of A and is denoted by A 1 . If the inverse of a matrix A exists, the

a b

inverse is found with Inverse[A]. Thus, assuming that

has an inverse

c d

(ad bc 0), the inverse is

In[954]:= Inverse

a, b , c, d

d

b

Out[954]=

,

,

b c a d

b c a d

EXAMPLE 5.1.7: Let A

3

8

5

4

0

2

c

a

,

b c a d

b c a d

5

3 and B

1

10

6

10

6

5

9

9

7.

12

Compute

(a) A B; (b) B 4A; (c) the inverse of AB; (d) the transpose of

A 2B B; and (e) det A A .

SOLUTION: We enter ma (corresponding to A) and mb (corresponding

to B) as nested lists where each element corresponds to a row of the

matrix. We suppress the output by ending each command with a semicolon.

337

338

Chapter 5 Matrices and Vectors

In[955]:= ma

3, 4, 5 , 8, 0, 3 , 5, 2, 1

10, 6, 9 , 6, 5, 7 ,

10, 9, 12

mb

Entering

In[956]:= ma mb//MatrixForm

13

10

4

5

10

Out[956]= 14

5

11

13

adds matrix ma to mb and expresses the result in traditional matrix

form. Entering

In[957]:= mb

Out[957]=

4ma//MatrixForm

2

10

29

26

5

5

30

1

8

subtracts four times matrix ma from mb and expresses the result in

traditional matrix form. Entering

In[958]:= Inverse

59

380

223

Out[958]=

570

49

114

Matrix products, when

deﬁned, are computed by

placing a period (.) between

the matrices being multiplied.

Note that a period is also

used to compute the dot

product of two vectors,

when the dot product is

deﬁned.

ma.mb

53

190

92

95

18

19

//MatrixForm

167

380

979

570

187

114

computes the inverse of the matrix product AB. Similarly, entering

In[959]:= Transpose ma 2mb .mb //MatrixForm

352

90

384

269

73

277

Out[959]=

373

98

389

computes the transpose of A

2B B and entering

In[960]:= Det ma

Out[960]= 190

computes the determinant of ma.

Tải bản đầy đủ (.pdf) (585 trang)