7
$\begingroup$

I want to replace the variable x in the result, not x in unrelated x[i].

ω[n_] := Product[x - x[i - 1], {i, 1, n}]

D[ω[3], x] /. x -> x[k]

In the code above, I want to get a result like (x[k] - x[0])*(x[k] - x[1]) + (x[k] - x[2])*(x[k] - x[1]) + (x[k] - x[0])*(x[k] - x[2]). how can I solve this problem skillfully?

$\endgroup$
1

1 Answer 1

11
$\begingroup$

You can use Replace with the option Heads -> False:

Replace[D[ω[3], x], x -> x[k], All, Heads -> False]
 (-x[0] + x[k]) (-x[1] + x[k]) + (-x[0] + x[k]) (-x[2] + x[k]) + 
  (-x[1] + x[k]) (-x[2] + x[k])

Alternatively, you can use ReplaceAll with the replacement rule {p_x :> p, x -> x[k]}:

D[ω[3], x] /. { p_x :> p, x -> x[k]}
 (-x[0] + x[k]) (-x[1] + x[k]) + (-x[0] + x[k]) (-x[2] + x[k]) + 
  (-x[1] + x[k]) (-x[2] + x[k])
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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