ok, so the 2^N terms are each a product of N terms,
a[i][j], where j can be 0 or 1 and i goes from 0 to N-1
ie, each one looks like, say
a[0][0] * a[1][0] * a[2][1] * a[3][0] * ... * a[N-1][1]
So, we want do some binary magic.
how about something like:
#!/usr/bin/perl -wl
use strict;
use Data::Dumper;
my $N = 4;
my $size = 32;
my $arr = [[1, 2],
[3, 4],
[5, 6],
[7, 8]];
my @C;
foreach my $m (0..2**$N - 1) {
my $vec = "";
vec($vec, 0, $size) = $m;
my @bits = split(//, unpack("B*", $vec));
splice(@bits, 0, $size - $N);
# so @bits now contains the binary expansion of $m
# with exactly $N bits, as you can verify by uncommenting
# print join "::", @bits;
$C[ $m ] = 1;
# now we multiply the $N terms $arr->[i][j]
# where j is the i-th bit in the binary expansion of $m
$C[ $m ] *= $arr->[ $_ ][ $bits[$_] ] for 0..$N-1;
}
print Dumper \@C;
note, $size must be a power of 2, for vec to work, and unless you're on a 64-bit machine, it probably cannot be more than 32.
also, this works for $N no more than 31(32?) - but you may have other problems (memory say), if you want to go above that.
Hope this helps.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.