Questions tagged [vectorization]

Use this tag if your code contains vectorized operations - where functions operate on whole arrays in one go. If it doesn't, but you want it to, then you should make those changes (perhaps with help from elsewhere) before presenting code for review.

Filter by
Sorted by
Tagged with
11 votes
3 answers
2k views

My blit function for my own graphics library

Here is a blit function for a graphics library I made. I've built a small graphics library that uses an palette-indexed spritesheet to hold all of the game's sprites. The blit function copies parts of ...
coqcauderanais's user avatar
11 votes
1 answer
580 views

AVX Vectorized Multi-threaded Mandelbrot Renderer

I wrote a Mandelbrot renderer in C++ that uses AVX intrinsics to calculate 8 pixels per thread in parallel. It can compute 1080p frames with 256 max iterations in about 40 milliseconds on a 2 core 4 ...
Saad's user avatar
  • 211
11 votes
1 answer
320 views

Calculate the closest point to many hyperbolic paraboloids

In this question I asked for a way to compute the closest projected point to a hyperbolic paraboloid using python. Using the iterative approximation answer, I'm able to use the code below to ...
Fnord's user avatar
  • 383
10 votes
2 answers
2k views

Vectorized and Multi Threaded Image Convolution

I created code for Image Convolution. The code is in my Image Convolution GitHub Repository. It includes the case for arbitrary Image Convolution and for Separable Kernel Convolution. The code is a ...
Royi's user avatar
  • 582
10 votes
2 answers
370 views

N-Body Optimization

I've created a serial C++ code for gravitational N-Body calculation. Because I expect to have upwards of 8-71 sparse bodies (ie, where Barnes-Hut is not necessarily practical) and running for long ...
Marius Popescu's user avatar
10 votes
1 answer
615 views

Vectorizing the product of an array of complex numbers

I am trying to write fast/optimal code to vectorize the product of an array of complex numbers. In simple C this would be: ...
Simd's user avatar
  • 191
9 votes
2 answers
19k views

Efficient element-wise function computation in Python

I have the following optimization problem: Given two np.arrays X,Y and a function K I ...
Daniele's user avatar
  • 91
8 votes
1 answer
163 views

Matlab Computer Vision

The code below takes a video as input (a drop onto a fabric surface) and I want it to return the area of the drop. Currently the code works, but I'm no Matlab expert, and wrote it entirely through ...
Chris Carr's user avatar
8 votes
1 answer
2k views

Vectorized numpy version of arange with multiple start stop

Given a single start and stop, numpy.arange is a good solution for building a NumPy array of evenly spaced values. However, given an array of start and an array of ...
andrew's user avatar
  • 215
7 votes
3 answers
344 views

Improving Matlab function within big simulation

I have a very big Matlab simulation project in my hands, which I wanted to optimize, since I'm running it many times to tune parameters and the like. Using Matlab's ...
titus.andronicus's user avatar
7 votes
2 answers
4k views

Rolling regressions in R

I want to run rolling regressions over each group and store the coefficient. The following works, but it's slow, since I have too many series and I want to run too many regressions for each group. ...
Konstantinos's user avatar
7 votes
1 answer
954 views

Implementation of linear regression in Python

I wrote an implementation for multivariate linear regression in Python, for data in this link: http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv My main focus is to avoid loops as much as possible ...
Algo's user avatar
  • 339
7 votes
3 answers
3k views

Converting Array of Floats to UINT8 (`char`) or UINT16 (`unsigned short`) Using SSE4

