select - Maple Help

select

selection from an expression

remove

removal from an expression

selectremove

selection or removal from an expression

 Calling Sequence select(fcn, expr, arg2, ..., argN) select[n](fcn, arg1, ..., argn-1, expr, argn+1, ..., argN) select[flatten](fcn, expr, arg2, ..., argN) select[inplace](fcn, expr, arg2, ..., argN) select[indices](fcn, expr, arg1, ..., argN) select[fold=(rfcn,zero)](fcn, expr, arg1, ..., argN) select[reduce=rfcn](fcn, expr, arg1, ..., argN) select[scan=sfcn](fcn, expr, arg1, ..., argN) remove(fcn, expr, arg2, ..., argN) remove[n](fcn, arg1, ..., argn-1, expr, argn+1, ..., argN) remove[flatten](fcn, expr, arg2, ..., argN) remove[inplace](fcn, expr, arg2, ..., argN) select[indices](fcn, expr, arg1, ..., argN) remove[fold=(rfcn,zero)](fcn, expr, arg1, ..., argN) remove[reduce=rfcn](fcn, expr, arg1, ..., argN) remove[scan=sfcn](fcn, expr, arg1, ..., argN) selectremove(fcn, expr, arg2, ..., argN) selectremove[n](fcn, arg1, ..., argn-1, expr, argn+1, ..., argN) selectremove[flatten](fcn, expr, arg2, ..., argN) selectremove[inplace](fcn, expr, arg2, ..., argN) selectremove[fold=(rfcn,zero)](fcn, expr, arg1, ..., argN) selectremove[reduce=rfcn](fcn, expr, arg1, ..., argN) selectremove[scan=rfcn](fcn, expr, arg1, ..., argN)

Parameters

 fcn - Boolean-valued procedure expr - any expression arg1, ..., argN - (optional) further arguments to fcn n - positive integer rfcn - a function to apply to the result of select or remove to reduce it to a single value zero - the starting value when computing the reduction

Description

 • The select function selects the operands of the expression expr which satisfy the Boolean-valued procedure fcn, and creates a new object of the same type as expr. Those operands for which fcn does not return true are discarded in the newly created object.
 The remove function does the opposite of select. It removes the operands of expr for which fcn returns true and creates a new object containing the remaining operands.
 The selectremove function behaves in the same manner as select(..) and remove(..), except that the result is computed efficiently, in a single pass over expression expr. The return value is a sequence of two objects of the same type as expr.
 • When expr is an ordered expression, such as a list or unevaluated function call, the operands of the output retain their relative order. If the expression expr is an expression of type ^ and the result consists only of an exponent with a null base, then the output is undefined.
 • A typical application of select and remove is to split the factors of a product, expr, into those which depend on a variable x and those which do not (that is, constants). This can be accomplished by using  v := select( has, expr, x ) followed by c := remove( has, expr, x ). It is more efficient, however, to use v, c := selectremove( has, expr, x ). Do not try to compute c := expr / v. Because of the way Maple simplifies expressions, factors do not cancel out in general. See the last example below.

