CombinatoricsTruthTable Method (Int32) |
Namespace: Accord.Math
Suppose we would like to generate a truth table (i.e. all possible combinations of a set of discrete symbols) for variables that contain different numbers symbols. Let's say, for example, that the first variable may contain symbols 0 and 1, the second could contain either 0, 1, or 2, and the last one again could contain only 0 and 1. Thus we can generate the truth table in the following way:
// Number of symbols for each variable int[] symbols = { 2, 3, 2 }; // Generate the truth table for the given symbols int[][] table = Combinatorics.TruthTable(symbols); // The generated table will be: { new int[] { 0, 0, 0 }, new int[] { 0, 0, 1 }, new int[] { 0, 1, 0 }, new int[] { 0, 1, 1 }, new int[] { 0, 2, 0 }, new int[] { 0, 2, 1 }, new int[] { 1, 0, 0 }, new int[] { 1, 0, 1 }, new int[] { 1, 1, 0 }, new int[] { 1, 1, 1 }, new int[] { 1, 2, 0 }, new int[] { 1, 2, 1 }, };