Skip to content

Commit

Permalink
Adding more functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
fverdugo committed May 19, 2019
1 parent 5f31db2 commit 0febd15
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ function (≈)(a::MultiValue,b::MultiValue)
a.array b.array
end

function ()(
a::AbstractArray{<:MultiValue}, b::AbstractArray{<:MultiValue})
if size(a) != size(b); return false; end
for (ai,bi) in zip(a,b)
if !(aibi); return false; end
end
true
end

# Addition / subtraction

for op in (:+,:-)
Expand Down
12 changes: 11 additions & 1 deletion src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,20 @@ end

# Conversions

function convert(::Type{<:MultiValue{S,T,N}},a::StaticArray{S,T,N}) where {S,T,N}
function convert(::Type{<:MultiValue{S,T,N,L}},a::StaticArray{S,T,N}) where {S,T,N,L}
MultiValue(a)
end

function convert(
::Type{<:MultiValue{S,T,N,L}},a::AbstractArray{T,N}) where {S,T,N,L}
b = convert(SArray{S,T,N,L},a)
MultiValue(b)
end

# Misc operations on the type itself

length(::Type{MultiValue{S,T,N,L}}) where {S,T,N,L} = L

# Custom type printing

function show(io::IO,v::MultiValue)
Expand Down
2 changes: 2 additions & 0 deletions test/OperationsTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ b = VectorValue(2,1,6)
@test a==a
@test a a
@test a!=b
@test [a,a] == [a,a]
@test [a,a] [a,a]

# Addition / subtraction

Expand Down
4 changes: 4 additions & 0 deletions test/TypesTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ a = @SVector ones(Int,3)
b = convert(VectorValue{3,Int},a)
@test isa(b,VectorValue{3,Int})

a = ones(Int,3)
b = convert(VectorValue{3,Int},a)
@test isa(b,VectorValue{3,Int})

# Custom type printing

s = "TensorValues.MultiValue{Tuple{3,2},Float64,2,6}(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)"
Expand Down

0 comments on commit 0febd15

Please sign in to comment.