From 14f1b80e94f83a85b3405fe9d59687874bfaca8c Mon Sep 17 00:00:00 2001 From: fverdugo Date: Wed, 15 May 2019 10:22:59 +0200 Subject: [PATCH] Removed type pretty printing and added more constructors --- src/Types.jl | 24 ++++++++++++------------ test/TypesTests.jl | 18 ++++++++++-------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/Types.jl b/src/Types.jl index 29e778a..f487f44 100644 --- a/src/Types.jl +++ b/src/Types.jl @@ -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) @@ -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 @@ -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) diff --git a/test/TypesTests.jl b/test/TypesTests.jl index d7472e9..8204a25 100644 --- a/test/TypesTests.jl +++ b/test/TypesTests.jl @@ -1,4 +1,4 @@ -module NDimValuesTests +module TypesTests using TensorValues using Test @@ -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)) @@ -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}) @@ -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