Questions tagged [vectorization]

Vectorization refers to a programming paradigm where functions operate on whole arrays in one go. This affords benefits in terms of function calls, memory access, parallelization and code expressiveness. Some programming languages, such as MATLAB, are optimised to give the best performance when vectorized.

Vectorization refers to a programming paradigm where the process of loop-based, scalar-oriented code is instead written using matrix and vector operations. Vectorization has the following benefits:

  • Performance: Vectorized code has better performance regarding function calls and memory access, and as a result, often runs much faster than the corresponding code containing loops.

  • Appearance: Vectorized code appears more like the textbook mathematical expressions, making the code more comprehensible.

  • Less Error Prone: vectorized code is shorter than loop based code, hence there are fewer opportunities to introduce programming bugs.

Some programming languages, in particular MATLAB, are optimised to give the best performance when vectorized.

5526 questions
2323
votes
10 answers

Why are elementwise additions much faster in separate loops than in a combined loop?

Suppose a1, b1, c1, and d1 point to heap memory, and my numerical code has the following core loop. const int n = 100000; for (int j = 0; j < n; j++) { a1[j] += b1[j]; c1[j] += d1[j]; } This loop is executed 10,000 times via another outer…
Johannes Gerer
  • 24,320
  • 5
  • 24
  • 33
539
votes
11 answers

Difference between map, applymap and apply methods in Pandas

Can you tell me when to use these vectorization methods with basic examples? I see that map is a Series method whereas the rest are DataFrame methods. I got confused about apply and applymap methods though. Why do we have two methods for applying a…
marillion
  • 8,506
  • 16
  • 41
  • 60
361
votes
4 answers

Is there an R function for finding the index of an element in a vector?

In R, I have an element x and a vector v. I want to find the first index of an element in v that is equal to x. I know that one way to do this is: which(x == v)[[1]], but that seems excessively inefficient. Is there a more direct way to do it? For…
Ryan C. Thompson
  • 37,328
  • 27
  • 87
  • 147
236
votes
7 answers

What is "vectorization"?

Several times now, I've encountered this term in matlab, fortran ... some other ... but I've never found an explanation what does it mean, and what it does? So I'm asking here, what is vectorization, and what does it mean for example, that "a loop…
Thomas Geritzma
  • 5,447
  • 6
  • 22
  • 19
143
votes
4 answers

Is the "*apply" family really not vectorized?

So we are used to say to every R new user that "apply isn't vectorized, check out the Patrick Burns R Inferno Circle 4" which says (I quote): A common reflex is to use a function in the apply family. This is not vectorization, it is loop-hiding.…
David Arenburg
  • 87,271
  • 15
  • 123
  • 181
130
votes
9 answers

Why can't R's ifelse statements return vectors?

I've found R's ifelse statements to be pretty handy from time to time. For example: ifelse(TRUE,1,2) # [1] 1 ifelse(FALSE,1,2) # [1] 2 But I'm somewhat confused by the following behavior. ifelse(TRUE,c(1,2),c(3,4)) # [1]…
Christopher DuBois
  • 38,442
  • 23
  • 68
  • 91
125
votes
6 answers

Efficient evaluation of a function at every cell of a NumPy array

Given a NumPy array A, what is the fastest/most efficient way to apply the same function, f, to every cell? Suppose that we will assign to A(i,j) the f(A(i,j)). The function, f, doesn't have a binary output, thus the mask(ing) operations won't…
Peter
  • 1,511
  • 3
  • 11
  • 10
122
votes
2 answers

Are for-loops in pandas really bad? When should I care?

Are for loops really "bad"? If not, in what situation(s) would they be better than using a more conventional "vectorized" approach?1 I am familiar with the concept of "vectorization", and how pandas employs vectorized techniques to speed up…
cs95
  • 274,032
  • 76
  • 480
  • 537
108
votes
11 answers

How can I apply a function to every row/column of a matrix in MATLAB?

You can apply a function to every item in a vector by saying, for example, v + 1, or you can use the function arrayfun. How can I do it for every row/column of a matrix without using a for loop?
FurtiveFelon
  • 12,838
  • 27
  • 72
  • 96
97
votes
8 answers

Do any JVM's JIT compilers generate code that uses vectorized floating point instructions?

Let's say the bottleneck of my Java program really is some tight loops to compute a bunch of vector dot products. Yes I've profiled, yes it's the bottleneck, yes it's significant, yes that's just how the algorithm is, yes I've run Proguard to…
Sean Owen
  • 63,876
  • 22
  • 135
  • 169
76
votes
3 answers

Why is vectorization, faster in general, than loops?

Why, at the lowest level of the hardware performing operations and the general underlying operations involved (i.e.: things general to all programming languages' actual implementations when running code), is vectorization typically so dramatically…
Ben Sandeen
  • 1,063
  • 1
  • 13
  • 15
68
votes
7 answers

numpy array TypeError: only integer scalar arrays can be converted to a scalar index

i=np.arange(1,4,dtype=np.int) a=np.arange(9).reshape(3,3) and a >>>array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) a[:,0:1] >>>array([[0], [3], [6]]) a[:,0:2] >>>array([[0, 1], [3, 4], [6,…
kinder chen
  • 1,177
  • 2
  • 10
  • 17
64
votes
1 answer

Does ifelse really calculate both of its vectors every time? Is it slow?

Does ifelse really calculate both the yes and no vectors -- as in, the entirety of each vector? Or does it just calculate some values from each vector? Also, is ifelse really that slow?
Ricardo Saporta
  • 51,025
  • 13
  • 129
  • 166
56
votes
3 answers

Is indexing vectors in MATLAB inefficient?

Background My question is motivated by simple observations, which somewhat undermine the beliefs/assumptions often held/made by experienced MATLAB users: MATLAB is very well optimized when it comes to the built-in functions and the fundamental…
angainor
  • 11,652
  • 2
  • 33
  • 54
55
votes
5 answers

Vectorized way of calculating row-wise dot product two matrices with Scipy

I want to calculate the row-wise dot product of two matrices of the same dimension as fast as possible. This is the way I am doing it: import numpy as np a = np.array([[1,2,3], [3,4,5]]) b = np.array([[1,2,3], [1,2,3]]) result = np.array([]) for…
Cupitor
  • 9,330
  • 16
  • 53
  • 88
1
2 3
99 100