Skip to content

Commit

Permalink
Added operations returning numbers on indexable cell values
Browse files Browse the repository at this point in the history
  • Loading branch information
fverdugo committed Jun 6, 2019
1 parent f192030 commit f628cda
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/CellValues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ eltype(::Type{C}) where C <: IterCellValue{T} where T = T

abstract type IndexCellValue{T,N} <: AbstractArray{T,N} end

function getindex(::IndexCellValue{T,N}, ::Vararg{Int,N})::T where {T,N}
function getindex(::IndexCellValue{T,N}, ::Vararg{<:Integer,N})::T where {T,N}
@abstractmethod
end

Expand Down
4 changes: 2 additions & 2 deletions src/CellwiseValues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ include("Gallery.jl")
include("Kernels.jl")
@reexport using CellwiseValues.Kernels

#include("NumberOperations.jl")
#@reexport using CellwiseValues.NumberOperations
include("NumberOperations.jl")
@reexport using CellwiseValues.NumberOperations

end # module
44 changes: 43 additions & 1 deletion src/NumberOperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ function apply(k::NumberKernel,v::Vararg{<:CellValue})
CellNumberFromKernel(k,v...)
end

function apply(k::NumberKernel,v::Vararg{<:IndexCellValue})
IndexCellNumberFromKernel(k,v...)
end

function apply(f::Function,v::Vararg{<:CellValue})
t = _eltypes(v)
T = Base._return_type(f,t)
_apply(T,f,v)
end

function apply(f::Function,v::Vararg{<:IndexCellValue})
t = _eltypes(v)
T = Base._return_type(f,t)
_apply(T,f,v)
end

function _apply(T,f,v)
@notimplemented
end
Expand Down Expand Up @@ -59,7 +69,6 @@ function _compute_type(k,v)
T
end


function length(self::CellNumberFromKernel)
vi, = self.cellvalues
length(vi)
Expand All @@ -85,4 +94,37 @@ end
(r, state)
end

struct IndexCellNumberFromKernel{T,K,V} <: IndexCellNumber{T,1}
kernel::K
cellvalues::V
end

function IndexCellNumberFromKernel(k::NumberKernel,v::Vararg{<:IndexCellValue})
_checks(v)
T = _compute_type(k,v)
K = typeof(k)
V = typeof(v)
IndexCellNumberFromKernel{T,K,V}(k,v)
end

function length(self::IndexCellNumberFromKernel)
vi, = self.cellvalues
length(vi)
end

size(self::IndexCellNumberFromKernel) = (length(self),)

function getindex(self::IndexCellNumberFromKernel,i::Integer)
vals = _getvalues(i,self.cellvalues...)
compute_value(self.kernel,vals...)
end

# TODO use a generated function?
_getvalues(i,v1) = (v1[i],)
_getvalues(i,v1,v2) = (v1[i],v2[i])
_getvalues(i,v1,v2,v3) = (v1[i],v2[i],v3[i])
_getvalues(i,v1,v2,v3,v4) = (v1[i],v2[i],v3[i],v4[i])
_getvalues(i,v1,v2,v3,v4,v5) = (v1[i],v2[i],v3[i],v4[i],v5[i])
_getvalues(i,v1,v2,v3,v4,v5,v6) = (v1[i],v2[i],v3[i],v4[i],v5[i],v6[i])

end # module NumberOperations
24 changes: 22 additions & 2 deletions test/NumberOperationsTests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
include("../src/NumberOperations.jl")
module NumberOperationsTests

using Test
using CellwiseValues
using ..CellValuesMocks
using ..NumberOperations
using StaticArrays
using TensorValues

Expand Down Expand Up @@ -35,4 +33,26 @@ for a in [[1,2,3], SVector(1.3,2.0), VectorValue(1.3,2.0)]
test_iter_cell_value(w,o)
end

for a in ax
v = TestIndexCellValue(a,l)
w = apply(-,v)
o = [ -vi for vi in v ]
test_index_cell_value(w,o)
end

for (a,b) in zip(ax,bx)
u = TestIndexCellValue(a,l)
v = TestIndexCellValue(b,l)
w = apply(-,u,v)
o = [ ui-vi for (ui,vi) in zip(u,v) ]
test_index_cell_value(w,o)
end

for a in [[1,2,3], SVector(1.3,2.0), VectorValue(1.3,2.0)]
v = TestIndexCellValue(a,l)
w = apply(sum,v)
o = [ sum(vi) for vi in v ]
test_index_cell_value(w,o)
end

end # module NumberOperationsTests

0 comments on commit f628cda

Please sign in to comment.