| 1 | =head1 Name | ||||||||||
| 2 | |||||||||||
| 3 | Math::Cartesian::Product - Generate the Cartesian product of zero or more lists. | ||||||||||
| 4 | |||||||||||
| 5 | =head1 Synopsis | ||||||||||
| 6 | |||||||||||
| 7 | use Math::Cartesian::Product; | ||||||||||
| 8 | |||||||||||
| 9 | cartesian {print "@_\n"} [qw(a b c)], [1..2]; | ||||||||||
| 10 | |||||||||||
| 11 | # a 1 | ||||||||||
| 12 | # a 2 | ||||||||||
| 13 | # b 1 | ||||||||||
| 14 | # b 2 | ||||||||||
| 15 | # c 1 | ||||||||||
| 16 | # c 2 | ||||||||||
| 17 | |||||||||||
| 18 | cartesian {print "@_\n"} ([0..1]) x 8; | ||||||||||
| 19 | |||||||||||
| 20 | # 0 0 0 0 0 0 0 0 | ||||||||||
| 21 | # 0 0 0 0 0 0 0 1 | ||||||||||
| 22 | # 0 0 0 0 0 0 1 0 | ||||||||||
| 23 | # ... | ||||||||||
| 24 | # 1 1 1 1 1 1 1 0 | ||||||||||
| 25 | # 1 1 1 1 1 1 1 1 | ||||||||||
| 26 | |||||||||||
| 27 | =cut | ||||||||||
| 28 | |||||||||||
| 29 | package Math::Cartesian::Product; | ||||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | sub cartesian(&@) # Generate the Cartesian product of zero or more lists | ||||||||||
| 35 | 40 | ▶ | {my $s = shift; # Subroutine to call to process each element of the product | ||||||||
|
Subroutine
cartesian: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 36 | |||||||||||
| 37 | 40 | my @C = @_; # Lists to be multiplied | |||||||||
| 38 | 40 | my @c = (); # Current element of Cartesian product | |||||||||
| 39 | 40 | my @P = (); # Cartesian product | |||||||||
| 40 | 40 | my $n = 0; # Number of elements in product | |||||||||
| 41 | |||||||||||
| 42 | # return 0 if @C == 0; # Empty product per Philipp Rumpf | ||||||||||
| 43 | |||||||||||
| 44 | 40 | ▶ | @C == grep {ref eq 'ARRAY'} @C or croak("Arrays of things required by cartesian"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | |||||||||||
| 46 | # Generate each Cartesian product when there are no prior Cartesian products. | ||||||||||
| 47 | |||||||||||
| 48 | 40 | my $p; $p = sub | |||||||||
| 49 | 2625881 | ▶ | {if (@c < @C) | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
| |||||||||||
| 50 | 65753 | {for(@{$C[@c]}) | |||||||||
| 51 | 2625843 | {push @c, $_; | |||||||||
| 52 | 2625843 | &$p(); | |||||||||
| 53 | 2625843 | pop @c; | |||||||||
| 54 | } | ||||||||||
| 55 | } | ||||||||||
| 56 | else | ||||||||||
| 57 | 2560128 | {my $p = [@c]; | |||||||||
| 58 | 2560128 | ▶ | push @P, bless $p if &$s(@$p); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | } | ||||||||||
| 60 | 40 | }; | |||||||||
| 61 | |||||||||||
| 62 | # Generate each Cartesian product allowing for prior Cartesian products. | ||||||||||
| 63 | |||||||||||
| 64 | 40 | my $q; $q = sub | |||||||||
| 65 | 42 | ▶ | {if (@c < @C) | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
| |||||||||||
| 66 | 10 | {for(@{$C[@c]}) | |||||||||
| 67 | 40 | {push @c, $_; | |||||||||
| 68 | 40 | &$q(); | |||||||||
| 69 | 40 | pop @c; | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | else | ||||||||||
| 73 | 32 | ▶ | {my $p = [map {ref eq __PACKAGE__ ? @$_ : $_} @c]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 32 | ▶ | push @P, bless $p if &$s(@$p); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | } | ||||||||||
| 76 | 40 | }; | |||||||||
| 77 | |||||||||||
| 78 | # Determine optimal method of forming Cartesian products for this call | ||||||||||
| 79 | |||||||||||
| 80 | 40 | ▶ | if (grep {grep {ref eq __PACKAGE__} @$_} @C) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 2 | {&$q | |||||||||
| 82 | } | ||||||||||
| 83 | else | ||||||||||
| 84 | 38 | {&$p | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 40 | $p = $q = undef; # Break memory loops per Philipp Rumpf | |||||||||
| 88 | @P # Product | ||||||||||
| 89 | 40 | } | |||||||||
| 90 | |||||||||||
| 91 | # Export details | ||||||||||
| 92 | |||||||||||
| 93 | 1 | require 5; | |||||||||
| 94 | 1 | require Exporter; | |||||||||
| 95 | |||||||||||
| 96 | 1 | ▶ | use vars qw(@ISA @EXPORT $VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 97 | |||||||||||
| 98 | 1 | @ISA = qw(Exporter); | |||||||||
| 99 | 1 | @EXPORT = qw(cartesian); | |||||||||
| 100 | 1 | $VERSION = '1.008'; # Monday 26 Jan 2015 | |||||||||
| 101 | |||||||||||
| 102 | =head1 Description | ||||||||||
| 103 | |||||||||||
| 104 | Generate the Cartesian product of zero or more lists. | ||||||||||
| 105 | |||||||||||
| 106 | Given two lists, say: [a,b] and [1,2,3], the Cartesian product is the | ||||||||||
| 107 | set of all ordered pairs: | ||||||||||
| 108 | |||||||||||
| 109 | (a,1), (a,2), (a,3), (b,1), (b,2), (b,3) | ||||||||||
| 110 | |||||||||||
| 111 | which select their first element from all the possibilities listed in | ||||||||||
| 112 | the first list, and select their second element from all the | ||||||||||
| 113 | possibilities in the second list. | ||||||||||
| 114 | |||||||||||
| 115 | The idea can be generalized to n-tuples selected from n lists where all the | ||||||||||
| 116 | elements of the first list are combined with all the elements of the second | ||||||||||
| 117 | list, the results of which are then combined with all the member of the third | ||||||||||
| 118 | list and so on over all the input lists. | ||||||||||
| 119 | |||||||||||
| 120 | It should be noted that Cartesian product of one or more lists where one or | ||||||||||
| 121 | more of the lists are empty (representing the empty set) is the empty set | ||||||||||
| 122 | and thus has zero members; and that the Cartesian product of zero lists is a | ||||||||||
| 123 | set with exactly one member, namely the empty set. | ||||||||||
| 124 | |||||||||||
| 125 | C<cartesian()> takes the following parameters: | ||||||||||
| 126 | |||||||||||
| 127 | 1. A block of code to process each n-tuple. this code should return true | ||||||||||
| 128 | if the current n-tuple should be included in the returned value of the | ||||||||||
| 129 | C<cartesian()> function, otherwise false. | ||||||||||
| 130 | |||||||||||
| 131 | 2. Zero or more lists. | ||||||||||
| 132 | |||||||||||
| 133 | C<cartesian()> returns an array of references to all the n-tuples | ||||||||||
| 134 | selected by the code block supplied as parameter 1. | ||||||||||
| 135 | |||||||||||
| 136 | C<cartesian()> croaks if you try to form the Cartesian product of | ||||||||||
| 137 | something other than lists of things. | ||||||||||
| 138 | |||||||||||
| 139 | The cartesian product of lists A,B,C is associative, that is: | ||||||||||
| 140 | |||||||||||
| 141 | (A X B) X C = A X (B X C) | ||||||||||
| 142 | |||||||||||
| 143 | C<cartesian()> respects associativity by allowing you to include a | ||||||||||
| 144 | Cartesian product produced by an earlier call to C<cartesian()> in the | ||||||||||
| 145 | set of lists whose Cartesian product is to be formed, at the cost of a | ||||||||||
| 146 | performance penalty if this option is chosen. | ||||||||||
| 147 | |||||||||||
| 148 | use Math::Cartesian::Product; | ||||||||||
| 149 | |||||||||||
| 150 | my $a = [qw(a b)]; | ||||||||||
| 151 | my $b = [cartesian {1} $a, $a]; | ||||||||||
| 152 | cartesian {print "@_\n"} $b, $b; | ||||||||||
| 153 | |||||||||||
| 154 | # a a a a | ||||||||||
| 155 | # a a a b | ||||||||||
| 156 | # a a b a | ||||||||||
| 157 | # ... | ||||||||||
| 158 | |||||||||||
| 159 | |||||||||||
| 160 | C<cartesian()> is easy to use and fast. It is written in 100% Pure Perl. | ||||||||||
| 161 | |||||||||||
| 162 | |||||||||||
| 163 | =head1 Export | ||||||||||
| 164 | |||||||||||
| 165 | The C<cartesian()> function is exported. | ||||||||||
| 166 | |||||||||||
| 167 | =head1 Installation | ||||||||||
| 168 | |||||||||||
| 169 | Standard Module::Build process for building and installing modules: | ||||||||||
| 170 | |||||||||||
| 171 | perl Build.PL | ||||||||||
| 172 | ./Build | ||||||||||
| 173 | ./Build test | ||||||||||
| 174 | ./Build install | ||||||||||
| 175 | |||||||||||
| 176 | Or, if you're on a platform (like DOS or Windows) that doesn't require | ||||||||||
| 177 | the "./" notation, you can do this: | ||||||||||
| 178 | |||||||||||
| 179 | perl Build.PL | ||||||||||
| 180 | Build | ||||||||||
| 181 | Build test | ||||||||||
| 182 | Build install | ||||||||||
| 183 | |||||||||||
| 184 | =head1 Author | ||||||||||
| 185 | |||||||||||
| 186 | PhilipRBrenan@appaapps.com | ||||||||||
| 187 | |||||||||||
| 188 | http://www.appaapps.com | ||||||||||
| 189 | |||||||||||
| 190 | =head1 Acknowledgements | ||||||||||
| 191 | |||||||||||
| 192 | With much help and good natured advice from Philipp Rumpf to whom I am greatly indebted. | ||||||||||
| 193 | |||||||||||
| 194 | =head1 See Also | ||||||||||
| 195 | |||||||||||
| 196 | =over | ||||||||||
| 197 | |||||||||||
| 198 | =item L<Math::Disarrange::List> | ||||||||||
| 199 | |||||||||||
| 200 | =item L<Math::Permute::List> | ||||||||||
| 201 | |||||||||||
| 202 | =item L<Math::Subsets::List> | ||||||||||
| 203 | |||||||||||
| 204 | =item L<Math::Transform::List> | ||||||||||
| 205 | |||||||||||
| 206 | =back | ||||||||||
| 207 | |||||||||||
| 208 | =head1 Copyright | ||||||||||
| 209 | |||||||||||
| 210 | Copyright (c) 2009 Philip R Brenan. | ||||||||||
| 211 | |||||||||||
| 212 | This module is free software. It may be used, redistributed and/or | ||||||||||
| 213 | modified under the same terms as Perl itself. | ||||||||||
| 214 | |||||||||||
| 215 | =cut | ||||||||||