 subs - Maple Help

subs

substitute subexpressions into an expression Calling Sequence subs(x=a,expr) subs(s1,...,sn,expr) subs[eval](x=a,expr) subs[eval](s1,...,sn,expr) subs[inplace](x=a,expr) subs[inplace](s1,...,sn,expr) subs[member](x=a,expr) subs[member](s1,...,sn,expr) Parameters

 x - expression a - expression expr - expression s1, ..., sn - equations, sets or lists of equations, or tables Description

 • The first form of the subs command substitutes a for x in the expression expr. Note that this command is similar to the eval command. Simple applications of replacing a symbol by a value in a formula should normally be done with the eval command. Differences between the two commands are highlighted in the examples later.
 • The second general form of the subs command makes the substitutions specified by first arguments in the last argument expr. Each of s1, ..., sn must be an equation, a list or set of equations, or a table. The substitutions are performed sequentially starting with s1. The substitutions within one set, list, or table are performed simultaneously.
 • When the substitutions are specified by an equation, or a list or set of equations, every occurrence of the left-hand side of an equation that appears in expr is replaced by the right-hand side of the equation.
 • When the substitutions are specified as a table, every occurrence of an index of the table that appears in expr is replaced by the corresponding entry of the table.
 • If there are multiple substitutions for the same expression within a list of substitutions, the first one is used. If the substitutions are specified by a set, the one that would appear first if the set were printed is used. (It is not possible for a table to contain multiple substitutions for the same expression.)
 • If a list, set, or table contains substitutions for both an expression and also for subexpressions thereof, the former takes precedence when the expression is encountered in expr.
 • Note that only subexpressions in expr that correspond to operands (ops) of a Maple object are matched.  This is termed "syntactic substitution".  A more powerful substitution is available using the algsubs command.
 • The inplace option only applies when substituting into a table or rtable (such as Array, Matrix, or Vector) with no indexing function. When inplace is specified, the input table or rtable is updated in-place. The result is the updated original expr table or rtable, rather than a copy.
 • The action of substitution is not followed by evaluation by default. There are two ways of achieving a full evaluation:
 – The eval command performs a full evaluation but takes care to delay substitutions which are not mathematically safe, as shown in the examples later.
 The eval command will not descend into any procedures appearing in the expression being evaluated.
 – The eval option to subs also performs a full evaluation while keeping the semantics the same as for subs. This option should be used with care.
 When a procedure is encountered in expr, subs will descend into it, performing substitution there as well. If the eval option was used, it will be ignored while substituting within procedures.
 • The member option causes subs to only apply substitutions directly to the top level members when expr is a list, set, function, selection, Array, Matrix, or Vector. If expr is not one of these, no substitutions are done.
 • The options eval, inplace, and member can be given together in the index as an expression sequence, and they can be specified in either order. • The subs command is thread-safe as of Maple 15. Examples

Examples showing simple substitution of the form x=a for an expression expr.

 > $\mathrm{subs}\left(x=2,{x}^{2}+x+1\right)$
 ${7}$ (1)
 > $\mathrm{subs}\left(x={r}^{\frac{1}{3}},3x\mathrm{ln}\left({x}^{3}\right)\right)$
 ${3}{}{{r}}^{{1}}{{3}}}{}{\mathrm{ln}}{}\left({r}\right)$ (2)
 > $\mathrm{subs}\left(\mathrm{sin}\left(x\right)=y,\frac{\mathrm{sin}\left(x\right)}{\mathrm{sqrt}\left(1-\mathrm{sin}\left(x\right)\right)}\right)$
 $\frac{{y}}{\sqrt{{1}{-}{y}}}$ (3)

The following examples show the difference between subs and algsubs, where the algsubs command substitutes subexpressions into an expression.

 > $\mathrm{subs}\left(a+b=y,{\left(a+b+c\right)}^{2}\right)$
 ${\left({a}{+}{b}{+}{c}\right)}^{{2}}$ (4)
 > $\mathrm{algsubs}\left(a+b=y,{\left(a+b+c\right)}^{2}\right)$
 ${\left({y}{+}{c}\right)}^{{2}}$ (5)
 > $\mathrm{subs}\left({a}^{2}=y,{a}^{3}\right)$
 ${{a}}^{{3}}$ (6)
 > $\mathrm{algsubs}\left({a}^{2}=y,{a}^{3}\right)$
 ${y}{}{a}$ (7)

The substitutions within a set or a list are performed simultaneously.

 > $\mathrm{subs}\left(\left\{x=y,y=x\right\},\left[x,y\right]\right)$
 $\left[{y}{,}{x}\right]$ (8)
 > $\mathrm{subs}\left(\left[x=y,y=x\right],\left[x,y\right]\right)$
 $\left[{y}{,}{x}\right]$ (9)

