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
A:=matrix⁡3,3,1,2,3,3,1,2,2,3,1
B≔matrix3,3,a,b,c,c,a,b,b,c,a
v:=vector⁡3,5.05,6.125,2.980
A:=123312231
B:=abccabbca
v:=5.056.1252.980
LinearAlgebra
P≔Matrix1,2,3,3,1,2,2,3,1
Q≔Matrixa,b,c,c,a,b,b,c,a
w≔Vector5.05,6.125,2.980
w2:=Vector⁡8,fill=π+2
P≔123312231
Q≔abccabbca
w≔5.056.1252.980
w2≔π+2π+2π+2π+2π+2π+2π+2π+2
For small Vectors and Matrices, the <..> and <<..>> constructor shortcuts may be more convenient.
x|y|z,a|b|c
x,y,z
Extending a given Matrix or Vector
The linalg package has routines stackmatrix and augment (or concat) that can extend matrices or conjoin them.
m:=matrix⁡2,3,i,j→i+j
n≔matrix2,2,i,j→π
linalgaugmentm,n
m:=234345
n:=ππππ
234ππ345ππ
m:=matrix⁡3,2,i,j→i+j
n:=matrix⁡2,2,i,j→π
linalgstackmatrixm,n
m:=233445
233445ππππ
These operations can be done directly using either the Matrix and Vector constructors or their shortcuts.
m:=Matrix⁡2,3,i,j→i+j
n:=Matrix⁡2,2,fill=π
Matrixm,n
m|n
m≔Matrix3,2,i,j→i+j
n≔Matrix2,2,fill=π
m,n
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.
m:=matrix3,3,i,j→i−j:n:=matrix3,3,i,j→i−j2: r:=matrix3,3,0: linalgblockmatrix2,3,m,n,r,n,r,m
012014000101101000210410000014000012101000101410000210
interfacertablesize=20:
M:=Matrix3,3,i,j→i−j:N:=Matrix3,3,i,j→i−j2: R:=Matrix3,3,0: MatrixM,N,R,N,R,M
The Matrix constructor reproduces the functionality of the linalg routine extend.
m:=matrix⁡3,3,i,j→i−j
linalgextendm,1,4,π
M:=Matrix⁡3,3,i,j→i−j
Matrix⁡4,7,M,fill=π
m:=0−1−210−1210
0−1−2ππππ10−1ππππ210πππππππππππ
The functionality of the linalg routine copyinto can also be accomplished with Matrices.
m:=matrix6,6,i,j→i−j:n:=matrix2,2,i,j→π:linalgcopyinton,m,3,3
0−1−2−3−4−510−1−2−3−421ππ−2−332ππ−1−243210−1543210
M:=Matrix6,6,i,j→i−j:N:=Matrix2,2,fill=π:NewM:=copy⁡M: NewM3..5,3..5≔N:NewM
Specially structured matrices and vectors
array1..3,1..3,identity
array1..5,seq⁡0,i=1..5
array1..3,1..3,diagonal
linalgband−1,2,−3,6
linalghilbert3
linalgvandermonde1,2,3,4
100010001
00000
`?`1,1000`?`2,2000`?`3,3
2−30000−12−30000−12−30000−12−30000−12−30000−12
11213121314131415
1111124813927141664
LinearAlgebra The naming convention for LinearAlgebra routines is to spell the operation name completely, beginning each word with an uppercase letter.
LinearAlgebraIdentityMatrix3
LinearAlgebraZeroVector5
LinearAlgebraDiagonalMatrixa,b,c;LinearAlgebraBandMatrix−1,2,−3,1,6
LinearAlgebraHilbertMatrix3;LinearAlgebraVandermondeMatrix1,2,3,4
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.
e2≔LinearAlgebraUnitVector17,3000
e217
e2229
1
0
Here is a 100x100 band Matrix.
N≔LinearAlgebraBandMatrix−1,2,−3,1,100
N47,47
N47,48
N47,49
2
−3
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
evalm⁡A+B
evalm⁡A−B
evalmA &* B
evalmA &* v
evalm⁡A⋅2.7
1+a2+b3+c3+c1+a2+b2+b3+c1+a
1−a2−b3−c3−c1−a2−b2−b3−c1−a
a+2⁢c+3⁢bb+2⁢a+3⁢cc+2⁢b+3⁢ac+2⁢b+3⁢aa+2⁢c+3⁢bb+2⁢a+3⁢cb+2⁢a+3⁢cc+2⁢b+3⁢aa+2⁢c+3⁢b
26.24027.23531.455
2.75.48.18.12.75.45.48.12.7
For maximal simplicity, you can use +, -, ., and ^ for binary operations.
P + Q
P . 9 − Q . 18
P . Q
P . w
P . 2.7
Q2
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.
LinearAlgebra:-MatrixAddQ, P
LinearAlgebra:-MatrixAddQ, P,9,−18
LinearAlgebra:-MatrixMatrixMultiplyQ, P
LinearAlgebra:-MatrixVectorMultiply⁡P, w
LinearAlgebra:-ScalarMultiplyP, 2.7
Unary operations and matrix properties
linalgtransposeA
linalginverseA
linalgcharpolyA,λ
linalgeigenvaluesA
linalgeigenvectorsA
linalgdetA
linalgtraceA
linalgnullspacematrix2,2,a,b,a,b
132213321
−518718118118−518718718118−518
λ3−3⁢λ2−15⁢λ−18
6,−32+12⁢I⁢3,−32−12⁢I⁢3
−32+12⁢I⁢3,1,−12+12⁢I⁢31−12−12⁢I⁢3,−32−12⁢I⁢3,1,−12−12⁢I⁢31−12+12⁢I⁢3,6,1,111
18
3
−ba1
Note that linalgkernel computes the same result as linalgnullspace.
linalgnormx,y,z,2
linalgnormx,y,z
linalgnormalizex,y,z
linalganglex,y,z,a,b,c
x2+y2+z2
max⁡x,y,z
xx2+y2+z2yx2+y2+z2zx2+y2+z2
arccos⁡x⁢a+y⁢b+z⁢cx2+y2+z2⁢a2+b2+c2
These operations and others are also available through the context menus. To use them, click a matrix and select the desired operation from the options that appear in the Context Panel.
LinearAlgebra:-TransposeP
LinearAlgebra:-MatrixInverse⁡P
LinearAlgebra:-CharacteristicPolynomialP,λ
LinearAlgebra:-Eigenvalues⁡P
LinearAlgebra:-EigenvectorsP
LinearAlgebra:-Determinant⁡P
LinearAlgebra:-TraceP
LinearAlgebra:-NullSpace⁡a|b,a|b
LinearAlgebra:-Norma,b,c,2
LinearAlgebra:-Norm⁡a,b,c
LinearAlgebra:-Normalize⁡a,b,c,2
LinearAlgebra:-VectorAngle⁡a,b,c,d,e,f
−18+λ3−3⁢λ2−15⁢λ
a2+b2+c2
max⁡a,b,c
arccos⁡a&conjugate0;⁢d+b&conjugate0;⁢e+c&conjugate0;⁢fa2+b2+c2⁢d2+e2+f2
Row and Column Operations
evalm⁡B
linalgcoldimB
linalgrowdimB
linalgvectdimv
linalgcolB,2
linalgrowB,3
linalgdelcolsB,2..2
linalgswaprowB,1,3
linalgswapcolB,1,3
linalgmulrowB,1,s
linalgmulcolB,1,s
abccabbca
bac
bca
accbba
bcacababc
cbabacacb
s⁢as⁢bs⁢ccabbca
s⁢abcs⁢cabs⁢bca
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.
useLinearAlgebrain ColumnDimensionP; RowDimensionP; DimensionP; Dimensionw; ColumnP,2; RowP,2; DeleteColumnP,2..2 end use
3,3
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.
useLinearAlgebrain RowOperationQ,1,s; RowOperationQ,1,3; RowOperationQ,1,3,−7,inplace=true end use
Another simple syntax is available to manipulate rows, columns, or subblocks of Matrices and Vectors.
M:=Matrix⁡3,3,1,2,3,−4,−5,−6,7,8,9
M1,1..3≔M2,1..3
M
M3,1..−1≔2 M2,1..3
M;
M2..3,2..3≔Matrix2,2,π,π,sin⁡x,cos⁡y
Solving Systems of Linear Equations
eqns≔linalggeneqnsA,x,y,z
eqns2≔linalggeneqnsA,x,y,z,v
eqns:=x+2⁢y+3⁢z=0,2⁢x+3⁢y+z=0,3⁢x+y+2⁢z=0
eqns2:=x+2⁢y+3⁢z=5.05,2⁢x+3⁢y+z=2.980,3⁢x+y+2⁢z=6.125
linalggenmatrixeqns2,x,y,z
linalggenmatrixeqns2,x,y,z,flag
123231312
1235.052312.9803126.125
linalglinsolveA,v
linalglinsolveA,B
1.144722223−0.26194444511.476388889
118⁢b+718⁢c−518⁢a718⁢a−518⁢b+118⁢c−518⁢c+718⁢b+118⁢a−518⁢c+718⁢b+118⁢a118⁢b+718⁢c−518⁢a718⁢a−518⁢b+118⁢c718⁢a−518⁢b+118⁢c−518⁢c+718⁢b+118⁢a118⁢b+718⁢c−518⁢a
eqns3:=LinearAlgebra:-GenerateEquations⁡P,x,y,z
eqns4:=LinearAlgebra:-GenerateEquations⁡P,x,y,z,w
eqns3:=x+2⁢y+3⁢z=0,3⁢x+y+2⁢z=0,2⁢x+3⁢y+z=0
eqns4:=x+2⁢y+3⁢z=5.05,3⁢x+y+2⁢z=6.125,2⁢x+3⁢y+z=2.980
LinearAlgebra:-GenerateMatrix⁡eqns4,x,y,z
LinearAlgebra:-GenerateMatrix⁡eqns4,x,y,z,augmented
LinearAlgebra:-LinearSolve⁡P,w
LinearAlgebra:-LinearSolve⁡P,Q
Matrix Reductions and Decompositions
Av≔linalgaugmentA,linalgvector1,2,1
Av:=123131222311
linalggausselimAv
linalgffgausselimAv
linalggaussjordAv
linalgfrobeniusAv
linalgjordanA
12310−5−7−100−185−45
12310−5−7−100184
10059010−1900129
00181015013
6000−32−12⁢I⁢3000−32+12⁢I⁢3
ATA:=evalmlinalgtransposeA &* A
ATA:=141111111411111114
linalgdefiniteATA,positive_def
linalgcholeskyATA
true
14001114⁢14514⁢4201114⁢141170⁢4265⁢3
linalgLUdecompA
linalgQRdecompA
1230−5−700−185
141114⁢141114⁢140514⁢421170⁢420065⁢3
useLinearAlgebrainPv:=P|1,2,1;GaussianElimination⁡Pv;LUDecomposition⁡Pv,method=FractionFree;ReducedRowEchelonForm⁡Pv;FrobeniusForm⁡P;JordanForm⁡P;HessenbergForm⁡Pend use
useLinearAlgebrain PTP≔TransposeP . P; IsDefinitePTP; LUDecompositionPTP,method=Cholesky; P1,P2≔QRDecompositionP end use
Vector Calculus
restart
v1:=x,y,z:v2≔a,b,c:
linalgdotprodv1,v2
linalginnerprodv1,v2
linalgcrossprodv1,v2
x⁢a&conjugate0;+y⁢b&conjugate0;+z⁢c&conjugate0;
x⁢a+y⁢b+z⁢c
y⁢c−z⁢bz⁢a−x⁢cx⁢b−y⁢a
f:=x2+x⁢z−y2⁢z+sint:
linalggradf,x,y,z
linalglaplacianf,x,y,z
linalglaplaciangr,θ,φ,r,θ,φ,coords=cylindrical
linalghessianf,x,y,z
2⁢x+z−2⁢y⁢zx−y2
2−2⁢z
∂∂r⁢g⁡r,θ,φ+r⁢∂2∂r2⁢g⁡r,θ,φ+∂2∂θ2⁢g⁡r,θ,φr+r⁢∂2∂φ2⁢g⁡r,θ,φr
2010−2⁢z−2⁢y1−2⁢y0
F:=x2,x⁢z,y2⁢z:
linalgcurlF,x,y,z
linalgdivergeF,x,y,z
linalgjacobianF,x,y
2⁢y⁢z−x0z
2⁢x+y2
2⁢x0z002⁢y⁢z
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 ex=1,0,0,ey=0,1,0, and ez=0,0,1. Set the coordinate system to work in using the SetCoordinates routine.
v1:=x,y,z:v2:=a,b,c:
useVectorCalculus,LinearAlgebrainSetCoordinatescartesianx,y,z;DotProductv1,v2;BilinearFormv1,v2;end use
cartesianx,y,z
x&conjugate0;⁢a+y&conjugate0;⁢b+z&conjugate0;⁢c
useVectorCalculusin SetCoordinates'cartesian'; Gradientf,x,y,z; Laplacianf,x,y,z; Laplacianur,θ,φ,cylindricalr,θ,φ; Hessianf,x,y,zend use
cartesian
∂∂ru⁡r,θ,φ+r⁢∂2∂r2u⁡r,θ,φ+∂2∂θ2u⁡r,θ,φr+r⁢∂2∂φ2u⁡r,θ,φr
000000000
Making a function out of a vector derivative, in this case, the Hessian matrix computed immediately above.
H:=unapply⁡,x,y,z:
H⁡1,1,s+t
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.
useVectorCalculusinF:=VectorFieldx2,x⁢z,y2⁢z,cartesianx,y,z:evalVFF,3,3,3;DivergenceF;CurlF;JacobianF,x,y,zend use
9927
y2+2⁢x
2⁢x00z0x02⁢y⁢zy2
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.
m:=matrix2,2,1,2,3,5:
withlinalg:
adjointm
inversem
5−2−31
−523−1
M:=Matrix2,2,1,2,3,5:
withLinearAlgebra:
AdjointM
MatrixInverseM
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.
linalgadjointm
LinearAlgebraAdjointM
Since the LinearAlgebra package is a module, it also offers another long-form syntax for calling a routine directly.
LinearAlgebra:-Adjoint⁡M
Basic arithmetic involving matrix and vector objects, as used by linalg, can be accomplished through use of evalm.
evalm⁡m+m &* m
8142136
Such arithmetic is available for Matrix and Vector objects, as used by LinearAlgebra, directly.
M+M.M
Use of evalm when performing operations with Matrix and Vector objects is not advised; it introduces unnecessary inefficiency to the operations.
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.
v≔'v': u≔'u':
v≔u
u:=vector2,1,2
v:=u
u:=12
v
u
evalu
evalv
12
v1≔7
u2≔13
eval⁡v
v1:=7
u2:=13
713
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.
V:='V':U≔'U':
V≔U
V:=U
U≔Vector2,1,2
V
U
V1≔7
U2≔13
V1:=7
U2:=13
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.
r≔'r':q≔'q':
m≔matrix2,2,1,2,3,5;
r≔linalgQRdecompm,'Q'='q';
eval⁡q
m:=1235
r:=101710⁢100110⁢10
110⁢10310⁢10310⁢10−110⁢10
The routines in the LinearAlgebra package use multiple-assignment instead of side-effects in order to assign auxiliary results.
R:='R':Q≔'Q':
M≔Matrix2,2,1,2,3,5;
R,Q≔LinearAlgebraQRDecompositionM,'output'='R','Q'
See Also
Last Name Evaluation, linalg, LinearAlgebra, Matrix, module, multiple assignment, rtable, rtable_eval, Vector, VectorCalculus
Return to Index for Example Worksheets
Download Help Document