subs
substitute subexpressions into an expression
Calling Sequence
Parameters
Description
Thread Safety
Examples
Compatibility
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)
x
-
expression
a
expr
s1, ..., sn
equations, sets or lists of equations, or tables
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.
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. You should only use the inplace option when substituting into an rtable (such as Array, Matrix, or Vector) with no indexing function. subs[inplace] applied to a sparse Matrix only changes non-zero elements.
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.
For more information on thread safety, see index/threadsafe.
Examples showing simple substitution of the form x=a for an expression expr.
subs⁡x=2,x2+x+1
7
subs⁡x=r13,3⁢x⁢ln⁡x3
3⁢r13⁢ln⁡r
subs⁡sin⁡x=y,sin⁡xsqrt⁡1−sin⁡x
y1−y
The following examples show the difference between subs and algsubs, where the algsubs command substitutes subexpressions into an expression.
subs⁡a+b=y,a+b+c2
a+b+c2
algsubs⁡a+b=y,a+b+c2
y+c2
subs⁡a2=y,a3
a3
algsubs⁡a2=y,a3
y⁢a
The substitutions within a set or a list are performed simultaneously.
subs⁡x=y,y=x,x,y
y,x
Substitutions can be specified using a table.
t≔table⁡:ta≔α:tb≔β:
subs⁡t,a⁢x+b
α⁢x+β
When the substitutions are in a sequence, they are applied one at a time, akin to the following:
x,x
subs⁡x=y,x,y
y,y
subs⁡y=x,
If there are multiple substitutions on the same expression, the first one is used.
subs⁡x=0,x=2,exp⁡x
ⅇ0
simplify⁡
1
If there are substitutions on both an expression and subexpressions thereof, the former is used.
subs⁡g⁡x=G,f⁡g⁡x=F,a+f⁡g⁡x
a+F
As shown in the following example, evaluation does not follow substitution by default.
subs⁡y=0,sin⁡y
sin⁡0
The option eval for subs can be used to perform a full evaluation while not altering the semantics for subs.
subseval⁡y=0,sin⁡y
0
Alternatively, you can use the eval command:
eval⁡sin⁡y,y=0
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≔piecewise⁡x=0,1,sin⁡xx
p≔1x=0sin⁡xxotherwise
subs⁡x=0,p
Error, numeric exception: division by zero
subseval⁡x=0,p
eval⁡p,x=0
Here is an example where subs[eval] is more effective than eval (since eval sometimes delays substitutions which involve dummy variables):
eval⁡Int⁡s⁢abs⁡f⁡x,x,s=signum⁡f⁡x
∫` `xsignum⁡f⁡x⁢f⁡_aⅆ_a
subseval⁡s=signum⁡f⁡x,Int⁡s⁢abs⁡f⁡x,x
∫signum⁡f⁡x⁢f⁡xⅆx
This example performs a substitution in-place. Notice that myVec is changed after calling subs[inplace].
myVec≔2,1,4
myVec≔214
subsinplace⁡4=3,myVec
213
myVec
Using the member option limits substitutions to the immediate entries of a container expresssion:
subsmember⁡A=Z,A,A+B
Z,A+B
subs will descend into procedures, whereas eval will not.
subs(a=b,[a, proc(x) x+a end]);
b,procxx+bend proc
eval([a, proc(x) x+a end],a=b);
b,procxx+aend proc
The eval option was introduced in Maple 2015.
For more information on Maple 2015 changes, see Updates in Maple 2015.
The subs command was updated in Maple 2018.
The s1 parameter was updated in Maple 2018.
The member option was introduced in Maple 2021.
For more information on Maple 2021 changes, see Updates in Maple 2021.
See Also
algsubs
applyrule
eval
limit
op
subsop
Download Help Document