Perl Monk, Perl Meditation PerlMonks

### Re^2: Obtaining terms in an expansion

by randyk (Parson)
 on Jan 05, 2006 at 22:03 UTC ( #521371=note: print w/replies, xml ) Need Help??

in reply to Re: Obtaining terms in an expansion
in thread Obtaining terms in an expansion

I should have supplied an example of what I was looking for. For example, for N=2:
```( arr[0][0] + arr[0][1] ) ( arr[1][0] + arr[1][1] )
what I want to find is
```C[0] = arr[0][0] arr[1][0]
C[1] = arr[0][0] arr[1][1]
C[2] = arr[0][1] arr[1][0]
C[3] = arr[0][1] arr[1][1]
It doesn't matter if the coefficients come out in this order, of course.

Replies are listed 'Best First'.
Re^3: Obtaining terms in an expansion
by BrowserUk (Pope) on Jan 06, 2006 at 00:26 UTC

If 2^N could become m^n, then you could use a recursive solution: (Updated: cleaned up slightly!)

```#! perl -slw
use strict;

sub expand{
return @{ \$_[0] } if @_ == 1;
map{
my \$x = \$_;
map{ \$x * \$_ } &expand;
} @{ pop @_ };
}

my @mat = ( [ -1, 2 ], [ 3, -4 ] );
printf +("%s " x @mat ) . '= ',  map{ "[ @\$_ ] " } @mat;
print join ' ', expand( @mat );

@mat = ( [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], );
printf +("%s " x @mat ) . '= ',  map{ "[ @\$_ ] " } @mat;
print join ' ', expand( @mat );

@mat = ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], );
printf +("%s " x @mat ) . '= ',  map{ "[ @\$_ ] " } @mat;
print join ' ', expand( @mat );

__END__
P:\test>junk
[ -1 2 ]  [ 3 -4 ]  = -3 6 4 -8
[ 1 2 ]  [ 3 4 ]  [ 5 6 ]  = 15 30 20 40 18 36 24 48
[ 1 2 3 ]  [ 4 5 6 ]  [ 7 8 9 ]  =
28 56 84 35 70 105 42 84 126 32 64 96 40 80 120 48 96 144 36 72 108 45
+ 90 135 54 108 162

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: Obtaining terms in an expansion
by InfiniteSilence (Curate) on Jan 05, 2006 at 22:54 UTC
I don't see an operator between arr[0][0] and arr[1][0], so I presume the parenthesis mean you want to multiply.

Your description does not adequately explain what you want, particularly the (a[N-1][0] + a[N-1][1]).

```0,0 1,0
0,0 1,1
0,1 1,0
0,1 1,1
Don't follow. They should be:
```0,0 1,0
0,0 1,1
1,0 1,0
1,1 1,1
2,0 1,0
2,1 1,1
3,0 1,0
3,1 1,1
...
The wrong element is incrementing -- a[0][N-1] * a[1][0], a[0][N-1] * a[1][1].

Furthermore, this makes no sense. Are a[0][1] and a[1][1] use repeatedly throughout? If so, why not change them into constants?

Celebrate Intellectual Diversity

Create A New User
Node Status?
node history
Node Type: note [id://521371]
help
Chatterbox?
and the web crawler heard nothing...

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

Results (147 votes). Check out past polls.

Notices?