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.
restart
withLinearAlgebra:
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].
A:=RandomMatrix⁡2,3
By default, when we swap the rows of A, we get a new Matrix, and A is unchanged:
RowOperation⁡A,1,2
A
If we want A to be updated, we indicate that by using "inplace" or "inplace=true":
RowOperation⁡A,1,2,inplace
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.
M:=1|2|3,2|−5|4,7|−8|5
Augment the Matrix with an identity Matrix of the same size.
S:=M|IdentityMatrix⁡3
Use elementary row operations to get zeroes below the main diagonal.
RowOperation⁡S,2,1,−2,inplace
RowOperation⁡S,3,1,−7,inplace
RowOperation⁡S,2,−19,inplace
RowOperation⁡S,3,2,22,inplace
RowOperation⁡S,3,−9100,inplace
Use elementary row operations to get zeros above the main diagonal
RowOperation⁡S,2,3,−29,inplace
RowOperation⁡S,1,3,−3,inplace
RowOperation⁡S,1,2,−2,inplace
Extract the inverse Matrix.
N≔S1..3,4..6
Multiply the original Matrix by its inverse to verify that it is, indeed, the inverse.
M.N
N.M
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:
M1:=IdentityMatrix⁡5
M2:=RowOperation⁡M1,1,4
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:
RowOperation⁡M1,1,4,inplace
Error, (in LinearAlgebra:-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:
MatrixOptions⁡M1,storage,shape
empty,identity
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).
M3:=IdentityMatrix⁡5,outputoptions=shape=rectangular
Now you can swap the rows inplace:
RowOperation⁡M3,1,4,inplace
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.
M:=RandomMatrix⁡99,outputoptions=shape=triangularupper
N is also an upper triangular Matrix, but this information is not readily available to the Determinant function because its shape is 'rectangular'.
N≔MatrixM
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:
t1:=time⁡:DeterminantM;time⁡−t1
0
0.003
With no information about N, the Determinant function does not necessarily make the most efficient algorithm choice, and takes longer:
t1:=time⁡:DeterminantN;time⁡−t1
0.004
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.)
t1:=time⁡:DeterminantN,method='minor';time⁡−t1
For more information, see the Determinant help page.
Return to Index for Example Worksheets
Download Help Document