My way is: (no guarantee to be the best way)
a = Cos[_];
rules = a -> Hold[a]
Cos[t] + 2 Cos[u] + 3 /. rules
It returns 3 + 3 Hold[a]
. If you don't put a
with Hold
, Mathematica will evalute it, which we do not expect.
You can also write a function to generate a list of rules when you need several such replaces.
The benefit of writing rules = a -> Hold[a]
(rather than rules = Cos[_] -> t
, which t
is another symbol to distinguish) is that you need not to care or to trace what is the definition of symbol a
. Every time you use the rule, it simply means replacing the definition of a by a, namely a -> Hold[a]
.
(update)
In order to increase the readability, using HoldForm
is also a good choice.
a = Cos[_];
rules = a -> HoldForm[a]
sentence = Cos[t] + 2 Cos[u] + 3 /. rules
It returns 3 + 3 a
.
sentence // FullForm
It returns Plus[3,Times[3,HoldForm[a]]]
.
So if you want to textually change "a" to a number or an another symbol:
In[]:= sentence
In[]:= sentence /. HoldForm[a] -> x
In[]:= sentence /. HoldForm[a] -> 15
Out[]= 3 + 3 a
Out[]= 3 + 3 x
Out[]= 48