Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

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

Search the Archive

Re: Logic and Truth Tables with Mathematica?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg129953] Re: Logic and Truth Tables with Mathematica?
  • From: Dana DeLouis <dana01 at icloud.com>
  • Date: Wed, 27 Feb 2013 23:44:34 -0500 (EST)
  • 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

> 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... at 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



  • Prev by Date: Re: Formatting Input Cells within a NotebookWrite[]
  • Next by Date: "Error" definition for nonlinear fit deviating from data points
  • Previous by thread: Re: Logic and Truth Tables with Mathematica?
  • Next by thread: A bug-looking behavior during integration