-
Notifications
You must be signed in to change notification settings - Fork 152
WIP Add size to abstract StaticArray type #134
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
Conversation
a647fca
to
770f5c9
Compare
OK, bulk of work is done, but I haven't updated the tests so many are failing. More alarmingly, I'm running into an inference crash in the unit tests; similar things have been reported elsewhere. |
770f5c9
to
33d3153
Compare
Tests have not been updated
ce3714d
to
a0418f6
Compare
Still having an inference problem in test/solve.jl. Can't reproduce, however.
This is ready for a look at. There is a problem with inference (world age failure) in a testset for solve, but it makes no sense to me. I would still like some feedback on design here, please. :) |
Afraid I don't have the energy to look at this tonight... maybe tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I read all this. A bit of a big change, once again (nice effort).
I'm surprised that this didn't cut down on the duplication more than it did.
On the design, I still think this is worth doing to be able to dispatch on size, and I'm not sure how better to arrange the type parameters. What you've got here is consistent with the way it worked in FixedSizeArrays, and I didn't regret the parameter ordering there (that I remember).
If only there were a better solution to all this than the Tuple
hack.
SArray{Size, T, L}(x::NTuple{L, T}) | ||
SArray{Size, T, L}(x1, x2, x3, ...) | ||
SArray{S, T, L}(x::NTuple{L, T}) | ||
SArray{S, T, L}(x1, x2, x3, ...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps drop the L
from the constructor usage docs here (the docs below go on to suggest that this is the right thing). Is there any reason people should ever need to specify L
explicitly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This I always find a bit weird. In some sense, we are documenting the type and the constructors. The one without T
and L
is documented below.
The L
is necessary to know about whenever you want to specify it as an element type of an array or a member of a struct, so this was my effort to get the user to find out about it's existence.
Though now I notice that I forgot the N
!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, righto. Yes, I find the constructor/type dichotomy odd too. I often wish the doc system would provide more help here, in terms of autodocumenting the type signature docs, in a way which is systematic but not intrusive. Oh ho, look at this trick:
module A
"""
$(B)
B is a thing with some type parameters
"""
type B{N,T,X,Y,Z}
end
end
help?> A.B
A.B{N,T,X,Y,Z}
B is a thing with some type parameters
Kinda close?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still, even if this was neater, it relies on package authors being systematic and good at writing docs...
@inline SArray(a::StaticArray) = SArray{size_tuple(a)}(Tuple(a)) # TODO fixme | ||
|
||
# Simplified show for the type | ||
show(io::IO, ::Type{SArray{S, T, N}}) where {S, T, N} = print(io, "SArray{$S,$T,$N}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.
@pure Size{S}(::Type{SArray{S}}) = Size(S) | ||
@pure Size{S,T}(::Type{SArray{S,T}}) = Size(S) | ||
@pure Size{S,T,N}(::Type{SArray{S,T,N}}) = Size(S) | ||
@pure Size{S,T,N,L}(::Type{SArray{S,T,N,L}}) = Size(S) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice′
@@ -53,9 +53,12 @@ end | |||
end | |||
end | |||
|
|||
@inline convert{S1,S2,T}(::Type{SMatrix{S1,S2}}, a::StaticArray{T}) = SMatrix{S1,S2,T}(Tuple(a)) | |||
@inline convert{S1,S2,T}(::Type{SMatrix{S1,S2}}, a::StaticArray{<:Any, T}) = SMatrix{S1,S2,T}(Tuple(a)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we want this anyway? It looks like reshape()
rather than a constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does allow reshaping... but primarily it provides the functionality to change T
.
Should we tighten this up at some point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(perhaps a separate PR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Chuck in a TODO?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue?
src/SUnitRange.jl
Outdated
@@ -1,8 +1,23 @@ | |||
# `Start` and `End` should be `Int` | |||
struct SUnitRange{Start,End} <: StaticVector{Int} | |||
struct SUnitRange{Start, End, L} <: StaticVector{L, Int} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this work just as well if parameterized only on Start
and L
, with End
being computed via a pure function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we could - users can use the @pure
constructor on line 20.
@@ -93,4 +84,4 @@ Creates a `SizedArray` wrapping `array` with the specified statically-known | |||
`dims`, so to take advantage of the (faster) methods defined by the static array | |||
package. | |||
""" | |||
(::Size{S}){S}(a::Array) = SizedArray{S}(a) | |||
(::Size{S}){S}(a::Array) = SizedArray{Tuple{S...}}(a) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not convinced this is a good pun!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. Make a PR :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heh :)
|
||
default_similar_type{T,S}(::Type{T}, s::Size{S}, ::Type{Val{0}}) = Scalar{T} | ||
default_similar_type{T,S}(::Type{T}, s::Size{S}, ::Type{Val{1}}) = SVector{S[1],T} | ||
default_similar_type{T,S}(::Type{T}, s::Size{S}, ::Type{Val{2}}) = SMatrix{S[1],S[2],T,prod(s)} | ||
default_similar_type{T,S,D}(::Type{T}, s::Size{S}, ::Type{Val{D}}) = SArray{Tuple{S...},T,D,prod(s)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work for the Scalar
case? I seem to get
julia> SArray{Tuple{},Int,0,0}(())
ERROR: ArgumentError: Size mismatch in Static Array parameters. Got size Tuple{}, dimension 0 and length 0.
Stacktrace:
[1] check_array_parameters(...) at /home/chris/.julia/v0.6/StaticArrays/src/util.jl:57
[2] StaticArrays.SArray{Tuple{},Int64,0,0}(::Tuple{}) at /home/chris/.julia/v0.6/StaticArrays/src/SArray.jl:22
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The length of a Scalar
is 1
.
Try SArray{Tuple{},Int,0,1}((3,))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also prod([]) === 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh. Duh!
# I see an inference error in the combined testset below. I can't reproduce | ||
# at the REPL or in a function for individual n, etc... speculatively, it | ||
# might be reusing A, b with different types in the same (non-toplevel) | ||
# scope, for which I've come accross inference bugs in the past. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somewhat disturbing.
Seems mitigated by the fact that this test case is rather odd with its loop over types though. May not be a problem in practice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's not a problem in practice, or at least I couldn't find any evidence of a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, cheers.
This is two commits squashed together, with significant modifications to backport to julia-0.5. b1714d9 ----------------------------- * Unify SArray,SMatrix,SVector After the recent refactor something that used to be an SMatrix became a two dimensional SArray (or vice versa - who can keep track). That situation seems unfortunate and unnecessary, so this is an attempt to unify them. This is decently breaking, because it requires the SArray dimension tuple to be e.g. `Tuple{1,2}` rather than `(1,2)`, but should allow us to avoid quite a bit of duplication. These are just the minimal changes to get this running. I would imagine, we could remove some further duplication in follow up patches. * Bring Scalar/MArray into the fold c4008ff ----------------------------- WIP Add size to abstract StaticArray type (#134) * Add size to abstract StaticArray type * Update SUnitRange + bufix
This is a continuation of #127 whereby we add a
Tuple{...}
size descriptor to the abstractStaticArray
type.I'm previewing this WIP to get feedback on the interface, such as whether we want
StaticArray{Size, T, N}
, or have the dimensions afterT
, or afterN
. Any comments appreciated, otherwise I'll carry on.