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
5
votes
1 answer

Why does numpy.broadcast "transpose" results of vstack and similar functions?

Observe: In [1]: import numpy as np In [2]: x = np.array([1, 2, 3]) In [3]: np.vstack([x, x]) Out[3]: array([[1, 2, 3], [1, 2, 3]]) In [4]: np.vstack(np.broadcast(x, x)) Out[4]: array([[1, 1], [2, 2], [3, 3]]) Similarly for…
zegkljan
  • 8,051
  • 5
  • 34
  • 49
5
votes
4 answers

Why does numpy.dot behave in this way?

I'm trying to understand why numpy's dot function behaves as it does: M = np.ones((9, 9)) V1 = np.ones((9,)) V2 = np.ones((9, 5)) V3 = np.ones((2, 9, 5)) V4 = np.ones((3, 2, 9, 5)) Now np.dot(M, V1) and np.dot(M, V2) behave as expected. But for V3…
5
votes
3 answers

numpy shorthand for taking jagged slice

I have an operation that I'm doing commonly which I'm calling a "jagged-slice" because I don't know the real name for it. It's best explained by example: a = np.random.randn(50, 10) entries_of_interest = np.random.randint(10, size = 50) # Vector of…
Peter
  • 12,274
  • 9
  • 71
  • 86
5
votes
1 answer

Numpy function to get shape of added arrays

tl;dr: How do I predict the shape returned by numpy broadcasting across several arrays without having to actually add the arrays? I have a lot of scripts that make use of numpy (Python) broadcasting rules so that essentially 1D inputs result in a…
Jareth Holt
  • 321
  • 1
  • 10
4
votes
2 answers

Sum of sparse vectors: bug or feature?

I recently stumbled upon the following behavior in MATLAB R2022a: >> a = sparse(1,2,1) a = (1,2) 1 >> b = sparse(2,1,18) b = (2,1) 18 >> a+b ans = (2,1) 18 (1,2) 1 (2,2) 19 The presence of the (2,2)…
Ratbert
  • 5,463
  • 2
  • 18
  • 37
4
votes
2 answers

Vectorized way to contract Numpy array using advanced indexing

I have a Numpy array of dimensions (d1,d2,d3,d4), for instance A = np.arange(120).reshape((2,3,4,5)). I would like to contract it so as to obtain B of dimensions (d1,d2,d4). The d3-indices of parts to pick are collected in an indexing array Idx of…
4
votes
2 answers

np.where for 2d array, manipulate whole rows

I want to rebuild the following logic with numpy broadcasting function such as np.where: From a 2d array check per row if the first element satisfies a condition. If the condition is true then return the first three elements as a row, else the last…
malwin
  • 652
  • 7
  • 18
4
votes
1 answer

Preserving Array's element-type union in loops or broadcasts to avoid dynamic dispatch

I want to do elementwise operations on Array{Union{A,B,C}, M}, and I want an output of Array{Union{A,B,C}, N} without dynamic dispatch caused by failure to infer the Union{A,B,C} element type. I pasted the examples I'm playing with below. I am aware…
BatWannaBe
  • 4,330
  • 1
  • 14
  • 23
4
votes
1 answer

Broadcasting on the middle dimension of a 3D array

In Python I can have an numpy array a with dimensions (2, 3, 2) and b with (3) and do c = a[:, :, :] + b[None, :, None] I did not manage to figure out how to do this with Julia broadcast, because I do not know how to select the middle dimension. c…
Chiel
  • 6,006
  • 2
  • 32
  • 57
4
votes
1 answer

Broadcasting over two arrays with different shapes (Numpy-Python)

Suppose I have the following arrays: first_array = array([[1, 8, 3, 9, 2], [2, 6, 4, 1, 9], [4, 2, 12, 8, 16], [5, 3, 7, 18, 21], [6, 20, 4, 8, 24]]) So an array…
4
votes
3 answers

Python list reversed can't be accessed more than once

Can someone explain this to me? import numpy as np arr = reversed(np.arange(11)) print(list(arr)) print(list(arr)) print(list(arr)) the output of this code is : why can't I access the arr variable more than once?
4
votes
2 answers

reducing loops with numpy

we are trying to implement the given Modified Gram Schmidt algorithm: We first tried to implement lines 5-7 in the next way: for j in range(i+1, N): R[i, j] = np.matmul(Q[:, i].transpose(), U[:, j]) u = U[:, j] - R[i, j] * Q[:, i] U[:,…
ronihm
  • 65
  • 4
4
votes
1 answer

Julia: applying function across fields of different struct instances

Let's imagine I have an array my_array whose elements are instances of myType, so the array type is given by Array{myType,1}. Now let's say each instance of myType within my_array has some field x that stores a vector of data (e.g.…
Conor
  • 691
  • 5
  • 14
4
votes
2 answers

Vectorizing Pairwise Column Element-wise Product in NumPy

I have two DataFrames: >>> d1 A B 0 4 3 1 5 2 2 4 3 >>> d2 C D E 0 1 4 7 1 2 5 8 2 3 6 9 >>> what_I_want AC AD AE BC BD BE 0 4 16 28 3 12 21 1 10 25 40 4 10 16 2 12 24 36 9 18 …
4
votes
3 answers

Numpy add with multiple arrays

Is there a way to add (as opposed to sum) multiple arrays together in a single operation? Obviously, np.sum and np.add are different operations, however, the problem I'm struggling with right now is that np.add only takes two arrays at once. I could…
Firnagzen
  • 1,212
  • 3
  • 13
  • 29