subsindets - Maple Help

subsindets

transform all subexpressions of a given type

 Calling Sequence 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 )

Parameters

 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

Description

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

Examples

This example changes every call to $F$ to a call to $H$.

 > $\mathrm{subsindets}\left(F\left(G\left(x\right),F\left(x,y\right)\right),'\mathrm{specfunc}\left(\mathrm{anything},F\right)',f↦\mathrm{subsop}\left(0=H,f\right)\right)$
 ${H}{}\left({G}{}\left({x}\right){,}{H}{}\left({x}{,}{y}\right)\right)$ (1)
 > $\mathrm{subsindets}\left[\mathrm{flat}\right]\left(F\left(2.3\right)+G\left(4,1.1\right),'\mathrm{float}',\mathrm{trunc}\right)$
 ${F}{}\left({2}\right){+}{G}{}\left({4}{,}{1}\right)$ (2)
 > $\mathrm{subsindets}\left[\mathrm{flat}\right]\left(F\left(G\left(x\right),F\left(x,y\right)\right),'\mathrm{specfunc}\left(\mathrm{anything},F\right)',f↦\mathrm{subsop}\left(0=H,f\right)\right)$
 ${H}{}\left({G}{}\left({x}\right){,}{F}{}\left({x}{,}{y}\right)\right)$ (3)
 > $\mathrm{subsindets}\left(F\left(G\left(x\right),F\left(x,y\right)\right),'\mathrm{specfunc}\left(\mathrm{symbol},F\right)',f↦\mathrm{subsop}\left(0=H,f\right)\right)$
 ${F}{}\left({G}{}\left({x}\right){,}{H}{}\left({x}{,}{y}\right)\right)$ (4)

Shift all symbols by a constant of $\mathrm{\pi }$.

 > $\mathrm{subsindets}\left(\left[\mathrm{sin}\left(x\right),\mathrm{cos}\left(y\right),\mathrm{tan}\left(z\right)\right],'\mathrm{symbol}',y↦y+\mathrm{\pi }\right)$
 $\left[{\mathrm{sin}}{}\left({x}{+}{\mathrm{\pi }}\right){,}{\mathrm{cos}}{}\left({y}{+}{\mathrm{\pi }}\right){,}{\mathrm{tan}}{}\left({z}{+}{\mathrm{\pi }}\right)\right]$ (5)

 > $\mathrm{expr}≔\mathrm{sin}\left(x+y\right)+x\left(x+1\right)+\left(x+1\right)\left(x+2\right)$
 ${\mathrm{expr}}{≔}{\mathrm{sin}}{}\left({x}{+}{y}\right){+}{x}{}\left({x}{+}{1}\right){+}\left({x}{+}{1}\right){}\left({x}{+}{2}\right)$ (6)
 > $\mathrm{subsindets}\left(\mathrm{expr},'\mathrm{*}',\mathrm{expand}\right)$
 ${\mathrm{sin}}{}\left({x}{+}{y}\right){+}{2}{}{{x}}^{{2}}{+}{4}{}{x}{+}{2}$ (7)
 > $\mathrm{expand}\left(\mathrm{expr}\right)$
 ${\mathrm{sin}}{}\left({x}\right){}{\mathrm{cos}}{}\left({y}\right){+}{\mathrm{cos}}{}\left({x}\right){}{\mathrm{sin}}{}\left({y}\right){+}{2}{}{{x}}^{{2}}{+}{4}{}{x}{+}{2}$ (8)

Using subsindets, you can avoid expanding things you might not want expanded.

 > $\mathrm{subsindets}\left(\mathrm{sin}\left(\left(3+I\right)\mathrm{\pi }\right)+{\left(x+y\right)}^{100},'\mathrm{sin}\left(\mathrm{anything}\right)',\mathrm{expand}\right)$
 ${-}{I}{}{\mathrm{sinh}}{}\left({\mathrm{\pi }}\right){+}{\left({x}{+}{y}\right)}^{{100}}$ (9)

