1
$\begingroup$

Here is what I have:

j1=2
Do[Print[w[m1, n1] = 
   ToExpression["w" <> ToString[m1] <> ToString[n1]]], {m1, 1, 
  j1}, {n1, 1, j1}]

That's what I got:

w11
w12
w21
w22

I wnat to generate a list with the product of this terms combined in groups of 2. For example:

{w11^2,w11*w12,w11*w21,w11*w22,w12^2,w12*w21,w12*w22,w21^2,w21*w22,w22^2}

Can somebody help me with this?

$\endgroup$

3 Answers 3

1
$\begingroup$

Here is one way

j1 = 2;

Table[ToExpression["w" <> ToString[m1] <> ToString[n1]], {m1, 1, j1}, {n1, 1, j1}] // 
  Flatten // Tuples[#, 2] & // Apply[Times, #, 1] &
$\endgroup$
1
  • $\begingroup$ It's almost that. I just had to use the command to remove duplicates. Thank you ;P $\endgroup$
    – user70148
    Commented Mar 17, 2020 at 20:26
1
$\begingroup$
vars = Symbol["w" <> ToString[#] <> ToString[#2]] & @@@ Tuples[{1, 2}, {2}]

{w11, w12, w21, w22}

Flatten @ Table[vars[[i]] vars[[j]], {i, 1, 4}, {j, 1, i}]

{w11^2, w11 w12, w12^2, w11 w21, w12 w21, w21^2, w11 w22, w12 w22, w21 w22, w22^2}

$\endgroup$
0
$\begingroup$
lst ={w11,w12,w21,w22}

Select[Tuples[{lst,lst}], OrderedQ]//Apply[Times,#,1]&

{w11^2,w11 w12,w11 w21,w11 w22,w12^2,w12 w21,w12 w22,w21^2,w21 w22,w22^2}

Original Answer

Times@@@((Tuples[{{lst[[#]]},lst[[#;;]]} ]&/@
  Range[4])//Catenate)
$\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.