4

I was trying to extract the parameter of the phase term of the sine function, and I found the problem that Mathamtica automatically try to change the sign of the sine function.

I got negative number or positive amplitude from the sine function. I don't really care what sine I am getting, but I want to get is consistent sign. Do I have a way to fix the sign of the amplitude or frequency part of the sine function?

example

2 Sin[2 x - 1]
2 Sin[-2 x - 1]
-2 Sin[-2 x - 1]
-2 Sin[2 x - 1]

Current output

-2 Sin[1 - 2 x]
-2 Sin[1 + 2 x]
2 Sin[1 + 2 x]
2 Sin[1 - 2 x]

Desired output

2 Sin[2 x - 1]
2 Sin[-2 x - 1]
2 Sin[1 + 2 x]
2 Sin[1 - 2 x]
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Saesun Kim
  • 1,810
  • 13
  • 24
  • 1
    Built-in properties of Sin are difficult to change. Could you provide some details about a more general problem you want to solve? It is probably possible to solve it in a more simple way. – Ray Shadow Apr 22 '17 at 22:46
  • Mathematica has many such immediate simplification it does. Myself, I wish it does not do that unless the user explicitly ask for simplification or re-write. But it is not a good idea to try to change this behavior. not worth the trouble. – Nasser Apr 22 '17 at 22:51
  • thank you for the comment, I was just wondering there is any easy way to turn off, but I guess I can just extract the number from the function, and convert it manually by code. thank you! – Saesun Kim Apr 22 '17 at 22:54
  • In terms of extracting the phase term, this is fundamentally not uniquely defined anyway... -180 degrees and +180 degrees are the same thing. So there is an inherent ambiguity in the representation. – bill s Apr 22 '17 at 23:05

2 Answers2

2

This implementation ensures that Sin would not evaluate immediately.

ClearAll@times;
SetAttributes[times, HoldAllComplete];
times /: times[Times[x_, Sin[y_]]] /; Positive[x] := x HoldForm[Sin[y]];
times /: times[Times[x_, Sin[y_]]] := (-x) Sin[Times[-1, y]];

times[2 Sin[2 x - 1]]
times[2 Sin[-2 x - 1]]
times[-2 Sin[-2 x - 1]]
times[-2 Sin[2 x - 1]]

(*

2 \!\(\*TagBox[RowBox[{"Sin", "[",RowBox[{RowBox[{"2", " ", "x"}], "-", "1"}], "]"}],HoldForm]\)
2 \!\(\*TagBox[RowBox[{"Sin", "[",RowBox[{RowBox[{RowBox[{"-", "2"}], " ", "x"}], "-", "1"}], "]"}],HoldForm]\)
2 Sin[1 + 2 x]
2 Sin[1 - 2 x] *)

enter image description here

Ali Hashmi
  • 8,950
  • 4
  • 22
  • 42
2

If you only want to print the Sin with the arguments unchanged you can use Defer or HoldForm:

Defer[Sin[2x + 1]]
HoldForm[Sin[2x + 1]]
(* Sin[2x + 1] *)
(* Sin[2x + 1] *)

Another possibility is to use Inactivate to inactivate either the whole expression or just the Plus symbol:

enter image description here

Of course, each of the above has a very specific usage and is more or less suitable depending on how you want to handle the expression afterwards.

glS
  • 7,623
  • 1
  • 21
  • 61