-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Indices which run over a shorter range than axes(A,d)
#56
Comments
I see the temptation but this isn't supported. At present, every index of every array runs the full range. Thus in every operation combining two arrays, the dimensions sharing an index must agree. This rule comes from broadcasting, where things like It's not crazy to imagine other rules, but they will add complexity both to the interface and to the implementation. It could be that Besides complexity within the macro, one issue with allowing this would be that |
You can of course write Maybe also worth noting that Tullio has a slightly different rule for indices. By default it's the same, but as soon as an index appears with a shift like |
Thanks for the response! I think this is a common use case for casting, though: to subindex and slice at the same time. It may not be always possible to create a view because, e.g., the right-hand side may be of the form of a vector of matrices from a prior cast, e.g.,
and here I don't see an elegant alternative. Here P[k][i,j] is 3x4 and we want H[k][i,j] to be 3x3. |
Indeed. And mixing a hand-written julia> vm = [rand(3,4) for _ in 1:5];
julia> @cast A[i,j,k] := view(vm[k], :, 1:2)[i,j];
ERROR: DimensionMismatch: arrays could not be broadcast to a common size; got a dimension with lengths 5 and 2
julia> @pretty @cast A[i,j,k] := view(vm[k], :, 1:2)[i,j];
begin
@boundscheck vm isa Tuple || (ndims(vm) == 1 || throw(ArgumentError("expected a vector or tuple vm[k]")))
local dolphin = 1:2
local pig = @__dot__(view(vm, :, dolphin)) # @. hits the view too
local armadillo = lazystack(pig)
A = armadillo
end
julia> @cast A[i,j,k] |= view(vm[k], :, Ref(1:2))[i,j]; # this works but it's weird, really not encouraged!
julia> summary(A)
"3×2×5 Array{Float64, 3}" |
I think this is a common enough use case where sane notation is needed. E.g., if I want to put these arrays into a struct array, working with matrices of the from P[k][i,j] is the most natural. |
axes(A,d)
Some possible notations might be: @cast A[i,j,k] |= vm[k][i, (j in 1:2)];
@cast A[i,j,k] |= vm[k][i, view(j)] j in 1:2; # maybe easier If in the mood you can try to see where in macro.jl this needs to happen. It's all recursive and I forget how it works repeatedly... It would also require a bit of thought about how this intersects with other features like reversed axes and |
You might overestimate my Julia proficiency a bit :), but I can take a look. |
I think some of the use cases for tensor cast would not be needed if eachslice accepted tuples for dims, i.e., eachslice(P,dims=(2,3)). Is there some technical reason why this feature is not there? |
Julia 1.9 will have this. And ideally JuliaLang/Compat.jl#663 would make this "official" version available before then. |
why doesn't this work?
It fails with
Probably i am missing something basic.
Thanks.
The text was updated successfully, but these errors were encountered: