I have the following expression with many Sign
functions:
phase = 2 Sign[t] (1/2 (-1 + e) - 1/2 (1 + e) Sign[t]) + (1/2 (-1 + e1) +
1/2 (1 + e1) Sign[d - t1]) (-1 + Sign[t1]) - (1/2 (-e + e1) +
1/2 (e + e1) Sign[d + t - t1]) (-1 +
Sign[-t + t1]) - (1/2 (-1 + e2) + 1/2 (1 + e2) Sign[d - t2]) (-1 +
Sign[t2]) + (1/2 (-e + e2) + 1/2 (e + e2) Sign[d + t - t2]) (-1 +
Sign[-t + t2]) +
2 (1/2 (-e1 + e2) + 1/2 (e1 + e2) Sign[t1 - t2]) Sign[-t1 + t2]
All variables are assumed to be real. There are 10 different arguments inside Sign
, namely,
t, d-t1, t1, d+t-t1, -t+t1, d-t2, t2, d+t-t2, -t+t2, t1-t2
I can assume that all of the above values are nonzero, hence Sign
function always returns 1 or -1. Then, the above expression exp
can be written in piecewise format with $2^{10} = 1024$ cases. How can I convert into this form?
I tried as follows. First, borrowing the result from Converting HeavisideTheta[]s and Sign[]s functions to a single Piecewise[], define a function
ToPiecewise[f_] :=
PiecewiseExpand[
f /. {Sign[x_] :>
Piecewise[{{1, x > 0}, {-1, x < 0}, {Indeterminate, x == 0}}]}]
Since I am only interested when the argument of Sign
is nonzero, I put Indeterminate
for x==0
. This ToPiecewise
function converts single Sign
in piecewise format.
By doing
Sum[Simplify[PiecewiseExpand[ToPiecewise[phase[[i]]]]], {i, 1, 6}]
I obtain the following:
The result correct, but there are some problems.
Anyway, I should some over these terms and collapse into a single piecewise function. However,
% // PiecewiseExpand
takes huge amount of time, hence making me gave up.
- Some results in the picture are redundant. For example, why one needs the case marked in red? I think removing these redundancy is the key to boost up the performance, but don't know how to do that.
True
: The conditions in the pieces are not exhaustive ifx
is complex. TryPiecewiseExpand[expr, assumptions, Reals]
(useassumptions = True
if no assumptions). $\endgroup$ToPiecewise[expr_] := expr /. Sign[x_] -> Which[x > 0, 1, x < 0, -1, True, Indeterminate];
and thenPiecewiseExpand[phase // ToPiecewise, True, Reals, Method -> {"ConditionSimplifier" -> Reduce}]
$\endgroup$PiecewiseExpand[ToPiecewise[phase[[6]]], True, Reals]
gives the same redundant result. $\endgroup$PiecewiseExpand
withMethod -> {"OrderlessConditions" -> True}
. $\endgroup$