# How can I replace a function? Something like f[x] /. f -> -g. ReplaceAll doesnt seem to work properly when there is a minus sign

How can I replace a function with minus another function? This: Sin[x] /. Sin -> Cos works just fine. But this: Sin[x] /. Sin -> -Cos gives (-Cos)[x] and I cannot evaluate it, in this output x is not recongnized as the function's argument. Seems simple but I could not find a way to do this. Help, please!

• Sin[x] /. Sin[aa_]:>-Cos[aa] ?? Commented Nov 16, 2018 at 15:05

Try

Sin[x] /. Sin[x] -> -Cos[x]


which gives you -Cos[x]

If you have a slightly more complicated problem like Sin[x] + Sin[y] and you want to replace both those Sin then this

Sin[x] + Sin[y] /. Sin[z_] :> -Cos[z]


will give you -Cos[x] - Cos[y]

• The second expression worked perfectly to solve my problem. Thank you, Bill! Commented Nov 15, 2018 at 20:29
• It is best to use RuleDelayed for named pattern replacements unless you specifically know you need otherwise. I edited your answer accordingly. I hope you don't mind. Commented Nov 16, 2018 at 11:07
• Bill, and what about Sin[x]+Sin[f[x]]
– ayr
Commented Nov 2, 2022 at 12:40
• @dtn Sin[x]+Sin[f[x]]/.Sin[u_]->Cos[u] returns Cos[x]+Cos[f[x]] but I don't know how to test that in your situation to tell whether it is working for you or not. Does that work for you? If not then can you provide something that I can use to test this?
– Bill
Commented Nov 4, 2022 at 7:12

I usually replace the f with a pure function this way:

Sin[x] /. Sin -> (-Cos[#] &)


It substitutes for all f, if there is more than one form of f in the expression

Sin[x] + Sin[x^2]/. Sin -> (-Cos[#] &)
(*  -Cos[x] - Cos[x^2]  *)


The same approach works if there are pure derivatives in the expression, such as when using a known solution to factor and reduce the order of a linear ODE (in the example below, $$y= x$$ is a solution, so factor $$y = x \int u \,dx$$ to get a first-order equation for $$u$$):

y''[x] + x^2 y'[x] - x y[x] == 0 /. {y -> (# Integrate[u[#], #] &)} // Simplify
(*  (2 + x^3) u[x] + x u'[x] == 0  *)


It is possible to force Mathematica handle head operations of this kind as follows:

Needs["GeneralUtilities"];

Unprotect[Times];

(-h_?NumericFunctionQ)[arg__] := -h[arg];

Protect[Times];


Now:

Sin[x] /. Sin -> -Cos

-Cos[x]


Standard caveats of modifying built-ins apply.

• I think this suffers from serious IC (inadequate caveatation). I'll bet you didn't even know that disease existed... Commented Nov 17, 2018 at 16:16
• @Daniel lol -- yup, that's new to me. I expect a performance hit, perhaps serious, but I didn't think of anything this would break outright. Can you enlighten me? Commented Nov 17, 2018 at 21:05
• Anything that messes with Plus, Times, or Power is potentially problematic, insofar as much of the internal uses do not go through top-level code (for reasons of efficiency) and hence might give results other than expected. Commented Nov 17, 2018 at 21:08
• @Daniel Is that true even for symbolic evaluations such as this? I am aware that packed arrays bypass top level definitions, but I didn't anticipate that here. Commented Nov 17, 2018 at 21:54
• There is a considerable amount of usage of those functions in internal code, even in the absence of packed arrays. So yes. Also, unprotecting and changing those functions can hasten the End of Days. Did I neglect to mention that? Silly of me. Commented Nov 17, 2018 at 22:25

The following works :

Sin[x] /. Sin[xx_] -> -Cos[xx]


-Cos[x]

It is safer to use (if x is allready defined) :

Sin[x] /. Sin[xx_] :> -Cos[xx]


The following seems to work too :

Sin[x] /. Sin -> Composition[Minus,Cos]


though I have never used this latest form.

• any advice from anybody about the latest form ? Commented Nov 15, 2018 at 20:21
• I think the Composition version (shorthand is Minus @* Cos) is good. Commented Nov 15, 2018 at 20:27
• The form with Composition certainly works if x is just a symbol (and you can use the shortcut version: Sin[x] /. Sin -> Minus@*Cos), so that Sin[x] remains unevaluated before the replacement is done. However, if x has a numeric value, as in Sin[Pi/2], then it won't work, because Sin[Pi/2] will be evaluated to give 1 and then there's nothing left in which to replace Sin by anything! Commented Nov 15, 2018 at 20:28
• @murray: isn't that a general problem with all of these replacement rules? If Sin[arg] evaluates to an exact value on the left side of /., none of the above answers will work because none of the replacement rules will ever see the Sin` function. The only way to handle this problem, is by using some form of holding. Commented Nov 16, 2018 at 11:43
• @SjoerdSmit: Yes, precisely the problem! Commented Nov 16, 2018 at 19:52