I want to make such changes: $(A\, p + B\, q)[i] \to A\, p[i]+B\, q[i]$

But the replacement below can not do this

(A p + B q)[i] /. {(x_[y_]) -> ((x) /.{p -> p[y],q->q[y]})}

Why is this not working and how to realize this kind of changes?



Your transformation rule won't work because the RHS of a rule will evaluate immediately. Thus,

{(x_[y_]) -> ((x) /.{p -> p[y],q->q[y]})}

is equivalent to {x_[y_] -> x}.

As to how to do what you want, you can use RuleDelayed if you want to keep the same pattern.

(A p + B q)[i] /. {(x_[y_]) :> ((x) /.{p -> p[y],q->q[y]})}
(* A p[i] + B q[i] *)

There's lots of other ways to do this also. Here's one of them using Through, which distributes operators.

Through[(A p + B q)[i]] /. (x_*y_)[z_] -> x*y[z]
(* A p[i] + B q[i] *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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