Substitute a variable by a value (Maple and Mupad compatibility) : subs

### 5.12.17 Substitute a variable by a value (Maple and Mupad compatibility) : subs

In Maple and in Mupad, one would use the subs command to substitute a variable by a value in an expression. But the order of the arguments differ between Maple and Mupad. Therefore, to achieve compatibility, Xcas subs command arguments order depends on the mode

- In Maple mode, subs takes two arguments : an equality (variable=substitution value) and the expression.
  To substitute several variables in an expression, use a list of equality (variable names = substitution value) as first argument.
- In Mupad or Xcas or TI, subs takes two or three arguments : an expression and an equality (variable=substitution value) or an expression, a variable name and the substitution value.
  To substitute several variables, subs takes two or three arguments :
  - an expression of variables and a list of (variable names = substitution value),
  - an expression of variables, a list of variables and a list of their substitution values.

subs returns the expression with the substitution done. Note that subs does not quote its argument, hence in a normal evaluation process, the substitution variable should be purged otherwise it will be replaced by its assigned value before substitution is done.
Input in Maple mode (if the variable a is purged else input purge(a)) :

subs(a=2,a`^`2+1)

Output

2`^`2+1

Input in Maple mode (if the variables a and b are purged else input purge(a,b)):

subs([a=2,b=1],a`^`2+b)

Output :

2`^`2+1

Input :

subs(a`^`2+1,a=2)

or :

subs(a`^`2+1,a,2)

Output (if the variable a is purged else input purge(a)) :

5

Input :

subs(a`^`2+b,[a=2,b=1])

or :

subs(a`^`2+b,[a,b],[2,1])

Output (if the variables a and b are purged else input purge(a,b)) :

2`^`2+1