4
$\begingroup$

I'm trying to expand the following polynomial

  Expand[  (A1 a1 + A2 a2 + A3 a3 + A4 a4 + A5 a5 + A6 a6 + A7 a7 + A8 a8) 
           (D1 a1 + D2 a2 + D3 a3 + D4 a4 + D5 a5 + D6 a6 + D7 a7 + D8 a8)
         + (H1 a1 + H2 a2 + H3 a3 + H4 a4 + H5 a5 + H6 a6 + H7 a7 + H8 a8)
           (E1 a1 + E2 a2 + E3 a3 + E4 a4 + E5 a5 + E6 a6 + E7 a7 + E8 a8)] 

and collect terms (for example, rewrite this as

  (A1 D1 + ....+ H1 E1) a1^2 + (A1 D2 + ....) a1 a2 + (A1 D3 + ... ) a1 a3 + ...
+ (A8 D8 + ... + ) a8^2 )

where I am thinking of the capital letters A1, A2, ...., D1, D2, ... H1, H2, ..., E1, E2, ... as some coefficients and a_i's as variables.

I could do this by hand but what if, instead of having 8 variables and two terms, I have k variables and n terms? Expanding the products and collecting terms by going through all the monomials by hand are doable but probably not that clever.

$\endgroup$
0

2 Answers 2

6
$\begingroup$

Something like this?

expr = (A1 a1 + A2 a2 + A3 a3 + A4 a4 + A5 a5 + A6 a6 + A7 a7 + 
      A8 a8) (D1 a1 + D2 a2 + D3 a3 + D4 a4 + D5 a5 + D6 a6 + D7 a7 + 
      D8 a8) + (H1 a1 + H2 a2 + H3 a3 + H4 a4 + H5 a5 + H6 a6 + 
      H7 a7 + H8 a8) (E1 a1 + E2 a2 + E3 a3 + E4 a4 + E5 a5 + E6 a6 + 
      E7 a7 + E8 a8);

Collect[expr, {a1, a2, a3, a4, a5, a6, a7, a8}]

Typing "collect" into the Documentation Center search field will bring up this page as the first result. It is a good idea to try a few keywords like this any time you are looking for a function. Here it happens that the function is itself called Collect.

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Thanks Mr.Wizard. I tried that and it worked! =) I'm definitely going to come here to discuss Mathematica related questions, rather than going here: reference.wolfram.com/mathematica/tutorial/… It's somewhat difficult teaching yourself how to use this program... $\endgroup$ Jul 2, 2012 at 7:11
  • 1
    $\begingroup$ @math-visitor okay, I'm glad I guessed right. :-) Be sure to make use of Mathematica Chat for getting help and especially help using the help system. The documentation is usually quite thorough but finding the section you need can be a challenge. $\endgroup$
    – Mr.Wizard
    Jul 2, 2012 at 7:13
  • $\begingroup$ I will definitely join Mathematica Chat in the the future, and for mathematica.SE members to help me with the online Mathematica help section! $\endgroup$ Jul 2, 2012 at 7:16
6
$\begingroup$

These operations will be helpful :

find maximal exponent for every variable :

Exponent[expr, {a1, a2, a3, a4, a5, a6, a7, a8}]
{2, 2, 2, 2, 2, 2, 2, 2}

find the coefficient of a1^2 :

Coefficient[expr, a1, 2]
A1 D1 + E1 H1

find all coefficients of {a1^2, a2^2, a3^2, a4^2, a5^2, a6^2, a7^2, a8^2}, e.g.

Column[ ({#, Coefficient[expr, #, 2]} & /@ {a1, a2, a3, a4, a5, a6, a7, a8}]
 {{a1, A1 D1 + E1 H1},
  {a2, A2 D2 + E2 H2},
  {a3, A3 D3 + E3 H3},
  {a4, A4 D4 + E4 H4},
  {a5, A5 D5 + E5 H5},
  {a6, A6 D6 + E6 H6},
  {a7, A7 D7 + E7 H7},
  {a8, A8 D8 + E8 H8}}

In general you can simplify every coefficient working with Collect, however your example is "simple enough" :

Collect[expr, {a1, a2, a3, a4, a5, a6, a7, a8}, Simplify] === 
Collect[expr, {a1, a2, a3, a4, a5, a6, a7, a8}]
True
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.