Vectorized NumPy linspace for multiple start and stop values

Here’s an approach using broadcasting – def create_ranges(start, stop, N, endpoint=True): if endpoint==1: divisor = N-1 else: divisor = N steps = (1.0/divisor) * (stop – start) return steps[:,None]*np.arange(N) + start[:,None] Sample run – In [22]: # Setup start, stop for each row and no. of elems in each row …: start = np.array([1,4,2]) …: … Read more

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

Straightforward way to do that is: import numpy as np a=np.array([[1,2,3],[3,4,5]]) b=np.array([[1,2,3],[1,2,3]]) np.sum(a*b, axis=1) which avoids the python loop and is faster in cases like: def npsumdot(x, y): return np.sum(x*y, axis=1) def loopdot(x, y): result = np.empty((x.shape[0])) for i in range(x.shape[0]): result[i] = np.dot(x[i], y[i]) return result timeit npsumdot(np.random.rand(500000,50),np.random.rand(500000,50)) # 1 loops, best of 3: … Read more

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

So, basically, you want your code to run faster. JNI is the answer. I know you said it didn’t work for you, but let me show you that you are wrong. Here’s Dot.java: import java.nio.FloatBuffer; import org.bytedeco.javacpp.*; import org.bytedeco.javacpp.annotation.*; @Platform(include = “Dot.h”, compiler = “fastfpu”) public class Dot { static { Loader.load(); } static float[] … Read more

Is it possible to vectorize recursive calculation of a NumPy array where each element depends on the previous one?

You might think this would work: import numpy as np n = len(Tm) t = np.empty(n) t[0] = 0 # or whatever the initial condition is t[1:] = Tm[1:] + (t[0:n-1] – Tm[1:])**(-tau[1:]) but it doesn’t: you can’t actually do recursion in numpy this way (since numpy calculates the whole RHS and then assigns it … Read more