[Date Index]
[Thread Index]
[Author Index]
Re: Evaluation of boolean functions in Mathematica 8
*To*: mathgroup at smc.vnet.net
*Subject*: [mg127568] Re: Evaluation of boolean functions in Mathematica 8
*From*: Dana DeLouis <dana2010 at me.com>
*Date*: Sat, 4 Aug 2012 21:45: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
Thank you Bob for the excellent suggestions.
I've never used Grid before, so I got side-tracked on learning about this function.
I know there are many variations one can do along this theme.
So, here's a variation that incorporated some of your ideas.
I added a column that attempts to find a simpler version if it finds one.
What is considered simpler is of course subjective, so this is only an attempt. :>)
// ===== Main Function =====
TruthTable[equ_,steps_List:{}]:=Module[{v,data,min,hdg},
v = BooleanVariables[equ];
AppendTo[v,{steps,equ}];
(* If shorter version found, add that to the end *)
min=BoolMin[equ];
If[UnsameQ[min,equ],AppendTo[v,min]];
v=Flatten[v];
(* Calculate table *)
data=BooleanTable[v];
(* Adjust Table Data *)
data = data/.{True->Style["T",Blue,Bold],False->Style["F",LightGray]};
(* Add Ref numbers *)
data=Join[Map[List,Range[Length[data]]],data,2];
(* Add 2 Heading Rows *)
hdg = Join[{"#"},v];
PrependTo[data,Style[TraditionalForm[#],Bold]&/@hdg];
PrependTo[data,hdg];
data =ReplacePart[data,{1,1}->""];
Grid[data,
Frame->All,
Alignment->Center,
ItemSize->Automatic,
Background -> {None,{LightBlue,LightBlue},{{{3,-1},{-1,-1}}->LightYellow}}
]
]
// ===== Aux functions =====
// Note: Physically try each of the Form Options, and pick the shortest LeafCount.
// If there's a tie with original equation, return original equation.
BoolMin[equ_]:=Module[{opts,v,len},
opts = {"DNF","SOP","CNF","POS","ANF","NOR","NAND","AND","OR"} ;
v=BooleanMinimize[equ,#]&/@opts;
(* Place Original equation at beginning of list *)
PrependTo[v,equ];
len=LeafCount/@v;
First[Pick[v,len,Min[len]]]
]
// ===== End of Functions =====
// Simple
equ=And[p,q,r];
TruthTable[equ]
// If you want to see the intermediate step of And[x,y], add it to the list.
equ=Not[Or[And[x,y],z]];
TruthTable[equ, {And[x,y]} ]
// This is a longer equation. The last column suggests a simpler version without d.
// Both columns are printed so as to visually confirm this.
equ=And[And[Xor[a,b],c],Xor[a,c],Or[c,d]];
TruthTable[equ]
Again, there are lots of variations along this theme.
I like having TraditionalForm in the title at least for now.
This way, I can learn the symbols that I don't use often.
Thank you for your suggestions.
Interesting subject. :>)
= = = = = =
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: Using Workbench to debug Compile[] ??**
Next by Date:
**Why no OSX SystemDialogInput["RecordSound"]?**
Previous by thread:
**Re: Evaluation of boolean functions in Mathematica 8**
Next by thread:
**Re: Mathematica as a New Approach to Teaching Maths**
| |