diff --git a/src/CellArrays.jl b/src/CellArrays.jl index 6d703e0..c917c86 100644 --- a/src/CellArrays.jl +++ b/src/CellArrays.jl @@ -1,6 +1,7 @@ module CellArrays using CellwiseValues +using CellwiseValues.CellValues: _test_iter_cell_value export CellArray export CellMatrix @@ -50,7 +51,7 @@ collect(a::CellArray) = [ copy(ai) for ai in a ] function test_iter_cell_array( icv::CellArray{T,N}, a::AbstractArray{<:AbstractArray{T,N}}) where {T,N} - test_iter_cell_value(icv,a) + _test_iter_cell_value(icv,a) end function test_index_cell_array( diff --git a/src/CellMapOperations.jl b/src/CellMapOperations.jl index d92b9b4..de75956 100644 --- a/src/CellMapOperations.jl +++ b/src/CellMapOperations.jl @@ -1,20 +1,27 @@ module CellMapOperations using CellwiseValues +using CellwiseValues.MapOperations: _compute_S, _compute_M +using CellwiseValues.MapOperations: MapFromKernel +using CellwiseValues.ArrayOperations: _compute_N, _compute_T import CellwiseValues: evaluate -import MapOperations: _stype: _m +import CellwiseValues.MapOperations: _stype, _m import CellwiseValues.ArrayOperations: _nd, _eltype import CellwiseValues: apply import Base: iterate import Base: length +function apply(k::ArrayKernel,m::CellMap,v::Vararg{<:CellValue}) + CellMapFromKernel(k,m,v...) +end + struct CellMapFromKernel{S,M,T,N,R,K,V} <: IterCellMap{S,M,T,N,R} kernel::K cellvalues::V end -function CellMapFromKernel(k::ArrayKernel,v::Vararg{<:CellValues}) +function CellMapFromKernel(k::ArrayKernel,v::Vararg{<:CellValue}) S = _compute_S(v) M = _compute_M(v) T = _compute_T(k,v) @@ -70,8 +77,8 @@ function _compute_R(k,v) T = _compute_T(k,m) N = _compute_N(k,m) K = typeof(k) - V = m - C = tuple([ _cache_type(mi) for mi in m]...) + V = Tuple{m...} + C = Tuple{[ _cache_type(mi) for mi in m]...} MapFromKernel{S,M,T,N,K,V,C} end diff --git a/src/CellMaps.jl b/src/CellMaps.jl index 87edeca..f3881b4 100644 --- a/src/CellMaps.jl +++ b/src/CellMaps.jl @@ -2,6 +2,7 @@ module CellMaps using Test using CellwiseValues +using CellwiseValues.CellValues: _test_iter_cell_value export CellMap export IterCellMap @@ -9,6 +10,12 @@ export IndexCellMap import CellwiseValues: evaluate export test_iter_cell_map export test_index_cell_map +export test_index_cell_map_with_index_arg + +import Base: iterate +import Base: length +import Base: size +import Base: getindex """ Abstract object that traverses a set of cells and at every cell returns a @@ -26,11 +33,82 @@ Abstract object that for a given cell index returns a `Map{S,M,T,N}` """ const CellMap{S,M,T,N} = Union{IterCellMap{S,M,T,N},IndexCellMap{S,M,T,N}} +# evaluation + """ Return the cellwise maps of a `CellMap` on a cellwise set of points """ -function evaluate(::CellMap{S,M,T,N},::CellArray{S,M})::CellArray{T,N} where {S,M,T,N} - @abstractmethod +function evaluate(cm::CellMap{S,M,T,N},ca::CellArray{S,M}) where {S,M,T,N} + IterCellMapValue(cm,ca) +end + +function evaluate(cm::IndexCellMap{S,M,T,N},ca::IndexCellArray{S,M}) where {S,M,T,N} + IndexCellMapValue(cm,ca) +end + +struct IterCellMapValue{T,N,V,A} <: IterCellArray{T,N,CachedArray{T,N,Array{T,N}}} + cm::V + ca::A +end + +function IterCellMapValue(cm::CellMap{S,M,T,N},ca::CellArray{S,M}) where {S,M,T,N} + V = typeof(cm) + A = typeof(ca) + IterCellMapValue{T,N,V,A}(cm,ca) +end + +length(v::IterCellMapValue) = length(v.ca) + +@inline function iterate(v::IterCellMapValue{T,N}) where {T,N} + cmnext = iterate(v.cm) + canext = iterate(v.ca) + r = CachedArray(T,N) + _iterate(cmnext,canext,r) +end + +@inline function iterate(v::IterCellMapValue,state) + cmstate, castate, r = state + cmnext = iterate(v.cm,cmstate) + canext = iterate(v.ca,castate) + _iterate(cmnext,canext,r) +end + +@inline function _iterate(cmnext,canext,r) + if cmnext === nothing; return nothing end + if canext === nothing; return nothing end + m, cmstate = cmnext + a, castate = canext + s = return_size(m,size(a)) + setsize!(r,s) + evaluate!(m,a,r) + state = (cmstate,castate,r) + (r, state) +end + +struct IndexCellMapValue{T,N,V,A} <: IndexCellArray{T,N,CachedArray{T,N,Array{T,N}},1} + cm::V + ca::A + r::CachedArray{T,N,Array{T,N}} +end + +function IndexCellMapValue(cm::IndexCellMap{S,M,T,N},ca::IndexCellArray{S,M}) where {S,M,T,N} + V = typeof(cm) + A = typeof(ca) + r = CachedArray(T,N) + IndexCellMapValue{T,N,V,A}(cm,ca,r) +end + +length(v::IndexCellMapValue) = length(v.ca) + +size(v::IndexCellMapValue) = (length(v),) + +function getindex(v::IndexCellMapValue,i::Integer) + m = v.cm[i] + a = v.ca[i] + s = return_size(m,size(a)) + setsize!(v.r,s) + evaluate!(m,a,v.r) + v.r end # Testers @@ -44,7 +122,7 @@ function test_iter_cell_map( @test length(m) == length(a) c = evaluate(m,a) - test_iter_cell_array(c,b) + _test_iter_cell_value(c,b) for (mi,ai,bi) in zip(m,a,b) @assert isa(mi,Map{S,M,T,N}) @@ -73,4 +151,34 @@ function test_index_cell_map( end +function test_index_cell_map_with_index_arg( + m::IndexCellMap{S,M,T,N}, + a::IndexCellArray{S,M}, + b::AbstractArray{<:AbstractArray{T,N}}) where {S,M,T,N} + + test_index_cell_map(m,a,b) + + c = evaluate(m,a) + @test isa(c,IndexCellArray) + + @test length(c) == length(m) + @test length(c) == length(a) + + for i in 1:length(c) + mi = m[i] + bi = b[i] + ci = c[i] + @assert bi ≈ ci + end + + for i in 1:length(m) + mi = m[i] + ai = a[i] + bi = b[i] + @assert isa(mi,Map{S,M,T,N}) + @assert evaluate(mi,ai) ≈ bi + end + +end + end # module CellMaps diff --git a/src/CellValues.jl b/src/CellValues.jl index 413e52f..e65dbcc 100644 --- a/src/CellValues.jl +++ b/src/CellValues.jl @@ -57,6 +57,10 @@ end # Testers function test_iter_cell_value(icv::CellValue{T},a::AbstractArray{T}) where T + _test_iter_cell_value(icv,a) +end + +function _test_iter_cell_value(icv,a) @test length(icv) == length(a) diff --git a/src/CellwiseValues.jl b/src/CellwiseValues.jl index 9ff354c..2e8d8d9 100644 --- a/src/CellwiseValues.jl +++ b/src/CellwiseValues.jl @@ -5,6 +5,9 @@ using Reexport include("Helpers.jl") @reexport using CellwiseValues.Helpers +include("CachedArrays.jl") +@reexport using CellwiseValues.CachedArrays + include("CellValues.jl") @reexport using CellwiseValues.CellValues @@ -29,9 +32,6 @@ include("Kernels.jl") include("NumberOperations.jl") @reexport using CellwiseValues.NumberOperations -include("CachedArrays.jl") -@reexport using CellwiseValues.CachedArrays - include("ArrayOperations.jl") @reexport using CellwiseValues.ArrayOperations diff --git a/test/CellMapOperationsTests.jl b/test/CellMapOperationsTests.jl index e69de29..f91306b 100644 --- a/test/CellMapOperationsTests.jl +++ b/test/CellMapOperationsTests.jl @@ -0,0 +1,50 @@ +include("CellValuesMocks.jl") +include("MapsMocks.jl") +include("../src/CellMapOperations.jl") +module CellMapOperationsTests + +using Test +using CellwiseValues +using TensorValues + +using ..CellValuesMocks +using ..MapsMocks + +l = 10 + +a = VectorValue(10,10) +b = VectorValue(15,20) +p1 = VectorValue(1,1) +p2 = VectorValue(2,2) +p3 = VectorValue(3,3) +p = [p1,p2,p3] +m = MockMap(a) +r = evaluate(m,p) + +cm = TestIterCellValue(m,l) +cp = TestIterCellValue(p,l) +rm = [ CachedArray(r) for i in 1:l] + +test_iter_cell_map(cm,cp,rm) + +cm2 = apply(-,cm,broadcast=true) +rm = [ CachedArray(-r) for i in 1:l] + +@test length(cm2) == length(cm) +@test length(cm2) == length(rm) + +test_iter_cell_map(cm2,cp,rm) + + + +cm = TestIndexCellValue(m,l) +cp = TestIndexCellValue(p,l) +rm = [ CachedArray(r) for i in 1:l] + +test_index_cell_map_with_index_arg(cm,cp,rm) + +ca2 = evaluate(cm,cp) + +test_index_cell_array(ca2,rm) + +end # module CellMapOperationsTests diff --git a/test/CellMapsTests.jl b/test/CellMapsTests.jl new file mode 100644 index 0000000..7b6d296 --- /dev/null +++ b/test/CellMapsTests.jl @@ -0,0 +1,31 @@ +module CellMapsTests + +using Test +using CellwiseValues +using TensorValues + +using ..CellValuesMocks +using ..MapsMocks + +l = 10 + +a = VectorValue(10,10) +b = VectorValue(15,20) +p1 = VectorValue(1,1) +p2 = VectorValue(2,2) +p3 = VectorValue(3,3) +p = [p1,p2,p3] +m = MockMap(a) +r = evaluate(m,p) + +cm = TestIterCellValue(m,l) +cp = TestIterCellValue(p,l) +rm = [ CachedArray(r) for i in 1:l] +test_iter_cell_map(cm,cp,rm) + +cm = TestIndexCellValue(m,l) +cp = TestIndexCellValue(p,l) +rm = [ CachedArray(r) for i in 1:l] +test_index_cell_map_with_index_arg(cm,cp,rm) + +end # module CellMapsTests diff --git a/test/CellValuesMocks.jl b/test/CellValuesMocks.jl index 0805b64..35c4e50 100644 --- a/test/CellValuesMocks.jl +++ b/test/CellValuesMocks.jl @@ -8,6 +8,7 @@ import Base: iterate import Base: length import Base: size import Base: getindex +import CellwiseValues: evaluate struct TestIndexCellValue{T} <: IndexCellValue{T,1} a::T