vectorization

Replacing for loops with function call inside with broadcasting/vectorized solution

不羁的心 提交于 2021-02-11 13:26:09
问题 Problem: When using broadcasting, rather than broadcasting scalars to match the arrays, the vectorized function is instead, for some reason, shrinking the arrays to scalars. MWE: Below is a MWE. It contains a double for loop. I am having trouble writing faster code that does not use the for loops, but instead, uses broadcasting/vectorized numpy. import numpy as np def OneD(x, y, z): ret = np.exp(x)**(y+1) / (z+1) return ret def ThreeD(a,b,c): value = OneD(a[0],b[0], c) value *= OneD(a[1],b[1]

Is it possible to use mldivide “\” on a 3D matrix in Matlab

旧时模样 提交于 2021-02-11 12:54:48
问题 Is it possible to use mldivide ( \ ) on a 3D matrix in MATLAB? I would like to avoid using a for loop? Sample: A = rand(4, 100, 5); B = rand(4,4); I need to perform: C = B\A; What I'm doing now: Apply the mldivide on a for loop for each "slice" i: for i = 1:size(A, 3) C(:,:,i) = B \ A(:,:,i); end 回答1: You can reshape A into a 2D matrix to perform the division and then back to the expected size afterwards. The reshape operations should be relatively quick due to the fact that MATLAB doesn't

Broadcasting/Vectorizing inner and outer for loops in python/NumPy

允我心安 提交于 2021-02-11 06:30:32
问题 Purpose I have turned a double for loop into a single for loop using vectorization . I would like to now get rid of the last loop . I want to slice an Nx3 array of coordinates and calculate distances between the sliced portion and the remaining portion without using a for loop . Two cases (1) the slice is always 3x3 . (2) the slice is variable i.e., Mx3 where M is always significantly smaller than N Vectorizing the interaction of 1 row of the slice interacting with the remainder is

R how to vectorize a function with multiple if else conditions

梦想与她 提交于 2021-02-10 20:15:08
问题 Hi I am new to vectorizing functions in R. I have a code similar the following. library(truncnorm) library(microbenchmark) num_obs=10000 Observation=seq(1,num_obs) Obs_Type=sample(1:4, num_obs, replace=T) Upper_bound = runif(num_obs,0,1) Lower_bound=runif(num_obs,2,4) mean = runif(num_obs,10,15) df1= data.frame(Observation,Obs_Type,Upper_bound,Lower_bound,mean) df1$draw_value = 0 Trial_func=function(df1){ for (i in 1:nrow(df1)){ if (df1[i,"Obs_Type"] ==1){ #If Type == 1; then a=-Inf, b =

R how to vectorize a function with multiple if else conditions

自作多情 提交于 2021-02-10 20:08:33
问题 Hi I am new to vectorizing functions in R. I have a code similar the following. library(truncnorm) library(microbenchmark) num_obs=10000 Observation=seq(1,num_obs) Obs_Type=sample(1:4, num_obs, replace=T) Upper_bound = runif(num_obs,0,1) Lower_bound=runif(num_obs,2,4) mean = runif(num_obs,10,15) df1= data.frame(Observation,Obs_Type,Upper_bound,Lower_bound,mean) df1$draw_value = 0 Trial_func=function(df1){ for (i in 1:nrow(df1)){ if (df1[i,"Obs_Type"] ==1){ #If Type == 1; then a=-Inf, b =

vectorization : too many indices for array

你离开我真会死。 提交于 2021-02-10 17:30:10
问题 a=b=np.arange(9).reshape(3,3) i=np.arange(3) mask=a<i[:,None,None]+3 and b[np.where(mask[0])] >>>array([0, 1, 2]) b[np.where(mask[1])] >>>array([0, 1, 2, 3]) b[np.where(mask[2])] >>>array([0, 1, 2, 3, 4]) Now I wanna vectorize it and print them all together, and I try b[np.where(mask[i])] and b[np.where(mask[i[:,None,None]])] Both of them show IndexError: too many indices for array 回答1: In [165]: a Out[165]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In [166]: mask Out[166]: array([[[ True,

Efficient SSE FP `floor()` / `ceil()` / `round()` Rounding Functions Without SSE4.1?

我怕爱的太早我们不能终老 提交于 2021-02-10 04:13:39
问题 How can I round a __m128 vector of floats up/down or to the nearest integer, like these functions? Round - roundf() Ceil - ceilf() or SSE4.1 _mm_ceil_ps . Floor - floorf() or SSE4.1 _mm_floor_ps . I need to do this without SSE4.1 roundps ( _mm_floor_ps / _mm_ceil_ps / _mm_round_ps(x, _MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC) . roundps can also truncate toward zero, but I don't need that for this application. I can use SSE3 and earlier. (No SSSE3 or SSE4) So the function declaration would

Efficient SSE FP `floor()` / `ceil()` / `round()` Rounding Functions Without SSE4.1?

假装没事ソ 提交于 2021-02-10 04:13:30
问题 How can I round a __m128 vector of floats up/down or to the nearest integer, like these functions? Round - roundf() Ceil - ceilf() or SSE4.1 _mm_ceil_ps . Floor - floorf() or SSE4.1 _mm_floor_ps . I need to do this without SSE4.1 roundps ( _mm_floor_ps / _mm_ceil_ps / _mm_round_ps(x, _MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC) . roundps can also truncate toward zero, but I don't need that for this application. I can use SSE3 and earlier. (No SSSE3 or SSE4) So the function declaration would

Efficient SSE FP `floor()` / `ceil()` / `round()` Rounding Functions Without SSE4.1?

天大地大妈咪最大 提交于 2021-02-10 04:08:30
问题 How can I round a __m128 vector of floats up/down or to the nearest integer, like these functions? Round - roundf() Ceil - ceilf() or SSE4.1 _mm_ceil_ps . Floor - floorf() or SSE4.1 _mm_floor_ps . I need to do this without SSE4.1 roundps ( _mm_floor_ps / _mm_ceil_ps / _mm_round_ps(x, _MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC) . roundps can also truncate toward zero, but I don't need that for this application. I can use SSE3 and earlier. (No SSSE3 or SSE4) So the function declaration would

Efficient SSE FP `floor()` / `ceil()` / `round()` Rounding Functions Without SSE4.1?

牧云@^-^@ 提交于 2021-02-10 04:05:20
问题 How can I round a __m128 vector of floats up/down or to the nearest integer, like these functions? Round - roundf() Ceil - ceilf() or SSE4.1 _mm_ceil_ps . Floor - floorf() or SSE4.1 _mm_floor_ps . I need to do this without SSE4.1 roundps ( _mm_floor_ps / _mm_ceil_ps / _mm_round_ps(x, _MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC) . roundps can also truncate toward zero, but I don't need that for this application. I can use SSE3 and earlier. (No SSSE3 or SSE4) So the function declaration would