All Questions
Tagged with numpy vectorization
1,952
questions
1
vote
1
answer
63
views
Python for loop numpy vectorization
Do you have any suggestions how I can speed up the "f_img_update" function in the code below? I tried adding numba @jit decorator, but without luck. I guess that numpy vectorization might ...
1
vote
1
answer
53
views
Numpy for loop vectorization - points, triangles, area, volume calculation
I have two numpy arrays - one containing coordinates of 3D points and the second with triangles composed from those points. I need to calculate top surface 2D area and volume of this triangles (...
2
votes
2
answers
113
views
Is it possible to vectorize a loop of if-else statement in Python?
I am wondering the possibility of turning a for loop with several if-else statements into vectorize version in Python. Suppose I have several numpy arrays a,b,c,d , where a,b,c,d have the same length. ...
0
votes
2
answers
71
views
Faster numpy calculations than reshaping with einsum
Consider the following in Python: A has dimension (T,), U has dimension (L,T) and G has dimension (K,T), Y is (L,L,T). My code outputs a numer1 and numer2 with dimensions (T, LK, 1), . Consider that ...
0
votes
2
answers
59
views
How do I threshold a numpy array, where the false condition depends on the number changed?
I am trying to threshold a 2D numpy array, where if the value is greater than a threshold, it is set to 255, else it is set to a value following this equation:
1 + tanh(PHI * (n - THRESHOLD))
where n ...
2
votes
1
answer
99
views
Vectorized Reset of Accumulated Sum Based on Conditions in Python
I'm trying to reset the accumulation of a sum when a desired condition is met, all in a vectorized manner, without using loops. I'll first show a simple example and then the real problem.
import ...
1
vote
2
answers
62
views
Accessing columns of a numpy ndarray in a vectorized manner
I have a list of lists of indices like so:
outer_list = [[0], [1, 2], [1, 2], [3, 5], [4], [3, 5]]
Each inner list contains the index number of itself within the outer list.
I also have a numpy array ...
0
votes
1
answer
75
views
Vectorising this function in python efficiently
Problem Description
I have written Python code to compute the following function, for fixed y_i and fixed z_i.
In practice, I will have many different vectors x at which I will want to evaluate this. ...
1
vote
1
answer
68
views
Is there a way to vectorise this logic in python?
I'm working on a financial simulation problem and I have a very simple piece of code which leverages a for loop to get a result.
I think it should be possible to vectorise this, but I also think that ...
1
vote
1
answer
54
views
Vectorize a Numpy Function
For instance, I want to vectorize the function that returns a determinant of a matrix.
So I try the following codes:
data1test=np.random.rand(2,2)
data2test=np.random.rand(2,2)
data3test=np.random....
1
vote
1
answer
52
views
Vectorization of nested for loops with condition in NumPy
I have the following Python snippet to count the number of times the elements x and y (from X and Y) verify the conditions x<=i and y<=j concomitantly, where i and j are indices:
import numpy as ...
2
votes
1
answer
57
views
Why is NumPy's vectorized evaluation slower when storing vectors as class attributes?
I am writing a helper class to evaluate a parametrized function over a grid. Since the grid does not change with the parameter, I chose to create it once and for all as a class attribute.
However, I ...
0
votes
2
answers
80
views
Python3 extract various subsets from list/array into a single new variable
In matlab for an 1xM array let us say a=1,1.5,2,2.5,...10
a=1:0.5:10;
one can easily extract several subsets of the array in 1 line of code
b=a([1:3 10:13]);.
making b=1,1.5,2,5.5,6,6.5,7. If one ...
0
votes
0
answers
65
views
How to apply a function to the subarrays of a (m,n,n) numpy array without using a for-loop
Starting with a (m,n,n) numpy array, lets say I want to apply some function to every (n,n) sub-array without using a for-loop.
The two functions I'd like to implement would be to first use np.multiply ...
0
votes
1
answer
51
views
How to apply a function to each element of a linspace without using a for-loop
Starting with a linspace
t = np.linspace(0,10, 100)
and an array
a = np.array([1,2,44,2, 13,...])
I would like to get an array b, of the same length of the linspace whose elements are the array a ...