-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from JuliaArrays/Scalar
Added a `Scalar` type for 0-dimensional array
- Loading branch information
Showing
6 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
""" | ||
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}}){T}(x::Tuple{T}) = Scalar{T}(x[1]) | ||
|
||
@pure size(::Type{Scalar}) = () | ||
@pure size{T}(::Type{Scalar{T}}) = () | ||
|
||
getindex(v::Scalar) = v.data | ||
@inline function getindex(v::Scalar, i::Int) | ||
@boundscheck if i != 1 | ||
error("Attempt to index Scalar at index $i") | ||
end | ||
v.data | ||
end | ||
|
||
@inline Tuple(v::Scalar) = (v.data,) | ||
|
||
# A lot more compact than the default array show | ||
Base.show{T}(io::IO, ::MIME"text/plain", x::Scalar{T}) = print(io, "Scalar{$T}(", x.data, ")") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@testset "Scalar" begin | ||
@test Scalar(2) .* [1, 2, 3] == [2, 4, 6] | ||
@test Scalar([1 2; 3 4]) .+ [[1 1; 1 1], [2 2; 2 2]] == [[2 3; 4 5], [3 4; 5 6]] | ||
@test (Scalar(1) + Scalar(1.0))::Scalar{Float64} ≈ Scalar(2.0) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters