Skip to content

Commit

Permalink
Added indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
fverdugo committed May 14, 2019
1 parent ee998ef commit 47d85a1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Indexing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

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

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

@propagate_inbounds function getindex(
a::MultiValue{S,T,N}, I::Vararg{Integer,N}) where {S,T,N}
@inbounds a.array[I...]
end

@propagate_inbounds function getindex(a::MultiValue, i::Integer)
@inbounds a.array[i]
end

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

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

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

eachindex(a::MultiValue) = eachindex(a.array)
6 changes: 6 additions & 0 deletions src/TensorValues.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TensorValues

using StaticArrays
using Base: @propagate_inbounds

export MultiValue
export TensorValue
Expand All @@ -12,10 +13,15 @@ export det, inv
import Base: show
import Base: zero, one
import Base: +, -, *, /, \, ==,
import Base: getindex, iterate, eachindex
import Base: size, length, eltype

import LinearAlgebra: det, inv

include("Types.jl")

include("Indexing.jl")

include("Operations.jl")

end # module
33 changes: 33 additions & 0 deletions test/IndexingTests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module IndexingTests

using Test
using TensorValues

a = (3,4,5,1)

v = VectorValue{4}(a)

@test eltype(v) == Int
@test eltype(typeof(v)) == Int

@test size(v) == (4,)
@test length(v) == 4

for (k,i) in enumerate(eachindex(v))
@test v[i] == a[k]
end

t = TensorValue{2}(a)

@test size(t) == (2,2)
@test length(t) == 4

for (k,i) in enumerate(eachindex(t))
@test t[i] == a[k]
end

@test t[2,1] == 4

@test t[2] == 4

end # module IndexingTests
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ end
include("OperationsTests.jl")
end

@testset "IndexingTests" begin
include("IndexingTests.jl")
end

end # module TensorValuesTests

0 comments on commit 47d85a1

Please sign in to comment.