Substitutions can be specified using a table.

 > $t≔\mathrm{table}\left(\right):$$t\left[a\right]≔\mathrm{\alpha }:$$t\left[b\right]≔\mathrm{\beta }:$
 > $\mathrm{subs}\left(t,ax+b\right)$
 ${\mathrm{\alpha }}{}{x}{+}{\mathrm{\beta }}$ (10)

When the substitutions are in a sequence, they are applied one at a time, akin to the following:

 > $\mathrm{subs}\left(x=y,y=x,\left[x,y\right]\right)$
 $\left[{x}{,}{x}\right]$ (11)
 > $\mathrm{subs}\left(x=y,\left[x,y\right]\right)$
 $\left[{y}{,}{y}\right]$ (12)
 > $\mathrm{subs}\left(y=x,\right)$
 $\left[{x}{,}{x}\right]$ (13)

If there are multiple substitutions on the same expression, the first one is used.

 > $\mathrm{subs}\left(\left[x=0,x=2\right],\mathrm{exp}\left(x\right)\right)$
 ${{ⅇ}}^{{0}}$ (14)
 > $\mathrm{simplify}\left(\right)$
 ${1}$ (15)

If there are substitutions on both an expression and subexpressions thereof, the former is used.

 > $\mathrm{subs}\left(\left[g\left(x\right)=G,f\left(g\left(x\right)\right)=F\right],a+f\left(g\left(x\right)\right)\right)$
 ${a}{+}{F}$ (16)

As shown in the following example, evaluation does not follow substitution by default.

 > $\mathrm{subs}\left(y=0,\mathrm{sin}\left(y\right)\right)$
 ${\mathrm{sin}}{}\left({0}\right)$ (17)

The option eval for subs can be used to perform a full evaluation while not altering the semantics for subs.

 > $\mathrm{subs}\left[\mathrm{eval}\right]\left(y=0,\mathrm{sin}\left(y\right)\right)$
 ${0}$ (18)

Alternatively, you can use the eval command:

 > $\mathrm{eval}\left(\mathrm{sin}\left(y\right),y=0\right)$
 ${0}$ (19)

The following calls to subs and subs[eval] will return an error since Maple does simultaneous substitution in all parameters. (However, eval will work correctly.)

 > $p≔\mathrm{piecewise}\left(x=0,1,\frac{\mathrm{sin}\left(x\right)}{x}\right)$
 ${p}{≔}\left\{\begin{array}{cc}{1}& {x}{=}{0}\\ \frac{{\mathrm{sin}}{}\left({x}\right)}{{x}}& {\mathrm{otherwise}}\end{array}\right\$ (20)
 > $\mathrm{subs}\left(x=0,p\right)$
 > $\mathrm{subs}\left[\mathrm{eval}\right]\left(x=0,p\right)$
 > $\mathrm{eval}\left(p,x=0\right)$
 ${1}$ (21)

Here is an example where subs[eval] is more effective than eval (since eval sometimes delays substitutions which involve dummy variables):

 > $\mathrm{eval}\left(\mathrm{Int}\left(s\mathrm{abs}\left(f\left(x\right)\right),x\right),s=\mathrm{signum}\left(f\left(x\right)\right)\right)$
 $\genfrac{}{}{0}{}{{\int }{s}{}\left|{f}{}\left({x}\right)\right|\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{x}}{\phantom{\left\{{s}{=}{\mathrm{signum}}{}\left({f}{}\left({x}\right)\right)\right\}}}{|}\genfrac{}{}{0}{}{\phantom{{\int }{s}{}\left|{f}{}\left({x}\right)\right|\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{x}}}{\left\{{s}{=}{\mathrm{signum}}{}\left({f}{}\left({x}\right)\right)\right\}}$ (22)
 > $\mathrm{subs}\left[\mathrm{eval}\right]\left(s=\mathrm{signum}\left(f\left(x\right)\right),\mathrm{Int}\left(s\mathrm{abs}\left(f\left(x\right)\right),x\right)\right)$
 ${\int }{\mathrm{signum}}{}\left({f}{}\left({x}\right)\right){}\left|{f}{}\left({x}\right)\right|\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{x}$ (23)

Using the member option limits substitutions to the immediate entries of a container expresssion:

 > $\mathrm{subs}\left[\mathrm{member}\right]\left(A=Z,\left[A,A+B\right]\right)$
 $\left[{Z}{,}{A}{+}{B}\right]$ (24)

subs will descend into procedures, whereas eval will not.

 > subs(a=b,[a, proc(x) x+a end]);
 $\left[{b}{,}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{+}{b}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}\right]$ (25)
 > eval([a, proc(x) x+a end],a=b);
 $\left[{b}{,}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{+}{a}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}\right]$ (26) Compatibility

 • The eval option was introduced in Maple 2015.