5
$\begingroup$

Basically, the title says it all. I am familiar with python, not so much with mathematica. For the program, I am starting out with three unique lists of values for which I am using a triple nested For loop to extract each term. Then, I am multiplying these extracted terms together and appending them to a new blank list. Since I feel I may not be explaining my intentions that well, my basic goal is to go from:

listA = {1, 2, 3}

listB = {4, 5, 6}

listC = {7, 8}

listFinal = {}

to then appending listFinal in a manner to obtain:

listFinal = {1*4*7, 1*4*8, 1*5*7, 1*5*8, 1*6*7, 1*6*8, 2*4*7, 2*4*8, etc...}

When I would create this loop in python, I would start with a blank list (listFinal), that I would then append these multiplied values to after indexing each value at the end of a triple nested For loop.

However, when I attempt to create this program in mathematica, I am left with a non-appended (blank) listFinal. Do you all know of a way to fix my nested For loop so it would function as intended, or use a different method of achieving my desired results? I've been searching through forums and have been met with the consensus that for loops are not the most efficient/best method of programming in mathematica. However, I am most familiar with using them, which is why my attempts so far have been with only for loops.

My original program is as follows:

m = List[.9, .8, .7, .8, .7, .6]
g = List[1, .95, .9, .95, .85, .75]
b = List[.85, .75, .65]

effectiveFlux = List[];

For[i = 1, i < 7, i++,
 For[j = 1, j < 7, j++,
  For[k = 1, k < 4, k++,
   var = m[[{i}]]*g[[{j}]]*b[[{k}]]
     Append[effectiveFlux, var]]]]

Thank you in advance!

$\endgroup$
5
  • $\begingroup$ Use AppendTo [ ] ... but that isn't good Mathematica coding style $\endgroup$ Commented Mar 30, 2016 at 0:48
  • $\begingroup$ Have you looked at Outer[]? $\endgroup$ Commented Mar 30, 2016 at 0:50
  • $\begingroup$ I just tried AppendTo and I get the error message that "Objects of unequal length... cannot be combined" Does this have to do with the fact that my three initial lists have a different number of elements? $\endgroup$
    – MikeS
    Commented Mar 30, 2016 at 0:53
  • $\begingroup$ Outer[] seems to look very promising, I will be trying that in a moment to see how it works out! $\endgroup$
    – MikeS
    Commented Mar 30, 2016 at 0:55
  • $\begingroup$ @MikeS You also need a semicolon after the var assignment $\endgroup$ Commented Mar 30, 2016 at 0:55

4 Answers 4

5
$\begingroup$

It is understandable that you feel more comfortable using loops than list-processing functions at first. But have a little faith on what you've already read: loops have very restricted good applications in Mathematica.

m = List[.9, .8, .7, .8, .7, .6];
g = List[1, .95, .9, .95, .85, .75];
b = List[.85, .75, .65];

effectiveFlux = Flatten@Outer[Times, m, g, b]
$\endgroup$
3
  • $\begingroup$ Using this method, I get the error that "nonatomic expression expected at position 2 in Outer[Times, m, g, b]" Any idea what might be causing that? $\endgroup$
    – MikeS
    Commented Mar 30, 2016 at 1:34
  • $\begingroup$ @MikeS Try to run it again in a fresh kernel (restart Mathematica) $\endgroup$ Commented Mar 30, 2016 at 1:38
  • $\begingroup$ That seems to have done the trick. Thank you so much!!!! $\endgroup$
    – MikeS
    Commented Mar 30, 2016 at 1:41
5
$\begingroup$
ef2 = Times @@@ Tuples[{m, g, b}];
ef3 = Tuples[Hold[Times][m, g, b]] // ReleaseHold;
ef4 = Tuples[times[m, g, b]] /. times -> Times;
ef5 = Distribute[{m, g, b}, List, List, List, Times];

ef2

{0.765, 0.675, 0.585, 0.72675, 0.64125, 0.55575, 0.6885, 0.6075, 0.5265, 0.72675, 0.64125, 0.55575, 0.65025, 0.57375, 0.49725, 0.57375, 0.50625, 0.43875, 0.68, 0.6, 0.52, 0.646, 0.57, 0.494, 0.612, 0.54, 0.468, 0.646, 0.57, 0.494, 0.578, 0.51, 0.442, 0.51, 0.45, 0.39, 0.595, 0.525, 0.455, 0.56525, 0.49875, 0.43225, 0.5355, 0.4725, 0.4095, 0.56525, 0.49875, 0.43225, 0.50575, 0.44625, 0.38675, 0.44625, 0.39375, 0.34125, 0.68, 0.6, 0.52, 0.646, 0.57, 0.494, 0.612, 0.54, 0.468, 0.646, 0.57, 0.494, 0.578, 0.51, 0.442, 0.51, 0.45, 0.39, 0.595, 0.525, 0.455, 0.56525, 0.49875, 0.43225, 0.5355, 0.4725, 0.4095, 0.56525, 0.49875, 0.43225, 0.50575, 0.44625, 0.38675, 0.44625, 0.39375, 0.34125, 0.51, 0.45, 0.39, 0.4845, 0.4275, 0.3705, 0.459, 0.405, 0.351, 0.4845, 0.4275, 0.3705, 0.4335, 0.3825, 0.3315, 0.3825, 0.3375, 0.2925}

Equal @@ {ef2, ef3, ef4, ef5,effectiveFlux}

True

$\endgroup$
2
$\begingroup$

To make mental transition from For smoother, consider also Table way of looping:

tmp = Flatten@
  Table[m[[i]] g[[j]] b[[k]], {i, Length@m}, {j, Length@g}, {k, Length@b}]

Check

ef2 == effectiveFlux == tmp
(* True *)
$\endgroup$
0
$\begingroup$
efAlt = Distribute[{m , g , b}, List, List, List, Times]

and

efAlt == effectiveFlux == ef2

True

where

m = List[.9, .8, .7, .8, .7, .6];
g = List[1, .95, .9, .95, .85, .75];
b = List[.85, .75, .65];

In addition:

Distribute[{{1, 2, 3}, {4, 5, 6}, {7, 8}}, List]

{

{1, 4, 7}, {1, 4, 8}, {1, 5, 7}, {1, 5, 8}, {1, 6, 7}, {1, 6, 8},

{2, 4, 7}, {2, 4, 8}, {2, 5, 7}, {2, 5, 8}, {2, 6, 7}, {2, 6, 8},

{3, 4, 7}, {3, 4, 8}, {3, 5, 7}, {3, 5, 8}, {3, 6, 7}, {3, 6, 8}

}

$\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.