
About Indexing


•

Indexing refers to the act of putting an index (or subscript) on a variable assigned to an Array, Matrix, or Vector. For example, if M is a Matrix, then a simple indexing operation is M[1,2], which will extract the element in the first row and second column of M. This can also be acheived using a subscript: ${M}_{1,2}$. More complicated indexing operations involve selecting or assigning multiple entries.

•

Maple understands two distinct notations for indexing. Mathematical indexing is achieved via square brackets, M[index], and Programmer indexing is achieved via round brackets, M(index). Only rtable subtypes (Array, Matrix, Vector) understand programmer indexing. In other contexts this is understood as invoking a function call. The distinctions between mathematical and programmer indexing are outlined below.



Selecting Elements: Fully Specified Index


•

In the most basic case, N integers are supplied in the index for a given Ndimensional Array. Provided the Array's dimensions all begin at 1, both mathematical and programmer indexing will return the same single element.

>

M := Matrix(3,3,(i,j)>3*i+j3);

${M}{\u2254}\left[\begin{array}{ccc}{1}& {2}& {3}\\ {4}& {5}& {6}\\ {7}& {8}& {9}\end{array}\right]$
 (1) 
•

Unlike Matrices and Vectors, Arrays can have dimensions beginning with values other than 1. Indexing with square brackets respects the actual index, while using round brackets normalizes the dimensions to begin with 1.

>

A := Array(10..12,43..42,(i,j)>i*j):

•

When an Array has dimensions beginning at one, negative integer indices can be used to count backwards from the end of a dimension.

>

V := Vector([1,2,3,4]):

•

Because programmer indexing is always relative to 1, negative indices can be used for any Array. Mathematical indexing will raise an exception when it sees a negative index unless that value is actually within the Array's specified bounds.

>

A := Array(5..9,[5,6,7,8,9]):

•

Referencing an outofbounds index always raises an error.



Selecting Elements: Overspecified Index


•

With mathematical indexing, an exception is raised when more integers are specified in an index than there are dimensions in the given array. Programmer indexing allows any Array to be treated as anydimensional. Thus, a 2x2 Array can be indexed as if it was 2x2x1, which is conceptually the same. Additionally, row vectors can be treated as 1xN arrays.

>

V := Vector[row]([1,2,3,4]);

${V}{\u2254}\left[\begin{array}{cccc}{1}& {2}& {3}& {4}\end{array}\right]$
 (9) 
>

V := Vector[column]([1,2,3,4]);

${V}{\u2254}\left[\begin{array}{c}{1}\\ {2}\\ {3}\\ {4}\end{array}\right]$
 (11) 
${M}{\u2254}\left[\begin{array}{cc}{1}& {2}\\ {3}& {4}\end{array}\right]$
 (13) 


Selecting Elements: Underspecified Index


•

When an index contains fewer elements than there are dimensions in the array being indexed, squarebracket indexing returns the subarray implicitly specified with the full range of each missing dimension.


Programmer indexing instead takes a view of the array as if it contained the same number of dimensions as specified indices, where the last dimension is the size of the product of all remaining dimensions. Practically, this is the same as computing the Fortranorder offset into the array datablock, which is part of the reason for classifying roundbracket indexing as "programmer" indexing. Programmer indexing allows you to access each element in an array with a single integer.

>

A := Array([[1,2],[3,4]]);

${A}{\u2254}\left[\begin{array}{cc}{1}& {2}\\ {3}& {4}\end{array}\right]$
 (15) 
$\left[\begin{array}{cc}{1}& {2}\end{array}\right]$
 (17) 
>

for i from 1 to ArrayNumElems(A,'All') do
A(i) := 2*A(i);
end do:
A;

$\left[\begin{array}{cc}{2}& {4}\\ {6}& {8}\end{array}\right]$
 (18) 
•

The order of elements returned corresponds to the actual order in which they are stored in the underlying data structure. Therefore, underspecified indexing of C_order arrays will yield a different order than Fortran_order arrays.

>

A := Array([[1,2],[3,4]],order=Fortran_order);

${A}{\u2254}\left[\begin{array}{cc}{1}& {2}\\ {3}& {4}\end{array}\right]$
 (19) 
>

C := Array([[1,2],[3,4]],order=C_order);

${C}{\u2254}\left[\begin{array}{cc}{1}& {2}\\ {3}& {4}\end{array}\right]$
 (20) 
>

A(1), A(2), A(3), A(4);

${1}{,}{3}{,}{2}{,}{4}$
 (21) 
>

C(1), C(2), C(3), C(4);

${1}{,}{2}{,}{3}{,}{4}$
 (22) 
•

