I have a 2D array containing numbers. I am attempting to work out the product of multiplying a single number from each sub-array with one from each of the other sub-arrays; I then need to do this for all possible combinations.
The aim is that I input a file of the frequency of individual events, and get an output of the probability of a particular series of these events happening, with one event from each set.
I have fudged together some code with the help of a previous question:
for my $aref ( getCartesian(@freq) ) {
my $p = 1;
foreach my $n (@$aref) {
$p = $p * $n;
}
print "$p\n";
}
sub getCartesian {
my @input = @_;
my @ret = map [$_], @{ shift @input };
for my $a2 (@input) {
@ret = map {
my $v = $_;
map [@$v, $_], @$a2;
}
@ret;
}
return @ret;
}
where @freq
is an array of arrays, such as:
@freq = [0.1, 0.2, 0.3,]
[0.4, 0.5, 0.6,]
[0.7, 0.8, 0.9,]; `and ~ 20 more sub arrays`
This works fine for a small test file, but when I give it my required input of 24 sub-arrays with 3 items each, the generation of combinations is clearly far too intensive, with 3^24 possibilities. I have run it on a machine with 22 GB RAM, and it maxed out after 4 minutes before any output.
My question is, how could I modify the code so that I can print out $p
for each combination, without having to hold the whole set of combinations in memory, which kills it. I presume that time would be the only limiting factor for computation then, not resources.
Edit: a method in base Perl without packages would be great. I don't have admin on the HPC facility sadly,