2
$\begingroup$

How can I replace a constant a in a certain expression with a function f[z_]:=1+z?

I tried the following:

expr = a/c
(*a/c*)
expr = Replace[expr, a -> f[z]]
(*a/c*)

The desired result would be expr=(1 + z)/c

$\endgroup$
  • 1
    $\begingroup$ If really want to update expr, try expr = expr /. a -> f[z] $\endgroup$ – m_goldberg Jul 19 '17 at 0:42
  • 2
    $\begingroup$ Or use the correct function name, which is ReplaceAll. $\endgroup$ – Itai Seggev Jul 19 '17 at 3:10
3
$\begingroup$

Use the third argument of Replace to specify the level as 1:

f[x_] := 1 + x;
expr = a/c;
expr = Replace[expr, a -> f[z], 1]

(1 + z)/c

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.