Linear Algebra Options
In the sections below are several commented examples using options that are common to many of the LinearAlgebra package functions. They illustrate how you can exercise greater control over Maple behavior.
>
|
|
>
|
|
|
Inplace
|
|
|
Introduction
|
|
The inplace option determines whether a function creates a new object or uses one of its inputs to store the output. Most functions default to returning a new object. The exceptions to this are LinearAlgebra[Map] and LinearAlgebra[Zip].
>
|
|
| (1.1.1) |
By default, when we swap the rows of A, we get a new Matrix, and A is unchanged:
>
|
|
| (1.1.2) |
>
|
|
| (1.1.3) |
If we want A to be updated, we indicate that by using "inplace" or "inplace=true":
>
|
|
| (1.1.4) |
>
|
|
| (1.1.5) |
Performing an operation in place saves memory, and can also save having to create new variable names for intermediate results. In the following example, a 3x3 Matrix is augmented with a 3x3 identity Matrix, and then the inverse is found manually.
|
|
Matrix Inverse Example
|
|
Create the Matrix.
>
|
|
| (1.2.1) |
Augment the Matrix with an identity Matrix of the same size.
>
|
|
| (1.2.2) |
Use elementary row operations to get zeroes below the main diagonal.
>
|
|
| (1.2.3) |
>
|
|
| (1.2.4) |
>
|
|
| (1.2.5) |
>
|
|
| (1.2.6) |
>
|
|
| (1.2.7) |
Use elementary row operations to get zeros above the main diagonal
>
|
|
| (1.2.8) |
>
|
|
| (1.2.9) |
>
|
|
| (1.2.10) |
Extract the inverse Matrix.
>
|
|
| (1.2.11) |
Multiply the original Matrix by its inverse to verify that it is, indeed, the inverse.
>
|
|
| (1.2.12) |
>
|
|
| (1.2.13) |
|
|
|
Output Options
|
|
Most LinearAlgebra functions that return a Matrix allow you some control over the form of the output Matrix. This control is gained by using the outputoptions parameter. What follows is an example of why you might use outputoptions with the IdentityMatrix function.
Suppose that you want to create a Matrix that is almost an identity Matrix: just two of its rows are swapped. You could use the IdentityMatrix function as a starting point to create your Matrix, and then swap two rows:
>
|
|
>
|
|
| (2.1) |
That worked, but now you have two Matrices: M1 and M2. You really do not require the original identity Matrix. It would have been better to perform the operation in place, but when you try that, you get an error:
>
|
|
Error, (in LinearAlgebra:-LA_Main:-RowOperation) invalid assignment to identity diagonal
| |
The error arises because the default Matrix returned by the IdentityMatrix function uses no storage, just a mathematical shape of 'identity'. Take a closer look:
>
|
|
| (2.2) |
The 'empty' storage means that no space is allocated for entries, and the 'identity' index function means that only 1's are permitted on the main diagonal and only 0's off the diagonal.
To create an identity Matrix that can be updated, use outputoptions to specify rectangular shape (so that the values that can go in each location are unrestricted, and that rectangular storage is used).
>
|
|
| (2.3) |
Now you can swap the rows inplace:
>
|
|
| (2.4) |
|
|
Method
|
|
Some functions allow you to specify the method (algorithm) to be used to carry out the function. For example, the Determinant function lets you choose from many methods, including 'float', 'minor', and 'rational'. If you know your Matrix has particular characteristics, you can specify an appropriate algorithm, making the computation faster.
M is an upper triangular Matrix, and this information is readily available to the Determinant function.
>
|
|
| (3.1) |
N is also an upper triangular Matrix, but this information is not readily available to the Determinant function because its shape is 'rectangular'.
>
|
|
| (3.2) |
The Determinant function uses the fact that M is upper triangular to select an appropriate algorithm and returns an answer in a negligible amount of time:
| (3.3) |
With no information about , the Determinant function does not necessarily make the most efficient algorithm choice, and takes longer:
| (3.4) |
When the user specifies the method, the Determinant function can work as quickly on N as it did on M. (The minor expansion is done along the first column, and since the first column of an upper triangular Matrix is mostly zeros, the computation is very fast for upper triangular Matrices.)
| (3.5) |
>
|
|
For more information, see the Determinant help page.
|
Return to Index for Example Worksheets
|