changeset 1123:185911ca45f0

Merging feature/lazy_arrays
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Wed, 05 Oct 2022 20:46:10 +0200
parents 5f701e795d44 (current diff) 6e9d6cb5211c (diff)
children 2d231546c091 560ff49ff17f
files
diffstat 2 files changed, 20 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/LazyTensors/lazy_array.jl	Tue Oct 04 22:28:08 2022 +0200
+++ b/src/LazyTensors/lazy_array.jl	Wed Oct 05 20:46:10 2022 +0200
@@ -93,17 +93,22 @@
 
 
 
-# NOTE: Är det knas att vi har till exempel * istället för .* ??
-# Oklart om det ens går att lösa..
+# Overload +,-,*,/ for LazyArrays 
+# Element wise operation for `*` and `/` are not overloaded for due to conflicts with the behavior
+# of regular `*` and `/` for AbstractArrays. Use tilde versions instead.
 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b
+Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b
+
 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a +̃ b
-Base.@propagate_inbounds Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b
+Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b
 
-Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b
-Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b
+Base.@propagate_inbounds Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b
 Base.@propagate_inbounds Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b
 
-# Element wise operation for `*` and `\` are not overloaded due to conflicts with the behavior
-# of regular `*` and `/` for AbstractArrays. Use tilde versions instead.
+Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::T) where {T,D} = a +̃ b
+Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::T) where {T,D} = a -̃ b
+
+Base.@propagate_inbounds Base.:+(a::T, b::LazyArray{T,D}) where {T,D} = a +̃ b
+Base.@propagate_inbounds Base.:-(a::T, b::LazyArray{T,D}) where {T,D} = a -̃  b
 
 export +̃, -̃, *̃, /̃
--- a/test/LazyTensors/lazy_array_test.jl	Tue Oct 04 22:28:08 2022 +0200
+++ b/test/LazyTensors/lazy_array_test.jl	Wed Oct 05 20:46:10 2022 +0200
@@ -67,11 +67,18 @@
     @test isa(v1 + v2, LazyArray)
     @test isa(v2 + v1, LazyArray)
     @test isa(v1 - v2, LazyArray)
-    @test isa(v2 - v1, LazyArray)
+    @test isa(v2 - v1, LazyArray)    
+    @test isa(v1 + s, LazyArray)
+    @test isa(s + v1, LazyArray)
+    @test isa(v1 - s, LazyArray)
+    @test isa(s - v1, LazyArray)
     for i ∈ eachindex(v2)
         @test (v1 + v2)[i] == (v2 + v1)[i] == r_add_v[i]
         @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub_v[i]
+        @test (v1 + s)[i] == (s + v1)[i] == r_add_s[i]
+        @test (v1 - s)[i] == -(s - v1)[i] == r_sub_s[i]
     end
+    
     @test_throws BoundsError (v1 + v2)[4]
     v2 = [1., 2, 3, 4]
     @test_throws DimensionMismatch v1 + v2