8

Why doesn't this work?

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> Sin[x]/Cos[x]

I get back:

(* (1 - Tan[x])/(-Cos[x] + Sin[x]) *)

I also tried:

(1 - Tan[x])/(Sin[x] - Cos[x]) /. {Tan[x] -> Sin[x]/Cos[x]}

Got the same reply.

I also tried:

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> (Sin[x]/Cos[x])

Still got the same answer.

And though this is an incorrect substitution, it works:

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> Sin[x] + Cos[x]

Update: Ok, here is what I tried using suggestions from folks below. I started with:

(1 - tan[x])/(sin[x] - cos[x]) /. tan[x] -> sin[x]/cos[x]

Which gave me:

(* (1 - sin[x]/cos[x])/(-cos[x] + sin[x]) *)

Then:

List @@ %

Which gave me:

(* {1/(-cos[x] + sin[x]), 1 - sin[x]/cos[x]} *)

Then I multiplied numerator and denominator by cos[x]:

%*{1/cos[x], cos[x]}

Which gave me:

(* {1/(cos[x] (-cos[x] + sin[x])), cos[x] (1 - sin[x]/cos[x])} *)

Then I did an expand:

% // Expand

Which gave me:

(* {1/(cos[x] (-cos[x] + sin[x])), cos[x] - sin[x]} *)

I was surprised that the denominator (first element in list) did not expand. Next:

Times @@ %

Which gave me:

(* (cos[x] - sin[x])/(cos[x] (-cos[x] + sin[x])) *)

Then:

% // Cancel

Which gave me:

(* -(1/cos[x]) *)

Then:

% /. cos[x] -> Cos[x]

Which gave me a final answer:

(* -Sec[x] *)

I actually didn't use the % sign. Rather, I used Shift-Cmd-L (Shift-Ctrl-L on Windows) to replay the output of the previous step, but thought that would crowd things a bit if I put it in here.

Interesting. How would other folks handle this process? It would be interesting to hear.

march
  • 23,399
  • 2
  • 44
  • 100
David
  • 14,883
  • 4
  • 44
  • 117
  • Try Tan -> (Sin[#]/Cos[#] &). – J. M.'s missing motivation Dec 13 '15 at 05:09
  • I don't know if this is what you're looking for, but (1 - Tan[x])/(Sin[x] - Cos[x]) // Simplify gives -Sec[x]. –  Dec 13 '15 at 05:11
  • @Rahul I am looking at how to apply pencil and paper type steps to simplify the expression. I am aware of the //Simplify expression. – David Dec 13 '15 at 05:13
  • @J.M. Gave your suggestion a try, but it also doesn't work. – David Dec 13 '15 at 05:16
  • 1
    Your replacement is working, I think. The problem is that Mathematica automatically simplifies Sin[x]/Cos[x] to Tan[x]. If you're going to do step-by-step math, perhaps consider using undefined symbols like sin[x] and cos[x]. – march Dec 13 '15 at 05:18
  • 1
    Ah, I forgot the auto-simplification! (@march, that's the answer I believe.) You'll probably want to use Activate[]/Inactivate[] here. – J. M.'s missing motivation Dec 13 '15 at 05:21
  • What are trying to accomplish by this substitution? You can get the simplification -Sec[x] directly from (1 - Tan[x])/(Sin[x] - Cos[x]) // Simplify. I would think that is more useful for any further computation. – m_goldberg Dec 13 '15 at 05:40
  • @m_goldberg I added an update to my original post of what I was trying to do. – David Dec 13 '15 at 06:18
  • Just in case you didn't know: Numerator[]/Denominator[] is the kosher way of extracting parts of a rational expression. – J. M.'s missing motivation Dec 13 '15 at 06:25
  • I probably don't understand the question: expr = (1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> Sin[x]/Cos[x]; and TrigReduce[expr] gives -Sec[x]. – Syed Mar 08 '24 at 16:05

3 Answers3

24

Mathematica auto simplifies simple trig expressions like these, but you can turn off this setting via SystemOptions:

SetSystemOptions["SimplificationOptions" -> "AutosimplifyTrigs" -> False];

Now we see your change is left untouched without the need of HoldForm and friends.

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> Sin[x]/Cos[x]
(1 - Sin[x]/Cos[x])/(-Cos[x] + Sin[x])
Greg Hurst
  • 35,921
  • 1
  • 90
  • 136
8

Mathematica automatically simplifies the Sin[x]/Cos[x] after ReplaceAll (/.). Defer is a useful function for preventing that:

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> Defer[Sin[x]/Cos[x]]

(*(1 - Sin[x]/Cos[x])/(-Cos[x] + Sin[x])*)

HoldForm would also work:

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> HoldForm[Sin[x]/Cos[x]]

(* (1 - Sin[x]/Cos[x])/(-Cos[x] + Sin[x]) *)

Note that the head HoldForm stays in the expression (but not displayed).

InputForm[(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> HoldForm[Sin[x]/Cos[x]]]

(* (1 - HoldForm[Sin[x]/Cos[x]])/(-Cos[x] + Sin[x]) *)
JungHwan Min
  • 4,664
  • 1
  • 22
  • 36
5

Mathematica automatically simplifies Sin[x]/Cos[x] to Tan[x]. Consider:

rules = Tan[x] -> Sin[x]/Cos[x]
(* Tan[x]

Using RuleDelayed doesn't help:

Tan[x] /. Tan[x] :> Sin[x]/Cos[x]
(* Tan[x] *)

Instead, since you are doing step-by-step manipulations anyway, just use non-built-in symbols:

(1 - tan[x])/(sin[x] - cos[x]) /. {tan[x] -> sin[x]/cos[x]} // Simplify
(* -(1/cos[x]) *)

Alternatively, as suggested by J.M., we can use Inactivate:

expr = Inactivate[(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] :> Sin[x]/Cos[x], Tan | Sin | Cos] // Simplify
expr // Activate
(* -(1/Cos[x]) *)
(* -Sec[x] *)

We specify that only Tan, Sin, and Cos should be Inactivated by using the pattern Tan | Sin | Cos (Alternatives[Tan, Sin, Cos]).

march
  • 23,399
  • 2
  • 44
  • 100
  • 3
    @JHM. It depends, I think. The nice thing about Inactivate here is that you can continue to use all of Mathematica's simplification functions (like Simplify) on the expression. The only thing that changes is the fact that Tan, Sin, and Cos are essentially treated as non-built-in symbols. (That said, I've never used Defer, so I don't know its power.) – march Dec 13 '15 at 05:32
  • 1
    With respect to Defer[]: it renders whatever it's enclosing inert to evaluation, up until you copy and paste it into another cell. So, Defer[Sin[x]/Cos[x]] will give Sin[x]/Cos[x] as output, but if you copy this output into a new cell and evaluate, you get Tan[x]. In contrast, trying the same experiment with HoldForm[] will not give Tan[x]; you need ReleaseHold[] for that. (It admittedly is all becoming a bit confusing at this point.) – J. M.'s missing motivation Dec 13 '15 at 05:40
  • @march. Oh, I didn't realize that! Defer simply does not evaluate the expression altogether, so you cannot apply simplifications inside it; it may not be useful in some cases. – JungHwan Min Dec 13 '15 at 05:50