2

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.

chia
  • 21
  • 3

2 Answers2

2

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}} *)
Arnoud Buzing
  • 9,801
  • 2
  • 49
  • 58
0

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}]
chia
  • 21
  • 3