1Math::Cartesian::ProducUts(e3rpmC)ontributed Perl DocumeMnattaht:i:oCnartesian::Product(3pm)
2
3
4

Name

6       Math::Cartesian::Product - Generate the Cartesian product of zero or
7       more lists.
8

Synopsis

10        use Math::Cartesian::Product;
11
12        cartesian {print "@_\n"} [qw(a b c)], [1..2];
13
14        #  a 1
15        #  a 2
16        #  b 1
17        #  b 2
18        #  c 1
19        #  c 2
20
21        cartesian {print "@_\n"} ([0..1]) x 8;
22
23        #  0 0 0 0 0 0 0 0
24        #  0 0 0 0 0 0 0 1
25        #  0 0 0 0 0 0 1 0
26        #  ...
27        #  1 1 1 1 1 1 1 0
28        #  1 1 1 1 1 1 1 1
29
30        print "@$_\n" for
31          cartesian {"@{[reverse @_]}" eq "@_"}
32            ([' ', '*']) x 8;
33
34        #       * *
35        #     *     *
36        #     * * * *
37        #   *         *
38        #   *   * *   *
39        #   * *     * *
40        #   * * * * * *
41        # *             *
42        # *     * *     *
43        # *   *     *   *
44        # *   * * * *   *
45        # * *         * *
46        # * *   * *   * *
47        # * * *     * * *
48        # * * * * * * * *
49

Description

51       Generate the Cartesian product of zero or more lists.
52
53       Given two lists, say: [a,b] and [1,2,3], the Cartesian product is the
54       set of all ordered pairs:
55
56        (a,1), (a,2), (a,3), (b,1), (b,2), (b,3)
57
58       which select their first element from all the possibilities listed in
59       the first list, and select their second element from all the
60       possibilities in the second list.
61
62       The idea can be generalized to n-tuples selected from n lists where all
63       the elements of the first list are combined with all the elements of
64       the second list, the results of which are then combined with all the
65       member of the third list and so on over all the input lists.
66
67       It should be noted that Cartesian product of one or more lists where
68       one or more of the lists are empty (representing the empty set) is the
69       empty set and thus has zero members; and that the Cartesian product of
70       zero lists is a set with exactly one member, namely the empty set.
71
72       cartesian() takes the following parameters:
73
74       1. A block of code to process each n-tuple. this code should return
75       true if the current n-tuple should be included in the returned value of
76       the cartesian() function, otherwise false.
77
78       2. Zero or more lists.
79
80       cartesian() returns an array of references to all the n-tuples selected
81       by the code block supplied as parameter 1 if called in list context,
82       else it returns a count of the selected n-tuples.
83
84       cartesian() croaks if you try to form the Cartesian product of
85       something other than lists of things or prior Cartesian products.
86
87       The cartesian product of lists A,B,C is associative, that is:
88
89         (A X B) X C = A X (B X C)
90
91       cartesian() respects associativity by allowing you to include a
92       Cartesian product produced by an earlier call to cartesian() in the set
93       of lists whose Cartesian product is to be formed, at the cost of a
94       performance penalty if this option is chosen.
95
96         use Math::Cartesian::Product;
97
98         my $a = [qw(a b)];
99         my $b = [cartesian {1} $a, $a];
100         cartesian {print "@_\n"} $b, $b;
101
102         # a a a a
103         # a a a b
104         # a a b a
105         # ...
106
107       cartesian() is easy to use and fast. It is written in 100% Pure Perl.
108

Export

110       The cartesian() function is exported.
111

Installation

113       Standard Module::Build process for building and installing modules:
114
115         perl Build.PL
116         ./Build
117         ./Build test
118         ./Build install
119
120       Or, if you're on a platform (like DOS or Windows) that doesn't require
121       the "./" notation, you can do this:
122
123         perl Build.PL
124         Build
125         Build test
126         Build install
127

Author

129       Philip R Brenan at gmail dot com
130
131       http://www.appaapps.com
132

Acknowledgements

134       With much help and good natured advice from Philipp Rumpf and Justin
135       Case to whom I am indebted.
136

See Also

138       Math::Disarrange::List
139       Math::Permute::List
140       Math::Permute::Lists
141       Math::Permute::Partitions
142       Math::Subsets::List
143       Math::Transform::List
144
146       Copyright (c) 2009-2015 Philip R Brenan.
147
148       This module is free software. It may be used, redistributed and/or
149       modified under the same terms as Perl itself.
150
151
152
153perl v5.38.0                      2023-07-20     Math::Cartesian::Product(3pm)
Impressum