The problem is given image in 32 Bit Floating Point Format (float) how to convert it to UINT8 (char) or UNIT16 (...
Royi's user avatar
  • 582
7 votes
1 answer
8k views

Remove duplicates in list of lists where an item in each list is ignored when determining duplicity

I have lines_list_copy of the form: [['a1', 'b1', 'c1', 'd1', 'e1'], ['a2', 'b2', 'c2', 'd2', 'e2'], ... ] I needed to ...
Frikster's user avatar
  • 203
7 votes
1 answer
156 views

Catalogue manipulation

I have a catalogue of 56000 objects and I want to replace some columns with new values for instance replace NaN with -99 and <...
Dalek's user avatar
  • 133
7 votes
1 answer
2k views

Merging overlapping/intersecting string vectors

I am trying to merge overlapping/intersecting sets given as a list of string vectors below in R (my actual data set has thousands of such sets). The overlap/intersection is based on the ...
Crops's user avatar
  • 173
6 votes
2 answers
8k views

Compute a numerical derivative

Since I could not get numpy.gradient() to compute a derivative successfully, I wrote a script to compute it manually. Running the script below will output a plot of ...
user avatar
6 votes
2 answers
3k views

Remove outliers from a point cloud

This function accepts a cloud of points, and returns those points that are within delta distance of the average (mean) position. ...
adrienlucca.net's user avatar
6 votes
1 answer
293 views

How to make my groupby and transpose operations efficient?

I have a DataFrame of size 3,745,802 rows and 30 columns. I would like to perform certain groupby and ...
The Great's user avatar
  • 237
6 votes
1 answer
121 views

Calculating parking and charging statistics for a car-sharing service

I am looking for help in designing my code more efficiently and also in keeping the slope of my personal learning curve steep: For a research project I have per car data from two different car ...
Jonathan's user avatar
6 votes
0 answers
389 views

C# Generic high performance vectorized math operations

I'm writing a library which simplifies the usage of intrinsics in C#. It's a generic library which supports all numeric types. The goal of this library is to perform these operations at the highest ...
Gilad Freidkin's user avatar
5 votes
1 answer
492 views

Vectorize Matlab sum

I have the following Matlab function: function [res] = a3_funct(x) res = 0; for i = 1:size(x,1) res = res + abs(x(i))^(i+1); end end It's ...
Seanny123's user avatar
  • 1,527
5 votes
2 answers
560 views

Jacobian product

Do you have in mind a way to compute the following sum $$R_{i,j,k} = \sum_{l=0}^{2} J_{i,j,l+3k} v_{i,j,l}$$ obviously in a vectorised fashion? This may sound like an equivalent of: ...
John Ludos's user avatar
5 votes
1 answer
144 views

Vector comparison inside for-loop

The following code finds no-rain periods in a time series that were preceded by rain over a threshold (e.g. 10mm in 2 days [k=48]). ...
naught101's user avatar
  • 153
5 votes
2 answers
97 views

Extract the largest value for each day from a matrix

I have a matrix in which the right-most elements are repeated YYYYMMDD dates in descending order, for example: ...
Stewie Griffin's user avatar
5 votes
1 answer
2k views

Evaluating a series of Legendre polynomials

The following function represents the electrostatic potential, in spherical coordinates, due to a ring of charge \$q=1\$ and radius \$R=1\$, placed in the plane \$x\$-\$y\$: $$\phi(r,\theta) = \sum_{...
vagoberto's user avatar
  • 153
5 votes
2 answers
101 views

Efficient calculation of bond overlap

For a small library dealing with molecules, I have to calculate a so called connectivity table for the chemical bonds. I assume that there is a bond between atom i and atom j, if the distance between ...
mcocdawc's user avatar
  • 944
5 votes
1 answer
110 views

Vectorization for temperature simulation

I'm new to Matlab and I would like to know tips in order to reduce the processing time of such program. The scheme of the actual code is very similar to typical C++ code and I would like to vectorize ...
jaugas's user avatar
  • 59
4 votes
1 answer
132 views

Biometric matching score calculator

I'm working on a biometric matching system and I would like to have few suggestions regarding vectorizing the following code: ...
shreyas kamath's user avatar
4 votes
2 answers
1k views

PyTorch Vectorized Implementation for Thresholding and Computing Jaccard Index

I have been trying to optimize a code snippet which finds the optimal threshold value in a n_patch * 256 * 256 probability map to get the highest Jaccard index ...
yiping's user avatar
  • 73
4 votes
1 answer
880 views

Optimization of multiple arrays for filtering

My app will be running on an iPad with A7. I pass in 3 JSON arrays and right now do some simple filtering, in the future the operation will be much more complicated. I realize the ...
μολὼν.λαβέ's user avatar
4 votes
1 answer
175 views

Vectorized 16-bit addition in Standard C

The idea is to add a given 16-bit number N to each element of an array of 16-bit integers of arbitrary length, taking advantage of 64-bit integer types and instructions to perform the additions 4 at a ...
user16217248's user avatar
4 votes
1 answer
149 views

Vectorized crosstabulation in Python for two arrays with two categories each

I have two Python lists, label and presence. I want to do cross-tabulation and get the count for each block out of four, such as ...
StatguyUser's user avatar
4 votes
1 answer
1k views

C# System.Numerics.Vector<T> Clamp

The software that I develop uses large floating-point arrays up to the maximum size that can be allocated in C#. I have a large number of algorithms such as convolutions and filters that get executed ...
deegee's user avatar
  • 143
4 votes
2 answers
465 views

Vectorizing a pixel-averaging operation in Numpy

I am reading from a file containing some segments (irregular parcels of the image) and trying to average the entire segment to have one pixel value. This is the code I use: ...
shaunakde's user avatar
  • 245
4 votes
2 answers
128 views

Vectorizing a polar coordinate conversion loop in a fingerprint-matching process

I am working on a fingerprint matching technique and it takes a lot of computation time to obtain the result. I am trying to implement the matching process given in A Minutia Matching Algorithm in ...
shreyas kamath's user avatar
4 votes
1 answer
432 views

Bernoulli trials using a condition in a vectorized operation

I was wondering how to vectorize the following code instead of using the for-loop and still be able to check for a condition. ...
Dataman's user avatar
  • 227
4 votes
1 answer
370 views

Implementation of Iterative Closest Point in C++

Here, is my implementation of Iterative Closest Point algorithm in C++. The code is written using the Eigen library. I have tried to implement an efficient coding methodology best to my knowledge ...
Aryaman Patel's user avatar
4 votes
1 answer
50 views

optimize binary classification method for speed

I have the following code for determining TP, TN, FP and FN values for binary classification given two sparse vectors as input (using the sparse library): ...
horcle_buzz's user avatar
4 votes
1 answer
92 views

Can this numpy code be vectorized?

I've written the following function to produce n realizations of a CIR process for a given set of parameters: ...
John Smith's user avatar
4 votes
2 answers
578 views

Vectorizing a complex nested for loop in R (running models on different subsets of a data set, subsetting the data differently for each loop)

This is my first time posting so hopefully I manage to get my question across in a clear and concise manner. I'm not sure this is the right place but someone on StackOverflow suggested I ask here. I ...
Tal's user avatar
  • 41
4 votes
1 answer
95 views

Vectorizing Two Sample Kolmogorov-Smirnov test

I'm implementing the Two-sample Kolmogorov-Smirnov test in MATLAB. I must admit I know very little of formal statistics and was simply trying to implement the description in wikipedia's page. Right ...
Sciencertobe's user avatar
4 votes
1 answer
895 views

Implementation of MATLAB's `im2col`

The function im2col in MATLAB is very useful to vectorize Patch based Image Processing algorithms. The problem is the function isn't optimized and doesn't use C ...
Royi's user avatar
  • 582
4 votes
1 answer
314 views

Optimizations of C Code - Bilateral Filter

I'm working on a nice approximation for the Bilateral Filter. I have a working code which runs pretty fast yet still I think much can be improved. The code (C Code,...
Royi's user avatar
  • 582
3 votes
2 answers
7k views

Generate random unit vectors around circle

I'm trying to generate a bunch of uniformly distributed unit vectors around the unit circle. Here's my code, which is working, but unfortunately I have a for-loop. How do I get rid of this for-loop? <...
Seanny123's user avatar
  • 1,527
3 votes
1 answer
921 views

Using Gibbs sampling to segment an image

I have been reading some NumPy guides but can't seem to figure it out. My TA told me I should be able to speed up my code by using a NumPy array instead of a for ...
marcopolo's user avatar
  • 131
3 votes
1 answer
90 views

Generic pixel class to seamlessly alpha-blend and convert between different pixel structure layouts

Does what it says in the title. I just finished this and wanted to share with someone. Looking for possible optimizations, bugs (most of it is tested to work) or any constructive criticism. ...
user5434231's user avatar
3 votes
1 answer
224 views

Vectorizing matrix operation instead of for loop in circular matrices

I have a vector \$X_k\$ and a matrix \$Y_{k,j}\$, where \$k \in \{1,\dots,K\}\$ and \$j \in \{1, \dots, J\}\$. They are circular, meaning that $$\eqalign{X_{k+K} &= X_k \\ Y_{k+K,j} &= Y_{k,j} ...
Xbel's user avatar
  • 197
3 votes
1 answer
68 views

Determining whether a list of pathways and its genes are all included in another list

I am evaluating if a list of pathways and its genes has pathways included in another pathways. So the way I thought about it is to create a function that does just a single comparison. ...
llrs's user avatar
  • 281
3 votes
1 answer
64 views

Function that fills a time series row-by-row by using the values in the row before

Summary: performance issue with for-loop, vectorization possible? I am processing a time series of securities price data, stored in a pandas dataframe df_buy_sell. ...
sudonym's user avatar
  • 153