Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The following Maple code can simplify abs(sin(x)) into -sin(x) given the condition -3Pi/4 < x < -Pi/2

simplify(abs(sin(x))) assuming -3*Pi < x, x < -5*Pi/2;

I tried what I thought would be the equivalent Mathematica code:

FullSimplify[Abs[Sin[x]], Assumptions -> -3 Pi/4 < x < -Pi/2] 

but Mathematica does not give the answer I expected.

Another related example.

Starting with Sqrt[1 + Sin[x]] + Sqrt[1 - Sin[x]] and the condition -2Pi < x < -3Pi/2, I want to get -2 Cos[x/2], Here are some of things I tried:

Sqrt[1 + Sin[x]] + Sqrt[1 - Sin[x]] /. Sqrt[x_] :> Sqrt[TrigFactor[x]]
Refine[%, Assumptions -> -2 Pi < x < -3 Pi/2]
FullSimplify[%, Assumptions -> -2 Pi < x < -3 Pi/2]
share|improve this question
3  
Note that Mathematica considers Abs[Sin[x]] to be simpler than Times[-1, Sin[x]] (see e.g. this and this). That aside, there does seem to be a problem with simplifying the sign of trig functions. Compare Simplify[Sign[Sin[x]], 0 < x < Pi] and Simplify[Sign[Sin[x]], -Pi < x < 0] –  Simon Woods Apr 22 at 9:34
    
However, f[e_] := 100 Count[e, _Abs | _Sqrt, {0, Infinity}] + LeafCount[e]; FullSimplify[Abs[Sin[x]], -3 Pi/4 < x < -Pi/2, ComplexityFunction -> f] doesn't seem to work either. Did I miss anything? –  Yi Wang Apr 22 at 9:46
1  
If I use ComplexityFunction to forbid Abs, the result is Sqrt[Sin[x]^2]. If further forbid Power, the result is a piecewise function. If further forbid Piecewise, the result returns to Abs[Sin[x]]. So it appears like it's not a complexity function problem. –  Yi Wang Apr 22 at 9:53
3  
@SimonWoods : Furthermore, FullSimplify[Abs[Sin[x]] == -Sin[x], Assumptions -> -3 Pi/4 < x < -Pi/2] does not simplify it either. Thus this should not be a complexity function issue. –  Yi Wang Apr 22 at 9:56
1  
@YiWang, I agree this is not a complexity function issue - I just wanted to point out that the first example would not "simplify" to the desired result even if Mathematica handled the assumptions properly. –  Simon Woods Apr 22 at 10:52

3 Answers 3

