comparison LazyTensors/test/runtests.jl @ 263:b577b5f64530 boundary_conditions

Add lazy elementwise operations for array with scalar
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Wed, 04 Dec 2019 19:02:18 +0100
parents d4cd4882ee9f
children 11010bb74260
comparison
equal deleted inserted replaced
262:f1e90a92ad74 263:b577b5f64530
108 Base.getindex(v::DummyArray{T,D}, I::Vararg{Int,D}) where {T,D} = v.data[I...] 108 Base.getindex(v::DummyArray{T,D}, I::Vararg{Int,D}) where {T,D} = v.data[I...]
109 109
110 # Test lazy operations 110 # Test lazy operations
111 v1 = [1, 2.3, 4] 111 v1 = [1, 2.3, 4]
112 v2 = [1., 2, 3] 112 v2 = [1., 2, 3]
113 r_add = v1 .+ v2 113 s = 3.4
114 r_sub = v1 .- v2 114 r_add_v = v1 .+ v2
115 r_times = v1 .* v2 115 r_sub_v = v1 .- v2
116 r_div = v1 ./ v2 116 r_times_v = v1 .* v2
117 r_div_v = v1 ./ v2
118 r_add_s = v1 .+ s
119 r_sub_s = v1 .- s
120 r_times_s = v1 .* s
121 r_div_s = v1 ./ s
117 @test isa(v1 +̃ v2, LazyArray) 122 @test isa(v1 +̃ v2, LazyArray)
118 @test isa(v1 -̃ v2, LazyArray) 123 @test isa(v1 -̃ v2, LazyArray)
119 @test isa(v1 *̃ v2, LazyArray) 124 @test isa(v1 *̃ v2, LazyArray)
120 @test isa(v1 /̃ v2, LazyArray) 125 @test isa(v1 /̃ v2, LazyArray)
126 @test isa(v1 +̃ s, LazyArray)
127 @test isa(v1 -̃ s, LazyArray)
128 @test isa(v1 *̃ s, LazyArray)
129 @test isa(v1 /̃ s, LazyArray)
130 @test isa(s +̃ v1, LazyArray)
131 @test isa(s -̃ v1, LazyArray)
132 @test isa(s *̃ v1, LazyArray)
133 @test isa(s /̃ v1, LazyArray)
121 for i ∈ eachindex(v1) 134 for i ∈ eachindex(v1)
122 @test (v1 +̃ v2)[i] == r_add[i] 135 @test (v1 +̃ v2)[i] == r_add_v[i]
123 @test (v1 -̃ v2)[i] == r_sub[i] 136 @test (v1 -̃ v2)[i] == r_sub_v[i]
124 @test (v1 *̃ v2)[i] == r_times[i] 137 @test (v1 *̃ v2)[i] == r_times_v[i]
125 @test (v1 /̃ v2)[i] == r_div[i] 138 @test (v1 /̃ v2)[i] == r_div_v[i]
139 @test (v1 +̃ s)[i] == r_add_s[i]
140 @test (v1 -̃ s)[i] == r_sub_s[i]
141 @test (v1 *̃ s)[i] == r_times_s[i]
142 @test (v1 /̃ s)[i] == r_div_s[i]
143 @test (s +̃ v1)[i] == r_add_s[i]
144 @test (s -̃ v1)[i] == -r_sub_s[i]
145 @test (s *̃ v1)[i] == r_times_s[i]
146 @test (s /̃ v1)[i] == 1/r_div_s[i]
126 end 147 end
127 @test_throws BoundsError (v1 +̃ v2)[4] 148 @test_throws BoundsError (v1 +̃ v2)[4]
128 v2 = [1., 2, 3, 4] 149 v2 = [1., 2, 3, 4]
129 # Test that size of arrays is asserted when not specified inbounds 150 # Test that size of arrays is asserted when not specified inbounds
130 @test_throws DimensionMismatch v1 +̃ v2 151 @test_throws DimensionMismatch v1 +̃ v2
135 @test isa(v1 + v2, LazyArray) 156 @test isa(v1 + v2, LazyArray)
136 @test isa(v2 + v1, LazyArray) 157 @test isa(v2 + v1, LazyArray)
137 @test isa(v1 - v2, LazyArray) 158 @test isa(v1 - v2, LazyArray)
138 @test isa(v2 - v1, LazyArray) 159 @test isa(v2 - v1, LazyArray)
139 for i ∈ eachindex(v2) 160 for i ∈ eachindex(v2)
140 @test (v1 + v2)[i] == (v2 + v1)[i] == r_add[i] 161 @test (v1 + v2)[i] == (v2 + v1)[i] == r_add_v[i]
141 @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub[i] 162 @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub_v[i]
142 end 163 end
143 @test_throws BoundsError (v1 + v2)[4] 164 @test_throws BoundsError (v1 + v2)[4]
144 v2 = [1., 2, 3, 4] 165 v2 = [1., 2, 3, 4]
145 # Test that size of arrays is asserted when not specified inbounds 166 # Test that size of arrays is asserted when not specified inbounds
146 @test_throws DimensionMismatch v1 + v2 167 @test_throws DimensionMismatch v1 + v2