 Matrix - Maple Programming Help

Matrix

construct a Matrix

 Calling Sequence Matrix(r, c, init, ro, sym, sc, sh, st, o, dt, f, a)

Parameters

 r - (optional) non-negative integer or integer range with left-hand endpoint 1; row dimension of Matrix c - (optional) non-negative integer or integer range with left-hand endpoint 1; column dimension of Matrix init - (optional) Maple procedure, table, array, list, Array, Matrix, Vector, set of equations, or expression of type algebraic; initial values for the Matrix ro - (optional) equation of the form readonly=true or false; specify whether Matrix entries can be changed sym - (optional) equation of the form symbol=name; specifies the symbolic name to be used for the Matrix entries sc - (optional) equation of the form scan=name or scan=list specifying the structure and/or data order for interpreting initial values; interpreter for initial values in parameter init sh - (optional) equation of the form shape=name or shape=list specifying one or more built-in or user-defined indexing functions; storage allocation for Matrix entries st - (optional) equation of the form storage=name, where name is a permitted storage mode; storage requirements for Matrix entries o - (optional) equation of the form order=name, where name is either C_order or Fortran_order; specifies if Matrix entries are stored by rows or columns dt - (optional) equation of the form datatype=name, where name is any Maple type, float, float, double, complex, or integer[n] for n=1,2,4, or 8; type of data stored in Matrix f - (optional) equation of the form fill=value, where value is of the type specified by the dt parameter; specifies Matrix entries at locations not otherwise set a - (optional) equation of the form attributes=list, where list specifies permitted attributes; specifies additional mathematical properties of the Matrix

