Skip to content
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

@cast works for hcat but not for [ ] #60

Open
prittjam opened this issue Oct 11, 2022 · 1 comment
Open

@cast works for hcat but not for [ ] #60

prittjam opened this issue Oct 11, 2022 · 1 comment

Comments

@prittjam
Copy link

prittjam commented Oct 11, 2022

This works,

@cast P[i,j,k] := hcat(R[:,:,k],X₁[:,k])[i,j]

but this does not

@cast P[i,j,k] := [R[:,:,k] X₁[:,k]][i,j]

It gives the error,

DimensionMismatch: stack expects uniform slices, got axes(x) == (1:3,) while first had (1:3, 1:4)

Should it? Thanks!

@mcabbott
Copy link
Owner

Here's what it's doing:

julia> @pretty @cast P[i,j,k] := [R[:,:,k] X₁[:,k]][i,j]
begin
    @boundscheck ndims(R) == 3 || throw(ArgumentError("expected a 3-tensor R[:, :, k]"))
    @boundscheck axes(R, 3) == axes(X₁, 2) || throw(DimensionMismatch("range of index k must agree"))
    @boundscheck ndims(X₁) == 2 || throw(ArgumentError("expected a 2-tensor X₁[:, k]"))
    local vicuña = sliceview(R, (:, :, *))
    local alligator = sliceview(X₁, (:, *))
    local dolphin = @__dot__([vicuña alligator])  # <-- this broadcast does nothing
    local redpanda = lazystack(dolphin)
    P = redpanda
end

julia> :([vicuña alligator]) |> dump
Expr
  head: Symbol hcat
  args: Array{Any}((2,))
    1: Symbol vicuña
    2: Symbol alligator

julia> @. hcat(1:2, 3:4)
2-element Vector{Matrix{Int64}}:
 [1 3]
 [2 4]

julia> @. [1:2 3:4]
2×2 Matrix{Int64}:
 1  3
 2  4

It wouldn't be impossible to make it recognise Expr(:hcat, ...) at some early stage, and convert it to a function call. Likewise vcat, vect... from :([1 2 3; 4 5]) |> dump also row not hvcat? And Float32[1:2 3:4] is typed_hcat. But I haven't done so. Probably writing out hcat is best for now.

What will never work is Vector[1:2, 3:4], as the macro cannot distinguish this from indexing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants