@@ -899,17 +899,26 @@ Adapt.adapt_storage(::ArrayConverter, xs::AbstractArray) = convert(Array, xs)
899899 @test t. b. d isa Array
900900end
901901
902- struct MyArray{T,N} <: AbstractArray{T,N}
903- A:: Array{T,N}
904- end
905- MyArray {T} (:: UndefInitializer , sz:: Dims ) where T = MyArray (Array {T} (undef, sz))
906- Base. IndexStyle (:: Type{<:MyArray} ) = IndexLinear ()
907- Base. getindex (A:: MyArray , i:: Int ) = A. A[i]
908- Base. setindex! (A:: MyArray , val, i:: Int ) = A. A[i] = val
909- Base. size (A:: MyArray ) = Base. size (A. A)
910- Base. BroadcastStyle (:: Type{<:MyArray} ) = Broadcast. ArrayStyle {MyArray} ()
911- Base. similar (bc:: Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray}} , :: Type{ElType} ) where ElType =
912- MyArray {ElType} (undef, size (bc))
902+ for S in (1 , 2 , 3 )
903+ MyArray = Symbol (:MyArray , S)
904+ @eval begin
905+ struct $ MyArray{T,N} <: AbstractArray{T,N}
906+ A:: Array{T,N}
907+ end
908+ $ MyArray {T} (:: UndefInitializer , sz:: Dims ) where T = $ MyArray (Array {T} (undef, sz))
909+ Base. IndexStyle (:: Type{<:$MyArray} ) = IndexLinear ()
910+ Base. getindex (A:: $MyArray , i:: Int ) = A. A[i]
911+ Base. setindex! (A:: $MyArray , val, i:: Int ) = A. A[i] = val
912+ Base. size (A:: $MyArray ) = Base. size (A. A)
913+ Base. BroadcastStyle (:: Type{<:$MyArray} ) = Broadcast. ArrayStyle {$MyArray} ()
914+ end
915+ end
916+ Base. similar (bc:: Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray1}} , :: Type{ElType} ) where ElType =
917+ MyArray1 {ElType} (undef, size (bc))
918+ Base. similar (bc:: Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray2}} , :: Type{ElType} ) where ElType =
919+ MyArray2 {ElType} (undef, size (bc))
920+ Base. BroadcastStyle (:: Broadcast.ArrayStyle{MyArray1} , :: Broadcast.ArrayStyle{MyArray3} ) = Broadcast. ArrayStyle {MyArray1} ()
921+ Base. BroadcastStyle (:: Broadcast.ArrayStyle{MyArray2} , S:: Broadcast.DefaultArrayStyle ) = S
913922
914923@testset " broadcast" begin
915924 s = StructArray {ComplexF64} ((rand (2 ,2 ), rand (2 ,2 )))
@@ -927,19 +936,34 @@ Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray}}, ::Type{El
927936 # used inside of broadcast but we also test it here explicitly
928937 @test isa (@inferred (Base. dataids (s)), NTuple{N, UInt} where {N})
929938
930- s = StructArray {ComplexF64} ((MyArray (rand (2 )), MyArray (rand (2 ))))
931- @test_throws MethodError s .+ s
939+ # Make sure we can handle style with similar defined
940+ # And we can handle most conflict
941+ # s1 and s2 has similar defined, but s3 not
942+ # s2 are conflict with s1 and s3. (And it's weaker than DefaultArrayStyle)
943+ s1 = StructArray {ComplexF64} ((MyArray1 (rand (2 )), MyArray1 (rand (2 ))))
944+ s2 = StructArray {ComplexF64} ((MyArray2 (rand (2 )), MyArray2 (rand (2 ))))
945+ s3 = StructArray {ComplexF64} ((MyArray3 (rand (2 )), MyArray3 (rand (2 ))))
946+ s4 = StructArray {ComplexF64} ((rand (2 ), rand (2 )))
947+
948+ function _test_similar (a, b, c)
949+ try
950+ d = StructArray {ComplexF64} ((a. re .+ b. re .- c. re, a. im .+ b. im .- c. im))
951+ @test typeof (a .+ b .- c) == typeof (d)
952+ catch
953+ @test_throws MethodError a .+ b .- c
954+ end
955+ end
956+ for s in (s1,s2,s3,s4), s′ in (s1,s2,s3,s4), s″ in (s1,s2,s3,s4)
957+ _test_similar (s, s′, s″)
958+ end
932959
933960 # test for dimensionality track
961+ s = s1
934962 @test Base. broadcasted (+ , s, s) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{1} }
935963 @test Base. broadcasted (+ , s, 1 : 2 ) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{1} }
936964 @test Base. broadcasted (+ , s, reshape (1 : 2 ,1 ,2 )) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{2} }
937965 @test Base. broadcasted (+ , reshape (1 : 2 ,1 ,1 ,2 ), s) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{3} }
938-
939- a = StructArray ([1 ;2 + im])
940- b = StructArray ([1 ;;2 + im])
941- @test a .+ b == a .+ collect (b) == collect (a) .+ b == collect (a) .+ collect (b)
942- @test a .+ Any[1 ] isa StructArray
966+ @test Base. broadcasted (+ , s, MyArray1 (rand (2 ))) isa Broadcast. Broadcasted{<: Broadcast.AbstractArrayStyle{Any} }
943967
944968 # issue #185
945969 A = StructArray (randn (ComplexF64, 3 , 3 ))
@@ -951,6 +975,28 @@ Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray}}, ::Type{El
951975 # issue #189
952976 v = StructArray ([(a= " s1" ,), (a= " s2" ,)])
953977 @test @inferred (broadcast (el -> el. a, v)) == [" s1" , " s2" ]
978+
979+ # ambiguity check (can we do this better?)
980+ function _test (a, b, c)
981+ if a isa StructArray || b isa StructArray || c isa StructArray
982+ d = @inferred a .+ b .- c
983+ @test d == collect (a) .+ collect (b) .- collect (c)
984+ @test d isa StructArray
985+ end
986+ end
987+ testset = (StructArray ([1 ;2 + im]),
988+ StructArray ([1 2 + im]),
989+ 1 : 2 ,
990+ (1 ,2 ),
991+ (@SArray [1 2 ]),
992+ StructArray (@SArray [1 1 + 2im ]))
993+ for aa in testset, bb in testset, cc in testset
994+ _test (aa, bb, cc)
995+ end
996+
997+ a = @SArray randn (3 ,3 );
998+ b = StructArray {ComplexF64} ((a,a))
999+ @test a[:,1 ] .+ b isa StructArray && (a[:,1 ] .+ b). re isa SizedMatrix
9541000end
9551001
9561002@testset " staticarrays" begin
0 commit comments