Description

 • The Matrix(..) function is the constructor for the Matrix data structure. It is one of the principal data structures on which the LinearAlgebra routines operate.
 • Only small Matrices are displayed inline in Maple. By default, a small Matrix is defined as one whose dimensions are in the range 1..25 (Command-line version of Maple)  or the range 1..10 (worksheet version of Maple). Any Matrix whose dimension(s) are larger than this size is displayed by using a placeholder. For information on how to view the placeholder, see structuredview.
 You can display larger Matrices inline by using the interface(rtablesize=value) command, where value is an integer that represents the upper bound of the dimension range.
 To change the environment so that Maple always displays rtables inline, include the interface(rtablesize=infinity) command in a Maple initialization file (see maple).
 • All parameters are optional. However, enough information must be provided in the calling sequence for the mathematical shape of the Matrix and the storage requirements for its entries to be determined. In particular, if no parameters are specified, the result is a 0 x 0 Matrix.
 • The Matrix(r) function constructs an r x r Matrix whose entries are determined by the fill value in parameter f (default = 0). If the row dimension is not provided, it defaults to zero.
 The Matrix(r,c) function constructs an r x c Matrix whose entries are determined by the fill value in parameter f (default = 0). If the column dimension is not provided, it defaults to the row dimension. The column dimension cannot be specified without specifying the row dimension.
 • The Matrix(init) function constructs a Matrix whose shape and entries are determined by parameter init.
 The Matrix(r,c,init) function constructs an r x c Matrix whose initial entries are determined by parameter init (and parameter f if all of the entries in the Matrix are not set by init). If the initial entries of the Matrix are not provided, all of the entry values default to the fill value (default = 0).
 Parameter init can take any one of the following forms. (Dimensions must be provided in the calling sequence if init is one of the first four forms (procedure, expression, table, or set).)
 procedure
 The procedure must accept pairs of positive integer arguments (i.e., Matrix indices) and return the value for the corresponding entry in the Matrix.
 expression of type algebraic
 The expression is evaluated as a function (or procedure) which takes a pair of positive integers as arguments (i.e., Matrix indices) and returns the value of the corresponding entry of the Matrix.
 table
 The table is referenced by indices of the form [i, j] where i and j are positive integers (i.e., Matrix indices). (The table may include entries for indices of other forms.  These will be ignored.)
 set of equations
 The set of equations must take the form (i, j) =  where i and  j are positive integers (i.e., Matrix indices). It is an error to specify entries for locations that are outside the index range of the resulting Matrix.
 array or Array
 This must be a 2-D table-based array or rtable-based Array. The indexing is shifted to be 1-based. It is an error to specify entries for locations that are outside the index range of the resulting Matrix.
 Matrix
 The Matrix constructor accepts another Matrix as an initializer for the entries.  It is an error to specify entries for locations that are outside the index range of the resulting Matrix.
 list or list of lists
 The case when init is a list of scalars (specifically, of objects which are of type algebraic and not of type indexable), at least one dimension is explicitly given and the only other options given are the readonly, datatype and/or order options is treated specially. In this case, the dimensions of the resulting Matrix are determined as described above, and then the list init is partitioned accordingly.  The resulting list of lists is then used to initialize the Matrix, as described below.  Note that if the row dimension is r and the column dimension is c and $\mathrm{nops}\left(\mathrm{init}\right), then init will be padded with zeroes as needed.
 Otherwise, if init is a list whose elements are not themselves lists, it is interpreted as [init].
 If init is a list of lists, the first row of the Matrix is constructed from init, the second row from init, and so on, unless a scan parameter is provided. It is an error to specify entries for locations that are outside the index range of the resulting Matrix.
 The sublists in init must satisfy the following Conformance Rule: The components of the sublists of the init parameter  must define a complete partition of the Matrix being constructed.
 Vector
 If the sh parameter is included in the calling sequence and set to shape = diagonal or shape = [diagonal], then the Vector is used to initialize the main diagonal of the resulting Matrix, regardless of the Vector's orientation. Otherwise, the elements of the Vector are placed in the first column or row of the resulting Matrix, according to the orientation of the Vector.
 • If the ro option is included in the calling sequence as either readonly or readonly=true, then the resulting Matrix is "readonly" (i.e., its entries cannot be changed after construction).
 • The sym option, in the form symbol=name, provides a symbolic name to use for the Matrix entries if no initializer is provided.  This parameter is ignored if an initializer is given.
 • Parameters may be included more than once.  If more than 2 integers are included in the calling sequence, the first is r, the second is c, and the last is init (algebraic expression case). Otherwise, if a parameter is repeated, the last occurrence is the effective one.
 • Parameters may be given in any order (except that the interpretation of integer parameters is always that the first is r and the second is c).
 • The remaining parameters in the calling sequence have been provided for achieving maximal efficiency in computations with Matrices.
 scan=name or scan=list
 Specifies the structure and/or the data order for interpreting the initial values of a Matrix when init is a list. If the scan parameter is not provided in the calling sequence and init is a list, scan defaults to [rectangular,rows].  In the case that init is not a list, the scan parameter is ignored. For more information regarding scan methods in Matrices, see scan.
 shape=name or shape=list
 Specifies one or more built-in or user-defined indexing functions that determine the nonzero structure of the Matrix. If more than one indexing function is included, they must be entered as a list. The default shape is [] (none). For Matrices, the built-in indexing functions are:

 storage=name
 Specifies how physical memory is allocated for storing the entries of the Matrix.   The default storage is rectangular.
 order=name
 Specifies the internal ordering of the Matrix structure. Permissible values are C_order (stored by rows) and Fortran_order (stored by columns). The default order is Fortran_order.
 datatype=name
 Specifies the type of data stored in the Matrix. It can be one of integer[n], float[n], double, complex[n], or any Maple type specification.
 For integer[n], n can be 1, 2, 4, or 8, indicating the number of bytes per integer. For float[n], n can be 4 or 8, again indicating the number of bytes per floating-point number. The double datatype is the same as the float datatype. For complex[n], the only allowable value of n is 8, indicating the number of bytes allocated for each of the real and imaginary parts of the values stored in the Matrix. The default datatype is the general Maple type anything.
 fill=value
 Specifies the value for unspecified Matrix elements. This value must meet the same evaluation requirements as the initializer values. The default fill value is 0.
 Note:  The fill value is only used to fill those elements of the Matrix that are unspecified in spite of the indexing function. For instance, the fill value is not used to fill the lower triangle of an upper triangular Matrix. In the case of a sparse Matrix, the fill value is always ignored.
 attributes=list
 Specifies the attributes with which the Matrix is created. This option can be used to attach mathematical properties to the Matrix. Anything that is a valid argument to setattribute can be an attribute of a Matrix.
 This information can be used by various routines, for example, when choosing between algorithms.  In particular, the following LinearAlgebra package routines efficiently choose an appropriate algorithm if attributes=[positive_definite] is included in the calling sequence to construct the Matrices used in the routines.

 The default is to have no attributes.
 • After creation, it is possible to change the entries and some of the mathematical properties of the Matrix.  See Matrix assignment and Matrix options for more information.
 • For additional information on accessing elements or groups of elements of a Matrix, see Matrix indexing.
 • If expr is a Matrix expression, the $\mathrm{nops}\left(\mathrm{expr}\right)$ function returns 3.
 If expr is a Matrix expression, the $\mathrm{op}\left(n,\mathrm{expr}\right)$ function returns the following operands for the specified values of n.

 n returned operand 0 Matrix 1 dimensions as an expression sequence of two integers 2 Matrix elements as a set of equations of the form (indices)=value  * 3 Matrix options as an expression sequence of equations of the form option=value  **

 * Only the elements that explicitly need to be specified, taking into account storage, are returned.
 ** The subtype option is not returned since it is implicitly Matrix. The user and built-in indexing functions are returned as options.  The scan and fill options are not returned.
 • Matrices do not have last name evaluation.

