select - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


select

selection from an expression

remove

removal from an expression

selectremove

selection or removal from an expression

 

Calling Sequence

Parameters

Description

Arguments and Options

Objects Supporting select, remove, and selectremove

Thread Safety

Examples

Compatibility

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.

"DataFrame" objects

"DataSeries" objects

 

 

Thread Safety

• 

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.

Examples

integersseq10..20

integers10,11,12,13,14,15,16,17,18,19,20

(1)

selectisprime,integers

11,13,17,19

(2)

removeisprime,integers

10,12,14,15,16,18,20

(3)

selectremoveisprime,integers

11,13,17,19,10,12,14,15,16,18,20

(4)

f2expaxsinxlny

f2ⅇaxsinxlny

(5)

selecthas,f,x

ⅇaxsinx

(6)

removehas,f,x

2lny

(7)

selectremovehas,f,x

ⅇaxsinx,2lny

(8)

fyx

fyx

(9)

selectremovehas,f,x

undefined,y

(10)

findetsf

fx,y,yx

(11)

selecttype,f,name

x,y

(12)

removetype,f,name

yx

(13)

selectremovetype,f,name

x,y,yx

(14)

f2lnxy+1

f2lnxy+1

(15)

cremovehas,f,x

c2y+2

(16)

fc

2lnxy+12y+2

(17)

selecthas,f,x

lnx

(18)

removex2<x&comma;1&comma;2&comma;3

12

(19)

removeflattenx2<x&comma;1&comma;2&comma;3

12

(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.

selectx2<x&comma;1|2&comma;3|4

34

(21)

selectflattenx2<x&comma;1|2&comma;3|4

34

(22)

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

unassigna&comma;b&comma;c&comma;d

selectindicestype&comma;a&comma;b&comma;c&comma;d&comma;odd

a&comma;c

(23)

selectindicesrow&comma;colrow=col&comma;a|b&comma;c|d

ad

(24)

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

selectreduce=`+`isprime&comma;integers

60

(25)

removefold=`*`&comma;1isprime&comma;integers

145152000

(26)

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

betweenlo&comma;x&comma;hiloxhi&colon;

select2between&comma;12&comma;integers&comma;17

12&comma;13&comma;14&comma;15&comma;16&comma;17

(27)

selectremove2between&comma;5&comma;integers&comma;15

10&comma;11&comma;12&comma;13&comma;14&comma;15,16&comma;17&comma;18&comma;19&comma;20

(28)

selectremove2,reduce=`+`between&comma;5&comma;integers&comma;15

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.

nums1&comma;2&comma;3&comma;4&comma;5&comma;6&comma;7&comma;8&comma;9&comma;10

nums1&comma;2&comma;3&comma;4&comma;5&comma;6&comma;7&comma;8&comma;9&comma;10

(30)

namesmap2nprintf&comma;a%03d&comma;nums

namesa001&comma;a002&comma;a003&comma;a004&comma;a005&comma;a006&comma;a007&comma;a008&comma;a009&comma;a010

(31)

selectormaptype&comma;nums&comma;odd

selectortrue&comma;false&comma;true&comma;false&comma;true&comma;false&comma;true&comma;false&comma;true&comma;false

(32)

selectselector&comma;nums

1&comma;3&comma;5&comma;7&comma;9

(33)

selectselector&comma;names

a001&comma;a003&comma;a005&comma;a007&comma;a009

(34)

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

Lselecttype&comma;1&comma;2&comma;3&comma;4&comma;5&comma;6&comma;7&comma;8&comma;9&comma;10&comma;odd

L1&comma;3&comma;5&comma;7&comma;9

(35)

prefixesseqL1..n&comma;n=1..numelemsL

prefixes1&comma;1&comma;3&comma;1&comma;3&comma;5&comma;1&comma;3&comma;5&comma;7&comma;1&comma;3&comma;5&comma;7&comma;9

(36)

mapxmapreduce=`+`yy&comma;x&comma;prefixes

1&comma;4&comma;9&comma;16&comma;25

(37)

selectscan=`+`type&comma;1&comma;2&comma;3&comma;4&comma;5&comma;6&comma;7&comma;8&comma;9&comma;10&comma;odd

1&comma;4&comma;9&comma;16&comma;25

(38)

Compatibility

• 

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.

See Also

member

membertype

selectfun