在Julia,findall(f,v)
功能使我得以在适用于该元素的病媒中收集内容。 例如,findall(iseven,[1,2,3])
回归代码<>>>> > 。 现在,在基础(或简单实施)中,我实际上获得了不同的病媒,而不是仅仅得到真实情况? 我指的是<代码>partition(iseven, [1,2,3]) Back code>( (2),[1,3])。
我可以进行冷静的执行,但我looking想的是某种效率,最好是基地的现有功能。
在Julia,findall(f,v)
功能使我得以在适用于该元素的病媒中收集内容。 例如,findall(iseven,[1,2,3])
回归代码<>>>> > 。 现在,在基础(或简单实施)中,我实际上获得了不同的病媒,而不是仅仅得到真实情况? 我指的是<代码>partition(iseven, [1,2,3]) Back code>( (2),[1,3])。
我可以进行冷静的执行,但我looking想的是某种效率,最好是基地的现有功能。
一种办法是:
separate(p, x) = ( t = findall(p, x);
(@view(x[t]), @view(x[setdiff(eachindex(x), t)])) )
和<代码> 您可以:
julia> x = [1,2,3,5,6];
julia> separate(iseven, x)
([2, 6], [1, 3, 5])
julia> E, O = collect.(separate(iseven, x))
([2, 6], [1, 3, 5])
julia> E
2-element Vector{Int64}:
2
6
如果没有<条码>,条码> 功能回报观点,如果复制原病媒的成本很高,就会很好。
如果你想很快的话,将尝试以下<代码>groupby(fn,x)的功能。 该功能分配了与<代码>x类似的病媒,并向该病媒传达了看法。 该表仅对原始病媒进行过一次跟踪,在第一类中插入了<条码>x条码>的内容,其中<条码>fn条码>给出了<条码>真实性<>>和第二组的其他内容。
function groupby(fn, x)
g = similar(x)
i, j = 0, -1
for e in x
if fn(e)
g[i+=1] = e
else
g[end-(j+=1)] = e
end
end
@views g[1:i], reverse(g[i+1:end])
end
x = [1, 2, 3, 5, 6];
groupby(iseven, x)
([2, 6], [1, 3, 5])
比较:
x = rand(1:1000, 1000)
@btime separate(iseven, $x) # 42.100 μs (20 allocations: 34.66 KiB)
@btime groupby(iseven, $x) # 1.380 μs (2 allocations: 12.12 KiB)
如果速度确实至关重要,那么变数将可减少时间和记忆资源。 例如:
function separate!(p::F, x) where {F <: Function}
b, e, L = firstindex(x), lastindex(x), length(x)
L == 1 && (return p(x[b]) ? (x, eltype(x)[]) : (eltype(x)[], x))
L == 0 && return (eltype(x)[], eltype(x)[])
while b < e
p(x[b]) && (b += 1; continue)
p(x[e]) || (e -= 1; continue)
x[b], x[e] = x[e], x[b]
end
@view(x[begin:b-1]), @view(x[b:end])
end
比其他版本快2倍。 为了保护原病媒,可使用<代码>eparate!(p, 拷贝(x)。 不同答案的基准是:
x = rand(1:1000, 1000);
@btime separate(iseven, $x); # One-liner
# 14.447 μs (20 allocations: 34.98 KiB)
@btime groupby(iseven, $x); # AboAmmar s
# 1.277 μs (2 allocations: 11.81 KiB)
@btime separate!(iseven, y) setup=(y = copy(x));
# 479.990 ns (1 allocation: 96 bytes)
I m trying to figure out how to define the dimensions of a tuple when calling a function or contructing a struct when the arguments contain a NTuple type. If I leave the dimension variable (which ...
我想利用开端和终点拖车来构筑CartesianIndices。 例如,我想在开端=(2,3)和端=(4,5)的情况下建造Cartesian Indices((2,4,3:5)。
I have a numerical scheme which I would like to optimize as well as make more readable by organizing it as follows. I have two numerical schemes, "numeric_method_1" and "...
Though I ve looked through several pages documenting various differentiation tools in Julia, I have yet to find the following simple functionality. I want to define a function which is differentiable ...
The question is fairly simple. How do I return the minimum purchase date for each customer using Tidier? using Tidier, DataFrames, Plots, CSV #params f = "path" df = CSV.File(f) |> ...
Following this post, I defined a macro inside a function: macro Name(arg) string(arg) end function myfunc(df) @Name df end tf = 3 myfunc(tf) What I want is: tf But what I actually got ...
Say I have a Julia trait that relates to two types: one type is a sort of "base" type that may satisfy a sort of partial trait, and the other is an associated type that is uniquely ...
In python, I do import numpy as np data = np.memmap( mydata.bin , dtype= <i4 , mode= r ) How can I implement this in Julia? I was reading about readbytes and htol but couldn t figure out.