|
|
Re: Logic and Truth Tables with Mathematica?
Posted:
Feb 27, 2013 11:43 PM
|
|
> Can Mathematica, given a relation such as P->Q generate the truth table?
Hi. You are looking for the function 'BooleanTable
If interested, here is a custom function that I use. The 2 heading rows are written in both normal form, and traditional form. You can add any additional intermediate steps. I've added line numbers to the output. If it finds a possible shorter version, it is added to the end of the table.
TruthTable[Implies[P,Q]]
For another example, I want to add 'And[p,s] to the table. The last column has a suggestion for a shorter version.
equ=(((p||q)&&(p\[Implies]r)&&(q\[Implies]s))&&p)||q;
TruthTable[equ, {And[p,s]} ]
= = = = = = = = = = HTH :>) Dana DeLouis Mac & Mathematica 9 = = = = = = = = = =
TruthTable[equ_,steps_List:{}]:=Module[{v,data,min,hdg},
(* Note: For steps, add any intermediate steps would like to see inside { } Note: Requiers Aux Function 'BoolMin[ ] *)
v = BooleanVariables[equ]; v=Join[v,BooleanVariables[steps]] //Union; AppendTo[v,{steps,equ}];
(* If shorter version found, add that to the end as a possible suggestion *)
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 *) (* Normal Form, and Traditional Form *) hdg = Join[{"#"},v]; PrependTo[data,Style[TraditionalForm[#],Bold]&/@hdg]; PrependTo[data,hdg];
(* Clear the upper left corner *) data =ReplacePart[data,{1,1}->""];
Grid[data, Frame->All, Alignment->Center, ItemSize->Automatic, Background -> = {None,{LightBlue,LightBlue},{{{3,-1},{-1,-1}}->LightYellow}} ] ]
= = = = = Aux Function = = = = =
// Attempts to find a shorter version by trying all options.
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; (* With multiple Minimum leaf counts, if original equation is in list, then keep that *) First[Pick[v,len,Min[len]]] ]
= = = = = = = = = =
On Thursday, July 12, 2001 3:01:44 AM UTC-4, hea...@in-tch.com wrote: > Hi, > Can Mathematica, given a relation such as P->Q generate the truth table? > Is there a book available that talks about Mathematica and logic? > Thanks, > Heath
|
|