1

I have this set of equations I want to perform:

x = np.linspace(0, 2, 3)
y = np.linspace(x, x+2, 3)

I then want to populate the 2D array with a calculation that does:

a = 2*x + y

So for example, given an array:

x = [0, 1, 2]

Then, the array y is:

y = [[0, 1, 2],
     [1, 2, 3],
     [2, 3, 4]]

When I perform the operation a = 2*x + y I should get the array:

a = [[0, 1, 2],
     [3, 4, 5],
     [6, 7, 8]]

How do I do this, keeping in mind I want to perform this operation quickly for array of size up to 10000x10000 (or larger)?

Athena
  • 320
  • 2
  • 12
  • 1
    `np.add.outer(2*x+x,x)` with `x` as an array? – Divakar Jan 13 '19 at 08:17
  • @Divakar What about in the case where my `y` linspace has non-integer increments, e.g. `y = np.linspace(x, x+2, 1000)` ? How will that work in that case? – Athena Jan 13 '19 at 08:46
  • 1
    What exactly do you intend to get with `np.linspace(x, x+2, 3)`? If `y = [[0, 1, 2], [1, 2, 3], [2, 3, 4]]` is the one for the given sample, then it won't give you the final expected output. – Divakar Jan 13 '19 at 08:55
  • @Divakar In the code I have, the lower bound of the second linspace being controlled by the x, linspace: e.g. `y = np.linspace(x, upper_limit, num_of_nums)` So for every value in x, then I am trying to create a y linspace with the value of x as a lower bound. I then want to use the y linspace and the x linspace to generate a 2D array of all the possible combinations that `a = 2*x + y` has. Is that making sense? It is a bit hard for me to articulate clearly. I can rephrase if that isn't helping – Athena Jan 13 '19 at 09:05
  • 1
    See this - https://stackoverflow.com/questions/40624409/. – Divakar Jan 13 '19 at 09:08
  • Thanks, I can combine that with @U9-Forward answer, to generate the output – Athena Jan 13 '19 at 09:14

1 Answers1

1

Or do your code adding two Ts:

print((2*x+y.T).T)

Output:

[[0 1 2]
 [3 4 5]
 [6 7 8]]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114