Skip to content

Commit

Permalink
Fixing constructors for degenerated cases
Browse files Browse the repository at this point in the history
  • Loading branch information
fverdugo committed Jun 17, 2019
1 parent 798fb5c commit cebb5df
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 11 deletions.
60 changes: 49 additions & 11 deletions src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}
Expand Down
86 changes: 86 additions & 0 deletions test/TypesTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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})
Expand All @@ -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]
Expand All @@ -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})
Expand Down Expand Up @@ -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}
Expand All @@ -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

Expand Down

0 comments on commit cebb5df

Please sign in to comment.