Linear Algebra Syntax Shortcuts - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Applications and Example Worksheets : Algebra : examples/LA_Syntax_Shortcuts

Linear Algebra Syntax Shortcuts

The examples in this worksheet demonstrate shortcuts for some Linear Algebra commands.  They illustrate how you can save keystrokes when using Maple interactively.

restart

withLinearAlgebra:

Creating Matrices and Vectors

The Matrix and Vector functions give you great control over the type of Matrix or Vector you create.  For example, you can specify shape, storage mode, and datatype.  If you are creating a Matrix or Vector interactively and do not need to specify special features, you may find it faster to use some shorthand notations:

Instead of using:

Matrix6,3,4,8,9,14

6348914

(1.1)

you can enter:

6,3,4;8,9,14

6348914

(1.2)

or,

6|3|4,8|9|14

6348914

(1.3)

If you prefer to enter your data in column-major order, use commas to separate entries, and vertical bars to separate rows:

6,8|3,9|4,14

6348914

(1.4)

or,

6,8|3,9|4,14

6348914

(1.5)

Instead of using,

Vector7,3,2

732

(1.6)

you can enter:

7,3,2

732

(1.7)

Instead of using,

Vectorrow11,27,6

11276

(1.8)

you can enter:

11|27|6

11276

(1.9)

You can also enter Matrices and Vectors using the Matrix palette.

Addition

Adding a Scalar to a Matrix, Vector, or Array

When you try to add a scalar to a Vector, you get an error because there is no mathematical meaning for this operation.

3+7,8,9

Error, a constant cannot be added to a Vector; use +~ for elementwise addition instead of +

When you add a scalar to a Matrix, the scalar is interpreted as the scalar multiplied by an appropriately sized Identity Matrix.  Another way to think of this is that the scalar is added to the entries on the main diagonal.

7+1,2,3;4,5,6;7,8,9 = 7IdentityMatrix3 + 1,2,3;4,5,6;7,8,9

82341267816=82341267816

(2.1.1)

10+2,5,11;4,6,7=10IdentityMatrix2,3+2,5,11;4,6,7

125114167=125114167

(2.1.2)

When you add a scalar to an Array, the scalar is added to every entry in the Array.

7+Array1..3,1..3,1,2,3,4,5,6,7,8,9

8910111213141516

(2.1.3)

Adding Rectangular Objects

The MatrixAdd and VectorAdd functions let you add a multiple of one Matrix (or Vector) to another.  This functionality can also be obtained by using the + and * operators:

Adding (multiples of) Vectors:

3,4+37,5

2419

(2.2.1)

Adding two 2x3 Matrices (one is constructed in row-major order and the other in column-major order, but the addition still works since the resulting Matrices are the same size):

27,2,3;8,9,634,3|6,5|12,19

2143073345

(2.2.2)

You can also add Arrays:

3Array1,7+Array8,23

544

(2.2.3)

Multiplication

The method by which multiplication is carried out varies with the type of objects involved, and the operators used to request multiplication.

Commutative Multiplication

The '*' operator is used to perform commutative multiplication.  When you use the '*' operator, Maple assumes it can safely reorder the operands.

xyz;zyx

xyz

xyz

(3.1.1)

Since multiplying a Vector, Matrix, or Array by a scalar is commutative, you can use the * operator to perform that operation:

3  1,2,3;4 7,8;1,6t;3 Arrayw,y,y,z

369

2832424t

3w3y3y3z

(3.1.2)

Matrix and Vector multiplication is not commutative, so attempting to use the '*' operator when both operands are Matrices or Vectors results in an error.

1,3  4|6

Error, (in rtable/Product) use *~ for elementwise multiplication of Vectors or Matrices; use . (dot) for Vector/Matrix multiplication

There is a form of commutative multiplication for Arrays.  The '*' operator carries out pointwise multiplication (each entry in the first Array is multiplied by the corresponding entry in the second Array):

Array1,2,3,4 Arrayw,x,y,z

w2x3y4z

(3.1.3)

Pointwise (or elementwise) multiplication of Matrices or Vectors can be performed by using the '~' ("elementwise") operator:

1,2,3;4,5,6~a,b,c;d,e,f

a2b3c4d5e6f

(3.1.4)

Noncommutative Multiplication

The '.' operator is used to perform noncommutative multiplication.  When you use the '.' operator, Maple knows it must not reorder the operands.

