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

Added a Scalar type for 0-dimensional array #50

Merged
merged 4 commits into from
Oct 12, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Scalar.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Scalar{T}(x::T)

Construct a statically-sized 0-dimensional array that contains a single element,
`x`. This type is particularly useful for influencing broadcasting operations.
"""
immutable Scalar{T} <: StaticArray{T,0}
data::T
end

@inline (::Type{Scalar}){T}(x::Tuple{T}) = Scalar{T}(x[1])
Copy link

@TotalVerb TotalVerb Oct 12, 2016

Choose a reason for hiding this comment

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

Doesn't this prevent scalarizing tuples? Can this maybe be a convert method instead?

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 StaticArrays interface requires a constructor that accepts a Tuple. I may have messed this up a little though.

(PS - side effect is small - it only does this for a one-tuple which is scalar-like anyway).

Copy link

@TotalVerb TotalVerb Oct 12, 2016

Choose a reason for hiding this comment

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

This code looks like it's preventing scalarizing any tuple, though.

Copy link
Member

Choose a reason for hiding this comment

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

I got caught up on this too, but I think I'm ok after reading Andy's comment - Tuple{T} being the tuple containing exactly one T (as quite distinct from Array{T} containing many T).

Choose a reason for hiding this comment

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

Ah right. The new version does not have the issue with even tuples containing one T though, which is even better 😄.


@pure size(::Type{Scalar}) = ()
@pure size{T}(::Type{Scalar{T}}) = ()

@inline function getindex(v::Scalar)
Copy link

@TotalVerb TotalVerb Oct 12, 2016

Choose a reason for hiding this comment

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

Are these @inline annotations actually needed? Also, this function can probably be defined as getindex(v::Scalar) = v.data.

Copy link
Member Author

Choose a reason for hiding this comment

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

As a rule I haven't been trusting the heuristics of the compiler anywhere (at times a very large amount of code is inlined). But in this case you are right, I think it will always work.

v.data
end

@inline Tuple(v::Scalar) = (v.data,)
5 changes: 3 additions & 2 deletions src/StaticArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import Base: @pure, @propagate_inbounds, getindex, setindex!, size, similar,
ones, zeros, eye, cross, vecdot, reshape, fill, fill!, det, inv,
eig, trace, vecnorm, dot

export StaticArray, StaticVector, StaticMatrix
export SArray, SVector, SMatrix
export StaticScalar, StaticArray, StaticVector, StaticMatrix
export Scalar, SArray, SVector, SMatrix
export MArray, MVector, MMatrix
export FieldVector, MutableFieldVector

Expand All @@ -21,6 +21,7 @@ export similar_type
include("util.jl")

include("core.jl")
include("Scalar.jl")
include("SVector.jl")
include("FieldVector.jl")
include("SMatrix.jl")
Expand Down
3 changes: 3 additions & 0 deletions src/abstractarray.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
typealias StaticScalar{T} StaticArray{T,0}

@pure length{T<:StaticArray}(a::Union{T,Type{T}}) = prod(size(a))
@pure length{T<:StaticScalar}(a::Union{T,Type{T}}) = 1

@pure function size{T<:StaticArray}(a::Union{T,Type{T}}, d::Integer)
s = size(a)
Expand Down