9
votes
My blit function for my own graphics library
it seems to be optimized quite well by the compiler, as it uses a lot of vectorized instructions
Well, kind of. GCC didn't use vectorization at all, and Clang used some of it but in a very strange ...
- 7,409
8
votes
Accepted
Scaling and incrementing non-zero elements of a NumPy matrix
This is a typical use case for numpy.where:
cPrime = np.where(C, C * 30 + 1, C)
This is about twice as fast as ...
- 49.4k
8
votes
Accepted
Vectorized and Multi Threaded Image Convolution
One problem that immediately jumps out at me is the loop-carried dependency through addps, which has a latency of either 3 or 4 (depending on the processor) while ...
- 7,409
8
votes
Accepted
Implementation of linear regression in Python
Just reviewing normalizeFeatures.
Instead of a comment explaining what the function does, write a docstring. (Docstrings are available from the interactive ...
- 49.4k
8
votes
AVX Vectorized Multi-threaded Mandelbrot Renderer
There are some & 0xff operations that are not necessary:
(aMask & (~iMask & 0xff)), because the bits reset by ...
- 7,409
7
votes
Accepted
Rolling regressions in R
For each group in your data table, your code computes the coefficient b1 from a linear regression y = b0 + b1*x + epsilon, and ...
- 1,136
7
votes
Accepted
Improving Matlab function within big simulation
Vectorization
If you look at all the calls to mean, you see that what you are calculating in each loop is a "Cumulative moving average", but in the oposite ...
- 2,057
7
votes
Biometric matching score calculator
Regarding the vectorized code:
permute can be considered a "Zero-cost operation".
Logical operations are performed fastest using ...
- 2,057
7
votes
My blit function for my own graphics library
There are a few things that can be lifted out of loops.
The three screen related variables, screen_height, screen_width, and <...
- 7,646
6
votes
Improving Matlab function within big simulation
A few notes:
I'm confused about the design of your function. If you are only processing column vectors, why not just process a row vector instead? That's what I would expect when calling this ...
- 21.8k
6
votes
Accepted
Generate random unit vectors around circle
To answer your question, you need to add a new dimension to the ndarray:
vecs /= mags[..., np.newaxis]
However
uniformly ...
- 9,563
6
votes
Accepted
Optimization of multiple arrays for filtering
There may exist a way to make this particular loop run faster. In fact, I'm sure there probably is... however, it's kind of a futile effort.
There's a lot of time also being wasted elsewhere where ...
- 25.2k
6
votes
Accepted
Remove duplicates in list of lists where an item in each list is ignored when determining duplicity
Maybe there's an elegant way for me to instead not delete the e column and still remove duplicates efficiently without considering the e values?
Your set-based ...
- 2,738
6
votes
Accepted
Compute a numerical derivative
You can use np.roll to compute the centered differences as a vectorised operation rather than in a for loop:
...
- 28.3k
5
votes
Accepted
Matlab Computer Vision
There's a lot going on in this script, but I've tried to cover all of the important parts.
Style and conventions:
I think your coding style is very good.
You're using nice a descriptive variable ...
- 2,057
5
votes
Accepted
Evaluating a series of Legendre polynomials
Compute legendre(n) once per loop iteration instead of twice.
Use np.hypot instead of ...
- 49.4k
5
votes
Generate random unit vectors around circle
If you just want to do this for the unit circle, you can use numpy.random.uniform to choose the angle \$\theta\$ defining the unit vector.
If you want to deal with ...
- 16.2k
5
votes
Generic pixel class to seamlessly alpha-blend and convert between different pixel structure layouts
The code seems to get more and more questionable as we read downward. Starting at the bottom:
...
- 19.2k
4
votes
Efficient element-wise function computation in Python
You can avoid the nested loops using numpy.meshgrid to build a table of entries in x and y, ...
- 49.4k
4
votes
Accepted
Vectorized numpy version of arange with multiple start stop
This code looks good to me: the docstring is clear and the implementation is simple and efficient. So I have only a few minor points.
The code doesn't fit in 80 columns, meaning that we have to ...
- 49.4k
4
votes
Extract the largest value for each day from a matrix
A few notes: (I implemented this in Octave, so there may be differences between my code and a proper Matlab implementation, but there shouldn't be).
First we care about only unique dates, right? So ...
- 21.8k
4
votes
Accepted
Horizontally concatenating the entries of two matrices, and padding with NaNs
I see two major way to simplify and speed this up. First, rather than copying a to c at every step of the loop, you can define <...
- 2,364
4
votes
Accepted
Converting Array of Floats to UINT8 (`char`) or UINT16 (`unsigned short`) Using SSE4
why not load 4 packed __m128 and then store 32 Pixels at once
I think that's 16 pixels, but it's a good plan. The pack instructions were used inefficiently (in the linked question that was not really ...
- 7,409
4
votes
4
votes
Accepted
Find the minimum value that data could have had before it was rounded
You can firstly change by difference != 0 and then use na.locf to replace NAs by last ...
- 366
4
votes
Accepted
Determining whether a list of pathways and its genes are all included in another list
If you were to add a cat("hello") at the top of your all_in function, you would find that your function is called 25 times, once ...
- 3,495
4
votes
Accepted
Vectorizing matrix operation instead of for loop in circular matrices
This is easy using numpy.roll, for example:
zx = np.roll(x, 1) * (np.roll(x, 2) + np.roll(x, -1)) - x
- 49.4k
4
votes
Accepted
PyTorch Vectorized Implementation for Thresholding and Computing Jaccard Index
I think a different approach is needed to achieve a better performance. The current approach recomputes the Jaccard similarity from scratch for each possible threshold value. However, going from one ...
- 2,301
4
votes
Accepted
Vectorizing equations for fsolve
Yes. You can simplify this code. I made two main changes.
First, I forget that \$x_{50}\$ is known and write an equation for it just like all the other variables, then I replace that equation by the ...
- 241
4
votes
Accepted
Vectorized crosstabulation in Python for two arrays with two categories each
Algorithm
If you look at your code, and follow the if-elif part, you see that there are 4 combinations of i and ...
- 9,020
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
vectorization × 99performance × 46
python × 45
numpy × 31
matlab × 23
r × 12
c × 11
matrix × 7
c++ × 6
python-3.x × 6
array × 5
pandas × 5
statistics × 5
simd × 5
image × 4
c# × 3
random × 3
mathematics × 3
machine-learning × 3
iteration × 3
numerical-methods × 3
x86 × 3
sse × 3
beginner × 2
algorithm × 2