tables - Maple Help

table

create a table

 Calling Sequence table(F, L) table(t) table[weak](t)

Parameters

 F - (optional) indexing function L - (optional) list or set of initial table entries t - (optional) table or array

Description

 • A table is created either explicitly by a call to the table function or implicitly by assigning to an indexed name. Unlike arrays, where indices must be integers, the indices (or keys) of a table can be any value.
 • The table function creates a table with initial values specified by L. If L is a list or set of equations then the left-hand side is taken to be the table index (key), and the right-hand side the entry (value). Otherwise, L is taken to be a list of entries with indices 1, 2, ... . The use of a set of initial values in the latter case is ambiguous because there is no ordering of set elements, and hence the ordering of the entries in the result may not correspond to the ordering in which the entries are given. If L is not specified, then an empty table is created.
 Tables created with a list initializer, for example, table([a=A, b=B, c=C]), are created such that the assignment of the table entries proceeds from left to right.  That is, the first element of the list is inserted into the table first, the second is inserted second, and so on.
 • New entries can be added to a table using the subscript notation. Thus, T := table([4]) is equivalent to:
 T := table() and T[1] := 4,
 or equivalently:
 T[1] := 4 if T was unassigned (implicit creation).
 Entries can be removed from a table by assigning a table entry to its own name using evaln.  Thus, T[1] := evaln(T[1]) removes the entry 4 from T.
 • The indexing function F can be a procedure or a name specifying how indexing is to be performed; if null then ordinary indexing is implied. The built-in indexing functions are: symmetric, antisymmetric, sparse, diagonal, identity, exception, readonly and case_insensitive. For more information on indexing functions, see indexfcn.
 • Tables have special evaluation rules (like procedures) so that if the name T has been assigned a table then T evaluates to T. The call op(T) yields the actual table structure; and op(op(T)) yields the components of the table, which are the indexing function (if there is one) and a list of equations for the tables values.
 • The indices function can be used to obtain a sequence of a table's indices and likewise, the entries function returns a sequence of the table's entries.
 • When the table constructor is called with a single argument, t, whose value is an existing table, a copy of t is returned. If t is an array, the resulting copy will be a table object. Since tables are mutable objects, and assignment in Maple is by-reference, making a copy of a table to work with is useful when you do not want to modify the original table.
 • For information about weak tables and the table[weak] calling sequence, see weaktable.

 • The table command is thread-safe as of Maple 15.

Examples

Create an empty table:

 > $\mathrm{table}\left(\right)$
 ${table}{}\left(\left[\right]\right)$ (1)

Create a table with two entries 1=22 and 2=42:

 > $\mathrm{table}\left(\left[22,42\right]\right)$
 ${table}{}\left(\left[{1}{=}{22}{,}{2}{=}{42}\right]\right)$ (2)

Create a table with two entries at specified indices:

 > $S≔\mathrm{table}\left(\left[2=45,4=61\right]\right)$
 ${S}{≔}{table}{}\left(\left[{2}{=}{45}{,}{4}{=}{61}\right]\right)$ (3)

Fetch a value from a table using square-bracket notation:

 > $S\left[2\right]$
 ${45}$ (4)

When an index doesn't map to a value, the query will return unevaluated:

 > $S\left[1\right]$
 ${{S}}_{{1}}$ (5)

Use the assigned function to check if an entry exists

 > $\mathrm{assigned}\left(S\left[1\right]\right)$
 ${\mathrm{false}}$ (6)
 > $\mathrm{assigned}\left(S\left[2\right]\right)$
 ${\mathrm{true}}$ (7)

The sparse indexing function can be used to set a default for lookups rather than returning unevaluated

 > $T≔\mathrm{table}\left(\mathrm{sparse},\left[2=22,3=33\right]\right):$
 > $T\left[1\right]$
 ${0}$ (8)

The default for sparse tables is to return 0 when an index is not found. This can be changed to a constant value that you choose:

 > $T≔\mathrm{table}\left(\mathrm{sparse}="not found",\left[2=22,3=33\right]\right):$
 > $T\left[1\right]$
 ${"not found"}$ (9)
 > $T≔\mathrm{table}\left(\mathrm{sparse}=\mathrm{\infty },\left[2=22,3=33\right]\right):$
 > $T\left[1\right]$
 ${\mathrm{\infty }}$ (10)

Other indexing functions control the lookup based on indices and/or entries in the table:

 > $T≔\mathrm{table}\left(\mathrm{symmetric},\left[\left(c,b\right)=x\right]\right)$
 ${T}{≔}{table}{}\left({\mathrm{symmetric}}{,}\left[\left({b}{,}{c}\right){=}{x}\right]\right)$ (11)
 > $T\left[c,b\right]$
 ${x}$ (12)
 > $T\left[b,c\right]$
 ${x}$ (13)
 > $\left[\mathrm{entries}\left(T,'\mathrm{pairs}'\right)\right]$
 $\left[\left({b}{,}{c}\right){=}{x}\right]$ (14)

A table can contain callable procedures:

 > $\mathrm{DiffTab}≔\mathrm{table}\left(\left[\mathrm{sin}=\mathrm{cos},\mathrm{cos}=-\mathrm{sin}\right]\right):$
 > $\mathrm{DiffTab}\left[\mathrm{cos}\right]\left(x\right)$
 ${-}{\mathrm{sin}}{}\left({x}\right)$ (15)
 > $\mathrm{DiffTab}\left[\mathrm{cos}\right]\left(\frac{\mathrm{\pi }}{2}\right)$
 ${-1}$ (16)