0
$\begingroup$

I have an expression that involves many Sign functions. I want to expand this in the piecewise format.

My function is:

exp = 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]

Here, all variables can be regarded as real.

Using the answer provided in Converting HeavisideTheta[]s and Sign[]s functions to a single Piecewise[], I also borrow a user-defined function:

ToPiecewise[f_] := 
 PiecewiseExpand[
  f /. {Sign[x_] :> Piecewise[{{1, x >= 0}, {-1, x < 0}}]}]

What I want to eavluate is

ToPiecewise[exp]

but this is extremely slow! (Furthermore, aborting (Alt + .) does not work and I had to quit the kernel.) Surprisingly, if I consider a single term, for example the first one and evaluate

ToPiecewise[2 Sign[t] (1/2 (-1 + e) - 1/2 (1 + e) Sign[t])]

then the answer comes immediately. This happens for all other single terms. Why this strange behavior happen?

$\endgroup$

1 Answer 1

3
$\begingroup$

Consider:

exp = 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]

ToPiecewise[exp[[1]]]
ToPiecewise[exp[[2]]]
ToPiecewise[exp[[3]]] ...

![enter image description here

All the terms have different conditions. This makes the overall condition extremely complicated.

A way around this is to give every term its own conditions like:

ToPiecewise /@ exp

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks for your answer. However, it is sad since what I want is to simplify the whole expression into "single" piecewise command. I think this is not too hard, because the total case is $2^8=256$ which is not too large, where 8 comes from the different expressions inside Sign. $\endgroup$
    – eigenvalue
    Oct 9, 2021 at 15:54
  • $\begingroup$ Counting and multiplying the different conditions gives 7840 cases. $\endgroup$ Oct 9, 2021 at 18:35
  • $\begingroup$ I think your 7840 is obtained by $7840=2\times 4\times 4\times 5\times 7\times 7$. However, there is redundancy in some of the 6 terms. For example, for the 4th term, the condition t1-t2 >=0 and True is redundant. $\endgroup$
    – eigenvalue
    Oct 10, 2021 at 2:15
  • $\begingroup$ Oh, I also forgot to mention that I am not interested when Sign evaluates to Sign[0]. In other words, I am only interested when t != 0, t1 - t2 != 0, t2 != 0, t1 != 0, d - t1 != 0, d - t2 != 0, -t + t1 != 0, d + t - t1 != 0, d + t - t2 != 0, -t + t2 != 0. Then, the total case is 256. $\endgroup$
    – eigenvalue
    Oct 10, 2021 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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