Mercurial > repos > public > sbplib_julia
changeset 204:9d9f7b0e514b boundary_conditions
Modify the implementation of elementwise operations to be more direct and add a few tests
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Mon, 24 Jun 2019 14:04:38 +0200 |
parents | b5c9be7f391c |
children | 70e1f3401d82 |
files | LazyTensors/src/lazy_operations.jl LazyTensors/test/runtests.jl |
diffstat | 2 files changed, 40 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/LazyTensors/src/lazy_operations.jl Thu Jun 20 23:28:02 2019 +0200 +++ b/LazyTensors/src/lazy_operations.jl Mon Jun 24 14:04:38 2019 +0200 @@ -98,12 +98,20 @@ @inline /̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b) -Base.:+(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a +̃ b -Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = b + a -Base.:-(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a -̃ b +# NOTE: Är det knas att vi har till exempel * istället för .* ?? +# Oklart om det ens går att lösa.. +Base.:+(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b +Base.:+(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a +̃ b +Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b + +Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b +Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b -Base.:*(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a *̃ b -Base.:*(a::AbstractArray{T,D},b::LazyArray{T,D}) where {T,D} = b * a + +Base.:*(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a *̃ b +Base.:*(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a *̃ b +Base.:*(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a *̃ b + # TODO: / seems to be ambiguous # Base.:/(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a /̃ b # Base.:/(a::AbstractArray{T,D},b::LazyArray{T,D}) where {T,D} = a /̃ b
--- a/LazyTensors/test/runtests.jl Thu Jun 20 23:28:02 2019 +0200 +++ b/LazyTensors/test/runtests.jl Mon Jun 24 14:04:38 2019 +0200 @@ -56,3 +56,30 @@ @test_broken BoundsError == (m*m*v)[0] @test_broken BoundsError == (m*m*v)[7] end + + +@testset "Lazy elementwise operations" begin + struct DummyLazyArray{D} <: LazyArray{Int,D} + size::NTuple{D,Int} + end + Base.size(a::DummyLazyArray) = a.size + Base.getindex(a::DummyLazyArray, I...) = prod(I) + + + a = DummyLazyArray((3,)) + + @test (a+a)[3] == 6 + @test (a-a)[3] == 0 + @test (a*a)[3] == 9 + + a = DummyLazyArray((4,)) + b = [3,2,1,2] + + @test (a+b)[4] == 6 + @test (a-b)[4] == 2 + @test (a*b)[4] == 8 + + @test (b+a)[4] == 6 + @test (b-a)[4] == -2 + @test (b*a)[4] == 8 +end \ No newline at end of file