1
$\begingroup$

I want to take two lists of the same length: widths and weights, and sum a function (of 3 variables) over a single index using the elements from both lists that have that index. Then I want to plot the function.

If I only sum over one of the lists, I can use

scatterfunc[x1_, x2_, y1_] = 
  Sum[y1/(4*x1) + (1 - y1)/(4*x2) - 
  y1/(gamma + x1) - (1 - y1)/(gamma + x2), {gamma, widths}];
Manipulate[
 ContourPlot[scatterfunc[x1, x2, y1], {x1, 0, 1}, {x2, 0, 1}, 
 ColorFunction -> "GrayTones", PlotLegends -> Automatic], {y1, 0, 1}]

But I've been unable to find the equivalent of python's zip(widths,weights) in order to sum over a single index using both lists. I want something like

scatterfunc[x1_, x2_, y1_] = 
  Sum[y1/(4*x1) + (1 - y1)/(4*x2) - 
  alpha*y1/(gamma + x1) - (1 - y1)/(gamma + x2), {{gamma,alpha}, zip[widths,weights]}];
Manipulate[
 ContourPlot[scatterfunc[x1, x2, y1], {x1, 0, 1}, {x2, 0, 1}, 
 ColorFunction -> "GrayTones", PlotLegends -> Automatic], {y1, 0, 1}]

Thanks.

$\endgroup$

closed as off-topic by Jens, m_goldberg, MarcoB, Bob Hanlon, RunnyKine Jul 30 '15 at 2:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Jens, m_goldberg, MarcoB, Bob Hanlon, RunnyKine
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ zip() is effectively the same as Transpose[], but your problem looks as if it will be better served by MapThread[] + Total[]. $\endgroup$ – J. M. is away Jul 29 '15 at 16:46
  • $\begingroup$ You can do something like Sum[...ww[[1]]...ww[[2]],{ww,Transpose@{widths,weights}} ]. Where ww[[1]] is width, and ww[[2]] is weight. $\endgroup$ – N.J.Evans Jul 29 '15 at 16:47
  • 2
    $\begingroup$ You don't even need Sum at all because Plus and Times automatically thread over lists. Start by reading http://reference.wolfram.com/language/howto/CombineAndRearrangeLists.html $\endgroup$ – Jens Jul 29 '15 at 16:49
  • $\begingroup$ @N.J.Evans, this worked perfectly. I had tried transpose, but without '@'. I'm not super familiar with mathematica. What does '@' do? $\endgroup$ – chia Jul 29 '15 at 17:02
  • $\begingroup$ @chia, f @ x is just the same as f[x]; a prefix form of function application to a single argument. $\endgroup$ – J. M. is away Jul 29 '15 at 17:06
1
$\begingroup$

In python, the zip function does this:

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]

You can do this in the Wolfram Language, for example, with MapThread:

x = {1,2,3};
y = {4,5,6};
MapThread[ List, {x, y} ]  (* gives {{1,4}, {2,5}, {3,6}} *)
$\endgroup$
  • $\begingroup$ Isn't Transpose[] more "traditional"? $\endgroup$ – J. M. is away Jul 29 '15 at 16:47
  • $\begingroup$ Also, if one wants the full generality of zip(), Flatten[] with a properly set second argument might be more appropriate. $\endgroup$ – J. M. is away Jul 29 '15 at 16:56
0
$\begingroup$

Thanks @N.J.Evans, this solved my problem.

scatterfunc[x1_, x2_, y1_] = 
  Sum[y1/(4*x1) + (1 - y1)/(4*x2) - 
  ww[[2]]*y1/(ww[[1]] + x1) - (1 - y1)/(ww[[1]] + x2), {ww, 
  Transpose@{widths, weights}}];
Manipulate[
 ContourPlot[scatterfunc[x1, x2, y1], {x1, 0, 1}, {x2, 0, 1}, 
 ColorFunction -> "GrayTones", PlotLegends -> Automatic], {y1, 0, 1}]
$\endgroup$
  • 2
    $\begingroup$ Consider Total[y1/(4*x1) + (1 - y1)/(4*x2) - weights y1/(widths + x1) - (1 - y1)/(widths + x2)] to exploit listability. $\endgroup$ – J. M. is away Jul 29 '15 at 17:00

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