Let's see how to perform symbolic substitutions with Sage.
var('x, y') f(x) = 1 / x + 3 * x^2 + cos(x) f.show() print("Substitute for x with a keyword:") show(f.subs(x=(7 * x))) print("Substitute for x with a relational expression:") show(f.substitute(x == 7 * x)) print("Substitute sine for cosine:") show(f.substitute_function(cos, sin)) print("Substitute using a dictionary:") show(f.substitute({1 / x: y^3, cos(x):sin(x)}))
The results are shown in the following screenshot:
We used several methods to substitute both variables and functions in a callable symbolic expression. The methods called subs
and substitute
are identical. If you only need to replace a single symbol, a keyword argument can be used to specify which variable is to be replaced, and what expression should take its place. Multiple keywords can be used to make multiple substitutions at the same time. A relational expression can be used to replace a single...