From 6dc17d810cba2ec262c919b5d5284d7b889c05ee Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Tue, 6 Aug 2024 16:15:49 +0100 Subject: [PATCH 1/8] Banded * Strided is always simplifiable --- Project.toml | 2 +- ext/LazyArraysBandedMatricesExt.jl | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 9767e53..0b7c64f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "LazyArrays" uuid = "5078a376-72f3-5289-bfd5-ec5146d43c02" -version = "2.1.9" +version = "2.1.10" [deps] ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" diff --git a/ext/LazyArraysBandedMatricesExt.jl b/ext/LazyArraysBandedMatricesExt.jl index dc1ff8b..e596b2c 100644 --- a/ext/LazyArraysBandedMatricesExt.jl +++ b/ext/LazyArraysBandedMatricesExt.jl @@ -567,8 +567,8 @@ copy(M::Mul{<:DiagonalLayout, <:BandedLazyLayouts}) = simplify(M) copy(M::Mul{<:Union{ZerosLayout,DualLayout{ZerosLayout}}, <:BandedLazyLayouts}) = copy(mulreduce(M)) copy(M::Mul{<:BandedLazyLayouts, <:Union{ZerosLayout,DualLayout{ZerosLayout}}}) = copy(mulreduce(M)) -simplifiable(::Mul{<:BandedLazyLayouts, <:DiagonalLayout{<:OnesLayout}}) = Val(true) -simplifiable(::Mul{<:DiagonalLayout{<:OnesLayout}, <:BandedLazyLayouts}) = Val(true) +simplifiable(::Mul{<:BandedLayouts, <:DiagonalLayout{<:OnesLayout}}) = Val(true) +simplifiable(::Mul{<:DiagonalLayout{<:OnesLayout}, <:BandedLayouts}) = Val(true) copy(M::Mul{<:BandedLazyLayouts, <:DiagonalLayout{<:OnesLayout}}) = _copy_oftype(M.A, eltype(M)) copy(M::Mul{<:DiagonalLayout{<:OnesLayout}, <:BandedLazyLayouts}) = _copy_oftype(M.B, eltype(M)) @@ -644,8 +644,8 @@ copy(M::Mul{<:Union{AbstractStridedLayout,DualLayout{<:AbstractStridedLayout}}, simplifiable(M::Mul{<:AbstractInvLayout{<:BandedLayouts}, <:Union{PaddedColumns,PaddedLayout,AbstractStridedLayout}}) = Val(true) simplifiable(M::Mul{<:Union{PaddedRows,PaddedLayout,DualLayout{<:PaddedRows}}, <:BandedLayouts}) = Val(true) simplifiable(M::Mul{<:BandedLayouts, <:Union{PaddedColumns,PaddedLayout}}) = Val(true) -simplifiable(M::Mul{<:Union{AbstractStridedLayout,DualLayout{<:AbstractStridedLayout}}, <:BandedLazyLayouts}) = Val(true) -simplifiable(M::Mul{<:BandedLazyLayouts, <:AbstractStridedLayout}) = Val(true) +simplifiable(M::Mul{<:Union{AbstractStridedLayout,DualLayout{<:AbstractStridedLayout}}, <:BandedLayouts}) = Val(true) +simplifiable(M::Mul{<:BandedLayouts, <:AbstractStridedLayout}) = Val(true) copy(L::Ldiv{ApplyBandedLayout{typeof(*)}, Lay}) where Lay = copy(Ldiv{ApplyLayout{typeof(*)},Lay}(L.A, L.B)) copy(L::Ldiv{ApplyBandedLayout{typeof(*)}, Lay}) where {Lay<:AbstractLazyLayout} = copy(Ldiv{ApplyLayout{typeof(*)},Lay}(L.A, L.B)) From 965556046cd417976acc632b9af896a3feda9565 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Tue, 6 Aug 2024 16:58:58 +0100 Subject: [PATCH 2/8] fix tests --- src/LazyArrays.jl | 2 +- src/linalg/mul.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/LazyArrays.jl b/src/LazyArrays.jl index fb6fe84..008a627 100644 --- a/src/LazyArrays.jl +++ b/src/LazyArrays.jl @@ -38,7 +38,7 @@ import ArrayLayouts: AbstractQLayout, Dot, Dotu, Ldiv, Lmul, MatMulMatAdd, MatMu hermitianlayout, layout_getindex, layout_replace_in_print_matrix, ldivaxes, materialize, materialize!, mulreduce, reshapedlayout, rowsupport, scalarone, scalarzero, sub_materialize, sublayout, symmetriclayout, symtridiagonallayout, transposelayout, triangulardata, - triangularlayout, tridiagonallayout, zero!, transtype + triangularlayout, tridiagonallayout, zero!, transtype, OnesLayout import FillArrays: AbstractFill, getindex_value diff --git a/src/linalg/mul.jl b/src/linalg/mul.jl index 1cb944b..9fb7fc1 100644 --- a/src/linalg/mul.jl +++ b/src/linalg/mul.jl @@ -373,6 +373,7 @@ transtype(A::MulMatrix) = transtype(first(A.args)) #TODO: Why not all DiagonalLayout? @inline simplifiable(M::Mul{<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) @inline simplifiable(M::Mul{<:DiagonalLayout{<:AbstractFillLayout},<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) +@inline simplifiable(::Mul{<:DiagonalLayout{<:OnesLayout}, <:DiagonalLayout{<:OnesLayout}}) = Val(true) # ambiguity @inline simplifiable(M::Mul{<:Any,<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) @inline copy(M::Mul{<:DiagonalLayout{<:AbstractFillLayout},<:LazyLayouts}) = copy(mulreduce(M)) @inline copy(M::Mul{<:LazyLayouts,<:DiagonalLayout{<:AbstractFillLayout}}) = copy(mulreduce(M)) From 8c0d815dc0e9811f2b3dabf7b8ec6fdf9806e93e Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Tue, 6 Aug 2024 20:22:45 +0100 Subject: [PATCH 3/8] Update mul.jl --- src/linalg/mul.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/linalg/mul.jl b/src/linalg/mul.jl index 9fb7fc1..ecfa012 100644 --- a/src/linalg/mul.jl +++ b/src/linalg/mul.jl @@ -375,6 +375,7 @@ transtype(A::MulMatrix) = transtype(first(A.args)) @inline simplifiable(M::Mul{<:DiagonalLayout{<:AbstractFillLayout},<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) @inline simplifiable(::Mul{<:DiagonalLayout{<:OnesLayout}, <:DiagonalLayout{<:OnesLayout}}) = Val(true) # ambiguity @inline simplifiable(M::Mul{<:Any,<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) +@inline simplifiable(M::Mul{<:Union{AbstractStridedLayout,DualLayout{<:AbstractStridedLayout}}, <:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) @inline copy(M::Mul{<:DiagonalLayout{<:AbstractFillLayout},<:LazyLayouts}) = copy(mulreduce(M)) @inline copy(M::Mul{<:LazyLayouts,<:DiagonalLayout{<:AbstractFillLayout}}) = copy(mulreduce(M)) @inline copy(M::Mul{BroadcastLayout{typeof(*)},<:DiagonalLayout{<:AbstractFillLayout}}) = copy(mulreduce(M)) From 46027224df86a84c50df4c97ceeb927245f87cd4 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Tue, 6 Aug 2024 22:50:21 +0100 Subject: [PATCH 4/8] Update mul.jl --- src/linalg/mul.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/linalg/mul.jl b/src/linalg/mul.jl index ecfa012..aa53f62 100644 --- a/src/linalg/mul.jl +++ b/src/linalg/mul.jl @@ -376,6 +376,7 @@ transtype(A::MulMatrix) = transtype(first(A.args)) @inline simplifiable(::Mul{<:DiagonalLayout{<:OnesLayout}, <:DiagonalLayout{<:OnesLayout}}) = Val(true) # ambiguity @inline simplifiable(M::Mul{<:Any,<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) @inline simplifiable(M::Mul{<:Union{AbstractStridedLayout,DualLayout{<:AbstractStridedLayout}}, <:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) +@inline simplifiable(M::Mul{<:DiagonalLayout{<:AbstractFillLayout}, <:AbstractStridedLayout}) = Val(true) @inline copy(M::Mul{<:DiagonalLayout{<:AbstractFillLayout},<:LazyLayouts}) = copy(mulreduce(M)) @inline copy(M::Mul{<:LazyLayouts,<:DiagonalLayout{<:AbstractFillLayout}}) = copy(mulreduce(M)) @inline copy(M::Mul{BroadcastLayout{typeof(*)},<:DiagonalLayout{<:AbstractFillLayout}}) = copy(mulreduce(M)) From 591ed616118b64c0cbd8b4c03be3480192d83916 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Wed, 7 Aug 2024 07:43:26 +0100 Subject: [PATCH 5/8] Update multests.jl --- test/multests.jl | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/test/multests.jl b/test/multests.jl index cddfc3f..eb6e65a 100644 --- a/test/multests.jl +++ b/test/multests.jl @@ -1,4 +1,6 @@ -using Test, LinearAlgebra, LazyArrays, StaticArrays, FillArrays, Base64, Random, BandedMatrices +using Test, LinearAlgebra, LazyArrays, FillArrays, Base64, Random +using BandedMatrices +using StaticArrays import LazyArrays: MulAdd, MemoryLayout, DenseColumnMajor, DiagonalLayout, SymTridiagonalLayout, Add, AddArray, MulStyle, MulAddStyle, Applied, ApplyStyle, Lmul, ApplyArrayBroadcastStyle, DefaultArrayApplyStyle, Rmul, ApplyLayout, arguments, colsupport, rowsupport, lazymaterialize @@ -1176,8 +1178,21 @@ end @testset "simplifiable tests" begin A = randn(5,5) + b = randn(5) + D = Diagonal(Fill(2,5)) + E = Eye(5) @test LazyArrays.simplifiable(*, A) == Val(false) @test LazyArrays.simplify(Applied(*, A, A)) == A*A + @test LazyArrays.simplifiable(*, A, D) == Val(true) + @test LazyArrays.simplifiable(*, D, A) == Val(true) + @test LazyArrays.simplifiable(*, b', D) == Val(true) + @test LazyArrays.simplifiable(*, D, b) == Val(true) + @test LazyArrays.simplifiable(*, A, E) == Val(true) + @test LazyArrays.simplifiable(*, E, A) == Val(true) + @test LazyArrays.simplifiable(*, b', E) == Val(true) + @test LazyArrays.simplifiable(*, E, b) == Val(true) + @test LazyArrays.simplifiable(*, E, D) == Val(true) + @test LazyArrays.simplifiable(*, D, E) == Val(true) end end From 355db67d8f1b46275b4f5c844cc5b8986429b497 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Wed, 7 Aug 2024 07:43:44 +0100 Subject: [PATCH 6/8] v2.2 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 0b7c64f..0a4bd75 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "LazyArrays" uuid = "5078a376-72f3-5289-bfd5-ec5146d43c02" -version = "2.1.10" +version = "2.2" [deps] ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" From 35c188c5a3b4644b1f49a545fd7ae0056b347f56 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Wed, 7 Aug 2024 09:49:12 +0100 Subject: [PATCH 7/8] Update mul.jl --- src/linalg/mul.jl | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/linalg/mul.jl b/src/linalg/mul.jl index aa53f62..39afb86 100644 --- a/src/linalg/mul.jl +++ b/src/linalg/mul.jl @@ -371,21 +371,23 @@ applylayout(::Type{typeof(*)}, ::DualLayout{Lay}, args...) where Lay = DualLayou transtype(A::MulMatrix) = transtype(first(A.args)) #TODO: Why not all DiagonalLayout? -@inline simplifiable(M::Mul{<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) -@inline simplifiable(M::Mul{<:DiagonalLayout{<:AbstractFillLayout},<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) +@inline simplifiable(::Mul{<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) +@inline simplifiable(::Mul{<:DiagonalLayout{<:AbstractFillLayout},<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) +@inline simplifiable(::Mul{<:DiagonalLayout{<:OnesLayout},<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) +@inline simplifiable(::Mul{<:DiagonalLayout{<:AbstractFillLayout},<:DiagonalLayout{<:OnesLayout}}) = Val(true) @inline simplifiable(::Mul{<:DiagonalLayout{<:OnesLayout}, <:DiagonalLayout{<:OnesLayout}}) = Val(true) # ambiguity -@inline simplifiable(M::Mul{<:Any,<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) -@inline simplifiable(M::Mul{<:Union{AbstractStridedLayout,DualLayout{<:AbstractStridedLayout}}, <:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) -@inline simplifiable(M::Mul{<:DiagonalLayout{<:AbstractFillLayout}, <:AbstractStridedLayout}) = Val(true) +@inline simplifiable(::Mul{<:Any,<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) +@inline simplifiable(::Mul{<:Union{AbstractStridedLayout,DualLayout{<:AbstractStridedLayout}}, <:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) +@inline simplifiable(::Mul{<:DiagonalLayout{<:AbstractFillLayout}, <:AbstractStridedLayout}) = Val(true) @inline copy(M::Mul{<:DiagonalLayout{<:AbstractFillLayout},<:LazyLayouts}) = copy(mulreduce(M)) @inline copy(M::Mul{<:LazyLayouts,<:DiagonalLayout{<:AbstractFillLayout}}) = copy(mulreduce(M)) @inline copy(M::Mul{BroadcastLayout{typeof(*)},<:DiagonalLayout{<:AbstractFillLayout}}) = copy(mulreduce(M)) -@inline simplifiable(M::Mul{<:Union{ZerosLayout,DualLayout{ZerosLayout}},<:Union{ZerosLayout,DualLayout{ZerosLayout}}}) = Val(true) -@inline simplifiable(M::Mul{<:Union{ZerosLayout,DualLayout{ZerosLayout}}}) = Val(true) -@inline simplifiable(M::Mul{<:Any,<:Union{ZerosLayout,DualLayout{ZerosLayout}}}) = Val(true) -@inline simplifiable(M::Mul{<:Union{ZerosLayout,DualLayout{ZerosLayout}},<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) -@inline simplifiable(M::Mul{<:DiagonalLayout{<:AbstractFillLayout},<:Union{ZerosLayout,DualLayout{ZerosLayout}}}) = Val(true) +@inline simplifiable(::Mul{<:Union{ZerosLayout,DualLayout{ZerosLayout}},<:Union{ZerosLayout,DualLayout{ZerosLayout}}}) = Val(true) +@inline simplifiable(::Mul{<:Union{ZerosLayout,DualLayout{ZerosLayout}}}) = Val(true) +@inline simplifiable(::Mul{<:Any,<:Union{ZerosLayout,DualLayout{ZerosLayout}}}) = Val(true) +@inline simplifiable(::Mul{<:Union{ZerosLayout,DualLayout{ZerosLayout}},<:DiagonalLayout{<:AbstractFillLayout}}) = Val(true) +@inline simplifiable(::Mul{<:DiagonalLayout{<:AbstractFillLayout},<:Union{ZerosLayout,DualLayout{ZerosLayout}}}) = Val(true) # inv From ed78afcd7ef83f7b5f4ba8177de2a10a2492beaa Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Wed, 7 Aug 2024 11:23:30 +0100 Subject: [PATCH 8/8] Update multests.jl --- test/multests.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/multests.jl b/test/multests.jl index eb6e65a..1735ada 100644 --- a/test/multests.jl +++ b/test/multests.jl @@ -1193,6 +1193,8 @@ end @test LazyArrays.simplifiable(*, E, b) == Val(true) @test LazyArrays.simplifiable(*, E, D) == Val(true) @test LazyArrays.simplifiable(*, D, E) == Val(true) + @test LazyArrays.simplifiable(*, D, D) == Val(true) + @test LazyArrays.simplifiable(*, E, E) == Val(true) end end