vectorization

Applying a function over consecutive pairs of list elements in R without loops

情到浓时终转凉″ 提交于 2019-12-12 13:12:49
问题 I am trying to find an efficient (i.e. avoid using loops) way to apply a function that iteratively takes as arguments the current and previous (or next) elements of a list and returns a lists of the result (the length of which will necessarily be 1 element shorter). As a concrete example, I have a list of vertices defining a path in some graph vlist <- c(1,2,7,12,17) which come from a lattice graph constructed using the igraph function "lattice" G <- graph.lattice(c(5,7)) I want to apply the

Multidimensional arrays of 3-D objects: how to vectorise inner products

我与影子孤独终老i 提交于 2019-12-12 12:16:50
问题 In this question, I discussed two custom functions to multiply arrays of 3x3 matrices and 3x1 vectors, preserving the structure of the 3-dimensional (matrix) inner product and making the whole process as computationally efficient and fast as possible. I have now generalised those functions to multidimensional arrays (NxN) of 3x4 matrices and 3x1 vectors. Here are the functions I have written, which make use of for loops. BlockScalar This function should multiply the ij element (a scalar) of

How to vectorize text file in mahout?

早过忘川 提交于 2019-12-12 11:43:10
问题 I'm having a text file with label and tweets . positive,I love this car negative,I hate this book positive,Good product. I need to convert each line into vector value.If i use seq2sparse command means the whole document gets converted to vector,but i need to convert each line as vector not the whole document. ex : key : positive value : vectorvalue(tweet) How can we achieve this in mahout? /* Here is what i have done */ StringTokenizer str= new StringTokenizer(line,","); String label=str

Grouping elements with the same ID and finding the maximum value as well as its location

↘锁芯ラ 提交于 2019-12-12 11:23:39
问题 I have two vectors of length 16. The first one, r , for example is: r = [1;3;5;7;1;3;6;7;9;11;13;16;9;11;13;16]; r contains a list of IDs. I want to collect the indices of the duplicate IDs in r so that each group is a list of indices for one ID. I would then use these indices to access a second vector a and find the maximum value incident on the indices for each group. Therefore, I would like to produce an output vector using r and a such that: max(a(1),a(5)), max(a(2),a(6)), a(3), a(7), max

Computation is optimized only if variable updated in loop is local

梦想与她 提交于 2019-12-12 11:08:36
问题 For the following function, the code with optimizations is vectorized and the computation is performed in registers (the return value is returned in eax ). Generated machine code is, e.g., here: https://godbolt.org/z/VQEBV4. int sum(int *arr, int n) { int ret = 0; for (int i = 0; i < n; i++) ret += arr[i]; return ret; } However, if I make ret variable global (or, a parameter of type int& ), the vectorization is not used and the compiler stores the updated ret in each iteration to memory.

how to avoid loops

别来无恙 提交于 2019-12-12 10:47:46
问题 HI All, I'm new to R. I have two panel data files, with columns "id", "date" and "ret" file A has a lot more data than file B, but i'm primarily working with file B data. Combination of "id" and "date" is unqiue indentifier. Is there an elegent way of looking up for each (id, date) in B, I need to get the past 10 days ret from file A, and store them back into B? my naive way of doing it is to loop for all rows in B, for i in 1:length(B) { B$past10d[i] <- prod(1+A$ret[which(A$id == B$id[i] & A

Vectorized operations on cell arrays

最后都变了- 提交于 2019-12-12 10:36:07
问题 This post was triggered by following discussion on whether cell arrays are "normal arrays" and that vectorizaton does not work for cell arrays. I wonder why following vectorization syntax is not implemented in MATLAB, what speaks against it: >> {'hallo','matlab','world'} == 'matlab' ??? Undefined function or method 'eq' for input arguments of type 'cell'. internally it would be equivalent to [{'hallo'},{'matlab'},{'world'}] == {'matlab'} because MATLAB knows when to cast, following works: [{

Automatic vectorization of matrix multiplication

↘锁芯ラ 提交于 2019-12-12 10:19:12
问题 I'm fairly new with SIMD and wanted to try to see if I could get GCC to vectorise a simple action for me. So I looked at this post and wanted to do more or less the same thing. (but with gcc 5.4.0 on Linux 64bit, for a KabyLake processor) I essentially have this function: /* m1 = N x M matrix, m2 = M x P matrix, m3 = N x P matrix & output */ void mmul(double **m1, double **m2, double **m3, int N, int M, int P) { for (i = 0; i < N; i++) for (j = 0; j < P; j++) { double tmp = 0.0; for (k = 0; k

Vectorizing triple for loop in Python/Numpy with different array shapes

只愿长相守 提交于 2019-12-12 10:04:34
问题 I am new in Python/Numpy and is trying to improve my triple for loop into a more efficient calculation, but can't quiet figure out how to do it. The calculations is carried out on a grid of the size (25,35) and the shapes of arrays is: A = (8760,25,35) B = (12,25,35) The first dimensions in A corresponds to the number hours in a year (~8760), and the first dimension in B is the number of months(12). I want to use the values in B[0,:,:] for the first month, and B[1,:,:] for the second etc. So

inconsistent results using isreal

限于喜欢 提交于 2019-12-12 08:05:57
问题 Take this simple example: a = [1 2i]; x = zeros(1,length(a)); for n=1:length(a) x(n) = isreal(a(n)); end In an attempt to vectorize the code, I tried: y = arrayfun(@isreal,a); But the results are not the same: x = 1 0 y = 0 0 What am I doing wrong? 回答1: This certainly appears to be a bug, but here's a workaround: >> y = arrayfun(@(x) isreal(x(1)),a) ans = 1 0 Why does this work? I'm not totally sure, but it appears that when you perform an indexing operation on the variable before calling