You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to create a vector of SVectors from a Matrix using Tullio?
I tried the following:
using StaticArrays
using Tullio
r =rand(5, 1000);
v =Vector{SVector{5, Float64}}(undef, size(r, 2))
@tullio v[i][j] :=beginSVector{5, Float64}(r[:, i])
end (i in1:1000, j in1:5)
But it throws an error:
ERROR: UndefVarError: i not defined
Stacktrace:
[1] ℳ𝒶𝓀ℯ
@ C:\Users\Shayan\.julia\packages\Tullio\NGyNM\src\macro.jl:807 [inlined]
[2] (::Tullio.Eval{var"#ℳ𝒶𝓀ℯ#41"{var"#𝒜𝒸𝓉!#40"}, Nothing}) (args::Matrix{Float64})
@ Tullio C:\Users\Shayan\.julia\packages\Tullio\NGyNM\src\eval.jl:20
[3] top-level scope
@ C:\Users\Shayan\.julia\packages\Tullio\NGyNM\src\macro.jl:976
I'm looking for the best way to do this using Tullio if it is possible.
The text was updated successfully, but these errors were encountered:
Tullio doesn't really know how to make an array of arrays, nor even to write into them -- it thinks vs[i] is the name of the array, should really notice and have a better error:
julia> vs = [rand(3) for _ in1:4];
julia>@tullio vs[i][j] = i+j;
ERROR: UndefVarError:`i` not defined
julia>@tullio w[i] := vs[i][j]; # reading is fine
julia> w ≈sum.(vs)
true
It is possible to make slices without ever talking about their inner indices, so that it doesn't have to think about them:
julia> r =rand(5, 1000);
julia>using StaticArrays
julia>@tullio out[i] :=SVector{5, Float64}(r[:, i]);
julia> out[1]
5-element SVector{5, Float64} with indices SOneTo(5):0.77667679012589850.71549937741541970.375582538751402460.95442768194106280.841961127323515
julia>size(out)
(1000,)
julia>summary(@tullio _[i] := r[:, i])
"1000-element Vector{Vector{Float64}}"
julia>@btime@tullio out[i] :=SVector{5, Float64}($r[:, i]);
min 35.333 μs, mean 316.602 μs (1002 allocations, 132.86 KiB)
However, this is making r[:, 1]::Vector each time before making an SVector, so it won't be fast. Tullio does not have ambitions to be good at applying functions to slices, etc. It gets confused by things like @tullio _[i,j] := vcat(r[:,i],i)[j] and even if this did work, it would still be a loop over all i, j, thus applying the function N^2 times.
TensorCast is more interested in slices. And (with slightly obscure notation) has special paths for StaticArrays. This should just call reinterpret but appears to waste a few ns:
julia>using TensorCast
julia>@btime@cast out2[i] :=$r{:5, i};
min 24.515 ns, mean 184.877 ns (2 allocations, 80 bytes)
julia>@btimereinterpret(SVector{5, Float64}, $r)
min 4.500 ns, mean 4.648 ns (0 allocations)
Thank you so much for this comprehensive explanation! I figured out why I faced the problem. Nonetheless, wouldn't it be better if you merged these two packages?
Is it possible to create a vector of SVectors from a Matrix using Tullio?
I tried the following:
But it throws an error:
I'm looking for the best way to do this using Tullio if it is possible.
The text was updated successfully, but these errors were encountered: