MathGroup Archive 2012

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

Search the Archive

Re: Evaluation of boolean functions in Mathematica 8

  • To: mathgroup at smc.vnet.net
  • Subject: [mg127577] Re: Evaluation of boolean functions in Mathematica 8
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Sat, 4 Aug 2012 21:48:55 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20120804095946.36173683D@smc.vnet.net>

Re your side note, use FullForm

Clear[truthTable]

truthTable[equ_, form_: TraditionalForm] :=
 Module[{v = Flatten[
     {Union[BooleanVariables[equ]], equ}]},
  Grid[Join[
    {Style[form[#], Bold] & /@ v},
    BooleanTable[v]],
   Frame -> All,
   FrameStyle -> LightGray]]

equ = Riffle @@ (# @@@ Subsets[{p, q, r}, {2, 3}] & /@
     {Or, And});

abbrev = {True -> Style["T", Blue, Bold],
   False -> Style["F", Gray]};

truthTable[equ] /. abbrev

truthTable[equ, StandardForm] /. abbrev

truthTable[equ, FullForm] /. abbrev

Change default form to taste.


Bob Hanlon


On Sat, Aug 4, 2012 at 5:59 AM, Dana DeLouis <dana2010 at me.com> wrote:
>> g[True,True,True]
>> But the above statement does not evaluate g at the data-point given,...
>
> Hi.  Just some additional ideas.  Here's what you have:
>
> equ=And[p,q,r]
>
> f=BooleanConvert[equ,"BFF"]
> BooleanFunction[<3>][p,q,r]
>
> Notice that the equation has p,q,r assigned to it.
> Therefore, just assign a value to the variables.
>
> f//FullForm
> BooleanFunction[Rule["BDD",List[3,0,2,-1,1,3,-1,2,1,-1]]][p,q,r]
>
> f/.{p->True,q->True,r->False}
> False
>
>
> Slightly better might be:
>
> BooleanTable[f,{p,q,r}]
> {True,False,False,False,False,False,False,False}
>
>
> As a side note, notice that using x,y,z will not work as the function is expecting p,q,r.
>
> BooleanTable[f,{x,y,z}]
> < does not work >
>
>
> Using your equation, we see that there is only 1 output with True, and the rest are False.
> What generated the True condition?    All False, All True, or something in-between?
> Here's a general idea you may find interesting.
> I've includes "steps" so as to see intermediate steps one may want to look at leading up to the final solution.
>
>
> TruthTable[equ_,steps_List:{}]:=Module[{v},
> v = BooleanVariables[equ];
> AppendTo[v,{steps,equ}];
> v=Flatten[v];
>
> MatrixForm[BooleanTable[v],
> TableHeadings->{Automatic,v}]
> ]
>
>
> Here's your example.  It appears the output is true when all inputs are true.
> (I know... this example was easy)
>
>
> TruthTable[equ]
> (       p       q       r       p&&q&&r
> 1       True    True    True    True
> 2       True    True    False False
> 3       True    False True False
> 4       True    False False False
> 5       False True True False
> 6       False True False False
> 7       False False True False
> 8       False False False False
> )
>
>
> This example is also easy, but suppose one didn't know the intermediate steps.
> If one didn't know what the final NOT did, you could look at the inside part prior to the final NOT.
>
>
> equ=Not[Or[And[x,y],z]];
>
> // additional things one may want to look at:
>
> steps = {And[x,y],  Or[And[x,y],z]};
>
> TruthTable[equ,steps]
> (         x     y       z       x&&y    (x&&y)||z       !((x&&y)||z)
> 1       True    True    True    True    True    False
> 2       True    True    False True True False
>
> etc...
> )
>
>
> As a side note, does anyone know of a simple way to keep the input equation as And[p,q,r] ?
>
> This gets simplified to p&&q&&r.
> I've been trying to find a way (without typing it as string) to keep the original version of And[p,q,r]   in the heading row.
> ~Thanks.
>
>
> = = = = = = = = = =
> HTH  :>)
> Dana DeLouis
> Mac & Math 8
> = = = = = = = = = =
>
>
>
> On Aug 1, 4:57 am, lalwani.ak... at gmail.com wrote:
>> Hello,
>>
>> I want to evaluate a Boolean function by substituting True and False values for its variables, to get a True or False value at the output. Example,
>>
>> g = BooleanConvert[And[p, q, r], "BFF"]
>>
>> g[True,True,True]
>>
>> But the above statement does not evaluate g at the data-point given, but instead returns the statement,
>>
>> BooleanFunction[<3>][p,q,r][True,True,True]
>>
>> Can anybody please suggest how to proceed?
>>
>> Thanks a lot.



  • Prev by Date: Re: Mathematica as a New Approach to Teaching Maths
  • Next by Date: Problem finding maximum
  • Previous by thread: Re: Evaluation of boolean functions in Mathematica 8
  • Next by thread: Re: Evaluation of boolean functions in Mathematica 8