To address your first question, this:

    Simplify[Abs[Sin[x]], Assumptions -> {x >= \[Pi], x < 2 \[Pi]}, 
 ComplexityFunction -> (StringLength[ToString[#]] &)]

(*    -Sin[x]    *)

does the job. It is just since

StringLength[ToString[#]] &[Abs[Sin[x]]]
(*  11  *)

StringLength[ToString[#]] &[-Sin[x]]
(*   7   *)

It is more difficult with your second question. This is your initial expression

expr1 = Sqrt[1 + Sin[x]] + Sqrt[1 - Sin[x]];

Here we transform the terms under the radicals:

 expr2 = MapAt[TrigFactor, expr1, {{1, 1}, {2, 1}}]
(*   Sqrt[2] Sqrt[Sin[\[Pi]/4 - x/2]^2] + 
 Sqrt[2] Sqrt[Sin[\[Pi]/4 + x/2]^2]   *)

Now let us introduce a complexity function:

 Clear[cf];
cf[e_] := (Count[e, _Abs, Infinity]);

and try to simplify each radical separately:

    expr3 = MapAt[
  Simplify[#, -2 \[Pi] < x < -3 \[Pi]/2, ComplexityFunction -> cf] &, 
  expr2, {{1, 2}, {2, 2}}]

The outcome is strange:

(*      Sqrt[2] Abs[Sin[\[Pi]/4 + x/2]] - Sqrt[2] Sin[\[Pi]/4 - x/2]     *)

The Abs is removed in one case, but did not disappear in the other. It is even more strange taking into account that Sin[\[Pi]/4 + x/2] in the interval in question is strictly negative. One can make sure of this by plotting it:

 Plot[{Sin[\[Pi]/4 + x/2], 
  Abs[Sin[\[Pi]/4 + x/2]]}, {x, -2 \[Pi], -3 \[Pi]/2}, 
 PlotStyle -> {{Blue, Thickness[0.005]}, {Red, Thickness[0.005]}}, 
 PlotRange -> {-1.2, 1.2}]

enter image description here Here Sin[\[Pi]/4 + x/2]is shown in blue and Abs[Sin[\[Pi]/4 + x/2]]- in red. It is not clear what this complexity function does at all, since evaluation of this:

    cf[Abs[Sin[\[Pi]/4 + x/2]]]
cf[Abs[Sin[\[Pi]/4 - x/2]]]

returns 0 in both cases. However, it works in the one case in the expression above. I do not understand it, and would be grateful for explanations.

Nevertheless, this operation did not work as expected, but we know what it should have done, let us make a substitution:

  expr4 = expr3 /. Abs[a_] -> -a // Simplify

(*    -2 Cos[x/2]     *)
share|improve this answer

Obviously what a person thinks is simplified and what a given CAS thinks is simplified will not always be the same. This probably happens more often with functions like the trigonometric functions that admit so many identities. Often it does not matter that much, but when it does, a certain amount of personal intervention may be required.

One hump to get over in the first example Sqrt[1 + Sin[x]] + Sqrt[1 - Sin[x]] is that the expression has to go through several transformations before it gets simpler (according to Mathematica's measure of simple).

Another is simplifying the Piecewise expansion of Abs, which I (have to?) do using the same Solve method of chyaong

Finally, we can construct a complexity function that "has it in" for Abs, so that expressions without Abs tend to be selected.

Here are transformation functions for getting over the first two humps:

trigXFns = Sequence[
   Replace[#,
     e1_Piecewise :> 
      Module[{y}, 
       Replace[y /. First@Solve[y == e1 && $Assumptions], 
        e2_ConditionalExpression :> Simplify[e2]]
       ]] &,
   Composition[Simplify, ExpToTrig, Factor, PowerExpand, Factor, 
    TrigToExp]
   ];

Here is a simple way to avoid Abs when possible. The coefficient in front of Count controls how strongly to try to eliminate Abs.

LeafCount[#] + 20 Count[#, _Abs, {0, Infinity}] &

There is yet another issue in the OP's examples, namely, assumptions. The assumptions need to be passed to the transformation functions. This does not happen with the Assumptions option to Simplify. Therefore, we have to use Assuming (or Block[{$Assumptions = ...).

Assuming[-3 Pi/4 < x < -Pi/2,
 Simplify[Abs[Sin[x]],
  TransformationFunctions -> {Automatic, trigXFns},
  ComplexityFunction -> (LeafCount[#] + 20 Count[#, _Abs, {0, Infinity}] &)]
 ]
(* -Sin[x] *)

Assuming[-2 Pi < x < -3 Pi/2,
 Simplify[Sqrt[1 - Sin[x]] + Sqrt[1 + Sin[x]],
  TransformationFunctions -> {Automatic, trigXFns},
  ComplexityFunction -> (LeafCount[#] + 20 Count[#, _Abs, {0, Infinity}] &)]
 ]
(* 2 Cos[x/2] *)
share|improve this answer
Solve[{y==Sign[#]#&[Sin[x]],-3 Pi/4<x<-Pi/2},{y}]
Solve[{y==PiecewiseExpand[Abs@Sin[x],Reals],-3 Pi/4<x<-Pi/2},y]

Simplify[PiecewiseExpand[Abs@Sin@x,Reals],TransformationFunctions->{Automatic,Reduce[#&&-3Pi/4<x<-Pi/2,x]&}]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.