Sparse arrays, and arrays with other special storage and/or indexing functions, behave as if they were dense Fortran_order. In this way zeros can be fetched from sparse arrays, and indexing functions always get an equivalent fully specified index.

>

M := Matrix(10,10,storage=sparse):

${M}{}\left({2}\right){\u2254}\left[\begin{array}{cccccccccc}{0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}\\ {2}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}\\ {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}\\ {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}\\ {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}\\ {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}\\ {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}\\ {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}\\ {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}\\ {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}\end{array}\right]$
 (24) 
>

A := LinearAlgebra:IdentityMatrix(4);

${A}{\u2254}\left[\begin{array}{cccc}{1}& {0}& {0}& {0}\\ {0}& {1}& {0}& {0}\\ {0}& {0}& {1}& {0}\\ {0}& {0}& {0}& {1}\end{array}\right]$
 (26) 
•

When the empty index is specified, the entire array is returned.

>

A := Array([[1,2],[3,4]]):

$\left[\begin{array}{cc}{1}& {2}\\ {3}& {4}\end{array}\right]$
 (29) 
$\left[\begin{array}{cc}{1}& {2}\\ {3}& {4}\end{array}\right]$
 (30) 


Selecting Elements: Extracting Subblocks


•

Ranges and lists can be used in an index to extract subblocks. Within round brackets, arrays can be used in place of lists.

•

As with most positive integerindexed sequential data sets in Maple, negative numbers can be used to index relative to the end of an array. For a 1 dimensional array, $A$, the last element is ${A}_{\mathrm{1}}$. Similarly, the second last element is ${A}_{\mathrm{2}}$. Applying this to subblocks, ${A}_{1..\mathrm{1}}$ is the full range of the array. When referencing from the beginning, or to the end, the range endpoints can be omitted. So ${A}_{..}$ gives a copy of $A$, ${A}_{..n}$ extracts the first n elements of $A$, and ${A}_{n..}$ extracts the elements from n to the end of $A$.

•

The result of subselection $A\[\mathrm{i1},\mathrm{i2},...,\mathrm{iN}\]$ on an Ndimensional array will have 1 dimension per noninteger index. So, if i2 is a range, and i1, i3, .., iN are all integers, the resulting subarray will be 1dimensional. The width of that dimension will be the number of elements in the given list, or the number of elements in the span of the given range.

>

A := Array(1..2,1..2,1..2,fill=3):

${a}{\u2254}\left[\begin{array}{cc}{3}& {3}\end{array}\right]$
 (31) 
•

The result of subselection $A\left(\mathrm{i1}\,\mathrm{i2}\,\dots \,\mathrm{iN}\right)$ on an Ndimensional array is not the same as the indexing with square brackets. In this case, the number of dimensions of the result will correspond to the location of the last nonsingleton index. That is, if the ith index is the last noninteger index then the result will have i dimensions.

>

A := Array(1..2,1..2,1..2,fill=3):

${a}{\u2254}\left[\begin{array}{cc}{3}& {3}\end{array}\right]$
 (33) 
${1}{..}{1}{,}{1}{..}{2}$
 (34) 
•

The result is formed conceptually by iterating through all permutations of supplied indices and performing simple integer index extractions from the source array in order.

>

M := Matrix(3,3,(i,j)>3*i+j3);

${M}{\u2254}\left[\begin{array}{ccc}{1}& {2}& {3}\\ {4}& {5}& {6}\\ {7}& {8}& {9}\end{array}\right]$
 (35) 
$\left[\begin{array}{cc}{1}& {3}\\ {7}& {9}\end{array}\right]$
 (36) 
$\left[\begin{array}{cc}{9}& {7}\\ {3}& {1}\end{array}\right]$
 (37) 
•

The result of subselection $A\[\mathrm{i1},\mathrm{i2},...,\mathrm{iN}\]$ on an Mdimensional array will raise an exception if N > M. When N < M, the index is underspecified. The result will behave as if iN+1, .., iM were specified as the full range of the corresponding dimension.

>

M := Matrix(3,3,(i,j)>3*i+j3);

${M}{\u2254}\left[\begin{array}{ccc}{1}& {2}& {3}\\ {4}& {5}& {6}\\ {7}& {8}& {9}\end{array}\right]$
 (38) 
$\left[\begin{array}{ccc}{1}& {2}& {3}\\ {4}& {5}& {6}\end{array}\right]$
 (39) 
$\left[\begin{array}{ccc}{1}& {2}& {3}\\ {4}& {5}& {6}\end{array}\right]$
 (40) 
•

