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

Rewrite SLVector #18

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
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
julia 1.0
StaticArrays
Reexport
8 changes: 5 additions & 3 deletions src/LabelledArrays.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module LabelledArrays

using StaticArrays, LinearAlgebra
using LinearAlgebra
using Reexport

@reexport using StaticArrays

include("slvector.jl")
include("lvector.jl")

export SLVector, LVector, @SLVector, @LVector
export LVector, @SLVector, @LVector

end # module
46 changes: 46 additions & 0 deletions src/lvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,49 @@ macro LVector(vals,syms)
end
end
end

#####################################
# SLVector
#####################################
const SLVector = LVector{T,SVector{N,T},Syms} where {T,N,Syms}

"""
@SLVector ElementType Names

Creates an anonymous function that builds a labelled static vector with eltype
`ElementType` with names determined from the `Names`. The labbeled static vector
is just an `LVector` whose entries are `SVector{ElementType}`.

For example:

```julia
ABC = @SLVector Float64 (:a,:b,:c)
x = ABC(1.0,2.5,3.0)
x.a == 1.0
x.b == 2.5
x.c == x[3]
```

"""
macro SLVector(E,syms)
return quote
function (vals...,)
v = SVector{$(length(syms.args)), $E}(vals...)
T = typeof(v)
return LVector{$(esc(E)),T,$syms}(v)
end
end
end

Base.copy(x::SLVector{T,N,Syms}) where {T,N,Syms} = LVector{Syms}(x.__x)
function Base.similar(::SLVector{T,N,Syms}, ::Type{S}) where {T,N,Syms,S}
tmp = Vector{S}(undef, N)
LVector{Syms}(SVector{N}(tmp))
end
function Base.AbstractVector{S}(x::SLVector{T,N,Syms}) where {S,T,N,Syms}
LVector{Syms}(S.(x.__x))
end
function Base.broadcast(f, xs::SLVector{T,N,Syms}...) where {T,N,Syms}
result = broadcast(f, (x.__x for x in xs)...)
LVector{Syms}(result)
end
37 changes: 0 additions & 37 deletions src/slvector.jl

This file was deleted.

4 changes: 2 additions & 2 deletions test/diffeq.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using LabelledArrays, OrdinaryDiffEq, Test

@SLVector LorenzVector Float64 [x,y,z]
@SLVector LorenzParameterVector Float64 [σ,ρ,β]
LorenzVector = @SLVector Float64 (:x,:y,:z)
LorenzParameterVector = @SLVector Float64 (:σ,:ρ,:β)

function f(u,p,t)
x = p.σ*(u.y-u.x)
Expand Down
27 changes: 24 additions & 3 deletions test/slvectors.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using LabelledArrays
using Test

@SLVector ABC Int [a,b,c]
ABC = @SLVector Int (:a,:b,:c)
b = ABC(1,2,3)

@test b.a == 1
Expand All @@ -12,5 +12,26 @@ b = ABC(1,2,3)
@test b[3] == b.c

@test_throws UndefVarError fill!(a,1)
@test typeof(b) <: SLVector{3,Int}
typeof(b.+b) <: ABC
@test typeof(b.__x) <: SVector{3,Int}
bb = b.+b
@test bb isa LVector
@test eltype(bb) == Int
b1 = b.+1.0
@test b1 isa LVector
@test eltype(b1) == Float64

# Type stability tests
ABC_int = @SLVector Int (:a,:b,:c)
ABC_float = @SLVector Float64 (:a, :b, :c)
x = ABC_int(1,2,3)
y = ABC_float(4.,5.,6.)

@test typeof(copy(x)) == typeof(x)
@test typeof(similar(x)) == typeof(x)
@test typeof(similar(x, Float64)) == typeof(y)
@test typeof(convert(AbstractVector{Float64}, x)) == typeof(y)
@test_broken typeof(x .+ x) == typeof(x) # degrades to LVector of Vector{Int}
@test typeof(broadcast(+, x, x)) == typeof(x) # why does this work then?
@test_broken typeof(Float64.(x)) == typeof(y) # degrades to LVector of Vector{Float}
@test typeof(broadcast(Float64, x)) == typeof(y) # why does this work then?
@test_broken broadcast(+, x, y) # ERROR: conflicting broadcast rules defined