Substitute

Substitute

Substitute[ expr, a, b ] replaces all occurrences of an expression with another expression.

Usages

Substitute[ expr, a, b ]

Substitute[ expr, a, b ] replaces every occurrence of a in expr with b. The result is not calculated; use Simplify for that.

Example in out
Replace x^2 with p. Substitute[ x^4 + x^2 + 3x^2 + 6, x^2, p ] x^4+p+3p+6
Simplify the result. Simplify[ Substitute[ x^4 + x^2 + 3x^2 + 6, x^2, p ] ] 4p+x4+64p+x^{4}+6
Substitute a value for a variable. Substitute[ x^2 + 2x + 1, x, 3 ] 3^2+2*3+1
Substitute an expression; brackets are added where needed. Substitute[ x^2 + 2x + 1, x, a+1 ] (a+1)^2+2(a+1)+1
Replace a function. Substitute[ Sin[x] + Cos[x], Sin, Cos ] Cos[x]+Cos[x]
Replace elements of a list. Substitute[ {a, b, a}, a, c ] {c, b, c}
Replace a parameter in a mapping. f := x -> x^2 + a
r := Substitute[f, a, 3]
r = x -> x^2+3
Options

SubstituteMappingVariables

If set to True, variables in b with the same name as the variable of a mapping in expr are linked to that mapping variable. Default: False.

Syntax
Substitute[ expr, a, b, SubstituteMappingVariables=>True ]
Details
  • Whole sub-expressions: a is only replaced where it occurs as a complete part of the expression. x+y is not replaced in x+y+1, because that sum has the terms x, y and 1: Substitute[ x+y+1, x+y, z ] gives x+y+1. The order of terms and factors does not matter: Substitute[ 2x, x*2, z ] gives z.
  • Mapping variables are not replaced: in f := x -> x^2 + a, Substitute[f, x, 3] does not change f. Use FuncApplication or f[3] to evaluate a mapping.
Invalid input Result
Substitute[ x^2, x ] Left unevaluated: Substitute takes three arguments.
Substitute[ x^2, x, 3, Foo=>1 ] Error: Invalid associative argument for Substitute: Foo

See also: FuncApplication, RemoveFunction, Map, Simplify