Skip to content

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

Merged
merged 5 commits into from
Apr 21, 2017

Conversation

andyferris
Copy link
Member

This is a continuation of #127 whereby we add a Tuple{...} size descriptor to the abstract StaticArray 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 after T, or after N. Any comments appreciated, otherwise I'll carry on.

@andyferris andyferris force-pushed the size-in-staticarray-type branch from a647fca to 770f5c9 Compare April 3, 2017 12:49
@andyferris
Copy link
Member Author

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.

Andy Ferris added 2 commits April 19, 2017 14:06
@andyferris andyferris force-pushed the size-in-staticarray-type branch from ce3714d to a0418f6 Compare April 19, 2017 04:07
Still having an inference problem in test/solve.jl. Can't reproduce,
however.
@andyferris
Copy link
Member Author

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. :)

@coveralls
Copy link

coveralls commented Apr 19, 2017

Coverage Status

Coverage decreased (-0.6%) to 69.105% when pulling 2086cb1 on size-in-staticarray-type into b643ff5 on master.

@c42f c42f mentioned this pull request Apr 19, 2017
@c42f
Copy link
Member

c42f commented Apr 19, 2017

Afraid I don't have the energy to look at this tonight... maybe tomorrow.

SimonDanisch added a commit that referenced this pull request Apr 19, 2017
@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 69.697% when pulling d6480b0 on size-in-staticarray-type into b643ff5 on master.

Copy link
Member

@c42f c42f left a 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, ...)
Copy link
Member

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?

Copy link
Member Author

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!

Copy link
Member

@c42f c42f Apr 21, 2017

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?

Copy link
Member

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}")
Copy link
Member

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)
Copy link
Member

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))
Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(perhaps a separate PR)

Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue?

@@ -1,8 +1,23 @@
# `Start` and `End` should be `Int`
struct SUnitRange{Start,End} <: StaticVector{Int}
struct SUnitRange{Start, End, L} <: StaticVector{L, Int}
Copy link
Member

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?

Copy link
Member Author

@andyferris andyferris Apr 21, 2017

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)
Copy link
Member

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!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. Make a PR :)

Copy link
Member

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)}
Copy link
Member

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

Copy link
Member Author

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,))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also prod([]) === 1

Copy link
Member

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.
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

@c42f c42f left a 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.

@andyferris andyferris merged commit c4008ff into master Apr 21, 2017
c42f added a commit that referenced this pull request Apr 24, 2017
c42f added a commit that referenced this pull request Apr 24, 2017
c42f added a commit that referenced this pull request Apr 24, 2017
Various doc updates as fallout from merging #127, #134.
c42f pushed a commit that referenced this pull request May 3, 2017
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
@SimonDanisch SimonDanisch deleted the size-in-staticarray-type branch May 17, 2018 10:57
oschulz pushed a commit to oschulz/StaticArrays.jl that referenced this pull request Apr 4, 2023
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

Successfully merging this pull request may close these issues.

3 participants