diff --git a/src/Types.jl b/src/Types.jl index 40b06c6..fa4fc84 100644 --- a/src/Types.jl +++ b/src/Types.jl @@ -25,6 +25,19 @@ function (::Type{MultiValue{S}})(x::Tuple) where S<:Tuple MultiValue(array) end +function (::Type{MultiValue{S}})(x::Tuple{}) where S<:Tuple + s = """ + Unknown element type. + + Provide element type in the corresponding type parameter. + Examples: + MultiValue{Tuple{0,0},Int}() + TensorValue{0,Int}() + VectorValue{0,Int}() + """ + error(s) +end + function (::Type{MultiValue{S,T}})(x::Tuple) where {S<:Tuple,T} array = SArray{S,T}(x) MultiValue(array) @@ -49,15 +62,20 @@ function (::Type{TensorValue{D}})(x::Tuple) where D MultiValue{S}(x) end -function (::Type{TensorValue{D,T}})(x::Tuple) where {D,T} - S = Tuple{D,D} - MultiValue{S,T}(x) +function (::Type{TensorValue{0}})() + S = Tuple{0,0} + MultiValue{S}() end function (::Type{TensorValue{D}})(x::Vararg) where D TensorValue{D}(x) end +function (::Type{TensorValue{D,T}})(x::Tuple) where {D,T} + S = Tuple{D,D} + MultiValue{S,T}(x) +end + function (::Type{TensorValue{D,T}})(x::Vararg) where {D,T} TensorValue{D,T}(x) end @@ -73,6 +91,11 @@ function TensorValue(args::Vararg) TensorValue(args) end +function TensorValue() + S = Tuple{0,0} + MultiValue{S}() +end + function TensorValue(a::StaticArray) TensorValue(a.data) end @@ -84,31 +107,46 @@ function (::Type{VectorValue{D}})(x::Tuple) where D MultiValue{S}(x) end -function (::Type{VectorValue{D,T}})(x::Tuple) where {D,T} - S = Tuple{D} - MultiValue{S,T}(x) -end - -function (::Type{VectorValue{D}})(x::Vararg{Any,D}) where D +function (::Type{VectorValue{D}})(x::Vararg) where D VectorValue{D}(x) end -function (::Type{VectorValue{D,T}})(x::Vararg{Any,D}) where {D,T} - VectorValue{D,T}(x) +function (::Type{VectorValue{D,T}})() where {D,T} + S = Tuple{D} + MultiValue{S,T}() end function VectorValue(arg::NTuple{D,T}) where {D,T} VectorValue{D,T}(arg) end +function (::Type{VectorValue{D,T}})(x::Vararg{Number,D}) where {T,D} + VectorValue{D,T}(x) +end + function VectorValue(args::Vararg) VectorValue(args) end +function VectorValue() + S = Tuple{0} + MultiValue{S}() +end + function VectorValue(a::StaticArray) VectorValue(a.data) end +function VectorValue(a::SVector) + MultiValue(a) +end + +function VectorValue(a::MVector) + MultiValue(a) +end + + + # Initializers function zero(::Type{<:MultiValue{S,T,N,L}}) where {S,T,N,L} diff --git a/test/TypesTests.jl b/test/TypesTests.jl index 91b071b..7832c9c 100644 --- a/test/TypesTests.jl +++ b/test/TypesTests.jl @@ -33,6 +33,30 @@ v = MultiValue{Tuple{3,2},Float64}(1,2,3,4,5,6) @test isa(v,MultiValue{Tuple{3,2},Float64}) @test v.array == a +a = SVector(1) +v = MultiValue{Tuple{1}}((1,)) +@test isa(v,MultiValue{Tuple{1},Int}) +@test v.array == a + +v = MultiValue{Tuple{1}}(1) +@test isa(v,MultiValue{Tuple{1},Int}) +@test v.array == a + +a = SMatrix{1,1}(1) +v = MultiValue{Tuple{1,1}}(1) +@test isa(v,MultiValue{Tuple{1,1},Int}) +@test v.array == a + +a = SVector{0,Int}() +v = MultiValue{Tuple{0},Int}(()) +@test isa(v,MultiValue{Tuple{0},Int}) +@test v.array == a + +a = SMatrix{0,0,Int}() +v = MultiValue{Tuple{0,0},Int}() +@test isa(v,MultiValue{Tuple{0,0},Int}) +@test v.array == a + # Constructors (TensorValue) a = SMatrix{2,2}(1,2,3,4) @@ -69,8 +93,33 @@ t = TensorValue((1,2,3,4)) @test isa(t,TensorValue{2,Int}) @test t.array == [1 3;2 4] +t = TensorValue{0,Int}() +@test isa(t,TensorValue{0,Int}) +@test t.array == zeros(0,0) + +t = TensorValue{1}(10) +@test isa(t,TensorValue{1,Int}) +@test t.array == 10*ones(1,1) + +t = TensorValue{1}((10,)) +@test isa(t,TensorValue{1,Int}) +@test t.array == 10*ones(1,1) + +t = TensorValue{1,Float64}(10) +@test isa(t,TensorValue{1,Float64}) +@test t.array == 10*ones(1,1) + +t = TensorValue{1,Float64}((10,)) +@test isa(t,TensorValue{1,Float64}) +@test t.array == 10*ones(1,1) + # Constructors (VectorValue) +a = SVector(1) +g = VectorValue(a) +@test isa(g,VectorValue{1,Int}) +@test g.array == [1,] + a = SVector(1,2,3,4) g = VectorValue(a) @test isa(g,VectorValue{4,Int}) @@ -85,6 +134,34 @@ g = VectorValue{4}((1,2,3,4)) @test isa(g,VectorValue{4,Int}) @test g.array == [1,2,3,4] +g = VectorValue{1}((1,)) +@test isa(g,VectorValue{1,Int}) +@test g.array == [1,] + +g = VectorValue{0,Int}(()) +@test isa(g,VectorValue{0,Int}) +@test g.array == [] + +g = VectorValue{4}(1,2,3,4) +@test isa(g,VectorValue{4,Int}) +@test g.array == [1,2,3,4] + +g = VectorValue{1}(1) +@test isa(g,VectorValue{1,Int}) +@test g.array == [1,] + +g = VectorValue{1,Float64}(1) +@test isa(g,VectorValue{1,Float64}) +@test g.array == [1,] + +g = VectorValue{1,Float64}((1,)) +@test isa(g,VectorValue{1,Float64}) +@test g.array == [1,] + +g = VectorValue{0,Int}() +@test isa(g,VectorValue{0,Int}) +@test g.array == [] + g = VectorValue{4,Float64}((1,2,3,4)) @test isa(g,VectorValue{4,Float64}) @test g.array == [1,2,3,4] @@ -105,6 +182,10 @@ g = VectorValue((1,2,3,4)) @test isa(g,VectorValue{4,Int}) @test g.array == [1,2,3,4] +g = VectorValue(1) +@test isa(g,VectorValue{1,Int}) +@test g.array == [1,] + # Initializers z = zero(MultiValue{Tuple{3,2},Int,2,6}) @@ -137,6 +218,10 @@ a = ones(Int,3) b = convert(VectorValue{3,Int},a) @test isa(b,VectorValue{3,Int}) +a = ones(Int,1) +b = convert(VectorValue{1,Int},a) +@test isa(b,VectorValue{1,Int}) + # Misc operations on the type itself V = VectorValue{3,Int} @@ -149,6 +234,7 @@ V = VectorValue{3} # Custom type printing +v = MultiValue{Tuple{3,2},Float64}(1,2,3,4,5,6) s = "TensorValues.MultiValue{Tuple{3,2},Float64,2,6}(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)" @test string(v) == s