| 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 | 30 | ▶ | {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 | 30 | my @C = @_; # Lists to be multiplied | |||||||||
| 38 | 30 | my @c = (); # Current element of cartesian product | |||||||||
| 39 | 30 | my @P = (); # Cartesian product | |||||||||
| 40 | 30 | my $n = 0; # Number of elements in product | |||||||||
| 41 | |||||||||||
| 42 | # return 0 if @C == 0; # Empty product per Philipp Rumpf | ||||||||||
| 43 | |||||||||||
| 44 | 30 | ▶ | @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 | 30 | my $p; $p = sub | |||||||||
| 49 | 2625868 | ▶ | {if (@c < @C) | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
| |||||||||||
| 50 | 65744 | {for(@{$C[@c]}) | |||||||||
| 51 | 2625840 | {push @c, $_; | |||||||||
| 52 | 2625840 | &$p(); | |||||||||
| 53 | 2625840 | pop @c; | |||||||||
| 54 | } | ||||||||||
| 55 | } | ||||||||||
| 56 | else | ||||||||||
| 57 | 2560124 | {my $p = [@c]; | |||||||||
| 58 | 2560124 | ▶ | push @P, bless $p if &$s(@$p); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | } | ||||||||||
| 60 | 30 | }; | |||||||||
| 61 | |||||||||||
| 62 | # Generate each cartesian product allowing for prior cartesian products. | ||||||||||
| 63 | |||||||||||
| 64 | 30 | 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 | 30 | }; | |||||||||
| 77 | |||||||||||
| 78 | # Determine optimal method of forming cartesian products for this call | ||||||||||
| 79 | |||||||||||
| 80 | 30 | ▶ | if (grep {grep {ref eq __PACKAGE__} @$_} @C) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 2 | {&$q | |||||||||
| 82 | } | ||||||||||
| 83 | else | ||||||||||
| 84 | 28 | {&$p | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 30 | $p = $q = undef; # Break memory loops per Philipp Rumpf | |||||||||
| 88 | @P # Product | ||||||||||
| 89 | 30 | } | |||||||||
| 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.007'; # 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. In | ||||||||||
| 116 | particular, the cartesian product of zero lists is the empty set, as is | ||||||||||
| 117 | the cartesian product of any set of lists which contain a list with no | ||||||||||
| 118 | elements. | ||||||||||
| 119 | |||||||||||
| 120 | C<cartesian()> takes the following parameters: | ||||||||||
| 121 | |||||||||||
| 122 | 1. A block of code to process each n-tuple. this code should return true | ||||||||||
| 123 | if the current n-tuple should be included in the returned value of the | ||||||||||
| 124 | C<cartesian()> function, otherwise false. | ||||||||||
| 125 | |||||||||||
| 126 | 2. Zero or more lists. | ||||||||||
| 127 | |||||||||||
| 128 | C<cartesian()> returns an array of references to all the n-tuples | ||||||||||
| 129 | selected by the code block supplied as parameter 1. | ||||||||||
| 130 | |||||||||||
| 131 | C<cartesian()> croaks if you try to form the cartesian product of | ||||||||||
| 132 | something other than lists of things. | ||||||||||
| 133 | |||||||||||
| 134 | The cartesian product of lists A,B,C is associative, that is: | ||||||||||
| 135 | |||||||||||
| 136 | (A X B) X C = A X (B X C) | ||||||||||
| 137 | |||||||||||
| 138 | C<cartesian()> respects associativity by allowing you to include a | ||||||||||
| 139 | cartesian product produced by an earlier call to C<cartesian()> in the | ||||||||||
| 140 | set of lists whose cartesian product is to be formed, at the cost of a | ||||||||||
| 141 | performance penalty if this option is chosen. | ||||||||||
| 142 | |||||||||||
| 143 | use Math::Cartesian::Product; | ||||||||||
| 144 | |||||||||||
| 145 | my $a = [qw(a b)]; | ||||||||||
| 146 | my $b = [cartesian {1} $a, $a]; | ||||||||||
| 147 | cartesian {print "@_\n"} $b, $b; | ||||||||||
| 148 | |||||||||||
| 149 | # a a a a | ||||||||||
| 150 | # a a a b | ||||||||||
| 151 | # a a b a | ||||||||||
| 152 | # ... | ||||||||||
| 153 | |||||||||||
| 154 | |||||||||||
| 155 | C<cartesian()> is easy to use and fast. It is written in 100% Pure Perl. | ||||||||||
| 156 | |||||||||||
| 157 | |||||||||||
| 158 | =head1 Export | ||||||||||
| 159 | |||||||||||
| 160 | The C<cartesian()> function is exported. | ||||||||||
| 161 | |||||||||||
| 162 | =head1 Installation | ||||||||||
| 163 | |||||||||||
| 164 | Standard Module::Build process for building and installing modules: | ||||||||||
| 165 | |||||||||||
| 166 | perl Build.PL | ||||||||||
| 167 | ./Build | ||||||||||
| 168 | ./Build test | ||||||||||
| 169 | ./Build install | ||||||||||
| 170 | |||||||||||
| 171 | Or, if you're on a platform (like DOS or Windows) that doesn't require | ||||||||||
| 172 | the "./" notation, you can do this: | ||||||||||
| 173 | |||||||||||
| 174 | perl Build.PL | ||||||||||
| 175 | Build | ||||||||||
| 176 | Build test | ||||||||||
| 177 | Build install | ||||||||||
| 178 | |||||||||||
| 179 | =head1 Author | ||||||||||
| 180 | |||||||||||
| 181 | PhilipRBrenan@appaapps.com | ||||||||||
| 182 | |||||||||||
| 183 | http://www.appaapps.com | ||||||||||
| 184 | |||||||||||
| 185 | =head1 Acknowledgements | ||||||||||
| 186 | |||||||||||
| 187 | With much help and good natured advice from Philipp Rumpf to whom I am greatly indebted. | ||||||||||
| 188 | |||||||||||
| 189 | =head1 See Also | ||||||||||
| 190 | |||||||||||
| 191 | =over | ||||||||||
| 192 | |||||||||||
| 193 | =item L<Math::Disarrange::List> | ||||||||||
| 194 | |||||||||||
| 195 | =item L<Math::Permute::List> | ||||||||||
| 196 | |||||||||||
| 197 | =item L<Math::Subsets::List> | ||||||||||
| 198 | |||||||||||
| 199 | =item L<Math::Transform::List> | ||||||||||
| 200 | |||||||||||
| 201 | =back | ||||||||||
| 202 | |||||||||||
| 203 | =head1 Copyright | ||||||||||
| 204 | |||||||||||
| 205 | Copyright (c) 2009 Philip R Brenan. | ||||||||||
| 206 | |||||||||||
| 207 | This module is free software. It may be used, redistributed and/or | ||||||||||
| 208 | modified under the same terms as Perl itself. | ||||||||||
| 209 | |||||||||||
| 210 | =cut | ||||||||||