3
$\begingroup$

Suppose an output of Reduce[] function looks like this:

x2 > 0 && ((0 < x1 < x2 && y2 > 0 && 0 < y1 < y2 && z1 > 0 && z2 > z1 && t1 > c1 && 0 < t2 < c2) || (x1 > x2 && y2 > 0 && y1 > y2 && z1 > 0 && z2 > z1 && t1 > 0 && t2 > c2))

I.e., Boolean formulae with inequalities of two types:

1) $x>a$, to which we associate $\left(\frac{1}{x-a}\right)$,

2) $a<x<b$, to which we associate $\left(\frac{1}{x-a}-\frac{1}{x-b}\right)$.

I would like to automatically create from the above output the following formula, which preserves all Boolean logic and for which && means multiplied by and || means add the corresponding piece:

$\frac{1}{x_2}\Big(\big((\frac{1}{x_1}-\frac{1}{x_1-x_2})\frac{1}{y_2}(\frac{1}{y_1}-\frac{1}{y_1-y_2})\frac{1}{z_1}\frac{1}{z_2-z_1}\frac{1}{t_1-c_1}(\frac{1}{t_2}-\frac{1}{t_2-c_2})+\frac{1}{x_1-x_2}\frac{1}{y_2}\frac{1}{y_1-y_2}\frac{1}{z_1}\frac{1}{z_2-z_1}\frac{1}{t_1}\frac{1}{t_2-c_2}\big)\Big)$

Is there an easy way to do it, given that the number of parameters $x,y,z,t,\dots$, as well as the number of nested parentheses and boolean symbols ( smthng && ( smthng || smthng ) && ( smthng && smthng && smthng ))) can be anything?

UPDATE: Also, sometimes the output contains an equality like this one:

x2==x1-a

buried somewhere inside of the parentheses:

x2 > 0 && ((x2==x1-a || 0 < x1 < x2 && y2 > 0 && 0 < y1 < y2 && z1 > 0 && z2 > z1 && t1 > c1 && 0 < t2 < c2) || (x1 > x2 && y2 > 0 && y1 > y2 && z1 > 0 && z2 > z1 && t1 > 0 && t2 > c2))

which I would like to completely ignore and not to include in the final formula. What replacement rule should I use then?

$\endgroup$

1 Answer 1

4
$\begingroup$

Use some replacement rules. We make a function,

toBooleanFunction[expr_] := Simplify@expr //.
  {a_ > b_ :> 1/(a - b), a_ < x_ < b_ :> 1/(x - a) - 1/(x - b), a_ < b_ :> -1/(a - b), a_ > x_ > b_ :> -1/(x - a) + 1/(x - b), _Equal :> (## &[])} //. 
  {And -> Times, Or -> Plus}

Then:

Reduce[{x1 - x2 > a, a > 0, x1 > 0, x2 > 0, x3 == 0}, {x1, x2, x3}]
% // toBooleanFunction
(* -(1/(a (-a + x1) x2 (a - x1 + x2))) *)
$\endgroup$
10
  • $\begingroup$ Thank you! Could you also address the question I've added in the update above? $\endgroup$ Oct 25, 2016 at 22:27
  • $\begingroup$ @Physicsworks. See updates. $\endgroup$
    – march
    Oct 25, 2016 at 22:31
  • $\begingroup$ Thanks! Also, how would I apply these replacement rules without writing the full expression like you did in line 1 above and applying ReplaceAll to the expr in the 2nd line, because sometimes the expression contains thousands of terms. In other words, how would I apply these rules to the output of Reduce[] itself? Reduce[] ./ {rules} doesn't work. $\endgroup$ Oct 26, 2016 at 2:23
  • $\begingroup$ @Physicsworks I don't really understand your question. I don't see why that shouldn't work. $\endgroup$
    – march
    Oct 26, 2016 at 2:30
  • $\begingroup$ for instance, Reduce[{x1 - x2 > a, a > 0, x1 > 0, x2 > 0}, {x1, x2}] /. {rules} , where {rules} are the rules in line 2 of your answer above, doesn't work. $\endgroup$ Oct 26, 2016 at 2:33

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.