4
$\begingroup$

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]
$\endgroup$
4
  • 1
    $\begingroup$ 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. $\endgroup$
    – Ray Shadow
    Commented Apr 22, 2017 at 22:46
  • $\begingroup$ 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. $\endgroup$
    – Nasser
    Commented Apr 22, 2017 at 22:51
  • $\begingroup$ 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! $\endgroup$
    – Saesun Kim
    Commented Apr 22, 2017 at 22:54
  • $\begingroup$ 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. $\endgroup$
    – bill s
    Commented Apr 22, 2017 at 23:05

2 Answers 2

2
$\begingroup$

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

$\endgroup$
0
2
$\begingroup$

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.

$\endgroup$
0

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.