Maxima Book Chapter 7

Embed Size (px)

Citation preview

  • 8/13/2019 Maxima Book Chapter 7

    1/28

    Basic matrix and linear algebra functions inMaximaIn this chapter we present examples of matrix and linear algebrafunctions included in theAlgebramenu in the wxMaximainterface.

    Functions in theAlgebra menu

    The items in theAlgebra menu, shown in the figure to theright, are presented in the following sections.

    Generate matrix ...TheAlgebra > Generate matrix ...utilizes a function ofthe matrix sub-indices iandj, defined previous toinvoking the menu item, e.g.,

    Then, usefin the dialogue form that results from theAlgebra > Generate matrix ... menu item, e.g.,

    This will result in the command:

    7-1 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    2/28

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

    "sing a generic name with sub-indices:

    #nter matrix ...

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

    general matrix:

    7-2 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    3/28

    diagonal matrix:

    s$mmetric matrix:

    antis$mmetric matrix

    7-3 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    4/28

    Invert matrix%hen invoked from theAlgebramenu, the menu item Invert matrixproduces the inverse ofthe matrix referred to with %, or of a matrix referred to b$ name or listen in the INPUTline, e.g.,

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

    7-4 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    5/28

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

    #igenvaluesTheAlgebra > Eigenvale& menu item calculates the eigenvalues of a matrix, i.e., it findsthe roots of the characteristic pol$nomial of the matrix. ere is an example of thisfunction applied to matrix Adefined above:

    otice that the output of function eigenvale&consists of two lists. The first list is the listof 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:

    #igenvectorsTheAlgebra > Eigenvect!r&menu item calculates the eigenvectors of a matrix, i.e., itsolves for the vectors vfrom the eigenvalue e'uation Av *xv. 0or example, for the matrix Adefined above:

    7-5 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    6/28

    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 theeigenvectors of the matrix corresponding to the eigenvalues listed first.

    If we assign this output to a variable we can then extract the individual eigenvectors asfollows:

    1d2oint matrix

    The ad2oint matrix produced b$ theAlgebra > A$j!int matrixmenu item corresponds to thedefinition of the a$jgatematrix as given in http:33en.wikipedia.org3wiki31d2ugate. In thefollowing example we first put together a complex matrix Aand then calculate the ad2ointor ad2ugate matrix. 0irst, we define a couple of x real matricesA'andAI:

    7-6 Gilberto E. Urroz, 2008

    http://en.wikipedia.org/wiki/Adjugatehttp://en.wikipedia.org/wiki/Adjugate
  • 8/13/2019 Maxima Book Chapter 7

    7/28

    Then, we put together matrixA*AI4 iA':

    ext, we invoke theAlgebra > A$j!int matrix menu item to produce the ad2ugate (orad2oint) matrix:

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

    Transpose matrix

    TheAlgebra > Tran&!&e matrixmenu item produces the transpose of a matrix. 5ee thedefinition here: http:33en.wikipedia.org3wiki3Transpose. 0or example, for the matrixAdefined above, we have:

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

    7-7 Gilberto E. Urroz, 2008

    http://en.wikipedia.org/wiki/Transposehttp://en.wikipedia.org/wiki/Transpose
  • 8/13/2019 Maxima Book Chapter 7

    8/28

    produces the list shown below:

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

    6ap to list ...TheAlgebra > Ma t! li&t ...menu item produces a dialogue form that can be used to7map8 (or distribute) a function to the elements of a list. The application of this menuitem is illustrated b$ the following example:

    7-8 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    9/28

    1 second example of function mais show next:

    6ap to matrix ...TheAlgebra > Ma t! matrix ...menu item behaves similar to the Ma t! li&t... menuitem, 7mapping8 a function to all elements of a matrix. The application of this menu itemis 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:

    7-9 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    10/28

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

    Functions for creating matricesIn this section we present examples of Maximafunctions to generate matrices. 5omefunctions for creating matrices that are available in theAlgebramenu were introducedabove (genmatrix, matrixma, tran&!&e). The following examples demonstrate the use ofadditional functions:

    cop$matrix"se c!"matrixto cop$ a matrix into a variable name. 0or example, first create matrixA)

    then, cop$ matrixAinto *using c!"matrix:

    7-10 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    11/28

    columnvector1 column vector is a matrix of nrows and 9 column. 0unction c!lmnvect!rlets $ou build acolumn vector out of a list of values, e.g.,

    diag0unction $iag, which needs to be loaded separatel$, allows $ou to build a diagonal matrixbased on two or more matrices. 0or example, using matricesAand *, defined above, wecan build the following diagonal matrix:

    diagmatrix

    0unction $iagmatrix(n,a) creates a diagonal matrix of dimensions nnwith all its diagonalelements e'ual to a:

    7-11 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    12/28

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

    ematrix

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

    entermatrix0unction 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 n

    nidentit$ matrix:

    7-12 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    13/28

    matrix0unction matrix, which has been used before in this and other &hapters, allows the user toenter a matrix b$ defining the matrix rows as lists of the same length. The use of matrixisillustrated in the following example:

    submatrix0unction &bmatrixallows the user to extract a submatrix out of a matrix. To illustrate the

    use of function &bmatrixconsider the ++matrixA:

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

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

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

    7-13 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    14/28

    zeromatrix

    0unction 0er!matrix(m,n) creates a matrix of dimensions mnsuch that all its elements arezero values, e.g.,

    Functions for manipulating matricesThe 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 matrixAdefined above, andrepeated here:

    col0unction c!lextracts a column out of a matrix, e.g.,

    row0unction r!wextracts a row out of a matrix:

    7-14 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    15/28

    addcol0unction a$$c!lis used to append one or more columns to a matrix, e.g.,

    addrow0unction a$$r!wis 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 matricesAand *:

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

    7-15 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    16/28

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

    Traditional, n!n1c!mmtative, matrix mltilicati!nis achieved b$ using a dot (.) as themultiplication s$mbol:

    =owerAmatrix rai&e$ t! a &calar ex!nentialproduces a term-b$-term exponentiation, e.g.,

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

    7-16 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    17/28

    Matrix ex!nentiati!nuses a double caret (>>) and represents the result of repeatedmatrix multiplication, i.e., 1>>< * 1.1, 1>> * 1>>

  • 8/13/2019 Maxima Book Chapter 7

    18/28

    coefmatrix0unction c!efmatrix(li&t !f linear e2ati!n&;,li&t !f variable&;) can be used to extract thecoefficients from a li&t !f linear e2ati!n&containing the variables in the li&t !f variable&,e.g.,

    augcoefmatrix0unction agc!efmatrix (li&t !f linear e2ati!n&;,li&t !f variable&;) produces anaugmented 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 e2ati!n&. 0or example, for the s$stem of linear e'uations used in the c!efmatrixexample shown above, the resulting augmented matrix of coefficients is calculated asfollows:

    echelon and triangularize+oth functions echel!n(A) and trianglari0e(A) produce upper triangular matricesrepresenting row-reduced echelon forms of matrixA. The difference between these twofunctions is that function echel!nproduces a matrix such that its main diagonal elementsare reduced to the number . The following examples, using matricesAandAA createdabove, illustrate the application of these two functions highlighting their differences:

    7-18 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    19/28

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

    minorThe minor matrix (i,j) of a matrixAis the matrix that results from eliminating row iandcolumnj. 6inor matrices are used, for example, in the calculation of determinants.

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

    7-19 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    20/28

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

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

    tracematrix0unction tracematrix, which needs to be loaded with packagefnct&, calculates the traceof a matrix, e.g.,

    Functions in the eigenpackageThe functions described in this package are used in the calculation of eigenvalues and

    eigenvectors of matrices. 0unctions eigenvale&and eigenvect!r&, which belong to thispackage, were described in the context of theAlgebra menu at the beginning of thischapter. %hen functions eigenvale& and eigenvect!r&are invoked, the eigenpackage isinvoked automaticall$. To use the other functions make sure to load the package eigenbeforehand:

    innerproduct0unction innerr!$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 athree-dimensional ph$sical vector such as velocit$, acceleration, force, moment, ormomentum. In such cases, the inner product is referred also as a $!tproduct because the

    notation used is u v, where uand vare ph$sical vectors. 1 dot product is distinguished

    from a vector, or cr!&&, product which is expressed as u v. 5ome examples of functioninnerr!$ctare shown next:

    7-20 Gilberto E. Urroz, 2008

    http://en.wikipedia.org/wiki/Permanenthttp://en.wikipedia.org/wiki/Rank_(linear_algebrahttp://en.wikipedia.org/wiki/Permanenthttp://en.wikipedia.org/wiki/Rank_(linear_algebra
  • 8/13/2019 Maxima Book Chapter 7

    21/28

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

    unitvector

    0unction nitvect!rproduces the unit vector associated with a vector. 0or example, if vrepresents a ph$sical vector, the corresponding unit vector is ev* v/|v|. 0or the vector vshown 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

  • 8/13/2019 Maxima Book Chapter 7

    22/28

    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 exis the unit vector corresponding toxb$ using:

    7-22 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    23/28

    gramschmidt0unction gramschmidt performs a Gram-5chmidt orthogonalization for the rows of a matrix.This process is described in http:33en.wikipedia.org3wiki3GramB#

  • 8/13/2019 Maxima Book Chapter 7

    24/28

    The cholesk$ decomposition results in

    eigens@b$@2acobi0unction eigen&3b"3jac!bicalculates the eigenvalues of a s$mmetric real matrix b$ themethod of Facobi rotations (http:33mathworld.wolfram.com3Facobi?otation6atrix.html).&onsider the following example in which we first define a s$mmetric matrixA:

    The eigenvalues and eigenvectors are calculated as follows:

    7-24 Gilberto E. Urroz, 2008

    http://mathworld.wolfram.com/JacobiRotationMatrix.htmlhttp://mathworld.wolfram.com/JacobiRotationMatrix.html
  • 8/13/2019 Maxima Book Chapter 7

    25/28

    lu@factor0unction l3fact!rproduces the 4Udecomposition of a matrix. To learn about 4Udecomposition see, for example, http:33en.wikipedia.org3wiki3"@decomposition. Theexample shown below uses matrixAdefined above:

    Hilbert matrix and Vandermonde matrixThe ilbert matrix and Handermonde matrix are specialized matrices used in linear algebra.In this section we describe and present examples of functions that Maximaprovides withthe purpose of generating such matrices.

    hilbert@matrix

    0unction hilbert3matrix(n) produces the ilbert matrix of order n n. 1 ilbert matrix has

    elements h i , j= 1

    i j1. 1n example of a ilbert matrix is shown next:

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

    7-25 Gilberto E. Urroz, 2008

    http://en.wikipedia.org/wiki/LU_decompositionhttp://en.wikipedia.org/wiki/LU_decomposition
  • 8/13/2019 Maxima Book Chapter 7

    26/28

    onstant matrices! random matrices! matrix si"e! and individual elementsThis section addresses a few items not addressed in the previous sections.

    &onstant matrices5uppose that $ou want to produce a matrix with all its elements being the same constantvalue. ets refer to it as a c!n&tant matrix. The following, user-defined, function can beused to produce a matrix of mrows and ncolumns with all its elements e'ual to a value c:

    Two examples of application of function cmatrixare shown next:

    ?andom numbers and random matrices0unction cmatrix, in combination with function ran$!m, can be used to generate a random

    matrix of integer numbers. 0unction ran$!mx/produces a random number between 5andx. 0or example, the following command produces a list of 5random numbers between 5and 5:

    7-26 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    27/28

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

    If we want to produce a random numberxbetween values aand b, we can generate a

    random number rbetween 5.5and .5, and use the relationshipx 6 a 7 r(b1a/. 0orexample, to generate 5random values between 18and 5, we can use:

    If we wanted to convert this list to integer values onl$, we use functionfix mapped ontothe list shown above, i.e,

    ?andom matricesThe 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 betweenvalues aand b, then we can define functions ran$matrixand ran$matrixfixto generatematrices of floating-point or integer values, respectivel$:

    7-27 Gilberto E. Urroz, 2008

  • 8/13/2019 Maxima Book Chapter 7

    28/28

    #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 elementsIndividual elements of a matrix are referred to b$ using sub-indices as illustrated in thefollowing examples:

    7 28 ilb