Migrating linalg commands to LinearAlgebra and VectorCalculus
The LinearAlgebra and VectorCalculus modules together supersede the linalg package. This document shows how to translate common linalg command sequences to LinearAlgebra and VectorCalculus.
|
Introduction
|
|
The LinearAlgebra package (introduced in Maple 6) is a robust and efficient suite of routines for performing computational linear algebra. Together with the VectorCalculus package (introduced in Maple 8), LinearAlgebra provides more complete and efficient functionality than the older linalg package.
It is recommended that you use LinearAlgebra and VectorCalculus instead of linalg.
Advantages of LinearAlgebra and VectorCalculus over linalg include:
•
|
The numerical routines in LinearAlgebra for floating-point computation are far more efficient. They make calls to compiled C routines from the NAG library, whereas numerical evaluation in linalg relies solely on the built-in Maple numerics. You can therefore work with very large and/or very sparse Matrices efficiently.
|
•
|
The new matrix and vector constructors (called Matrix and Vector) are more intuitive and flexible. For example, LinearAlgebra provides built-in commands for constructing identity Matrices, zero Vectors, unit basis Vectors and many other structured Matrices. Furthermore, both constructors have options for output formatting, datatype of the entries, and more.
|
•
|
Matrices and Vectors in LinearAlgebra are new data structures, separate from arrays and lists. These data structures were designed explicitly for efficient linear algebra computations, whereas in linalg, matrices and vectors are stored as general arrays or lists. When creating Matrices and Vectors, you can optionally specify the datatype of their entries (for example, floating-point or complex), the shape (for example, triangular or symmetric), the storage method (for example, sparse or rectangular), and more. The LinearAlgebra routines you call on those matrices can then apply algorithms that take advantage of the matrices' structure.
|
•
|
The syntax for Matrix arithmetic is simpler. For example, LinearAlgebra avoids both the syntactic and computational overhead of evalm. You can apply the binary operators +, -, . and ^ directly.
|
•
|
LinearAlgebra offers two modes of use, one for maximal numeric efficiency, the other for maximal ease-of-use and readability.
|
•
|
The set of commands for vector calculus in the VectorCalculus package is more comprehensive than that in linalg. (For example, it includes an augmented int routine that makes multiple integration over complicated geometric regions extremely simple. It also has routines for differential geometry.)
|
•
|
VectorCalculus displays vectors as linear combinations of the basis vectors of the coordinate system you specify. This provides more useful information than a simple list of coordinate values, as linalg does.
|
•
|
Unlike linalg, VectorCalculus distinguishes constant vectors from vector fields. This reduces the risk of confusion about the types of the mathematical objects you create.
|
|
|
Constructing Matrices and Vectors
|
|
|
Entering matrices by data entry
|
|
linalg
>
|
|
>
|
|
>
|
|
| (2.1.1) |
LinearAlgebra
>
|
|
>
|
|
>
|
|
>
|
|
| (2.1.2) |
For small Vectors and Matrices, the <..> and <<..>> constructor shortcuts may be more convenient
>
|
|
>
|
|
| (2.1.3) |
|
|
Extending a given Matrix or Vector
|
|
The linalg package has routines stackmatrix and augment (or concat) that can extend matrices or conjoin them.
>
|
|
>
|
|
>
|
|
| (2.2.1) |
>
|
|
>
|
|
>
|
|
| (2.2.2) |
These operations can be done directly using either the Matrix and Vector constructors or their shortcuts.
>
|
|
>
|
|
>
|
|
>
|
|
| (2.2.3) |
>
|
|
>
|
|
>
|
|
>
|
|
| (2.2.4) |
The linalg routine blockmatrix assembles blocks of matrices together, which can also be accomplished directly using the Matrix constructor without having to specify the number of blocks.
>
|
|
| (2.2.5) |
>
|
|
>
|
|
| (2.2.6) |
The Matrix constructor reproduces the functionality of the linalg routine extend.
>
|
|
>
|
|
>
|
|
>
|
|
| (2.2.7) |
The functionality of the linalg routine copyinto can also be accomplished with Matrices.
| (2.2.8) |
| (2.2.9) |
|
|
Specially structured matrices and vectors
|
|
linalg
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
| (2.3.1) |
LinearAlgebra
The naming convention for LinearAlgebra routines is to spell the operation name completely, beginning each word with an uppercase letter.
>
|
|
>
|
|
>
|
|
| (2.3.2) |
A strength of LinearAlgebra is its ability to work with very large Matrices efficiently. Here, we create a 3000-dimensional unit Vector with a 1 in the 17th position.
>
|
|
| (2.3.3) |
>
|
|
>
|
|
| (2.3.4) |
Here is a 100x100 band Matrix.
>
|
|
| (2.3.5) |
>
|
|
>
|
|
>
|
|
| (2.3.6) |
Maple prints large Vectors and Matrices (one or both dimensions > 10) in the form of a data structure summary. To browse the contents of the vector or matrix , double-click the matrix output. Below is the interface for the 100x100 banded matrix created above.
|
|
|
Matrix Arithmetic
|
|
|
Binary operations
|
|
linalg
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
| (3.1.1) |
LinearAlgebra
For maximal simplicity, you can use +, -, ., and ^ for binary operations.
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
| (3.1.2) |
For maximal computational efficiency, use the named routines, which demand specific types for the operands (that is, Vector, Matrix, or scalar). These routines save computation time because, unlike the + and . operators, they do not need to determine the operand types.
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
| (3.1.3) |
|
|
Unary operations and matrix properties
|
|
linalg
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
| (3.2.1) |
Note that computes the same result as .
>
|
|
>
|
|
>
|
|
>
|
|
| (3.2.2) |
LinearAlgebra
These operations and others are also available through the context menus. To use them, right-click (COMMAND-click on Macintosh) a matrix and select the desired operation from the menu that appears.
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
| (3.2.3) |
|
|
|
Row and Column Operations
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
| (4.1) |
LinearAlgebra
Note: Inside a procedure, you can invoke LinearAlgebra commands (or those from any module), with the use <module names> in <module routine calls> end use; construction. This eliminates the need to invoke with(LinearAlgebra) or prepend LinearAlgebra:- to the names of the routines. The use statement is illustrated below.
>
|
|
| (4.2) |
Use the RowOperation and ColumnOperation routines for any general operation of Gaussian elimination (swapping rows/columns, multiplying rows/columns by scalars, or adding multiples of one row to another). You can overwrite the Matrix with the transformed one by including the option inplace=true, or simply inplace, at the end of the command.
>
|
|
| (4.3) |
Another simple syntax is available to manipulate rows, columns, or subblocks of Matrices and Vectors.
>
|
|
| (4.4) |
>
|
|
>
|
|
| (4.5) |
>
|
|
>
|
|
| (4.6) |
>
|
|
>
|
|
| (4.7) |
|
|
Solving Systems of Linear Equations
|
|
linalg
>
|
|
>
|
|
| (5.1) |
>
|
|
>
|
|
| (5.2) |
>
|
|
>
|
|
| (5.3) |
LinearAlgebra
>
|
|
>
|
|
| (5.4) |
>
|
|
>
|
|
| (5.5) |
>
|
|
>
|
|
| (5.6) |
|
|
Matrix Reductions and Decompositions
|
|
linalg
>
|
|
| (6.1) |
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
| (6.2) |
>
|
|
| (6.3) |
>
|
|
>
|
|
| (6.4) |
>
|
|
>
|
|
| (6.5) |
LinearAlgebra
>
|
|
| (6.6) |
>
|
|
| (6.7) |
|
|
Vector Calculus
|
|
>
|
|
linalg
>
|
|
>
|
|
>
|
|
>
|
|
| (7.1) |
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
| (7.2) |
>
|
|
>
|
|
>
|
|
>
|
|
| (7.3) |
VectorCalculus
The VectorCalculus package outputs vectors and vector fields as linear combinations of basis vectors. For example, in Cartesian coordinates, the standard basis vectors are , and . Set the coordinate system to work in using the SetCoordinates routine.
>
|
|
>
|
|
| (7.4) |
>
|
|
| (7.5) |
Making a function out of a vector derivative, in this case, the Hessian matrix computed immediately above.
>
|
|
>
|
|
| (7.6) |
In VectorCalculus, a vector field is a different data structure from a vector. Vector fields have their own constructor, VectorField. To evaluate the vector field at a particular vector, use the evalVF routine.
>
|
|
| (7.7) |
VectorCalculus provides far more functionality than what is possible using linalg. For a full overview, see the demonstration The Vector Calculus Package on the Maple Application Center.
|
|
Programming Hints
|
|
|
Package use
|
|
Both LinearAlgebra and linalg are implemented as Maple packages. Using with to load these packages results in their individual component routines' names becoming available for use directly.
>
|
|
>
|
|
>
|
|
>
|
|
| (8.1.1) |
>
|
|
>
|
|
>
|
|
>
|
|
| (8.1.2) |
The LinearAlgebra and linalg packages are implemented as modules. Since both are Maple packages, they share the following long-form calling syntax in the case that neither package has been loaded. The component routines can always be called directly.
>
|
|
>
|
|
| (8.1.3) |
>
|
|
>
|
|
| (8.1.4) |
Since the LinearAlgebra package is a module, it also offers another long-form syntax for calling a routine directly.
>
|
|
| (8.1.5) |
Basic arithmetic involving matrix and vector objects, as used by linalg, can be accomplished through use of evalm.
>
|
|
| (8.1.6) |
Such arithmetic is available for Matrix and Vector objects, as used by LinearAlgebra, directly.
>
|
|
Use of evalm when performing operations with Matrix and Vector objects is not advised; it introduces unnecessary inefficiency to the operations.
| (8.1.7) |
|
|
Evaluation
|
|
The matrix and vector objects used in the linalg package have last name evaluation. Assignment chains involving these objects are only normally evaluated to the last name in the chain. Here are some examples.
>
|
|
>
|
|
>
|
|
| (8.2.1) |
>
|
|
>
|
|
>
|
|
>
|
|
| (8.2.2) |
>
|
|
>
|
|
>
|
|
>
|
|
| (8.2.3) |
The Matrix and Vector objects in the LinearAlgebra package do not follow last name evaluation. As a result, you do not need to specify a full evaluation, at the Maple top-level, to display the entries.
>
|
|
| (8.2.4) |
>
|
|
>
|
|
>
|
|
| (8.2.5) |
>
|
|
>
|
|
>
|
|
>
|
|
| (8.2.6) |
|
|
Output objects versus side-effects
|
|
Several linalg routines make use of side-effects on names to assign auxiliary results.
Here, QRdecomp has made an assignment to q as a side-effect of the operation that computed r.
>
|
|
>
|
|
>
|
|
>
|
|
| (8.3.1) |
The routines in the LinearAlgebra package use multiple-assignment instead of side-effects in order to assign auxiliary results.
>
|
|
>
|
|
>
|
|
| (8.3.2) |
>
|
|
|
|
Return to Index for Example Worksheets
|