Questions tagged [array-broadcasting]

Broadcasting (or singleton expansion) applies a function element-wise across one or more multidimensional arrays, matching shapes of the arguments by repeating missing or singleton dimensions. Be sure to also tag the programming language; many languages with strong array support have implicit or explicit broadcasting behaviors, sometimes with idiosyncratic rules.

Many languages and frameworks have implementations of broadcasting (also known as singleton expansion), including but not limited to:

Some lower-level languages, like (with getelementptr) and (with synchronizing warps) support broadcasting between scalars and vectors, but without support for higher dimensional arrays.

879 questions
4
votes
2 answers

Accumulate values of "neigborhood" from edgelist with numpy

I have a undirected network where each node can be one of k types. For each node i, I need to calculate the number of neighbors that node i has of each type. Right now I am representing the edges with an edgelist where the columns are indexes of the…
fgregg
  • 3,173
  • 30
  • 37
4
votes
1 answer

How to broadcast an array by columns?

I want to add every columns of a matrix to a numpy array, but numpy.broadcast only allows to add every rows of a matrix to an array. How can I do this? My idea is to first transpose the matrix then add it to the array then transpose back, but this…
tian tong
  • 793
  • 3
  • 10
  • 21
4
votes
1 answer

Working with Numpy arrays, getting an Error

I'm trying to code functions, that compute financial greeks for B-S model. I started with something like this: def greeks_vanilla(S, K, r, q, t, T, sigma): import numpy as np from scipy.stats import norm tau = np.linspace(t, T, num =…
Photon Light
  • 757
  • 14
  • 26
4
votes
2 answers

How is broadcasting applying in this example of numpy?

I am learning numpy and am a bit confused about broadcasting, here is my set up. I have two matrices >>> y=np.array([1,2,3]) >>> v = np.array([1,2,3]) >>> r=np.reshape(v, (3, 1)) So r is (3*1) matrix while y is a rank 1 matrix with shape…
Max
  • 9,100
  • 25
  • 72
  • 109
4
votes
1 answer

NumPy broadcasting doesn't work

I am trying to broadcast the difference between two vectors. This works for a simple case like this: In[1] : data = np.array([1,2]) In[2] : centers = np.array([[2,2],[3,3]]) In[3] : data - center Out[3] : array([[-1, 0], [-2,…
farhawa
  • 10,120
  • 16
  • 49
  • 91
4
votes
3 answers

How to perform a rolling sum along a matrix axis?

Given matrix X with T rows and columns k: T = 50 H = 10 k = 5 X = np.arange(T).reshape(T,1)*np.ones((T,k)) How to perform a rolling cumulative sum of X along the rows axis with lag H? Xcum = np.zeros((T-H,k)) for t in range(H,T): Xcum[t-H,:] =…
pvstrln
  • 169
  • 3
  • 10
4
votes
2 answers

automatic array calculus in python (numpy)

I have a function that depends on several variables, let's say y=f(x1,x2,x3,x4). If each of the variables is a simple number, then the result should be a plain number. If one of the variables is an array, I need the result to be also an array. And…
Amenhotep
  • 920
  • 1
  • 13
  • 18
4
votes
1 answer

Ellipsis broadcasting in numpy.einsum

I'm having a problem understanding why the following doesn't work: I have an array prefactor that can be three-dimensional or six-dimensional. I have an array dipoles that has four dimensions. The first three dimensions of dipoles match the last…
jan
  • 1,408
  • 13
  • 19
4
votes
2 answers

Broadcasting function calls in np.array

I am trying creating an NumPy array filled with an object, and I was wondering if there was a way I could broadcast to the entire array for each object to do something. Code: class player: def __init__(self,num = 5): self.num = num …
user2243024
  • 55
  • 1
  • 4
4
votes
1 answer

Python, Numpy, method of adding arrays together in multiple dimensions (broadcasting)

I have a number of different size arrays with a common index. For example, Arr1 = np.arange(0, 1000, 1).reshape(100, 10) Arr2 = np.arange(0, 500, 1).reshape(100,5) Arr1.shape = (100, 10) Arr2.shape = (100, 5) I want to add these together into a…
dantes_419
  • 189
  • 3
  • 14
3
votes
3 answers

Fancy indexing calculation of adjacency matrix from adjacency list

Problem: I want to calculate at several times the adjacency matrix A_ij given the adjacency list E_ij, where E_ij[t,i] = j gives the edge from i to j at time t. I can do it with the following code: import numpy as np nTimes = 100 nParticles =…
Puco4
  • 491
  • 5
  • 16
3
votes
4 answers

Keep only sub-arrays with one unique value at position 0

Starting from a Numpy nd-array: >>> arr [ [ [10, 4, 5, 6, 7], [11, 1, 2, 3, 4], [11, 5, 6, 7, 8] ], [ [12, 4, 5, 6, 7], [12, 1, 2, 3, 4], [12, 5, 6, 7, 8] ], [ [15, 4, 5, 6,…
Jivan
  • 21,522
  • 15
  • 80
  • 131
3
votes
1 answer

Vectorization of Product Over

I am seeking a vectorized form of the following computation: import numpy as np D = 100 N = 1000 K = 10 X = np.random.uniform(0, 1, (K, N)) T = np.random.uniform(0, 1000, (D, N)) out = np.zeros((D, K)) for i in range(D): for j in range(K): …
rw435
  • 305
  • 2
  • 11
3
votes
2 answers

Combine numpy arrays of different sizes into a bigger matrix

I want to make all possible combinations of numpy arrays of different sizes into a bigger matrix. For examples a = np.array([1, 2, 3, 4, 5]) , b = np.array([6, 7, 8]), c = np.array([9, 10, 3, 4, 5]) the output should be: array([[1., 6., 9.], …
mmcmp
  • 33
  • 4
3
votes
1 answer

How to use the output of argmin as index with Numpy

I want to find the location of minima along a given axis in a rank-3 numpy array. I have obtained these locations with np.argmin, however I'm not sure how to "apply" this to the original matrix to get the actual minima. For example: import numpy as…
Pythonist
  • 1,937
  • 1
  • 14
  • 25