Skip to content

Commit

Permalink
Added convert and CartesianIndices, LinearIndices
Browse files Browse the repository at this point in the history
  • Loading branch information
fverdugo committed May 17, 2019
1 parent 1e20739 commit 5f31db2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ end

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

iterate(a::MultiValue) = iterate(a.array)
@inline iterate(a::MultiValue) = iterate(a.array)

iterate(a::MultiValue, state) = iterate(a.array, state)
@inline iterate(a::MultiValue, state) = iterate(a.array, state)

eachindex(a::MultiValue) = eachindex(a.array)

function CartesianIndices(a::MultiValue)
CartesianIndices(a.array)
end

function LinearIndices(a::MultiValue)
LinearIndices(a.array)
end
3 changes: 3 additions & 0 deletions src/TensorValues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import Base: +, -, *, /, \, ==, ≈
import Base: getindex, iterate, eachindex
import Base: size, length, eltype
import Base: reinterpret
import Base: convert
import Base: CartesianIndices
import Base: LinearIndices

import LinearAlgebra: det, inv

Expand Down
6 changes: 6 additions & 0 deletions src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ function one(::Type{<:MultiValue{S,T,N,L}}) where {S,T,N,L}
MultiValue{S,T,N,L}(z)
end

# Conversions

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

# Custom type printing

function show(io::IO,v::MultiValue)
Expand Down
6 changes: 6 additions & 0 deletions test/IndexingTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module IndexingTests

using Test
using TensorValues
using StaticArrays

a = (3,4,5,1)

Expand Down Expand Up @@ -34,4 +35,9 @@ for (k,ti) in enumerate(t)
@test ti == a[k]
end

v = @SMatrix zeros(2,3)
w = MultiValue(v)
@test CartesianIndices(w) == CartesianIndices(v)
@test LinearIndices(w) == LinearIndices(v)

end # module IndexingTests
6 changes: 6 additions & 0 deletions test/TypesTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ z = one(TensorValue{3,Int,9})
@test isa(z,TensorValue{3,Int,9})
@test z.array == [1 0 0; 0 1 0; 0 0 1]

# Conversions

a = @SVector 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 5f31db2

Please sign in to comment.