Skip to content

Commit

Permalink
Removed type pretty printing and added more constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
fverdugo committed May 15, 2019
1 parent a38b887 commit 14f1b80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
24 changes: 12 additions & 12 deletions src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ function (::Type{TensorValue{D,T}})(x::Vararg) where {D,T}
TensorValue{D,T}(x)
end

@generated function TensorValue(args::Vararg{T,DD}) where {T,DD}
@generated function TensorValue(arg::NTuple{DD,T}) where {T,DD}
SQ = sqrt(DD)
D = ceil(Int,SQ)
@assert D == SQ
:( TensorValue{$D,T}(args...) )
:( TensorValue{$D,T}(arg) )
end

function TensorValue(args::Vararg)
TensorValue(args)
end

# Constructors (VectorValue)
Expand All @@ -85,8 +89,12 @@ function (::Type{VectorValue{D,T}})(x::Vararg{Any,D}) where {D,T}
VectorValue{D,T}(x)
end

function VectorValue(args::Vararg{T,D}) where {T,D}
VectorValue{D,T}(args)
function VectorValue(arg::NTuple{D,T}) where {D,T}
VectorValue{D,T}(arg)
end

function VectorValue(args::Vararg)
VectorValue(args)
end

# Initializers
Expand All @@ -103,14 +111,6 @@ end

# Custom type printing

function show(io::IO,::Type{<:TensorValue{D,T,L}}) where {D,T,L}
print(io,"TensorValue{$D,$T,$L}")
end

function show(io::IO,::Type{<:VectorValue{D,T}}) where {D,T}
print(io,"VectorValue{$D,$T}")
end

function show(io::IO,v::MultiValue)
print(io,typeof(v))
print(io,v.array.data)
Expand Down
18 changes: 10 additions & 8 deletions test/TypesTests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module NDimValuesTests
module TypesTests

using TensorValues
using Test
Expand Down Expand Up @@ -50,6 +50,10 @@ t = TensorValue(1,2,3,4)
@test isa(t,TensorValue{2,Int})
@test t.array == [1 3;2 4]

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

# Constructors (VectorValue)

g = VectorValue{4}((1,2,3,4))
Expand All @@ -72,6 +76,10 @@ g = VectorValue(1,2,3,4)
@test isa(g,VectorValue{4,Int})
@test g.array == [1,2,3,4]

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

# Initializers

z = zero(MultiValue{Tuple{3,2},Int,2,6})
Expand All @@ -95,10 +103,4 @@ z = one(TensorValue{3,Int,9})
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

s = "TensorValue{2,Int64,4}(1, 2, 3, 4)"
@test string(t) == s

s = "VectorValue{4,Int64}(1, 2, 3, 4)"
@test string(g) == s

end # module NDimValuesTests
end # module TypesTests

0 comments on commit 14f1b80

Please sign in to comment.