Arguments and Options

 • Some options can be specified in square brackets as an index to the select, remove, or selectremove commands. These options can be used by themselves or in combination with other options. Any option described for select below can also be used with remove or selectremove unless otherwise noted.
 • select(fcn, expr, arg2, ..., argN) executes fcn(elem, arg2, ..., argN) for each operand or element of expr. The value returned by fcn must be true, false, or FAIL.
 • Instead of a Boolean-valued procedure, fcn can be specified as a list, Array, or Vector of Boolean- or integer-valued expressions. As each operand of expr is encountered, the corresponding entry of fcn is used to decide whether to keep (or remove) it:
 – A value of true or 1 indicates the element from expr is to be kept (for select) or dropped (for remove).
 – A value of false or 0 indicates the element from expr is to be dropped (for select) or kept (for remove).
 If fcn has too few entries to specify all the operands of expr, the missing entries are assumed to be false. If it has too many entries, the extra entries are ignored.
 • The form select[n](fcn, arg1, ..., argn-1, expr, argn+1, ..., argN), where n is a positive integer, passes the element of expr as the n-th argument of fcn. Thus select[1] is equivalent to select.
 • When expr is an Array, Vector, Matrix, or table, the select[flatten] form can be used. This causes the result to be flattened to a one-dimensional structure with all discarded elements removed (as opposed to having the discarded elements replaced with NULL).
 • When expr is an Array, Matrix, Vector, table, or an object implementing a mutable container, the select[inplace] form can be used. This causes the selection to be applied to the container in-place (for example, in the case of select[inplace], the non-selected entries in the container are removed from expr itself). When selectremove[inplace] is called, the first result is computed in-place, and the removed elements are returned in a new container that is returned as the second result.
 • The indices option applies only when selecting from an rtable, list, set, or function, and causes fcn to be passed the index of each entry instead of the value. Thus, the decision to keep or discard an entry can be made based on its index, rather than its value. Note that if an rtable has more than one dimension, multiple arguments are passed for the index, one per dimension.
 • The fold option specifies a function, rfcn, and an initial value, zero, used to reduce the result of select or remove to a single value instead of returning a modified copy of expr. The value is initialized to that specified by zero. Then, for each retained element of expr, rfcn is applied to the value and that element, and the value is updated.
 • The reduce option is similar to fold, except that the value is initialized to the first retained element of the result. Application of rfcn then begins with the value and the next element.
 • Both fold and reduce compute the single result without actually creating the modified copy of expr. Instead, the result value is updated as each individual is computed. This prevents the allocation of a potentially large amount of memory for an intermediate result that would be discarded.
 • An additonal option, evalhf, can be used with fold or reduce, in which case each call to rfcn is evaluated using evalhf, and the final result returned is a hardware float.
 Note that fold and reduce are equivalent when the result of selecting or removing elements of expr is not empty, and the specified zero is the natural zero (or identity) of the reduction function. For example, the natural zero of addition is 0, and of multiplication is 1.
 If the result of selecting or removing elements is empty, reduce will return undefined, whereas fold will return zero.
 • The fold or reduce options cannot be used in conjunction with the flatten or inplace options.
 • The scan option is related to reduce, except that instead reducing the result otherwise produced by select to a single value, each prefix of that result is reduced to a single result. It is equivalent to mapping map[reduce] over each prefix of the result produced by the call to select, without having to compute the intermediate prefix separately.
 • The fold, reduce, and scan options are mutually exclusive.

Objects Supporting select, remove, and selectremove

 • The following objects support the select, remove, and selectremove commands with a separate implementation. See the help pages linked below for more details.

 • Using the flatten and inplace options together is permitted, but is not thread safe.
 • The select, remove and selectremove commands are thread-safe as of Maple 15.