x.y.z;z.y.x

x.y.z

z.y.x

(3.2.1)

You can also use the '.' operator to perform scalar multiplication.  In 2-D math, you must put the scalar in parentheses so that Maple knows you intend to use the . as an operator, not a decimal point.  Scalar multiplication is the one situation in which '*' and '.' can be used interchangeably.  You may find it easier to use '*' or implicit multiplication, since it does not require parentheses.

(Note: In 1-D math, there is no need to use parentheses, however, you must type spaces on either side of the dot so that Maple knows you intend to use the . as an operator.)

 

3 . 1,2,3;4.7,8;1,6;3 Arrayw,y,y,z

369

28.32.4.24.

3w3y3y3z

(3.2.2)

If one of the operands is an unassigned variable, Maple does not know whether it represents a scalar, and so does not automatically carry out the multiplication.

x . 1,2,3;7,8;1,6 . x;x . Arrayw,y,y,z

x.123

7816.x

x.wyyz

(3.2.3)

To tell Maple to treat the variable as a scalar, use the simplify command:

simplifyx . 1,2,3;simplify7,8;1,6  . x;simplifyx . Arrayw,y,y,z

x2x3x

7x8xx6x

x.wx.yx.yx.z

(3.2.4)

You can carry out Matrix and Vector multiplication by using the '.' operator.

1,3 . 4|6;4|6 . 1,3;3,1;8,15 . 1,x;4,7

461218

22

13x+7528x105

(3.2.5)

On Arrays, the '.' operator performs component-wise multiplication.

Array1,2,3,4 . Arrayw,x,y,z

w2x3y4z

(3.2.6)

To carry out Matrix multiplication repeatedly (that is, to get a power of a Matrix), use the '^' operator:

M:=0.2,0.8|0.35,0.65;M.M.M.M.M.M.M.M.M.M=M10

M:=0.20.350.80.65

0.3043478300984370.3043478243319340.6956521699015630.695652175668067=0.3043478300984370.3043478243319340.6956521699015630.695652175668067

(3.2.7)

Transposition

To transpose a Vector or Matrix, use the powering operator with exponent '+':

1&comma;2&comma;3&plus;&semi; <1&comma;2&comma;3&semi;4&comma;5&comma;6&gt;&plus;

123

142536

(4.1)

To obtain the Hermitian transpose of a Vector or Matrix, use the powering operator with exponent '*':

1&plus;I&comma;2&plus;2 I&comma;3I&ast;&semi; 1&plus;I&comma;2&plus;2 I&comma;3&plus;3 I&semi;4&plus;4 I&comma;5&plus;5 I&comma;6&plus;6 I&ast;

1I22I3&plus;I

1I44I22I55I33I66I

(4.2)

Working with Subsections of Data

Extracting Data

The examples in this section use the following Matrix.  Note that (with one exception, indicated below) the same techniques can be used on Vectors and Arrays.

M:=Matrix5&comma;i&comma;j&rarr;3ij

M:=21012543218765411109871413121110

(5.1.1)


To extract a single entry, type its row and column coordinates in square brackets after the name of the Matrix:

M5&comma;4

11

(5.1.2)


The simplest way to extract a row is by using a single index.

M5

1413121110

(5.1.3)

To extract part of a row (or column) as a row Vector (or column Vector), use a range instead of a single index for the row (or column) coordinate:

M5&comma;1..3&comma;M3..5&comma;2

141312&comma;71013

(5.1.4)

 


To extract a sub-Matrix, replace both coordinates with ranges:

M3..5&comma;1..3

87611109141312

(5.1.5)


Notice that you can control whether the result is the entry, a 1-element row Vector, a 1-element column Vector, or a 1x1 Matrix based on whether you use single indices or ranges:

M3&comma;2&semi;                        &num; the value of the entry M3&comma; 2..2&semi;                    &num; a row VectorM3..3&comma;2&semi;                    &num; a column VectorM3..3&comma;2..2                &num; a 1x1 Matrix

7

7

7

7

(5.1.6)


If you do not want a contiguous set of entries, or if you wish to reorder rows or columns, use a list (which can also include ranges):

M1&comma;3&comma;2&comma;4&semi;           &num; 2nd and 4th entries of 1st and 3rd rowsM1..3&comma;5&comma;4&comma;1..5      &num; swap 4th and 5th rows

