Skip to content

Commit

Permalink
Creating MultiValues and co from StaticArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
fverdugo committed May 15, 2019
1 parent 14f1b80 commit 77936a2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ function (::Type{MultiValue{S,T}})(x::Vararg) where {S<:Tuple,T}
MultiValue{S,T}(x)
end

function MultiValue(a::StaticArray{S,T}) where {S,T}
MultiValue{S,T}(a.data)
end

# Constructors (TensorValue)

function (::Type{TensorValue{D}})(x::Tuple) where D
Expand Down Expand Up @@ -69,6 +73,10 @@ function TensorValue(args::Vararg)
TensorValue(args)
end

function TensorValue(a::StaticArray)
TensorValue(a.data)
end

# Constructors (VectorValue)

function (::Type{VectorValue{D}})(x::Tuple) where D
Expand Down Expand Up @@ -97,6 +105,10 @@ function VectorValue(args::Vararg)
VectorValue(args)
end

function VectorValue(a::StaticArray)
VectorValue(a.data)
end

# Initializers

function zero(::Type{<:MultiValue{S,T,N,L}}) where {S,T,N,L}
Expand Down
25 changes: 25 additions & 0 deletions test/TypesTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ using StaticArrays

# Constructors (MultiValue)

a = MArray{Tuple{3,2}}((1,2,3,4,5,6))
v = MultiValue(a)
@test isa(v,MultiValue{Tuple{3,2},Int})
@test v.array.data === a.data

a = SArray{Tuple{3,2}}((1,2,3,4,5,6))

v = MultiValue(a)
Expand All @@ -30,6 +35,16 @@ v = MultiValue{Tuple{3,2},Float64}(1,2,3,4,5,6)

# Constructors (TensorValue)

a = SMatrix{2,2}(1,2,3,4)
t = TensorValue(a)
@test isa(t,TensorValue{2,Int})
@test t.array == [1 3;2 4]

a = MMatrix{2,2}(1,2,3,4)
t = TensorValue(a)
@test isa(t,TensorValue{2,Int})
@test t.array == [1 3;2 4]

t = TensorValue{2}((1,2,3,4))
@test isa(t,TensorValue{2,Int})
@test t.array == [1 3;2 4]
Expand All @@ -56,6 +71,16 @@ t = TensorValue((1,2,3,4))

# Constructors (VectorValue)

a = SVector(1,2,3,4)
g = VectorValue(a)
@test isa(g,VectorValue{4,Int})
@test g.array == [1,2,3,4]

a = MVector(1,2,3,4)
g = VectorValue(a)
@test isa(g,VectorValue{4,Int})
@test g.array == [1,2,3,4]

g = VectorValue{4}((1,2,3,4))
@test isa(g,VectorValue{4,Int})
@test g.array == [1,2,3,4]
Expand Down

0 comments on commit 77936a2

Please sign in to comment.