Skip to content

Commit

Permalink
Added CellMapsOperations
Browse files Browse the repository at this point in the history
  • Loading branch information
fverdugo committed Jun 9, 2019
1 parent e0d3ec0 commit c8f4b29
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/CellArrays.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module CellArrays

using CellwiseValues
using CellwiseValues.CellValues: _test_iter_cell_value

export CellArray
export CellMatrix
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 11 additions & 4 deletions src/CellMapOperations.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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

Expand Down
114 changes: 111 additions & 3 deletions src/CellMaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ module CellMaps

using Test
using CellwiseValues
using CellwiseValues.CellValues: _test_iter_cell_value

export CellMap
export IterCellMap
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
Expand All @@ -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
Expand All @@ -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})
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/CellValues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions src/CellwiseValues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
50 changes: 50 additions & 0 deletions test/CellMapOperationsTests.jl
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions test/CellMapsTests.jl
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/CellValuesMocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c8f4b29

Please sign in to comment.