Sie sind auf Seite 1von 28

Basic matrix and linear algebra functions in Maxima

In this chapter we present examples of matrix and linear algebra functions included in the Algebra menu in the wxMaxima interface. Functions in the Algebra menu The items in the Algebra menu, shown in the figure to the right, are presented in the following sections. Generate matrix ... The Algebra > Generate matrix ... utilizes a function of the matrix sub-indices i and j, defined previous to invoking the menu item, e.g.,

Then, use f in the dialogue form that results from the Algebra > Generate matrix ... menu item, e.g.,

This will result in the command:


Gilberto E. Urroz, 2008

ther examples of genmatrix are shown next: !efining specific values of the matrix:

"sing a generic name with sub-indices:

#nter matrix ... The Algebra > Enter matrix ... menu item is used to enter a matrix of given dimensions. The resulting dialogue form provides the following options:

general matrix:


Gilberto E. Urroz, 2008

diagonal matrix:

s$mmetric matrix:

antis$mmetric matrix


Gilberto E. Urroz, 2008

Invert matrix %hen invoked from the Algebra menu, the menu item Invert matrix produces the inverse of the matrix referred to with %, or of a matrix referred to b$ name or listen in the INPUT line, e.g.,

&haracteristic pol$nomial The characteristic pol$nomial of a s'uare matrix A results from expanding the determinant of the matrix A-xI where I is the identit$ matrix with the same dimensions of A, i.e., char !l"(A) * det(A-xI). &onsider the following examples:


Gilberto E. Urroz, 2008

!eterminant The Algebra > #eterminant menu item calculates the determinant of a matrix. +$ default, $eterminant uses the most recent result, as illustrated in the following example:

#igenvalues The Algebra > Eigenval%e& menu item calculates the eigenvalues of a matrix, i.e., it finds the roots of the characteristic pol$nomial of the matrix. ,ere is an example of this function applied to matrix A defined above:

-otice that the output of function eigenval%e& consists of two lists. The first list is the list of eigenvalues, and the second list is the multiplicit$ of those values. In this example, there are . eigenvalues and none repeats. /ou can extract the eigenvalues as indicated in the following example, b$ assigning the output to a variable:

The individual values are extracted as follows:

#igenvectors The Algebra > Eigenvect!r& menu item calculates the eigenvectors of a matrix, i.e., it solves for the vectors v from the eigenvalue e'uation Av *xv. 0or example, for the matrix A defined above: 7-5
Gilberto E. Urroz, 2008

The output of this command includes the eigenvalues as the first element consisting of two lists as described above, i.e., eigenvalues and multiplicit$. The remaining lists are the eigenvectors of the matrix corresponding to the eigenvalues listed first. If we assign this output to a variable we can then extract the individual eigenvectors as follows:

1d2oint matrix The ad2oint matrix produced b$ the Algebra > A$j!int matrix menu item corresponds to the definition of the a$j%gate matrix as given in http:33en.wikipedia.org3wiki31d2ugate. In the following example we first put together a complex matrix A and then calculate the ad2oint or ad2ugate matrix. 0irst, we define a couple of .x. real matrices A' and AI:


Gilberto E. Urroz, 2008

Then, we put together matrix A * AI 4 iA':

-ext, we invoke the Algebra > A$j!int matrix menu item to produce the ad2ugate (or ad2oint) matrix:

"se function rectf!rm to simplif$ the matrix to:

Transpose matrix The Algebra > Tran& !&e matrix menu item produces the transpose of a matrix. 5ee the definition here: http:33en.wikipedia.org3wiki3Transpose. 0or example, for the matrix A defined above, we have:

