4
$\begingroup$

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!

$\endgroup$
1
  • 2
    $\begingroup$ Sin[x] /. Sin[aa_]:>-Cos[aa] ?? $\endgroup$ Commented Nov 16, 2018 at 15:05

4 Answers 4

4
$\begingroup$

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]

$\endgroup$
4
  • 1
    $\begingroup$ The second expression worked perfectly to solve my problem. Thank you, Bill! $\endgroup$ Commented Nov 15, 2018 at 20:29
  • 1
    $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Commented Nov 16, 2018 at 11:07
  • $\begingroup$ Bill, and what about Sin[x]+Sin[f[x]] $\endgroup$
    – ayr
    Commented Nov 2, 2022 at 12:40
  • $\begingroup$ @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? $\endgroup$
    – Bill
    Commented Nov 4, 2022 at 7:12
4
$\begingroup$

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  *)
$\endgroup$
2
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ I think this suffers from serious IC (inadequate caveatation). I'll bet you didn't even know that disease existed... $\endgroup$ Commented Nov 17, 2018 at 16:16
  • $\begingroup$ @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? $\endgroup$
    – Mr.Wizard
    Commented Nov 17, 2018 at 21:05
  • $\begingroup$ 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. $\endgroup$ Commented Nov 17, 2018 at 21:08
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Commented Nov 17, 2018 at 21:54
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Nov 17, 2018 at 22:25
2
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ any advice from anybody about the latest form ? $\endgroup$
    – andre314
    Commented Nov 15, 2018 at 20:21
  • 1
    $\begingroup$ I think the Composition version (shorthand is Minus @* Cos) is good. $\endgroup$
    – Carl Woll
    Commented Nov 15, 2018 at 20:27
  • $\begingroup$ 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! $\endgroup$
    – murray
    Commented Nov 15, 2018 at 20:28
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Nov 16, 2018 at 11:43
  • $\begingroup$ @SjoerdSmit: Yes, precisely the problem! $\endgroup$
    – murray
    Commented Nov 16, 2018 at 19:52

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.