Skip to content

Commit

Permalink
Defined apply function
Browse files Browse the repository at this point in the history
  • Loading branch information
fverdugo committed Jun 6, 2019
1 parent 4337c43 commit f192030
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/CellwiseValues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ using Reexport
# To be ported to TensorValues
using TensorValues
import Base: sum
import Base: +, -, *, /
sum(a::MultiValue) = sum(a.array)
for op in (:+,:-,:*)
@eval begin
($op)(a::MultiValue,b::Number) = MultiValue($op(a.array,b))
($op)(a::Number,b::MultiValue) = MultiValue($op(a,b.array))
end
end
(/)(a::MultiValue,b::Number) = MultiValue(a.array/b)

include("Helpers.jl")
@reexport using CellwiseValues.Helpers
Expand Down
22 changes: 21 additions & 1 deletion src/NumberOperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ function apply(k::NumberKernel,v::Vararg{<:CellValue})
CellNumberFromKernel(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(T,f,v)
@notimplemented
end

function _apply(T::Type{<:NumberLike},f,v)
k = NumberKernelFromFunction(f)
apply(k,v...)
end

function _eltypes(v)
tuple([ eltype(vi) for vi in v ]...)
end

struct CellNumberFromKernel{T,K,V} <: IterCellNumber{T}
kernel::K
cellvalues::V
Expand All @@ -34,12 +53,13 @@ function _checks(v)
end

function _compute_type(k,v)
t = [ eltype(vi) for vi in v ]
t = _eltypes(v)
T = compute_type(k,t...)
@assert T <: NumberLike
T
end


function length(self::CellNumberFromKernel)
vi, = self.cellvalues
length(vi)
Expand Down
27 changes: 12 additions & 15 deletions test/NumberOperationsTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,29 @@ using TensorValues

l = 10

k = NumberKernelFromFunction(-)

for a in [1.2, SVector(1.3,2.0), VectorValue(1.3,2.0)]
ax = [1.2, SVector(1.3,2.0), VectorValue(1.3,2.0), VectorValue(1.3,2.0)]
bx = [1, SVector(1.3,2.3), VectorValue(1.1,2.2),1]

for a in ax
v = TestIterCellValue(a,l)

w = apply(k,v)

w = apply(-,v)
o = [ -vi for vi in v ]

test_iter_cell_value(w,o)

end

k = NumberKernelFromFunction(sum)
for (a,b) in zip(ax,bx)
u = TestIterCellValue(a,l)
v = TestIterCellValue(b,l)
w = apply(-,u,v)
o = [ ui-vi for (ui,vi) in zip(u,v) ]
test_iter_cell_value(w,o)
end

for a in [[1,2,3], SVector(1.3,2.0), VectorValue(1.3,2.0)]

v = TestIterCellValue(a,l)

w = apply(k,v)

w = apply(sum,v)
o = [ sum(vi) for vi in v ]

test_iter_cell_value(w,o)

end

end # module NumberOperationsTests
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ include("MapsMocks.jl")

@time @testset "Kernels" begin include("KernelsTests.jl") end

@testset "NumberOperations" begin include("NumberOperationsTests.jl") end
@time @testset "NumberOperations" begin include("NumberOperationsTests.jl") end

end # module CellwiseValuesTests

0 comments on commit f192030

Please sign in to comment.