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 on functions with tuples as lvalues #61

Open
prittjam opened this issue Oct 18, 2022 · 3 comments
Open

@cast on functions with tuples as lvalues #61

prittjam opened this issue Oct 18, 2022 · 3 comments

Comments

@prittjam
Copy link

prittjam commented Oct 18, 2022

is there a way to use tensor cast for functions that return tuples? e.g.,

function axis_to_random_rotation3(x::AbstractVector)
    x′ = similar(x)
    x′ .= rand(eltype(x),3)
    y = cross(x,x′)
    z = cross(x,y)
    R = [x y z]
    R .= R./norm.(eachcol(R))'

    return R
end

renorm!(n) = n ./= norm(n)

@views function plane_to_random_pose(Π)
    renorm!(Π)
    R = axis_to_random_rotation3(Π[1:3])
    t = -Π[4].*Π[1:3]
    
    return R, t
end

Here I want to use tensor cast to shape the returned pose from a set of planes e.g.

Π = rand(4,100)
@cast something something  := plane_to_random_pose(Π[:,j])[i]
@prittjam prittjam changed the title tuple as lvalue casting on functions with tuples as lvalues Oct 18, 2022
@prittjam prittjam changed the title casting on functions with tuples as lvalues @cast on functions with tuples as lvalues Oct 18, 2022
@mcabbott
Copy link
Owner

This is essentially #15 . It would be nice to support, using StructArrays, but I haven't got around to writing it.

@prittjam
Copy link
Author

prittjam commented Oct 18, 2022

Thanks for the response. I think unwrapping directly to arrays from a broadcasted function with multiple return values should be fixed at the language level. Tensors are a common data representation, and broadcasting on them is awkward except for the simplest case with mapslices. @cast addresses some issues albeit the return types can be very complicated, but there are still common use cases that are not handled elegantly. There are some threads about this topic, so I don't think I'm the only one feeling the pain. Given that array is a fundamental Julia datatype, this lack of functionality surprises me. I also suspect I'm paying some hidden performance penalities with casting. I'm not yet a savvy enough Julia user to understand the tradeoffs.

@mcabbott
Copy link
Owner

Yes, lots of people would like this, I mean outside of TensorCast.

StructArrays seems to provide a pretty good implementation of this unzip_broadcast idea. I doubt anyone wants to build that into Base, but pulling out just enough to do this might be possible, haven't looked.

mapslices-like things we are trying to improve with things like stack(f, eachcol(x), y). Maybe there is more to be done at the intersection of these two.

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