1175

21012543218765414131211101110987

(5.1.7)


Note that an index list containing a single item (for example, [3]) yields the same results as a range, with the same beginning and end points.  

M3&comma; 2&semi;               &num; a row VectorM3&comma; 2&semi;                &num; a column VectorM3&comma; 2             &num; a 1x1 Matrix

7

7

7

(5.1.8)


You can use negative indices to count backwards from the end of a Vector or Matrix.  Use -1 to indicate the last row (or column), -2 to indicate the second last row (or column), and so on.  (This technique does not apply to Arrays since they can have negative indices.)

M1&comma;1&semi;          &num; entry in last row, last columnM2&comma;1..1        &num; all of the second row

10

54321

(5.1.9)

You can also use implicit range endpoints.  For example, 3.. means from the third row (or column) to the last row (or column), and ..3 means from the first row (or column) to the third row (or column).  Thus, the following syntax is equivalent to M3..5&comma;2 for the 5x5 Matrix M.

M3..&comma;2

You can also use other commands such as select to remove columns or rows of a matrix that do not match a given criteria.  For example, say we wanted to remove any row where the value in the first column is not even:

71013

(5.1.10)

even_rowsselecti  typeMi&comma; 1&comma;even&comma; seq1 .. upperboundM&comma; 1&colon;

Meven_rows

21012876541413121110

(5.1.11)

We can also filter out columns in a similar manner. Say we wanted to filter out any columns where the value in the second row is less than 3.

filtered_columnsselecti M2&comma; i&gt;2&comma; seq1 .. upperboundM&comma; 1&colon;

M..&comma;filtered_columns

21054387611109141312

(5.1.12)

Updating Data

You can overwrite data using the same notation as in the previous section.  This works as long as the right-hand side of your expression matches the size and shape of the left-hand side (it can be smaller than the left-hand side, but not larger).

M:=Matrix5&comma;5&comma;i&comma;j&rarr;7ji

M:=61320273451219263341118253231017243129162330

(5.2.1)

M1&comma;3  1&semi;                &num; update an entryM4&comma;1..2  2&comma;3&semi;   &num; replace part of a rowM4..5&comma;4  4&verbar;5&semi;   &num; replace part of a columnM2..2&comma;5..5  6&semi; &num; replace a sub-MatrixM&semi;

M1&comma;3:=1

M4&comma;1..2:=23

M4..5&comma;4:=45

M2..2&comma;5..5:=6

613127345121926641118253223174312916530

(5.2.2)

So far, we have seen how to index entries in a Matrix or Vector using square brackets, [].  There is a second way to index entries using round brackets, ().  This is called programmer indexing.  One use of programmer indexing is to grow a Matrix or Vector past its initially declared bounds.

 

The following attempts to assign to column 6 of the 5x5 Matrix M. Maple returns an error.

M1..5&comma;60&verbar;1&verbar;2&verbar;3&verbar;4&verbar;5

Error, Matrix index out of range

Using round brackets, however, the Matrix is enlarged to include a sixth column.

M1..5&comma;60&verbar;1&verbar;2&verbar;3&verbar;4&verbar;5

For more information on programmer indexing, see rtable_indexing.

M:=61312734051219266141118253222317431329165304

(5.2.3)

Elementary Row Operations

You can use the techniques from the previous two sections to carry out elementary row operations in place.  Note that these techniques are intended for convenient interactive Maple use.  For programs carrying out extensive operations, it will be more efficient to call the appropriate LinearAlgebra routine directly.

M:=Matrix5&comma;i&comma;j&rarr;i5&plus;j5

M:=12345678910111213141516171819202122232425

(5.3.1)

Swap two rows:

M1&comma;2  M2&comma;1&semi; M&semi;

M1&comma;2:=67891012345

67891012345111213141516171819202122232425

(5.3.2)

Multiply a row by a scalar:

M3  3  M3&semi;M&semi;

M3:=3336394245

67891012345333639424516171819202122232425

(5.3.3)

Add a multiple of one row to another:

M4  M4 16 M2&semi;M&semi;

M4:=015304560

6789101234533363942450153045602122232425

(5.3.4)

See Also

dot, Matrix and Vector Construction Shortcuts, Matrix and Vector Entry Extraction, Matrix palette, rtable_indexing, simplify

 

Return to Index for Example Worksheets


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam