vectorization

From a set of pairs, find all subsets s.t. no pair in the subset shares any element with a pair not in the subset

有些话、适合烂在心里 提交于 2020-08-20 06:12:57
问题 I have a set of pairs. Each pair is represented as [i,1:2] . That is, the ith pair are the numbers in the first and second column in the ith row. I need to sort these pairs into distinct groups, s.t. there is no element in any pair in the jth group that is in any group not j . For example: EXAMPLE 1: DATA > col1 <- c(3, 4, 6, 7, 10, 8) > col2 <- c(6, 7, 3, 4, 3, 1) > > dat <- cbind(col1, col2) > rownames(dat) <- 1:nrow(dat) > > dat col1 col2 1 3 6 2 4 7 3 6 3 4 7 4 5 10 3 6 8 1 For all pairs,

conditional vectorized calculation with numpy arrays without using direct masking

一笑奈何 提交于 2020-08-10 03:38:01
问题 following up on another question import numpy as np repeat=int(1e5) r_base = np.linspace(0,4,5) a_base = 2 np.random.seed(0) r_mat = r_base * np.random.uniform(0.9,1.1,(repeat,5)) a_array = a_base * np.random.uniform(0.9,1.1, repeat) # original slow approach def func_vetorized_level1(r_row, a): if r_row.mean()>2: result = np.where((r_row >= a), r_row - a, np.nan) else: result = np.where((r_row >= a), r_row + a, 0) return result # try to broadcast this func to every row of r_mat using list

NumPy: calculate cumulative median

左心房为你撑大大i 提交于 2020-08-04 04:41:37
问题 I have sample with size = n. I want to calculate for each i: 1 <= i <= n median for sample[:i] in numpy. For example, I counted mean for each i: cummean = np.cumsum(sample) / np.arange(1, n + 1) Can I do something similar for the median without cycles and comprehension? 回答1: Here's an approach that replicates elements along rows to give us a 2D array. Then, we would fill the upper triangular region with a big number so that later on when we sort the array along each row, would basically sort

Use C# Vector<T> SIMD to find index of matching element

穿精又带淫゛_ 提交于 2020-07-20 17:21:45
问题 Using C#'s Vector<T> , how can we most efficiently vectorize the operation of finding the index of a particular element in a set? As constraints, the set will always be a Span<T> of an integer primitive, and it will contain at most 1 matching element. I have come up with a solution that seems alright, but I'm curious if we can do better. Here is the approach: Create a Vector<T> consisting only of the target element, in each slot. Use Vector.Equals() between the input set vector and the vector

Numpy vectorize wrongly converts the output to be integer

北慕城南 提交于 2020-07-16 05:18:21
问题 I am struggling with the following code: import numpy as np e = np.linspace(0, 4, 10) def g(x): if x > 1: return x else: return 0 vg = np.vectorize(g) print(vg(e)) the result looks like this: [0 0 0 1 1 2 2 3 3 4] I also checked the dtype. It seems that the vectorize function is conveting the type to int64 from float64! 回答1: The documentation for np.vectorize explains: The data type of the output of vectorized is determined by calling the function with the first element of the input. This can

How to mix atomic and non-atomic operations in C++?

故事扮演 提交于 2020-07-08 05:25:09
问题 The std::atomic types allow atomic access to variables, but I would sometimes like non-atomic access, for example when the access is protected by a mutex. Consider a bitfield class that allows both multi-threaded access (via insert) and single-threaded vectorized access (via operator|=): class Bitfield { const size_t size_, word_count_; std::atomic<size_t> * words_; std::mutex mutex_; public: Bitfield (size_t size) : size_(size), word_count_((size + 8 * sizeof(size_t) - 1) / (8 * sizeof(size