vectorization

Put pairwise differences of matrix rows in 3-d array

▼魔方 西西 提交于 2020-01-04 04:14:08
问题 I have a matrix Y of shape (n, d). I already calculated the pairwise row-differences in the following way: I, J = np.triu_indices(Y.shape[0], 0) rowDiffs = (Y[I, :] - Y[J, :]) No i want to create a 3d-array, containing the differences of the rows i and j of Y at position (i,j, :). How would you do it? The aim of it is to replace this inefficient loop: for i in range(Y.shape[0]): for j in range(Y.shape[0]): C[i,:] = C[i,:] + W[i, j] * (Y[i, :]-Y[j, :]) 回答1: I have found some success with this:

How to dynamically reshape matrix block-wise? [duplicate]

China☆狼群 提交于 2020-01-04 02:33:07
问题 This question already has answers here : Collapsing matrix into columns (8 answers) Closed 3 years ago . Let's say I have A = [1:8; 11:18; 21:28; 31:38; 41:48] Now I would like to move everything from column 4 onward to the row position. How do I achieve this? A = 1 2 3 4 5 6 7 8 11 12 13 14 15 16 17 18 21 22 23 24 25 26 27 28 31 32 33 34 35 36 37 38 41 42 43 44 45 46 47 48 to A2 = 1 2 3 4 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44 5 6 7 8 15 16 17 18 35 36 37 38 45 46 47 48 reshape

Efficiently checking Euclidean distance for a large number of objects in Python

这一生的挚爱 提交于 2020-01-03 09:03:27
问题 In a route planning algorithm, I'm trying to perform a filter on a list of nodes based on distance to another node. I'm actually pulling the lists from a crude scene graph. I use the term "cell" to refer to a volume within a simple scenegraph from which we've fetched a list of nodes that are close to each other. Right now, I'm implementing this as: # SSCCE version of the core function def nodes_in_range(src, cell, maxDist): srcX, srcY, srcZ = src.x, src.y, src.z maxDistSq = maxDist ** 2 for

GCC couldn't vectorize 64-bit multiplication. Can 64-bit x 64-bit -> 128-bit widening multiplication be vectorized on AVX2?

感情迁移 提交于 2020-01-02 05:17:28
问题 I try to vectorize a CBRNG which uses 64bit widening multiplication. static __inline__ uint64_t mulhilo64(uint64_t a, uint64_t b, uint64_t* hip) { __uint128_t product = ((__uint128_t)a)*((__uint128_t)b); *hip = product>>64; return (uint64_t)product; } Is such a multiplication exists in a vectorized form in AVX2? 回答1: No. There's no 64 x 64 -> 128 bit arithmetic as a vector instruction. Nor is there a vector mulhi type instruction (high word result of multiply). [V]PMULUDQ can do 32 x 32 -> 64

Generalized Matrix Product

时光总嘲笑我的痴心妄想 提交于 2020-01-02 02:24:22
问题 I'm fairly new to MATLAB. Normal matrix multiplication of a M x K matrix by an K x N matrix -- C = A * B -- has c_ij = sum(a_ik * b_kj, k = 1:K) . What if I want this to be instead c_ij = sum(op(a_ik, b_kj), k = 1:K) for some simple binary operation op ? Is there any nice way to vectorize this in MATLAB (or maybe even a built-in function)? EDIT: This is currently the best I can do. % A is M x K, B is K x N % op is min C = zeros(M, N); for i = 1:M: C(i, :) = sum(bsxfun(@min, A(i, :)', B)); end

Replace numpy matrix elements with submatrices

爷,独闯天下 提交于 2020-01-01 19:00:51
问题 Given that I have a square matrix of indices, such as: idxs = np.array([[1, 1], [0, 1]]) and an array of square matrices of the same size as each other (not necessarily the same size as idxs ): mats = array([[[ 0. , 0. ], [ 0. , 0.5]], [[ 1. , 0.3], [ 1. , 1. ]]]) I'd like to replace each index in idxs with the corresponding matrix in mats , to obtain: array([[ 1. , 0.3, 1. , 0.3], [ 1. , 1. , 1. , 1. ], [ 0. , 0. , 1. , 0.3], [ 0. , 0.5, 1. , 1. ]]) mats[idxs] gives me a nested version of

Vectorized pythonic way to get count of elements greater than current element

半腔热情 提交于 2020-01-01 11:35:12
问题 I'd like to compare every entry in array b with its respective column to find how many entries (from that column) are larger than the reference. My code seems embarrassingly slow and I suspect it is due to for loops rather than vector operations. Can we 'vectorize' the following code? import numpy as np n = 1000 m = 200 b = np.random.rand(n,m) p = np.zeros((n,m)) for i in range(0,n): #rows for j in range(0,m): # cols r = b[i,j] p[i,j] = ( ( b[:,j] > r).sum() ) / (n) After some more thought, I

Python Numpy vectorize nested for-loops for combinatorics

笑着哭i 提交于 2020-01-01 10:07:26
问题 Given an nxn array A of real positive numbers, I'm trying to find the minimum of the maximum of the element-wise minimum of all combinations of three rows of the 2-d array. Using for-loops, that comes out to something like this: import numpy as np n = 100 np.random.seed(2) A = np.random.rand(n,n) global_best = np.inf for i in range(n-2): for j in range(i+1, n-1): for k in range(j+1, n): # find the maximum of the element-wise minimum of the three vectors local_best = np.amax(np.array([A[i,:],

How to write vectorized functions in MATLAB

﹥>﹥吖頭↗ 提交于 2020-01-01 05:39:26
问题 I am just learning MATLAB and I find it hard to understand the performance factors of loops vs vectorized functions. In my previous question: Nested for loops extremely slow in MATLAB (preallocated) I realized that using a vectorized function vs. 4 nested loops made a 7x times difference in running time . In that example instead of looping through all dimensions of a 4 dimensional array and calculating median for each vector, it was much cleaner and faster to just call median(stack, n) where

How to generate the first twenty powers of x?

爷,独闯天下 提交于 2019-12-31 07:19:26
问题 So, I've got X, a 300-by-1 vector and I'd like [1, X, X*X, X*X*X, ... , X*X*...*X], a 300-by-twenty matrix. How should I do this? X=[2;1] [X,X.*X,X.*X.*X] ans = 2 4 8 1 1 1 That works, but I can't face typing out the whole thing. Surely I don't have to write a for loop? 回答1: If you want to minimize the number of operations : cumprod(repmat(X(:),1,20),2) %// replace "20" by the maximum exponent you want Benchmarking : for X of size 300x1, maximum exponent 20. I measure time with tic , toc ,