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
Question: Error with creating Size-1 SVector of Size-1 SVector #1151
Comments
The default constructor for julia> SVector{1}(SVector(1))
1-element SVector{1, Int64} with indices SOneTo(1):
1
julia> SVector{1}((SVector(1), ))
1-element SVector{1, SVector{1, Int64}} with indices SOneTo(1):
[1] |
I see, thank you @N5N3! And I guess A follow-up due to curiosity: The need to have an additional tuple wrapper occurs only for 1-element SVector of an 1-element SVector, which can be seen as an inconsistent behavior. A relevant example case: if one works on the subsets of a given set of hyperplanes of a Maybe these cases are very infrequent and wouldn't worth any overhead, albeit very small, for the most users of the package, but I guess one way to ensure consistency is to provide an explicit constructor for |
julia> @code_warntype SA[SVector(1)]
MethodInstance for getindex(::Type{SA}, ::SVector{1, Int64})
from getindex(sa::Type{SA}, xs...) @ StaticArrays C:\Users\MYJ\.julia\packages\StaticArrays\a4r2v\src\initializers.jl:31
Arguments
#self#::Core.Const(getindex)
sa::Type{SA}
xs::Tuple{SVector{1, Int64}}
Body::SVector{1, SVector{1, Int64}}
1 ─ nothing
│ %2 = StaticArrays.length(xs)::Core.Const(1)
│ %3 = StaticArrays.Size(%2)::Core.Const(Size(1,))
│ %4 = StaticArrays.similar_type(sa, %3)::Core.Const(SArray{Tuple{1}})
│ %5 = (%4)(xs)::SVector{1, SVector{1, Int64}}
└── return %5 You will find a Tuple wrapper is added to ensure this, which matches the general rule of our array literals
The inner constructor of
Just as mentioned above, wrap your elements with a tuple would resolve all possible ambiguilty. |
@N5N3 Thank you again for your detailed answer!
Fair. I wouldn't be able to assess trade-offs, but as a user of the package, it is extremely beneficial that the interface of my_vector_a = SVector(0.8) # 1-element SVector{1, Float64}
my_vector_b = SVector(0.9) # 1-element SVector{1, Float64}
my_container = SVector(my_vector_a, my_vector_b) # 2-element SVector{2, SVector{1, Float64}}
container_subset_a = SVector{2}(my_container[j] for j in 1:2) # 2-element SVector{2, SVector{1, Float64}}
container_subset_b = SVector{1}(my_container[j] for j in 1:1) # 1-element SVector{1, Float64} |
The result should be |
Opened PR #1153. |
I'm wondering why it is not allowed to create 1-element
SVector
of a 1-elementSVector
by using the constructor with type parameters are given? One can create the following nested SArrays with the explicit constructor:Moreover using
SA
constructor seems to create 1-elementSVector
of a 1-elementSVector
.And the error message is:
Also asked in Julia Discourse
The text was updated successfully, but these errors were encountered: