selection from an expression
removal from an expression
selection or removal from an expression
Arguments and Options
Objects Supporting select, remove, and selectremove
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)
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)
arg1, ..., argN
(optional) further arguments to fcn
a function to apply to the result of select or remove to reduce it to a single value
the starting value when computing the reduction
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.
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 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.
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.
For more information on thread safety, see index/threadsafe.
integers ≔ seq⁡10..20
f ≔ 2⁢ⅇa⁢x⁢sin⁡x⁢ln⁡y
f ≔ yx
f ≔ indets⁡f
f ≔ 2⁢ln⁡x⁢y+1
c ≔ remove⁡has,f,x
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.
The inplace option lets a selection decision to be made based on an entry's index.
A reduction or fold function is applied to the selected values.
A selection function need not take the entry value as the first argument.
between ≔ lo,x,hi→lo≤xandx≤hi:
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.
nums ≔ 1,2,3,4,5,6,7,8,9,10
names ≔ map2⁡nprintf,a%03d,nums
selector ≔ map⁡type,nums,odd
The scan option is equivalent to mapping map[reduce] over each prefix of the data produced by a simple call to select:
L ≔ select⁡type,1,2,3,4,5,6,7,8,9,10,odd
prefixes ≔ seq⁡L1..n,n=1..numelems⁡L
The flatten option was introduced in Maple 17.
For more information on Maple 17 changes, see Updates in Maple 17.
The inplace option was introduced in Maple 2018.
For more information on Maple 2018 changes, see Updates in Maple 2018.
The select, remove and selectremove commands were updated in Maple 2021.
The select[n], select[indices], select[fold], select[reduce], select[scan], remove[n], remove[indices], remove[fold], remove[reduce], remove[scan], selectremove[n], selectremove[fold], selectremove[reduce], selectremove[scan] and options were introduced in Maple 2021.
For more information on Maple 2021 changes, see Updates in Maple 2021.
Download Help Document
What kind of issue would you like to report? (Optional)