0

I would like to avoid multiplying explicitely by the denominator, because I'm working with huge fractions.

Consider

f[x_,y_] := Cos[x - y]/(Sin[x] Cos[y])+ 1/Sin[x];

Mathematica transforms this into Csc[x] + Cos[x - y] Csc[x] Sec[y] which I'd like to avoid: my goal is precisely to get rid of the singularities. So I used

$PrePrint = # /. {Csc[z_] :> 1/Defer@Sin[z], Sec[z_] :> 1/Defer@Cos[z]} &;

but then Together[f[x,y]] does not put f with a common denominator, and Numerator just returns f[x,y].

So how can I get the numerator of f[x,y]? It should be Cos[x - y] + Cos[y].

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
anderstood
  • 14,301
  • 2
  • 29
  • 80

2 Answers2

1

From the documentation:

$PrePrint is applied after Out[n] is assigned, but before the output result is printed.

Try setting

$Post = # /. {Csc[z_] :> 1/Defer@Sin[z], Sec[z_] :> 1/Defer@Cos[z]} &;
Cos[x - y]/(Sin[x] Cos[y])
% // Numerator
LLlAMnYP
  • 11,486
  • 26
  • 65
  • This forces to print the output, which may be a nuisance if the expression takes dozens of lines. I edited the question, introducing the sum, for which the suggested solution fails. – anderstood Nov 02 '16 at 18:07
  • 1
    Then instead of $Post, $PrePrint, etc. just use the appropriate transformation rule, e.g. g = # /. {Csc[z_] :> 1/Defer@Sin[z], Sec[z_] :> 1/Defer@Cos[z]} &; and f[x,y] // g // Together // Numerator – LLlAMnYP Nov 02 '16 at 18:35
  • I see, thank you. A last thing: If I define h[x_,y_] = f[x,y] // g // Together // Numerator, then h[1.1,2.2] does not evaluate Cos[2.2] because of Defer. So I changed g to g = # /. {Csc[z_] :> 1/Hold@Sin[z], Sec[z_] :> 1/Hold@Cos[z]} &; and defined h[x_,y_] = f[x,y] // g // Together // Numerator // Releasehold. It's seems to work, do you see any problem with that? – anderstood Nov 02 '16 at 18:52
  • @anderstood At a glance, no problem, but I guess, some testing would be needed. I found, that the original approach with $Post results in nested Times[..., Times[...]] expressions that are cleaned up only when displayed. It's hard to say, whether you'll run into more similar problems for more complicated expressions. Try it out with your actual use case and report back if you run into further problems. – LLlAMnYP Nov 03 '16 at 07:41
1

You can change a system setting as first pointed out by Chip:

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

Then:

expr = Cos[x-y]/(Sin[x] Cos[y]) + 1/Sin[x]
Numerator @ Together[expr]

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

Cos[x - y] + Cos[y]

Carl Woll
  • 130,679
  • 6
  • 243
  • 355