Using round brackets with an underspecified or overspecified index is not the same as using square brackets. The result of subselection $A\left(\mathrm{i1}\,\mathrm{i2}\,\dots \,\mathrm{iN}\right)$ on an Mdimensional array will raise an exception if N > M when iN+1 and higher are nonsingleton. When N < M, the result will behave as if the array being indexed only had N dimensions as in the case of a simple allinteger underspecified index.

>

M := Matrix(3,3,(i,j)>3*i+j3);

${M}{\u2254}\left[\begin{array}{ccc}{1}& {2}& {3}\\ {4}& {5}& {6}\\ {7}& {8}& {9}\end{array}\right]$
 (41) 
$\left[\begin{array}{cc}{1}& {4}\end{array}\right]$
 (42) 
$\left[\begin{array}{ccc}{1}& {2}& {3}\\ {4}& {5}& {6}\end{array}\right]$
 (45) 
$\left[\begin{array}{ccc}{1}& {2}& {3}\\ {4}& {5}& {6}\end{array}\right]$
 (47) 


Modifying Elements: Assigning Subblocks


•

Regions of an array can be specified inside an index on the left side of an assignment statement. Using the same rules for selection to denote which elements will be affected, the specified subblock will be updated with the value on the right side of the assignment statement. If the right side is an rtablebased Array, Matrix, or Vector the assignment will basically insert the elements of the value array into the subblock of the array being assigned to. If the right side is not an rtablebased Array, Matrix, or Vector, every element specified in the array being assigned to will be updated with the whole value.

${A}{\u2254}\left[\begin{array}{ccc}{0}& {0}& {0}\\ {0}& {0}& {0}\\ {0}& {0}& {0}\end{array}\right]$
 (48) 
${A}{}\left({1}{..}{2}\right){\u2254}\left[\begin{array}{ccc}{1}& {0}& {0}\\ {1}& {0}& {0}\\ {0}& {0}& {0}\end{array}\right]$
 (49) 
$\left[\begin{array}{ccc}{2}& {2}& {2}\\ {2}& {2}& {2}\\ {0}& {0}& {0}\end{array}\right]$
 (50) 
>

A[1..2,1..2] := Matrix(2,2,fill=3): A;

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

A([1,2],2..3) := Matrix(2,2,fill=4);

${A}{}\left(\left[{1}{\,}{2}\right]{\,}{2}{..}{3}\right){\u2254}\left[\begin{array}{ccc}{3}& {4}& {4}\\ {3}& {4}& {4}\\ {0}& {0}& {0}\end{array}\right]$
 (52) 
•

When the value array is smaller than the specified region to be assigned to, square bracket indexing fills in the missing elements with zeros. This zero expansion is not done with round bracket indexing.

>

A := Array(1..3,1..3,fill=1);

${A}{\u2254}\left[\begin{array}{ccc}{1}& {1}& {1}\\ {1}& {1}& {1}\\ {1}& {1}& {1}\end{array}\right]$
 (53) 
>

A[1..2,1..1] := Matrix(1,2,fill=2): A;

$\left[\begin{array}{ccc}{2}& {2}& {0}\\ {0}& {0}& {0}\\ {1}& {1}& {1}\end{array}\right]$
 (54) 
•

Normally the dimensions of the right and left side of the assignment must match. A special case exists with roundbracket indexing when the left side selection specifies only one dimension. In that case the right side of the assignment is flattened. More precisely, roundbracket indexing is used to extract the elements from the value array.

${A}{\u2254}\left[\begin{array}{ccc}{0}& {0}& {0}\\ {0}& {0}& {0}\\ {0}& {0}& {0}\end{array}\right]$
 (55) 
>

A(1..6) := < 1, 2, 3; 4, 5, 6 >;

${A}{}\left({1}{..}{6}\right){\u2254}\left[\begin{array}{ccc}{1}& {5}& {0}\\ {4}& {3}& {0}\\ {2}& {6}& {0}\end{array}\right]$
 (56) 
>

A(1..6) := < <1, 2, 3>  < 4, 5, 6> >;

${A}{}\left({1}{..}{6}\right){\u2254}\left[\begin{array}{ccc}{1}& {4}& {0}\\ {2}& {5}& {0}\\ {3}& {6}& {0}\end{array}\right]$
 (57) 


Modifying Elements: Resizing


•

Attempting to assign to an element outside the bounds of the given array will result in an outofbounds exception with squarebracket indexing. This provides protection from accidentally assigning to an element outside your initial boundaries. Using roundbrackets, assigning to an outofbounds element will cause the array to grow so that it can hold that element.

${V}{\u2254}\left[\begin{array}{c}{1}\\ {2}\\ {3}\end{array}\right]$
 (58) 
${V}{}\left({4}\right){\u2254}\left[\begin{array}{c}{1}\\ {2}\\ {3}\\ {4}\end{array}\right]$
 (59) 


