Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: Obtaining terms in an expansion by ivancho
in thread Obtaining terms in an expansion by randyk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2021-05-14 02:11 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (147 votes). Check out past polls.