MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Simplifying a Boolean Expression

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68802] Re: Simplifying a Boolean Expression
  • From: Peter Pein <petsie at dordos.net>
  • Date: Sat, 19 Aug 2006 00:41:05 -0400 (EDT)
  • References: <ec3s0e$2v2$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hebhardt, Jon schrieb:
> Can Mathametica be used to simplify the following type of logic typically
> found in programming applications, and if so, how do I go about setting it
> up?
> 
>  
> 
> EXAMPLE
> 
> Variables A and T can have the values "none", "dense", "sparse"
> 
> The variable X will be assigned a value of either "onepass" or "twopass"
> based on the following logic:
> 
>  
> 
> IF
> 
> [(A="none") AND (T="none")] OR [(A="dense") OR (A="sparse") AND (T="none")]
> OR [(A="dense") AND (T="dense")] THEN X = "onepass"
> 
> ELSE IF
> 
> [{A="sparse") AND (T="sparse")] OR [(A="sparse") AND (T="dense")] THEN X =
> "twopass"
> 
>  
> 
> I want to be able to express this type of problem in Mathematica and have it
> return the simplest logic for assigning a value to X.  
> 
>  
> 
> Regards,
> 
> Jon
> 
>  
> 
> Work:  401-752-3821
> 
> Cell:    508-873-9780
> 
> 

Hi Jon,

First, tell Mathematica about the variables:

In[1]:= $Assumptions = Join[Unequal @@@ Subsets[{n, s, d}, {2}],
   Or @@@ Outer[Equal, {a, t}, {n, s, d}]]
Out[1]= {n != s, n != d, s != d, a == n || a == s || a == d, t == n || t 
== s || t == d}

Then use Piecewise[] to declare the logical value:
In[2]:= f = Piecewise[
   {{onepass,
     (a == n && t == n) || (a == d || (a == s && t == n))
     || (a == d && t == d)},
   {twopass, (a == s && t == s) || (a == s && t == d)}},
   Indeterminate];

Simplify it
In[3]:= sf = FullSimplify[f]
Out[3]= Piecewise[{{onepass, (t == n && (n == a || s == a)) || d == a},
    {twopass, s == a && (t == d || t == s)}}, Indeterminate]
It's not nice to have a or t on the rhs of an equation

In[4]:= sf = sf /. (x_) == (v:a | t) :> v == x
Out[4]= Piecewise[{{onepass, (t == n && (a == n || a == s)) || a == d},
    {twopass, a == s && (t == d || t == s)}}, Indeterminate]

To test the resulting expression, type e.g.:
In[5]:=
Refine[({sf /. #1, #1} & ) /@ Apply[{a -> #1, t -> #2} & , Tuples[{n, s, 
d}, 2], {1}]]
Out[5]=
{Indeterminate, {a -> n, t -> d}}
{Indeterminate, {a -> n, t -> s}}
{onepass, {a -> d, t -> d}}
{onepass, {a -> d, t -> n}}
{onepass, {a -> d, t -> s}}
{onepass, {a -> n, t -> n}}
{onepass, {a -> s, t -> n}}
{twopass, {a -> s, t -> d}}
{twopass, {a -> s, t -> s}}


HTH,
Peter


  • Prev by Date: Re: Rapid execution of gaussian integrals
  • Next by Date: RE: memory issues
  • Previous by thread: Re: Simplifying a Boolean Expression
  • Next by thread: RE: "Skip this Input cell" module?