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$

closed as off-topic by m_goldberg, Itai Seggev, LCarvalho, Daniel Lichtblau, yohbs Jul 20 '17 at 4:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Itai Seggev, LCarvalho, Daniel Lichtblau, yohbs
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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.