Group Abstract Group Abstract

Message Boards Message Boards

1
|
1.8K Views
|
3 Replies
|
1 Total Like
View groups...
Share
Share this post:

How do I use /. to replace a custom function?

Posted 2 years ago

Hi everyone,

I already assume that there's a really trivial solution to this, but I just can't figure it out and also couldn't find anybody else writing about this - why is

In[1]:= Sin[x]/.Sin->Cos
Out[1]:= Cos[x]

but

In[1]:=  A[x_]:=2x
         B[x_]:=3x
         A[x]/.A->B
Out[1]:= 2x

What's different with this replacement rule, and how to I get the desired result 3x?

3 Replies
Posted 2 years ago

An alternative:

Hold@A[x] /. A -> B // ReleaseHold
POSTED BY: Hans Milton
Posted 2 years ago

Another workaround

Unevaluated@A[x] /. A -> B
POSTED BY: Rohit Namjoshi
Posted 2 years ago

The first argument of ReplaceAll is evaluated before the replacement rule acts. The difference between the two cases is that evaluating Sin[x] just returns Sin[x], unchanged. So Sin is still visible to the replacement rule. But evaluating A[x] returns $2 x$, so A is no longer visible to the rule.

A workaround:

A[x] /. A[x] -> B[x]
POSTED BY: Hans Milton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard