@@ -1100,17 +1100,26 @@ Adapt.adapt_storage(::ArrayConverter, xs::AbstractArray) = convert(Array, xs)
1100
1100
@test t. b. d isa Array
1101
1101
end
1102
1102
1103
- struct MyArray{T,N} <: AbstractArray{T,N}
1104
- A:: Array{T,N}
1103
+ for S in (1 , 2 , 3 )
1104
+ MyArray = Symbol (:MyArray , S)
1105
+ @eval begin
1106
+ struct $ MyArray{T,N} <: AbstractArray{T,N}
1107
+ A:: Array{T,N}
1108
+ end
1109
+ $ MyArray {T} (:: UndefInitializer , sz:: Dims ) where T = $ MyArray (Array {T} (undef, sz))
1110
+ Base. IndexStyle (:: Type{<:$MyArray} ) = IndexLinear ()
1111
+ Base. getindex (A:: $MyArray , i:: Int ) = A. A[i]
1112
+ Base. setindex! (A:: $MyArray , val, i:: Int ) = A. A[i] = val
1113
+ Base. size (A:: $MyArray ) = Base. size (A. A)
1114
+ Base. BroadcastStyle (:: Type{<:$MyArray} ) = Broadcast. ArrayStyle {$MyArray} ()
1115
+ end
1105
1116
end
1106
- MyArray {T} (:: UndefInitializer , sz:: Dims ) where T = MyArray (Array {T} (undef, sz))
1107
- Base. IndexStyle (:: Type{<:MyArray} ) = IndexLinear ()
1108
- Base. getindex (A:: MyArray , i:: Int ) = A. A[i]
1109
- Base. setindex! (A:: MyArray , val, i:: Int ) = A. A[i] = val
1110
- Base. size (A:: MyArray ) = Base. size (A. A)
1111
- Base. BroadcastStyle (:: Type{<:MyArray} ) = Broadcast. ArrayStyle {MyArray} ()
1112
- Base. similar (bc:: Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray}} , :: Type{ElType} ) where ElType =
1113
- MyArray {ElType} (undef, size (bc))
1117
+ Base. similar (bc:: Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray1}} , :: Type{ElType} ) where ElType =
1118
+ MyArray1 {ElType} (undef, size (bc))
1119
+ Base. similar (bc:: Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray2}} , :: Type{ElType} ) where ElType =
1120
+ MyArray2 {ElType} (undef, size (bc))
1121
+ Base. BroadcastStyle (:: Broadcast.ArrayStyle{MyArray1} , :: Broadcast.ArrayStyle{MyArray3} ) = Broadcast. ArrayStyle {MyArray1} ()
1122
+ Base. BroadcastStyle (:: Broadcast.ArrayStyle{MyArray2} , S:: Broadcast.DefaultArrayStyle ) = S
1114
1123
1115
1124
@testset " broadcast" begin
1116
1125
s = StructArray {ComplexF64} ((rand (2 ,2 ), rand (2 ,2 )))
@@ -1128,19 +1137,34 @@ Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray}}, ::Type{El
1128
1137
# used inside of broadcast but we also test it here explicitly
1129
1138
@test isa (@inferred (Base. dataids (s)), NTuple{N, UInt} where {N})
1130
1139
1131
- s = StructArray {ComplexF64} ((MyArray (rand (2 )), MyArray (rand (2 ))))
1132
- @test_throws MethodError s .+ s
1140
+ # Make sure we can handle style with similar defined
1141
+ # And we can handle most conflict
1142
+ # s1 and s2 has similar defined, but s3 not
1143
+ # s2 are conflict with s1 and s3. (And it's weaker than DefaultArrayStyle)
1144
+ s1 = StructArray {ComplexF64} ((MyArray1 (rand (2 )), MyArray1 (rand (2 ))))
1145
+ s2 = StructArray {ComplexF64} ((MyArray2 (rand (2 )), MyArray2 (rand (2 ))))
1146
+ s3 = StructArray {ComplexF64} ((MyArray3 (rand (2 )), MyArray3 (rand (2 ))))
1147
+ s4 = StructArray {ComplexF64} ((rand (2 ), rand (2 )))
1148
+
1149
+ function _test_similar (a, b, c)
1150
+ try
1151
+ d = StructArray {ComplexF64} ((a. re .+ b. re .- c. re, a. im .+ b. im .- c. im))
1152
+ @test typeof (a .+ b .- c) == typeof (d)
1153
+ catch
1154
+ @test_throws MethodError a .+ b .- c
1155
+ end
1156
+ end
1157
+ for s in (s1,s2,s3,s4), s′ in (s1,s2,s3,s4), s″ in (s1,s2,s3,s4)
1158
+ _test_similar (s, s′, s″)
1159
+ end
1133
1160
1134
1161
# test for dimensionality track
1162
+ s = s1
1135
1163
@test Base. broadcasted (+ , s, s) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{1} }
1136
1164
@test Base. broadcasted (+ , s, 1 : 2 ) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{1} }
1137
1165
@test Base. broadcasted (+ , s, reshape (1 : 2 ,1 ,2 )) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{2} }
1138
1166
@test Base. broadcasted (+ , reshape (1 : 2 ,1 ,1 ,2 ), s) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{3} }
1139
-
1140
- a = StructArray ([1 ;2 + im])
1141
- b = StructArray ([1 ;;2 + im])
1142
- @test a .+ b == a .+ collect (b) == collect (a) .+ b == collect (a) .+ collect (b)
1143
- @test a .+ Any[1 ] isa StructArray
1167
+ @test Base. broadcasted (+ , s, MyArray1 (rand (2 ))) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{Any} }
1144
1168
1145
1169
# issue #185
1146
1170
A = StructArray (randn (ComplexF64, 3 , 3 ))
@@ -1155,6 +1179,23 @@ Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray}}, ::Type{El
1155
1179
1156
1180
@test identity .(StructArray (x= StructArray (a= 1 : 3 ))):: StructArray == [(x= (a= 1 ,),), (x= (a= 2 ,),), (x= (a= 3 ,),)]
1157
1181
@test (x -> x. x. a). (StructArray (x= StructArray (a= 1 : 3 ))) == [1 , 2 , 3 ]
1182
+
1183
+ @testset " ambiguity check" begin
1184
+ function _test (a, b, c)
1185
+ if a isa StructArray || b isa StructArray || c isa StructArray
1186
+ d = @inferred a .+ b .- c
1187
+ @test d == collect (a) .+ collect (b) .- collect (c)
1188
+ @test d isa StructArray
1189
+ end
1190
+ end
1191
+ testset = Any[StructArray ([1 ;2 + im]),
1192
+ 1 : 2 ,
1193
+ (1 ,2 ),
1194
+ ]
1195
+ for aa in testset, bb in testset, cc in testset
1196
+ _test (aa, bb, cc)
1197
+ end
1198
+ end
1158
1199
end
1159
1200
1160
1201
@testset " map" begin
0 commit comments