6ake list ... The Algebra > Ma(e li&t ... menu item produces a dialogue form that can be used to generate a list. The elements in the list are defined b$ an expression which is function of an index (sa$, () for the range of integer values specified for that index. 0or example, the following dialogue:


Gilberto E. Urroz, 2008

produces the list shown below:

1ppl$ to list ... The Algebra > A l" t! li&t ... menu item produces a dialogue form that can be used to appl$ an operator (e.g., 748, a sum) to the elements of a list. The application of this menu item is illustrated b$ the following example:

6ap to list ... The Algebra > Ma t! li&t ... menu item produces a dialogue form that can be used to 7map8 (or distribute) a function to the elements of a list. The application of this menu item is illustrated b$ the following example: 7-8
Gilberto E. Urroz, 2008

1 second example of function ma is show next:

6ap to matrix ... The Algebra > Ma t! matrix ... menu item behaves similar to the Ma t! li&t ... menu item, 7mapping8 a function to all elements of a matrix. The application of this menu item is illustrated b$ the following example in which we first define a .x. matrix 1:

Then, we invoke the Ma t! matrix ... menu item:

The result is the following matrix:


Gilberto E. Urroz, 2008

To find floating-point elements in the matrix use function fl!at:

Functions for creating matrices In this section we present examples of Maxima functions to generate matrices. 5ome functions for creating matrices that are available in the Algebra menu were introduced above (genmatrix, matrixma , tran& !&e). The following examples demonstrate the use of additional functions: cop$matrix "se c! "matrix to cop$ a matrix into a variable name. 0or example, first create matrix A)

then, cop$ matrix A into * using c! "matrix:


Gilberto E. Urroz, 2008

columnvector 1 column vector is a matrix of n rows and 9 column. 0unction c!l%mnvect!r lets $ou build a column vector out of a list of values, e.g.,

diag 0unction $iag, which needs to be loaded separatel$, allows $ou to build a diagonal matrix based on two or more matrices. 0or example, using matrices A and *, defined above, we can build the following diagonal matrix:

diagmatrix 0unction $iagmatrix(n,a) creates a diagonal matrix of dimensions nn with all its diagonal elements e'ual to a:


Gilberto E. Urroz, 2008

0unction $iagmatrix can be used to generate an identit$ matrix as illustrated below:

ematrix 0unction ematrix(n,m,a,i,j) creates a matrix of dimensions nm full of zero elements except for element :i,j; which is replaced b$ the value a, e.g.,

entermatrix 0unction entermatrix(n,m) provides for an interactive, if long, wa$ to enter a matrix, e.g.,

ident 0unction i$ent(n) allows to create an nn identit$ matrix:


Gilberto E. Urroz, 2008

matrix 0unction matrix, which has been used before in this and other &hapters, allows the user to enter a matrix b$ defining the matrix rows as lists of the same length. The use of matrix is illustrated in the following example:

submatrix 0unction &%bmatrix allows the user to extract a submatrix out of a matrix. To illustrate the use of function &%bmatrix consider the ++ matrix A:

To extract a matrix b$ eliminating rows from i, to i- and columns from j, to j- out of matrix A, use the general call &%bmatrix.i,,i-,A,j,,j-/. In the following example we eliminate rows < and . and columns < to . out of matrix A and store the resulting matrix into *:

To eliminate rows from i, to i-, onl$, use the modified call &%bmatrix(i,,i-,A/, e.g.,

To eliminate columns from j, to j-, onl$, use the modified call &%bmatrix(A,j,,j-/, e.g.,


Gilberto E. Urroz, 2008

zeromatrix 0unction 0er!matrix(m,n) creates a matrix of dimensions mn such that all its elements are zero values, e.g.,

Functions for manipulating matrices The following functions allows the user to extract or add rows and columns out of matrices. To illustrate the use of these functions we will refer to matrix A defined above, and repeated here:

col 0unction c!l extracts a column out of a matrix, e.g.,

row 0unction r!w extracts a row out of a matrix:


Gilberto E. Urroz, 2008

addcol 0unction a$$c!l is used to append one or more columns to a matrix, e.g.,

addrow 0unction a$$r!w is used to append one or more rows to a matrix, e.g.,

Matrix operations +asic matrix operations include addition, subtraction, multiplication, division, and powers. To illustrate those operations we will use the following matrices A and *:

1ddition and subtraction 1ddition and subtration are term-b$-term operations on matrices of the same dimensions. The examples below include linear combinations of additions and subtractions:


Gilberto E. Urroz, 2008

6ultiplication 6ultiplication can be term1b"1term, in which case we use an asterisk for the multiplication s$mbol, e.g.,

Traditional, n!n1c!mm%tative, matrix m%lti licati!n is achieved b$ using a dot (.) as the multiplication s$mbol:

=ower A matrix rai&e$ t! a &calar ex !nential produces a term-b$-term exponentiation, e.g.,

A &calar ba&e rai&e$ t! a matrix ex !nent is also a term-b$-term operation, e.g.,


Gilberto E. Urroz, 2008

Matrix ex !nentiati!n uses a double caret (>>) and represents the result of repeated matrix multiplication, i.e., 1>>< * 1.1, 1>>. * 1>><.1, and so on, e.g.,

!ivision !ivision of matrices is a term-b$-term operation, e.g.,

&on2ugate The c!nj%gate function, used to calculate the complex con2ugate of a number, can be used to find the con2ugate matrix of a matrix of complex numbers, e.g.,

Functions for linear algebra operations The functions whose operation is illustrated in this section are used in linear algebra applications. 5ome functions, such as a$j!int, char !l", $eterminant, eigen, and invert, were introduced as part of the Algebra menu items.


Gilberto E. Urroz, 2008

coefmatrix 0unction c!efmatrix(:li&t !f linear e2%ati!n&;,:li&t !f variable&;) can be used to extract the coefficients from a li&t !f linear e2%ati!n& containing the variables in the li&t !f variable&, e.g.,

augcoefmatrix 0unction a%gc!efmatrix (:li&t !f linear e2%ati!n&;,:li&t !f variable&;) produces an augmented matrix of coefficients similar to that produced b$ c!efmatrix, except that the last column contains the negatives of the right-hand side elements corresponding to the li&t !f e2%ati!n&. 0or example, for the s$stem of linear e'uations used in the c!efmatrix example shown above, the resulting augmented matrix of coefficients is calculated as follows:

echelon and triangularize +oth functions echel!n(A) and triang%lari0e(A) produce upper triangular matrices representing row-reduced echelon forms of matrix A. The difference between these two functions is that function echel!n produces a matrix such that its main diagonal elements are reduced to the number ,. The following examples, using matrices A and AA created above, illustrate the application of these two functions highlighting their differences:


Gilberto E. Urroz, 2008

mattrace 0unction mattrace, which is available b$ loading package 7nchrpl8, calculates the trace of a matrix (i.e., the sum of its main diagonal elements):

minor The minor matrix (i,j) of a matrix A is the matrix that results from eliminating row i and column j. 6inor matrices are used, for example, in the calculation of determinants.

ncharpol$ (alternate to charpol$) 0unction nchar !l", loaded with package nchr l, an alternative function to char !l", used to obtain the characteristic pol$nomial of a matrix A, e.g.,


Gilberto E. Urroz, 2008

permanent 0unction ermanent calculates the ermanent of a matrix. To understand the definition of the permanent of a matrix, see http:33en.wikipedia.org3wiki3=ermanent:

rank 0unction ran( calculates the ran( of a matrix. To understand the definition of the rank of a matrix, see http:33en.wikipedia.org3wiki3?ank@(linear@algebra) :

tracematrix 0unction tracematrix, which needs to be loaded with package f%nct&, calculates the trace of a matrix, e.g.,

Functions in the eigen package The functions described in this package are used in the calculation of eigenvalues and eigenvectors of matrices. 0unctions eigenval%e& and eigenvect!r&, which belong to this package, were described in the context of the Algebra menu at the beginning of this chapter. %hen functions eigenval%e& and eigenvect!r& are invoked, the eigen package is invoked automaticall$. To use the other functions make sure to load the package eigen beforehand:

innerproduct 0unction inner r!$%ct produces the inner product, or scalar product, of two lists of the same length that represent vectors. 1 three-element list, for example, ma$ represent a three-dimensional ph$sical vector such as velocit$, acceleration, force, moment, or momentum. In such cases, the inner product is referred also as a $!t product because the notation used is u v, where u and v are ph$sical vectors. 1 dot product is distinguished from a vector, or cr!&&, product which is expressed as u v. 5ome examples of function inner r!$%ct are shown next:


Gilberto E. Urroz, 2008

0or ph$sical vectors, the inner product of a vector with itself is the s'uare of its magnitude, e.g., given vector v, as shown below, its magnitude is AvA * magv:

unitvector 0unction %nitvect!r produces the unit vector associated with a vector. 0or example, if v represents a ph$sical vector, the corresponding unit vector is ev * v/|v|. 0or the vector v shown above, the unit vector can be calculated using:

which is the same than

uniteigenvectors &onsider the s$mmetric matrix A:

whose eigenvalues and eigenvectors are: 7-21

Gilberto E. Urroz, 2008

The three eigenvectors can be extracted b$ using:

0unction %niteigenvect!r& produces the unit eigenvectors of the matrix, e.g.,

The unit eigenvectors can be extracted using:

%e can check, for example, that ex, is the unit vector corresponding to x, b$ using:


Gilberto E. Urroz, 2008

gramschmidt 0unction gramschmidt performs a Gram-5chmidt orthogonalization for the rows of a matrix. This process is described in http:33en.wikipedia.org3wiki3GramB#<BCDBE.5chmidt@process. In this example we define a matrix A, and appl$ function gram&chmi$t to that matrix:

similarit$transform or simtran %hen applied to a real matrix, function &imilarit"tran&f!rm or &imtran produces the same output than function %niteigenvect!r&. 1 complete description of this function is available b$ using:

Functions for matrix decomposition 6atrix decomposition is useful in linear algebra applications. Maxima provides the following functions for matrix decomposition: cholesk$ 0unction ch!le&(" produces the &holesk$ decomposition of a s$mmetric, positive-definite matrix (http:33en.wikipedia.org3wiki3&holesk$@decomposition). 1n example is shown next, in which we first define a matrix A:


Gilberto E. Urroz, 2008

The cholesk$ decomposition results in

eigens@b$@2acobi 0unction eigen&3b"3jac!bi calculates the eigenvalues of a s$mmetric real matrix b$ the method of Facobi rotations (http:33mathworld.wolfram.com3Facobi?otation6atrix.html). &onsider the following example in which we first define a s$mmetric matrix A:

The eigenvalues and eigenvectors are calculated as follows:


Gilberto E. Urroz, 2008

lu@factor 0unction l%3fact!r produces the 4U decomposition of a matrix. To learn about 4U decomposition see, for example, http:33en.wikipedia.org3wiki3G"@decomposition . The example shown below uses matrix A defined above:

Hilbert matrix and Vandermonde matrix The ,ilbert matrix and Handermonde matrix are specialized matrices used in linear algebra. In this section we describe and present examples of functions that Maxima provides with the purpose of generating such matrices. hilbert@matrix 0unction hilbert3matrix(n) produces the ,ilbert matrix of order n n. 1 ,ilbert matrix has 1 elements h i , j= . 1n example of a ,ilbert matrix is shown next: i j1

vandermonde@matrix 1 Handermonde matrix is an n n matrix generated from a column vector (or a list) of length n. The resulting matrix is such that column j results from raising the elements of the originating column vector (or list) to the power (j1,). To calculate a Handermonde matrix in Maxima use van$erm!n$e3matrix(li&t), where li&t is a list of numbers, e.g.,


Gilberto E. Urroz, 2008

onstant matrices! random matrices! matrix si"e! and individual elements This section addresses a few items not addressed in the previous sections. &onstant matrices 5uppose that $ou want to produce a matrix with all its elements being the same constant value. GetIs refer to it as a c!n&tant matrix. The following, user-defined, function can be used to produce a matrix of m rows and n columns with all its elements e'ual to a value c:

Two examples of application of function cmatrix are shown next:

?andom numbers and random matrices 0unction cmatrix, in combination with function ran$!m, can be used to generate a random matrix of integer numbers. 0unction ran$!m.x/ produces a random number between 5 and x. 0or example, the following command produces a list of ,5 random numbers between 5 and ,5:


Gilberto E. Urroz, 2008

If we change the integer number ,5 to the floating point value ,5.5 in the ran$!m function call above, the resulting random numbers are floating point, e.g.,

If we want to produce a random number x between values a and b, we can generate a random number r between 5.5 and ,.5, and use the relationship x 6 a 7 r(b1a/. 0or example, to generate ,5 random values between 18 and ,5, we can use:

If we wanted to convert this list to integer values onl$, we use function fix mapped onto the list shown above, i.e,

?andom matrices The following functions can be used to generate matrix of random elements. 5uppose that the matrix has dimensions n m and that the random numbers will be generated between values a and b, then we can define functions ran$matrix and ran$matrixfix to generate matrices of floating-point or integer values, respectivel$:


Gilberto E. Urroz, 2008

#xamples of random matrices generated with these functions follow:

6atrix size The size of a matrix can be determined b$ function matrix3&i0e, e.g.,

The number of rows and columns can be extracted b$ using:

Individual elements Individual elements of a matrix are referred to b$ using sub-indices as illustrated in the following examples:


Gilberto E. Urroz, 2008