8
$\begingroup$

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.

$\endgroup$
10
  • $\begingroup$ Try Tan -> (Sin[#]/Cos[#] &). $\endgroup$ Dec 13, 2015 at 5:09
  • $\begingroup$ I don't know if this is what you're looking for, but (1 - Tan[x])/(Sin[x] - Cos[x]) // Simplify gives -Sec[x]. $\endgroup$
    – user484
    Dec 13, 2015 at 5:11
  • $\begingroup$ @Rahul I am looking at how to apply pencil and paper type steps to simplify the expression. I am aware of the //Simplify expression. $\endgroup$
    – David
    Dec 13, 2015 at 5:13
  • 1
    $\begingroup$ 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]. $\endgroup$
    – march
    Dec 13, 2015 at 5:18
  • 1
    $\begingroup$ Ah, I forgot the auto-simplification! (@march, that's the answer I believe.) You'll probably want to use Activate[]/Inactivate[] here. $\endgroup$ Dec 13, 2015 at 5:21

3 Answers 3

24
$\begingroup$

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])
$\endgroup$
2
  • $\begingroup$ Very nice. I seem to recall other questions where this option would be useful. (Just not sure how to find them.) $\endgroup$
    – Michael E2
    Dec 13, 2015 at 10:50
  • $\begingroup$ @MichaelE2 One example is: Mathematica Sec and Csc, but J.M. has already given an answer :). $\endgroup$ Dec 15, 2015 at 9:10
8
$\begingroup$

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]) *)
$\endgroup$
1
  • $\begingroup$ Defer[] or HoldForm[] would be the pre-version 10 approach, so it's good to have this here. $\endgroup$ Dec 13, 2015 at 5:31
5
$\begingroup$

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]).

$\endgroup$
3
  • 3
    $\begingroup$ @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.) $\endgroup$
    – march
    Dec 13, 2015 at 5:32
  • 1
    $\begingroup$ 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.) $\endgroup$ Dec 13, 2015 at 5:40
  • $\begingroup$ @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. $\endgroup$ Dec 13, 2015 at 5:50

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.