0
$\begingroup$

Suppose you have an expression like:

$$ (a + b + c + d)(e + f + g)(h + i) $$

Is there a method to expand this expression that does not involve multiplying only two groups at a time? Some way to distribute the terms of the first group ($a$, $b$, $c$ and $d$) across the second and third, and then distribute terms of the second group ($e$, $f$ and $g$) across the third, to arrive at the final sum?

Suppose I am writing a computer program. Is there a recursive method I could use for an arbitrary number of groups with arbitrary number of terms?

$\endgroup$
1
  • 1
    $\begingroup$ You can use any algorithm for generating a Cartesian product. The Python standard library has one here. $\endgroup$ Commented Feb 6 at 6:42

1 Answer 1

3
$\begingroup$

Generalizing your expression as the products of $m$ sums, where the $i^{th}$ sum has $n_i$ terms represented as $x_{i,j}$ for $j \in [1, n_i]$, we get $$\prod_{i=1}^m \sum_{j=1}^{n_i} x_{i,j}$$

Then the expansion can be written as choosing one term from each original sum $i$, taking their product, going through all combinations $C$ of choosing, and summing the products $$\sum_{C} \prod_{i=1}^m x_{i,C(i)}$$

The combinations $C$ can be interpreted as finding all solutions to the following problem, $$C(i) \in [1, n_i] \quad \forall \; i \in [1,m]$$

where the number of solutions is, $$\prod_{i=1}^m n_i$$

The following is one pseudo-code approach to generating these combinations, which follows the spirit of generating binary sequences

Initialize C to be 1 for all entries. This is the first combination
Run a loop with number of iterations equal to one less than the product of all entries of n
   Find the last entry in C which is not at the corresponding limit from n
   Increment the value of this entry in C
   Reset all entries occurring afterwards in C to 1
   The current value of C is a new combination
end
$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.