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

Add MScalar Type #591

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions src/MScalar.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
MScalar{T}(x::T)

Construct a mutable, statically-sized, 0-dimensional array that contains a
single element, `x`. This type is particularly useful for influencing
broadcasting operations.
"""
const MScalar{T} = MArray{Tuple{},T,0,1}

@inline MScalar(x::Tuple{T}) where {T} = MScalar{T}(x[1])
@inline MScalar(a::AbstractArray) = MScalar{typeof(a)}((a,))
@inline MScalar(a::AbstractScalar) = MScalar{eltype(a)}((a[],)) # Do we want this to convert or wrap?
@inline function convert(::Type{SA}, a::AbstractArray) where {SA <: MScalar}
return MScalar((a[],))
end
@inline convert(::Type{SA}, sa::SA) where {SA <: MScalar} = sa

# A lot more compact than the default array show
Base.show(io::IO, ::MIME"text/plain", x::MScalar{T}) where {T} = print(io, "MScalar{$T}(", x.data, ")")

# Simplified show for the type
show(io::IO, ::Type{MScalar{T}}) where {T} = print(io, "MScalar{T}")
3 changes: 2 additions & 1 deletion src/StaticArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end
export SOneTo
export StaticScalar, StaticArray, StaticVector, StaticMatrix
export Scalar, SArray, SVector, SMatrix
export MArray, MVector, MMatrix
export MScalar, MArray, MVector, MMatrix
export FieldVector
export SizedArray, SizedVector, SizedMatrix
export SDiagonal
Expand Down Expand Up @@ -108,6 +108,7 @@ include("Scalar.jl")
include("MArray.jl")
include("MVector.jl")
include("MMatrix.jl")
include("MScalar.jl")
include("SizedArray.jl")
include("SDiagonal.jl")

Expand Down
24 changes: 14 additions & 10 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,23 @@ end
exprs[current_ind...] = :(dest[$j] = f($(exprs_vals...)))

# increment current_ind (maybe use CartesianIndices?)
current_ind[1] += 1
for i ∈ 1:length(newsize)
if current_ind[i] > newsize[i]
if i == length(newsize)
more = false
break
if length(current_ind) >= 1
current_ind[1] += 1
for i ∈ 1:length(newsize)
if current_ind[i] > newsize[i]
if i == length(newsize)
more = false
break
else
current_ind[i] = 1
current_ind[i+1] += 1
end
else
current_ind[i] = 1
current_ind[i+1] += 1
break
end
else
break
end
else
break
end
j += 1
end
Expand Down
6 changes: 6 additions & 0 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ end
end
stride *= Size(S)[i]
end
if length(inds) < 1
ind_expr = :(1)
end
return quote
@_propagate_inbounds_meta
a[$ind_expr]
Expand All @@ -46,6 +49,9 @@ end
end
stride *= S[i]
end
if length(inds) < 1
ind_expr = :(1)
end
return quote
@_propagate_inbounds_meta
a[$ind_expr] = value
Expand Down
22 changes: 22 additions & 0 deletions test/MScalar.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@testset "MScalar" begin
@test MScalar(2) .* [1, 2, 3] == [2, 4, 6]
@test_throws DimensionMismatch (MScalar(2) .= (Scalar(2) .* [1, 2, 3])) == [2, 4, 6]
@test MScalar([1 2; 3 4]) .+ [[1 1; 1 1], [2 2; 2 2]] == [[2 3; 4 5], [3 4; 5 6]]
@test (MScalar(1) + MScalar(1.0))::MScalar{Float64} ≈ MScalar(2.0)
@test_throws BoundsError MScalar(2)[2]
@test MScalar(2)[] == 2
x = MScalar(2)
x[] = 1
@test x[] == 1
@testinf Tuple(MScalar(2)) === (2,)
@testinf Tuple(convert(MScalar{Float64}, [2.0])) === (2.0,)
a = Array{Float64, 0}(undef)
a[] = 2
@test MScalar(a)[] == 2
s = MScalar(a)
@test convert(typeof(s), s) === s
s[] = 1
@test s[] == 1
s .= 42
@test s[] == 42
end
6 changes: 6 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ end
@test @inferred(v2 .* 1.0)::typeof(v) == v
end

@testset "0-dimensional Array broadcast" begin
x = Array{Int, 0}(undef)
x .= Scalar(4)
@test x[] == 4
end

@testset "2x2 StaticMatrix with StaticVector" begin
m = @SMatrix [1 2; 3 4]
v = SVector(1, 4)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ include("SArray.jl")
include("MArray.jl")
include("FieldVector.jl")
include("Scalar.jl")
include("MScalar.jl")
include("SUnitRange.jl")
include("SizedArray.jl")
include("SDiagonal.jl")
Expand Down