Examples

A 2 x 2 Matrix.

 > $\mathrm{Matrix}\left(2\right)$
 $\left[\begin{array}{cc}{0}& {0}\\ {0}& {0}\end{array}\right]$ (1)

A 3 x 3 identity Matrix.

 > $\mathrm{Matrix}\left(3,\mathrm{shape}=\mathrm{identity}\right)$
 $\left[\begin{array}{ccc}{1}& {0}& {0}\\ {0}& {1}& {0}\\ {0}& {0}& {1}\end{array}\right]$ (2)

A 2 x 3 Matrix.

 > $\mathrm{Matrix}\left(2,3\right)$
 $\left[\begin{array}{ccc}{0}& {0}& {0}\\ {0}& {0}& {0}\end{array}\right]$ (3)

A 2 x 3 Matrix with all entries set to 5.

 > $\mathrm{m1}≔\mathrm{Matrix}\left(1..2,1..3,5\right)$
 ${\mathrm{m1}}{≔}\left[\begin{array}{ccc}{5}& {5}& {5}\\ {5}& {5}& {5}\end{array}\right]$ (4)

When the initial values are given as list of lists, the first row of the Matrix is constructed by the first list, the second row by the second list, and so on.

 > $\mathrm{m2}≔\mathrm{Matrix}\left(\left[\left[1,2,3\right],\left[4,5,6\right]\right]\right)$
 ${\mathrm{m2}}{≔}\left[\begin{array}{ccc}{1}& {2}& {3}\\ {4}& {5}& {6}\end{array}\right]$ (5)

Matrices with matching row and column dimensions can be added.

 > $\mathrm{Matrix}\left(\mathrm{m1}+\mathrm{m2}\right)$
 $\left[\begin{array}{ccc}{6}& {7}& {8}\\ {9}& {10}& {11}\end{array}\right]$ (6)

Matrices with same number of columns can be stacked.

 > $\mathrm{Matrix}\left(\left[\left[\mathrm{m1}\right],\left[\mathrm{m2}\right]\right]\right)$
 $\left[\begin{array}{ccc}{5}& {5}& {5}\\ {5}& {5}& {5}\\ {1}& {2}& {3}\\ {4}& {5}& {6}\end{array}\right]$ (7)

In the following calling sequence, the list specifies entries of the 3 x 2 Matrix.

