algsubs
substitute subexpressions into an expression
Calling Sequence
Parameters
Description
Examples
algsubs(a = b, f)
algsubs(a = b, f, v)
algsubs(a = b, f, v, options)
a, b
-
algebraic expressions
f
any expression
v
name or list or set of names
The function algsubs performs an algebraic substitution, replacing occurrences of a with b in the expression f. It is a generalization of the subs command, which only handles syntactic substitution. The purpose of algsubs can be seen from these five examples where subs fails.
algsubs( s^2=1-c^2, s^3 );
−c2+1⁢s
algsubs( a+b=d, 1+a+b+c );
1+d+c
algsubs( a*b=c, 2*a*b^2-a*b*d );
2⁢b⁢c−d⁢c
algsubs( a*b/c=d, 2*a*b^2/c );
2⁢d⁢b
algsubs( a^2=0, exp(2-a+a^2/2-a^3/6) );
ⅇ2−a
Where the expression being replaced has more than one variable, the result is ambiguous. For example, should algsubs(a+b=c, 2*a+b) return 2⁢c−b or a+c? What should algsubs(a+b=c, a+Pi*b) return? Should algsubs(x*y^2=c, x*y^4) return c⁢y2 or c2x? The algsubs command provides two modes for breaking ambiguities, an exact mode and a remainder mode (the default).
Both modes depend on the ordering of the variables that appear in a. This can be set to a specific ordering by specifying the variables v in a list as a third argument. If the variables are not explicitly given, the set of indeterminates in a, b which are functions and names defines the variables and their order. Also, the result is collected in the variables given.
Both modes require that monomials in a divide monomials in f for a substitution to occur. A monomial u in a divides a monomial v in f if for each variable x in u, then either 0<degree⁡u,x≤degree⁡v,x or degree⁡v,x≤degree⁡u,x<0.
For example, the monomial x2y2 divides the monomial x3y2 but not xy2 or x2y.
Note: The algsubs command currently works only with integer exponents.
Note that the requirement for monomials in a to divide monomials in f means that the negative powers of u in the following example are not substituted, and must be handled separately as shown.
f := a/u^4+b/u^2+c+d*u^2+e*u^4;
f≔au4+bu2+c+d⁢u2+e⁢u4
algsubs(u^2=v,f);
e⁢v2+d⁢v+c+bu2+au4
algsubs(1/u^2=1/v,f);
e⁢u4+d⁢u2+c⁢v2+b⁢v+av2
Hence, to substitute for both positive and negative powers.
algsubs(u^2=v,algsubs(1/u^2=1/v,f));
e⁢v4+d⁢v3+c⁢v2+b⁢v+av2
If the option remainder is specified, or no option is specified, a generalized remainder is computed. If the leading monomial of a divides the leading monomial of f then the leading monomial of f is replaced with the appropriate value, and this is repeated until the leading monomial in f is less than the leading monomial in a.
If the option exact is specified, then if the value a being replaced is a sum of terms ai, and the value f it is replacing is the sum of terms fi, then the replacement is made if and only if each monomial ai divides fi and sum⁡ai=c⁢sum⁡fi that is, the coefficients must all be the same. For example, algsubs( x^2+2*y=z, 3*x^2+6*y ) succeeds with c=3 but algsubs(x^2+2*y=z, 3*x^2+3*y) fails.
The algsubs command goes recursively through the expression f. Unlike the subs command it does not substitute inside indexed names, and function calls are applied to the result of a substitution. Like subs, it does not expand products or powers before substitution. Hence algsubs( x^2=0, (x+1)^3 ) yields x+13.
algsubs⁡a+b=c,exp⁡a+b+d
ⅇc+d
algsubs⁡P⁢VT=R,P2⁢VT2−P⁢R
−P⁢R+R⁢PT
algsubs⁡s+1t=y,s+1t+13−s−2t
y+13−1t−y
f≔sin⁡x3−cos⁡x⁢sin⁡x2+cos⁡x2⁢sin⁡x+cos⁡x3
algsubs⁡sin⁡x2=1−cos⁡x2,f
2⁢cos⁡x3+sin⁡x−cos⁡x
Truncating polynomials
f≔expand⁡u+w−1w3
f≔u3+3⁢u2⁢w−3⁢u2w+3⁢u⁢w2−6⁢u+3⁢uw2+w3−3⁢w+3w−1w3
algsubs⁡u2=0,f
w6−3⁢w4+3⁢w2−1w3+3⁢w4−2⁢w2+1⁢uw2
algsubs⁡u2=0,f,u,w
3⁢u⁢w2−6⁢u+3⁢uw2+w3−3⁢w+3w−1w3
algsubs⁡1w2=0,f,u,w
u3+3⁢u2⁢w−3⁢u2w+3⁢u⁢w2−6⁢u+w3−3⁢w+3w
algsubs⁡u⁢w=0,f
u3−3⁢u2w−6⁢u+3⁢uw2+w3−3⁢w+3w−1w3
algsubs⁡uw=0,f
u3+3⁢u2⁢w+3⁢u⁢w2−6⁢u+w3−3⁢w+3w−1w3
Ambiguous cases
f≔2⁢a+b
algsubs⁡a+b=c,f,a,b
2⁢c−b
algsubs⁡a+b=c,f,b,a
a+c
algsubs⁡a+b=c,f,exact
2⁢a+b
f≔a1+a2−1⁢xa1+a2+a1⁢ya1+a2−a2⁢a1+a2z
algsubs⁡a1+a2=p,f
x⁢p−1p+y⁢−a2+pp−a2⁢pz
algsubs⁡a1+a2=p,f,exact
x⁢p−1p+y⁢a1p−a2⁢pz
See Also
applyrule
simplify
simplify/siderels
subs
Download Help Document