Optional arguments may be passed to the transformer.

 > $\mathrm{expr}≔\mathrm{sin}\left(x+y\right)+x\left(x+3\right)+\mathrm{exp}\left(x+1\right)\left(y+2\right)$
 ${\mathrm{expr}}{≔}{\mathrm{sin}}{}\left({x}{+}{y}\right){+}{x}{}\left({x}{+}{3}\right){+}{{ⅇ}}^{{x}{+}{1}}{}\left({y}{+}{2}\right)$ (10)
 > $\mathrm{subsindets}\left(\mathrm{expr},'\mathrm{*}',\mathrm{expand}\right)$
 ${\mathrm{sin}}{}\left({x}{+}{y}\right){+}{{x}}^{{2}}{+}{3}{}{x}{+}{{ⅇ}}^{{x}}{}{ⅇ}{}{y}{+}{2}{}{{ⅇ}}^{{x}}{}{ⅇ}$ (11)
 > $\mathrm{subsindets}\left(\mathrm{expr},'\mathrm{*}',\mathrm{expand},\mathrm{exp}\right)$
 ${\mathrm{sin}}{}\left({x}{+}{y}\right){+}{{x}}^{{2}}{+}{3}{}{x}{+}{{ⅇ}}^{{x}{+}{1}}{}{y}{+}{2}{}{{ⅇ}}^{{x}{+}{1}}$ (12)
 > $\mathrm{expr}≔\left[a,b,3,4,c\right]$
 ${\mathrm{expr}}{≔}\left[{a}{,}{b}{,}{3}{,}{4}{,}{c}\right]$ (13)
 > $\mathrm{subsindets}\left(\mathrm{expr},\mathrm{symbol},\mathrm{^},x\right)$
 $\left[{{a}}^{{x}}{,}{{b}}^{{x}}{,}{3}{,}{4}{,}{{c}}^{{x}}\right]$ (14)
 > $\mathrm{subsindets}\left[2\right]\left(\mathrm{expr},\mathrm{symbol},x,\mathrm{^}\right)$
 $\left[{{x}}^{{a}}{,}{{x}}^{{b}}{,}{3}{,}{4}{,}{{x}}^{{c}}\right]$ (15)

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:

 > $\mathrm{expr}≔{x}^{2}+3x+f\left(x\right):$
 > $\mathrm{count}≔0:$
 > subsindets(expr,symbol,proc(s) global count; count:=count+1; cat(s,count) end);
 ${{\mathrm{x1}}}^{{2}}{+}{3}{}{\mathrm{x1}}{+}{f}{}\left({\mathrm{x1}}\right)$ (16)

In this next case, with the nocache option, the transformer function is guaranteed to be called once for every instance of each matching subexpression:

 > $\mathrm{count}≔0:$
 > subsindets['nocache'](expr,symbol,proc(s) global count; count:=count+1; cat(s,count) end);
 ${{\mathrm{x1}}}^{{2}}{+}{3}{}{\mathrm{x2}}{+}{f}{}\left({\mathrm{x3}}\right)$ (17)

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.

 > $\mathrm{expr}≔2+f\left(4,g\left(h\left(6\right)\right)\right)$
 ${\mathrm{expr}}{≔}{2}{+}{f}{}\left({4}{,}{g}{}\left({h}{}\left({6}\right)\right)\right)$ (18)
 > $\mathrm{subsindets}\left(\mathrm{expr},\mathrm{function},\mathrm{fn}↦\mathrm{subsindets}\left(\mathrm{fn},\mathrm{integer},i↦\mathrm{cat}\left(i,"in",\mathrm{op}\left(0,\mathrm{fn}\right)\right)\right)\right)$
 ${2}{+}{f}{}\left({\mathrm{4 in f}}{,}{g}{}\left({h}{}\left({\mathrm{6 in h}}\right)\right)\right)$ (19)

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.

Compatibility

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