subsindets
transform all subexpressions of a given type
Calling Sequence
Parameters
Description
Examples
Compatibility
subsindets( expr, atype, transformer, rest )
subsindets[eval]( expr, atype, transformer, rest )
subsindets[flat]( expr, atype, transformer, rest )
subsindets[nocache]( expr, atype, transformer, rest )
subsindets[N]( expr, atype, preargs, transformer, rest )
expr
-
anything; any Maple expression
atype
type; any Maple type expression
transformer
anything; an expression (typically, a procedure) to be called on each subexpression processed
rest
anything; (optional) expression sequence of extra arguments to be passed to transformer
N
integer; one more than the number of arguments passed to the transformer before the subexpression
preargs
anything; expression sequence of N-1 arguments to be passed to the transformer before the subexpression
The procedure subsindets is a particular combination of calls to subs and indets that allows you to efficiently transform all subexpressions of a given type by some algorithm. It encapsulates a common "pattern" used in expression manipulation and transformation.
Each subexpression of type atype is transformed by the supplied transformer procedure. Then, each subexpression is replaced in the original expression, using subs (or subs[eval], if the eval option is given), with the corresponding transformed expression.
If the flat option is supplied as an index to the subsindets command, then Maple will not recursively look inside any subexpressions of the given atype for further (nested) subexpressions of that type. This provides a hint to Maple that it can use a somewhat faster algorithm in this special case.
If the flat option is not supplied, then subexpressions of type atype at deeper levels of nesting are processed and replaced before subexpressions at shallower levels of nesting. This is illustrated in an example below.
By default, subsindets uses a cache to avoid repetitive type checking and repeated calls to transformer for multiple instances of the same subexpression (this cache is limited in size, so repeated calls may occur, but there is no guarantee that they will). The nocache option, specified as an index to the subsindets command, prevents the use of this cache and guarantees that transformer will be called once for each instance of any common subexpression matching atype. Note that using the nocache option can dramatically lower the performance of subsindets on expressions containing many common subexpressions.
If subsindets is called with an integer index, N, then each subexpression to be processed by transformer will be passed as the N-th argument to transformer. The sequence of N-1 arguments of subsindets between atype and transformer will be passed as the first N-1 arguments to transformer.
Any number of the options eval, flat, nocache, and N can be given in the index as an expression sequence, and they can be specified in any order.
This example changes every call to F to a call to H.
subsindets⁡F⁡G⁡x,F⁡x,y,specfunc⁡anything,F,f↦subsop⁡0=H,f
H⁡G⁡x,H⁡x,y
subsindetsflat⁡F⁡2.3+G⁡4,1.1,float,trunc
F⁡2+G⁡4,1
subsindetsflat⁡F⁡G⁡x,F⁡x,y,specfunc⁡anything,F,f↦subsop⁡0=H,f
H⁡G⁡x,F⁡x,y
subsindets⁡F⁡G⁡x,F⁡x,y,specfunc⁡symbol,F,f↦subsop⁡0=H,f
F⁡G⁡x,H⁡x,y
Shift all symbols by a constant of π.
subsindets⁡sin⁡x,cos⁡y,tan⁡z,symbol,y↦y+π
sin⁡x+π,cos⁡y+π,tan⁡z+π
Expand all products in an expression.
expr≔sin⁡x+y+x⁢x+1+x+1⁢x+2
subsindets⁡expr,`*`,expand
sin⁡x+y+2⁢x2+4⁢x+2
expand⁡expr
sin⁡x⁢cos⁡y+cos⁡x⁢sin⁡y+2⁢x2+4⁢x+2
Using subsindets, you can avoid expanding things you might not want expanded.
subsindets⁡sin⁡3+I⁢π+x+y100,sin⁡anything,expand
−I⁢sinh⁡π+x+y100
Optional arguments may be passed to the transformer.
expr≔sin⁡x+y+x⁢x+3+exp⁡x+1⁢y+2
expr≔sin⁡x+y+x⁢x+3+ⅇx+1⁢y+2
sin⁡x+y+x2+3⁢x+ⅇx⁢ⅇ⁢y+2⁢ⅇx⁢ⅇ
subsindets⁡expr,`*`,expand,exp
sin⁡x+y+x2+3⁢x+ⅇx+1⁢y+2⁢ⅇx+1
expr≔a,b,3,4,c
subsindets⁡expr,symbol,`^`,x
ax,bx,3,4,cx
subsindets2⁡expr,symbol,x,`^`
xa,xb,3,4,xc
The following example demonstrates the use of subsindets[nocache]
In this first case, without nocache, the transformer function will be called at least once, and most likely only once, though this is not guaranteed, for each matching subexpression:
expr≔x2+3⁢x+f⁡x:
count≔0:
subsindets(expr,symbol,proc(s) global count; count:=count+1; cat(s,count) end);
x12+3⁢x1+f⁡x1
In this next case, with the nocache option, the transformer function is guaranteed to be called once for every instance of each matching subexpression:
subsindets['nocache'](expr,symbol,proc(s) global count; count:=count+1; cat(s,count) end);
x12+3⁢x2+f⁡x3
This example illustrates the remark above about the order in which subexpressions at various levels of nesting depth are processed. In it, we replace each integer in the expression that occurs inside a function call with a name that indicates the name of the closest enclosing function. This is done by using a second call to subsindets as the transformer.
expr≔2+f⁡4,g⁡h⁡6
subsindets⁡expr,function,fn↦subsindets⁡fn,integer,i↦cat⁡i, in ,op⁡0,fn
2+f⁡4 in f,g⁡h⁡6 in h
This works because the outer call to subsindets processes h(6) (turning it into h(`6 in f`)) before it processes the calls to g or f; so when the transformer processes those calls, the integer 6 doesn't occur anymore.
The subsindets command was updated in Maple 2015.
The N parameter was introduced in Maple 2015.
The eval and nocache options were introduced in Maple 2015.
For more information on Maple 2015 changes, see Updates in Maple 2015.
See Also
eval
evalindets
frontend
indets
subs
type
Download Help Document