Note that specifying entries for locations that are outside the index range of the resulting Matrix will return an error.

 > $\mathrm{m3}≔\mathrm{Matrix}\left(3,2,\left[1,2,3,4,5\right]\right)$
 ${\mathrm{m3}}{≔}\left[\begin{array}{cc}{1}& {2}\\ {3}& {4}\\ {5}& {0}\end{array}\right]$ (8)

The option fill specifies entries for any otherwise unspecified Matrix elements.

 > $\mathrm{Matrix}\left(4,3,\mathrm{m3},\mathrm{fill}=9\right)$
 $\left[\begin{array}{ccc}{1}& {2}& {9}\\ {3}& {4}& {9}\\ {5}& {0}& {9}\\ {9}& {9}& {9}\end{array}\right]$ (9)

You can use functions to return values for corresponding entries in a Matrix.

 > $f≔\left(i,j\right)↦{x}^{i+j-1}:$
 > $\mathrm{Matrix}\left(2,f\right)$
 $\left[\begin{array}{cc}{x}& {{x}}^{{2}}\\ {{x}}^{{2}}& {{x}}^{{3}}\end{array}\right]$ (10)

Use set notation to specify entries at each location.

Method 1:

 > $s≔\left\{\left(1,1\right)=0,\left(1,2\right)=1\right\}:$
 > $\mathrm{Matrix}\left(1,2,s\right)$
 $\left[\begin{array}{cc}{0}& {1}\end{array}\right]$ (11)

Method 2:

 > $\mathrm{Matrix}\left(4,\left\{\left(1,2\right)=5,\left(1,3\right)=6,\left(1,4\right)=7\right\},\mathrm{fill}=1,\mathrm{shape}=\mathrm{symmetric}\right)$
 $\left[\begin{array}{cccc}{1}& {5}& {6}& {7}\\ {5}& {1}& {1}& {1}\\ {6}& {1}& {1}& {1}\\ {7}& {1}& {1}& {1}\end{array}\right]$ (12)

Create Matrix using symbolic name for the entries.

 > $\mathrm{Matrix}\left(2,3,\mathrm{symbol}=m\right)$
 $\left[\begin{array}{ccc}{{m}}_{{1}{,}{1}}& {{m}}_{{1}{,}{2}}& {{m}}_{{1}{,}{3}}\\ {{m}}_{{2}{,}{1}}& {{m}}_{{2}{,}{2}}& {{m}}_{{2}{,}{3}}\end{array}\right]$ (13)

It is an error to attempt to write to a read-only Matrix.

 > $A≔\mathrm{Matrix}\left(3,4,\left[\left[1,2,3\right],\left[4,5,6\right]\right],\mathrm{readonly}=\mathrm{true}\right)$
 ${A}{≔}\left[\begin{array}{cccc}{1}& {2}& {3}& {0}\\ {4}& {5}& {6}& {0}\\ {0}& {0}& {0}& {0}\end{array}\right]$ (14)
 > $A\left[1,1\right]≔\mathrm{Matrix}\left(\left[2\right]\right)$

A column Matrix is not a Vector, but it can be converted to one.

 > $\mathrm{v1}≔\mathrm{Matrix}\left(3,1,\left[1,2,3\right]\right)$
 ${\mathrm{v1}}{≔}\left[\begin{array}{c}{1}\\ {2}\\ {3}\end{array}\right]$ (15)
 > $\mathrm{whattype}\left(\mathrm{v1}\right)$
 ${\mathrm{Matrix}}$ (16)
 > $\mathrm{v1}≔\mathrm{convert}\left(\mathrm{v1},\mathrm{Vector}\right)$
 ${\mathrm{v1}}{≔}\left[\begin{array}{c}{1}\\ {2}\\ {3}\end{array}\right]$ (17)
 > $\mathrm{whattype}\left(\mathrm{v1}\right)$
 ${{\mathrm{Vector}}}_{{\mathrm{column}}}$ (18)

Compatibility

 • The datatype option was updated in Maple 2015.