From 80d4009f9197fdc349d65763534ed012ac1a4229 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Mon, 7 Sep 2020 13:56:47 +0100 Subject: [PATCH 1/5] More overloads for ambiguity --- Project.toml | 2 +- src/ArrayLayouts.jl | 2 ++ src/mul.jl | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 2402283..ac3624d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ArrayLayouts" uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" authors = ["Sheehan Olver "] -version = "0.4.6" +version = "0.4.7" [deps] FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" diff --git a/src/ArrayLayouts.jl b/src/ArrayLayouts.jl index 12b3d59..d569b68 100644 --- a/src/ArrayLayouts.jl +++ b/src/ArrayLayouts.jl @@ -158,6 +158,7 @@ for Typ in (:LayoutArray, :(Transpose{<:Any,<:LayoutMatrix}), :(Adjoint{<:Any,<: end getindex(A::LayoutVector, kr::AbstractVector) = layout_getindex(A, kr) +getindex(A::LayoutVector, kr::Colon) = layout_getindex(A, kr) _copyto!(_, _, dest::AbstractArray{T,N}, src::AbstractArray{V,N}) where {T,V,N} = Base.invoke(copyto!, Tuple{AbstractArray{T,N},AbstractArray{V,N}}, dest, src) @@ -177,6 +178,7 @@ copyto!(dest::AbstractArray{<:Any,N}, src::SubArray{<:Any,N,<:LayoutArray}) wher copyto!(dest::LayoutMatrix, src::AdjOrTrans{<:Any,<:LayoutArray}) = _copyto!(dest, src) copyto!(dest::LayoutMatrix, src::SubArray{<:Any,2,<:AdjOrTrans{<:Any,<:LayoutArray}}) = _copyto!(dest, src) copyto!(dest::AbstractMatrix, src::AdjOrTrans{<:Any,<:LayoutArray}) = _copyto!(dest, src) +copyto!(dest::SubArray{<:Any,2,<:LayoutMatrix}, src::SubArray{<:Any,2,<:AdjOrTrans{<:Any,<:LayoutArray}}) = _copyto!(dest, src) copyto!(dest::AbstractMatrix, src::SubArray{<:Any,2,<:AdjOrTrans{<:Any,<:LayoutArray}}) = _copyto!(dest, src) # ambiguity from sparsematrix.jl if VERSION ≥ v"1.5" diff --git a/src/mul.jl b/src/mul.jl index 999dcff..25e09da 100644 --- a/src/mul.jl +++ b/src/mul.jl @@ -242,6 +242,9 @@ dot(a, b) = materialize(Dot(a, b)) @inline LinearAlgebra.dot(a::LayoutArray, b::LayoutArray) = dot(a,b) @inline LinearAlgebra.dot(a::LayoutArray, b::AbstractArray) = dot(a,b) @inline LinearAlgebra.dot(a::AbstractArray, b::LayoutArray) = dot(a,b) +@inline LinearAlgebra.dot(a::LayoutArray{<:Number}, b::SparseArrays.SparseVectorUnion{<:Number}) = dot(a,b) +@inline LinearAlgebra.dot(a::SparseArrays.SparseVectorUnion{<:Number}, b::LayoutArray{<:Number}) = dot(a,b) + @inline LinearAlgebra.dot(a::SubArray{<:Any,N,<:LayoutArray}, b::AbstractArray) where N = dot(a,b) @inline LinearAlgebra.dot(a::SubArray{<:Any,N,<:LayoutArray}, b::LayoutArray) where N = dot(a,b) @inline LinearAlgebra.dot(a::AbstractArray, b::SubArray{<:Any,N,<:LayoutArray}) where N = dot(a,b) From df6253724aafde7afcd44fe2d9e9f3011b21071b Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Mon, 7 Sep 2020 14:17:41 +0100 Subject: [PATCH 2/5] Fix #40 --- src/mul.jl | 2 ++ test/runtests.jl | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/src/mul.jl b/src/mul.jl index 25e09da..12e9806 100644 --- a/src/mul.jl +++ b/src/mul.jl @@ -197,6 +197,8 @@ macro layoutmul(Typ) Base.:*(A::$Mod{<:Any,<:$Typ}, B::$Mod{<:Any,<:$Typ}) = ArrayLayouts.mul(A,B) Base.:*(A::$Mod{<:Any,<:$Typ}, B::AbstractMatrix) = ArrayLayouts.mul(A,B) Base.:*(A::AbstractMatrix, B::$Mod{<:Any,<:$Typ}) = ArrayLayouts.mul(A,B) + Base.:*(A::LinearAlgebra.AdjointAbsVec, B::$Mod{<:Any,<:$Typ}) = ArrayLayouts.mul(A,B) + Base.:*(A::LinearAlgebra.TransposeAbsVec, B::$Mod{<:Any,<:$Typ}) = ArrayLayouts.mul(A,B) Base.:*(A::$Mod{<:Any,<:$Typ}, B::AbstractVector) = ArrayLayouts.mul(A,B) Base.:*(A::$Mod{<:Any,<:$Typ}, B::ArrayLayouts.LayoutVector) = ArrayLayouts.mul(A,B) diff --git a/test/runtests.jl b/test/runtests.jl index 9a3a80d..be9963e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -136,6 +136,13 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor() b = randn(5) @test dot(b, A, b) ≈ b'*(A*b) ≈ b'A*b end + + @testset "dual vector * symmetric (#40)" begin + A = randn(3,3) + x = rand(3) + @test x' * Symmetric(MyMatrix(A)) ≈ x'Symmetric(A) + @test transpose(x) * Symmetric(MyMatrix(A)) ≈ transpose(x)Symmetric(A) + end end @testset "l/rmul!" begin From 9f50b5aa5a80f673382efa143190590077b33f11 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Mon, 7 Sep 2020 14:36:15 +0100 Subject: [PATCH 3/5] Update runtests.jl --- test/runtests.jl | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index be9963e..264d3fd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -32,22 +32,26 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor() # These need to test dispatch reduces to ArrayLayouts.mul, etc. @testset "LayoutArray" begin @testset "LayoutVector" begin - A = MyVector([1.,2,3]) + a = MyVector([1.,2,3]) B = randn(3,3) b = randn(3) - @test A == A.A == Vector(A) - @test A[1:3] == A.A[1:3] - @test stringmime("text/plain", A) == "3-element MyVector:\n 1.0\n 2.0\n 3.0" - @test B*A ≈ B*A.A - @test B'*A ≈ B'*A.A - @test transpose(B)*A ≈ transpose(B)*A.A - @test b'A ≈ transpose(b)A ≈ A'b ≈ transpose(A)b ≈ b'A.A - @test qr(B).Q*A ≈ qr(B).Q*A.A - - @test A'A == transpose(A)A == dot(A,A) == dot(A,A.A) == dot(A.A,A) == 14 - v = view(A,1:3) - @test dot(v,A) == dot(v,A.A) == dot(A,v) == dot(A.A,v) == dot(v,v) == 14 + @test a == a.A == Vector(a) + @test a[1:3] == a.A[1:3] + @test a[:] == a + @test stringmime("text/plain", a) == "3-element MyVector:\n 1.0\n 2.0\n 3.0" + @test B*a ≈ B*a.A + @test B'*a ≈ B'*a.A + @test transpose(B)*a ≈ transpose(B)*a.A + @test b'a ≈ transpose(b)a ≈ a'b ≈ transpose(a)b ≈ b'a.A + @test qr(B).Q*a ≈ qr(B).Q*a.A + + @test a'a == transpose(a)a == dot(a,a) == dot(a,a.A) == dot(a.A,a) == 14 + v = view(a,1:3) + @test dot(v,a) == dot(v,a.A) == dot(a,v) == dot(a.A,v) == dot(v,v) == 14 + + s = SparseVector(3, [1], [2]) + @test a's == s'a == dot(a,s) == dot(s,a) == dot(s,a.A) end @testset "LayoutMatrix" begin @@ -74,6 +78,8 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor() @test copyto!(Array{Float64}(undef,5,5), A') == A' @test copyto!(Array{Float64}(undef,5,5), view(A',:,:)) == A' + @test copyto!(view(MyMatrix(Array{Float64}(undef,5,5)),:,:), view(A',:,:)) == A' + @test qr(A).factors ≈ qr(A.A).factors @test qr(A,Val(true)).factors ≈ qr(A.A,Val(true)).factors @test lu(A).factors ≈ lu(A.A).factors From 22c3f63bfd0f413e962aa170a6f8ff726e076075 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Mon, 7 Sep 2020 16:11:31 +0100 Subject: [PATCH 4/5] Update runtests.jl --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 264d3fd..2895cb9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using ArrayLayouts, Random, FillArrays, Test, Base64 +using ArrayLayouts, Random, FillArrays, Test, SparseArrays, Base64 import ArrayLayouts: MemoryLayout, @_layoutlmul, triangulardata Random.seed!(0) From 83b93efd6ab1289ef3cf308e24dad2c4efcf37ec Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Mon, 7 Sep 2020 16:34:55 +0100 Subject: [PATCH 5/5] Update ldiv.jl --- src/ldiv.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ldiv.jl b/src/ldiv.jl index 7140cf4..b7d51ba 100644 --- a/src/ldiv.jl +++ b/src/ldiv.jl @@ -50,7 +50,8 @@ getindex(L::Ldiv, k...) = _getindex(indextype(L), L, k) concretize(L::AbstractArray) = convert(Array,L) concretize(L::Ldiv) = ldiv(concretize(L.A), concretize(L.B)) _getindex(::Type{Tuple{I}}, L::Ldiv, (k,)::Tuple{I}) where I = concretize(L)[k] -_getindex(::Type{Tuple{I,J}}, L::Ldiv, (k,j)::Tuple{I,J}) where {I,J} = Ldiv(L.A, L.B[:,j])[k] +_getindex(::Type{Tuple{I,J}}, L::Ldiv, (k,j)::Tuple{Colon,J}) where {I,J} = Ldiv(L.A, L.B[:,j]) +_getindex(::Type{Tuple{I,J}}, L::Ldiv, (k,j)::Tuple{I,J}) where {I,J} = L[:,j][k] check_ldiv_axes(A, B) = axes(A,1) == axes(B,1) || throw(DimensionMismatch("First axis of A, $(axes(A,1)), and first axis of B, $(axes(B,1)) must match"))