Examples

 > $\mathrm{integers}≔\left[\mathrm{seq}\left(10..20\right)\right]$
 ${\mathrm{integers}}{≔}\left[{10}{,}{11}{,}{12}{,}{13}{,}{14}{,}{15}{,}{16}{,}{17}{,}{18}{,}{19}{,}{20}\right]$ (1)
 > $\mathrm{select}\left(\mathrm{isprime},\mathrm{integers}\right)$
 $\left[{11}{,}{13}{,}{17}{,}{19}\right]$ (2)
 > $\mathrm{remove}\left(\mathrm{isprime},\mathrm{integers}\right)$
 $\left[{10}{,}{12}{,}{14}{,}{15}{,}{16}{,}{18}{,}{20}\right]$ (3)
 > $\mathrm{selectremove}\left(\mathrm{isprime},\mathrm{integers}\right)$
 $\left[{11}{,}{13}{,}{17}{,}{19}\right]{,}\left[{10}{,}{12}{,}{14}{,}{15}{,}{16}{,}{18}{,}{20}\right]$ (4)
 > $f≔2\mathrm{exp}\left(ax\right)\mathrm{sin}\left(x\right)\mathrm{ln}\left(y\right)$
 ${f}{≔}{2}{}{{ⅇ}}^{{a}{}{x}}{}{\mathrm{sin}}{}\left({x}\right){}{\mathrm{ln}}{}\left({y}\right)$ (5)
 > $\mathrm{select}\left(\mathrm{has},f,x\right)$
 ${{ⅇ}}^{{a}{}{x}}{}{\mathrm{sin}}{}\left({x}\right)$ (6)
 > $\mathrm{remove}\left(\mathrm{has},f,x\right)$
 ${2}{}{\mathrm{ln}}{}\left({y}\right)$ (7)
 > $\mathrm{selectremove}\left(\mathrm{has},f,x\right)$
 ${{ⅇ}}^{{a}{}{x}}{}{\mathrm{sin}}{}\left({x}\right){,}{2}{}{\mathrm{ln}}{}\left({y}\right)$ (8)
 > $f≔{y}^{x}$
 ${f}{≔}{{y}}^{{x}}$ (9)
 > $\mathrm{selectremove}\left(\mathrm{has},f,x\right)$
 ${\mathrm{undefined}}{,}{y}$ (10)
 > $f≔\mathrm{indets}\left(f\right)$
 ${f}{≔}\left\{{x}{,}{y}{,}{{y}}^{{x}}\right\}$ (11)
 > $\mathrm{select}\left(\mathrm{type},f,'\mathrm{name}'\right)$
 $\left\{{x}{,}{y}\right\}$ (12)
 > $\mathrm{remove}\left(\mathrm{type},f,'\mathrm{name}'\right)$
 $\left\{{{y}}^{{x}}\right\}$ (13)
 > $\mathrm{selectremove}\left(\mathrm{type},f,'\mathrm{name}'\right)$
 $\left\{{x}{,}{y}\right\}{,}\left\{{{y}}^{{x}}\right\}$ (14)
 > $f≔2\mathrm{ln}\left(x\right)\left(y+1\right)$
 ${f}{≔}{2}{}{\mathrm{ln}}{}\left({x}\right){}\left({y}{+}{1}\right)$ (15)
 > $c≔\mathrm{remove}\left(\mathrm{has},f,x\right)$
 ${c}{≔}{2}{}{y}{+}{2}$ (16)
 > $\frac{f}{c}$
 $\frac{{2}{}{\mathrm{ln}}{}\left({x}\right){}\left({y}{+}{1}\right)}{{2}{}{y}{+}{2}}$ (17)
 > $\mathrm{select}\left(\mathrm{has},f,x\right)$
 ${\mathrm{ln}}{}\left({x}\right)$ (18)
 > $\mathrm{remove}\left(x↦2
 $\left[\begin{array}{c}{1}\\ {2}\\ \left(\right)\end{array}\right]$ (19)
 > $\mathrm{remove}\left[\mathrm{flatten}\right]\left(x↦2
 $\left[\begin{array}{c}{1}\\ {2}\end{array}\right]$ (20)

The following example selects any entries of a matrix matching a certain criteria, showing NULL for any entries that do not match. To see an example of removing any rows or columns that do not match a given criteria, see the Working with Subsections of Data - Extracting Data section in the LA_syntax_shortcuts page.

 > $\mathrm{select}\left(x↦2
 $\left[\begin{array}{cc}\left(\right)& \left(\right)\\ {3}& {4}\end{array}\right]$ (21)
 > $\mathrm{select}\left[\mathrm{flatten}\right]\left(x↦2
 $\left[\begin{array}{cc}{3}& {4}\end{array}\right]$ (22)

The inplace option lets a selection decision to be made based on an entry's index.

 > $\mathrm{unassign}\left('a','b','c','d'\right)$
 > $\mathrm{select}\left[\mathrm{indices}\right]\left(\mathrm{type},\left[a,b,c,d\right],\mathrm{odd}\right)$
 $\left[{a}{,}{c}\right]$ (23)
 > $\mathrm{select}\left[\mathrm{indices}\right]\left(\left(\mathrm{row},\mathrm{col}\right)↦\mathrm{row}=\mathrm{col},⟨⟨a|b⟩,⟨c|d⟩⟩\right)$
 $\left[\begin{array}{cc}{a}& \left(\right)\\ \left(\right)& {d}\end{array}\right]$ (24)

A reduction or fold function is applied to the selected values.

 > $\mathrm{select}\left[\mathrm{reduce}=\mathrm{+}\right]\left(\mathrm{isprime},\mathrm{integers}\right)$
 ${60}$ (25)
 > $\mathrm{remove}\left[\mathrm{fold}=\left(\mathrm{*},1\right)\right]\left(\mathrm{isprime},\mathrm{integers}\right)$
 ${145152000}$ (26)

A selection function need not take the entry value as the first argument.

 > $\mathrm{between}≔\left(\mathrm{lo},x,\mathrm{hi}\right)↦\mathrm{lo}\le x\le \mathrm{hi}:$
 > $\mathrm{select}\left[2\right]\left(\mathrm{between},12,\mathrm{integers},17\right)$
 $\left[{12}{,}{13}{,}{14}{,}{15}{,}{16}{,}{17}\right]$ (27)
 > $\mathrm{selectremove}\left[2\right]\left(\mathrm{between},5,\mathrm{integers},15\right)$
 $\left[{10}{,}{11}{,}{12}{,}{13}{,}{14}{,}{15}\right]{,}\left[{16}{,}{17}{,}{18}{,}{19}{,}{20}\right]$ (28)
 > $\mathrm{selectremove}\left[2,\mathrm{reduce}=\mathrm{+}\right]\left(\mathrm{between},5,\mathrm{integers},15\right)$
 ${75}{,}{90}$ (29)

Using a list of Boolean values instead of a function to select operands is useful if the same selection is to be applied to multiple expressions.

 > $\mathrm{nums}≔\left[1,2,3,4,5,6,7,8,9,10\right]$
 ${\mathrm{nums}}{≔}\left[{1}{,}{2}{,}{3}{,}{4}{,}{5}{,}{6}{,}{7}{,}{8}{,}{9}{,}{10}\right]$ (30)
 > $\mathrm{names}≔\mathrm{map2}\left(\mathrm{nprintf},"a%03d",\mathrm{nums}\right)$
 ${\mathrm{names}}{≔}\left[{\mathrm{a001}}{,}{\mathrm{a002}}{,}{\mathrm{a003}}{,}{\mathrm{a004}}{,}{\mathrm{a005}}{,}{\mathrm{a006}}{,}{\mathrm{a007}}{,}{\mathrm{a008}}{,}{\mathrm{a009}}{,}{\mathrm{a010}}\right]$ (31)
 > $\mathrm{selector}≔\mathrm{map}\left(\mathrm{type},\mathrm{nums},\mathrm{odd}\right)$
 ${\mathrm{selector}}{≔}\left[{\mathrm{true}}{,}{\mathrm{false}}{,}{\mathrm{true}}{,}{\mathrm{false}}{,}{\mathrm{true}}{,}{\mathrm{false}}{,}{\mathrm{true}}{,}{\mathrm{false}}{,}{\mathrm{true}}{,}{\mathrm{false}}\right]$ (32)
 > $\mathrm{select}\left(\mathrm{selector},\mathrm{nums}\right)$
 $\left[{1}{,}{3}{,}{5}{,}{7}{,}{9}\right]$ (33)
 > $\mathrm{select}\left(\mathrm{selector},\mathrm{names}\right)$
 $\left[{\mathrm{a001}}{,}{\mathrm{a003}}{,}{\mathrm{a005}}{,}{\mathrm{a007}}{,}{\mathrm{a009}}\right]$ (34)

The scan option is equivalent to mapping map[reduce] over each prefix of the data produced by a simple call to select:

 > $L≔\mathrm{select}\left(\mathrm{type},\left[1,2,3,4,5,6,7,8,9,10\right],\mathrm{odd}\right)$
 ${L}{≔}\left[{1}{,}{3}{,}{5}{,}{7}{,}{9}\right]$ (35)
 > $\mathrm{prefixes}≔\left[\mathrm{seq}\left(L\left[1..n\right],n=1..\mathrm{numelems}\left(L\right)\right)\right]$
 ${\mathrm{prefixes}}{≔}\left[\left[{1}\right]{,}\left[{1}{,}{3}\right]{,}\left[{1}{,}{3}{,}{5}\right]{,}\left[{1}{,}{3}{,}{5}{,}{7}\right]{,}\left[{1}{,}{3}{,}{5}{,}{7}{,}{9}\right]\right]$ (36)
 > $\mathrm{map}\left(x↦\mathrm{map}\left[\mathrm{reduce}=\mathrm{+}\right]\left(y↦y,x\right),\mathrm{prefixes}\right)$
 $\left[{1}{,}{4}{,}{9}{,}{16}{,}{25}\right]$ (37)
 > $\mathrm{select}\left[\mathrm{scan}=\mathrm{+}\right]\left(\mathrm{type},\left[1,2,3,4,5,6,7,8,9,10\right],\mathrm{odd}\right)$
 $\left[{1}{,}{4}{,}{9}{,}{16}{,}{25}\right]$ (38)

Compatibility

 • The flatten option was introduced in Maple 17.