Skip to content

Commit

Permalink
add * mul ambiguities (#336)
Browse files Browse the repository at this point in the history
* add * mul ambiguities

* Update padded.jl
  • Loading branch information
dlfivefifty authored Jul 9, 2024
1 parent a7bd760 commit 8ee041b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "LazyArrays"
uuid = "5078a376-72f3-5289-bfd5-ec5146d43c02"
version = "2.1.5"
version = "2.1.6"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
36 changes: 19 additions & 17 deletions src/linalg/add.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,26 @@ _broadcasted_mul(A::AbstractMatrix, B::Tuple{AbstractMatrix,Vararg{Any}}) = (siz

for op in (:+, :-)
@eval begin
simplifiable(M::Mul{<:BroadcastLayout{typeof($op)}}) = Val(true)
simplifiable(M::Mul{<:Any,<:BroadcastLayout{typeof($op)}}) = Val(true)
simplifiable(M::Mul{<:BroadcastLayout{typeof($op)},<:BroadcastLayout{typeof($op)}}) = simplifiable(Mul{BroadcastLayout{typeof($op)},UnknownLayout}(M.A, M.B))
copy(M::Mul{Lay}) where Lay<:BroadcastLayout{typeof($op)} = broadcast($op, _broadcasted_mul(arguments(Lay(), M.A), M.B)...)
copy(M::Mul{Lay,<:LazyLayouts}) where Lay<:BroadcastLayout{typeof($op)} = broadcast($op, _broadcasted_mul(arguments(Lay(), M.A), M.B)...)
copy(M::Mul{<:Any,Lay}) where Lay<:BroadcastLayout{typeof($op)} = broadcast($op, _broadcasted_mul(M.A, arguments(Lay(), M.B))...)
copy(M::Mul{<:LazyLayouts,Lay}) where Lay<:BroadcastLayout{typeof($op)} = broadcast($op, _broadcasted_mul(M.A, arguments(Lay(), M.B))...)
copy(M::Mul{Lay,Lay}) where Lay<:BroadcastLayout{typeof($op)} = copy(Mul{Lay,UnknownLayout}(M.A, M.B))
simplify(M::Mul{Lay}) where Lay<:BroadcastLayout{typeof($op)} = copy(Mul{Lay,UnknownLayout}(M.A, M.B)) # TODO: remove, here for back-compat with QuasiArrays.jl
simplify(M::Mul{<:Any,Lay}) where Lay<:BroadcastLayout{typeof($op)} = copy(Mul{UnknownLayout,Lay}(M.A, M.B)) # TODO: remove, here for back-compat with QuasiArrays.jl
simplify(M::Mul{Lay,Lay}) where Lay<:BroadcastLayout{typeof($op)} = copy(M) # TODO: remove, here for back-compat with QuasiArrays.jl
simplifiable(M::Mul{BroadcastLayout{typeof($op)}}) = Val(true)
simplifiable(M::Mul{<:Any,BroadcastLayout{typeof($op)}}) = Val(true)
simplifiable(M::Mul{BroadcastLayout{typeof($op)},BroadcastLayout{typeof($op)}}) = simplifiable(Mul{BroadcastLayout{typeof($op)},UnknownLayout}(M.A, M.B))
copy(M::Mul{BroadcastLayout{typeof($op)}}) = broadcast($op, _broadcasted_mul(arguments(BroadcastLayout{typeof($op)}(), M.A), M.B)...)
copy(M::Mul{BroadcastLayout{typeof($op)},<:LazyLayouts}) = broadcast($op, _broadcasted_mul(arguments(BroadcastLayout{typeof($op)}(), M.A), M.B)...)
copy(M::Mul{<:Any,BroadcastLayout{typeof($op)}}) = broadcast($op, _broadcasted_mul(M.A, arguments(BroadcastLayout{typeof($op)}(), M.B))...)
copy(M::Mul{<:LazyLayouts,BroadcastLayout{typeof($op)}}) = broadcast($op, _broadcasted_mul(M.A, arguments(BroadcastLayout{typeof($op)}(), M.B))...)
copy(M::Mul{BroadcastLayout{typeof($op)},BroadcastLayout{typeof($op)}}) = copy(Mul{BroadcastLayout{typeof($op)},UnknownLayout}(M.A, M.B))
copy(M::Mul{BroadcastLayout{typeof($op)},ApplyLayout{typeof(*)}}) = copy(Mul{BroadcastLayout{typeof($op)},UnknownLayout}(M.A, M.B))
copy(M::Mul{ApplyLayout{typeof(*)},BroadcastLayout{typeof($op)}}) = copy(Mul{UnknownLayout,BroadcastLayout{typeof($op)}}(M.A, M.B))
simplify(M::Mul{BroadcastLayout{typeof($op)}}) = copy(Mul{BroadcastLayout{typeof($op)},UnknownLayout}(M.A, M.B)) # TODO: remove, here for back-compat with QuasiArrays.jl
simplify(M::Mul{<:Any,BroadcastLayout{typeof($op)}}) = copy(Mul{UnknownLayout,BroadcastLayout{typeof($op)}}(M.A, M.B)) # TODO: remove, here for back-compat with QuasiArrays.jl
simplify(M::Mul{BroadcastLayout{typeof($op)},BroadcastLayout{typeof($op)}}) = copy(M) # TODO: remove, here for back-compat with QuasiArrays.jl
end
end

simplifiable(M::Mul{Lay,BroadcastLayout{typeof(-)}}) where Lay<:BroadcastLayout{typeof(+)} = simplifiable(Mul{Lay,UnknownLayout}(M.A, M.B))
simplifiable(M::Mul{Lay,BroadcastLayout{typeof(+)}}) where Lay<:BroadcastLayout{typeof(-)} = simplifiable(Mul{Lay,UnknownLayout}(M.A, M.B))
simplifiable(M::Mul{BroadcastLayout{typeof(+)},BroadcastLayout{typeof(-)}}) = simplifiable(Mul{BroadcastLayout{typeof(+)},UnknownLayout}(M.A, M.B))
simplifiable(M::Mul{BroadcastLayout{typeof(-)},BroadcastLayout{typeof(+)}}) = simplifiable(Mul{BroadcastLayout{typeof(-)},UnknownLayout}(M.A, M.B))

copy(M::Mul{Lay,BroadcastLayout{typeof(-)}}) where Lay<:BroadcastLayout{typeof(+)} = copy(Mul{Lay,UnknownLayout}(M.A, M.B))
copy(M::Mul{Lay,BroadcastLayout{typeof(+)}}) where Lay<:BroadcastLayout{typeof(-)} = copy(Mul{Lay,UnknownLayout}(M.A, M.B))
simplify(M::Mul{Lay,BroadcastLayout{typeof(-)}}) where Lay<:BroadcastLayout{typeof(+)} = copy(Mul{Lay,UnknownLayout}(M.A, M.B)) # TODO: remove, here for back-compat with QuasiArrays.jl
simplify(M::Mul{Lay,BroadcastLayout{typeof(+)}}) where Lay<:BroadcastLayout{typeof(-)} = copy(Mul{Lay,UnknownLayout}(M.A, M.B)) # TODO: remove, here for back-compat with QuasiArrays.jl
copy(M::Mul{BroadcastLayout{typeof(+)},BroadcastLayout{typeof(-)}}) = copy(Mul{BroadcastLayout{typeof(+)},UnknownLayout}(M.A, M.B))
copy(M::Mul{BroadcastLayout{typeof(-)} ,BroadcastLayout{typeof(+)}}) = copy(Mul{BroadcastLayout{typeof(-)} ,UnknownLayout}(M.A, M.B))
simplify(M::Mul{BroadcastLayout{typeof(+)},BroadcastLayout{typeof(-)}}) = copy(Mul{BroadcastLayout{typeof(+)},UnknownLayout}(M.A, M.B)) # TODO: remove, here for back-compat with QuasiArrays.jl
simplify(M::Mul{BroadcastLayout{typeof(-)} ,BroadcastLayout{typeof(+)}}) = copy(Mul{BroadcastLayout{typeof(-)} ,UnknownLayout}(M.A, M.B)) # TODO: remove, here for back-compat with QuasiArrays.jl
8 changes: 4 additions & 4 deletions src/padded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,10 @@ end

for op in (:+, :-)
@eval begin
simplifiable(M::Mul{<:BroadcastLayout{typeof($op)},<:Union{PaddedColumns,PaddedLayout}}) = Val(true)
simplifiable(M::Mul{<:DualLayout{<:PaddedRows},<:BroadcastLayout{typeof($op)}}) = Val(true)
copy(M::Mul{Lay,<:Union{PaddedColumns,PaddedLayout}}) where Lay<:BroadcastLayout{typeof($op)} = copy(Mul{Lay,UnknownLayout}(M.A, M.B))
copy(M::Mul{<:DualLayout{<:PaddedRows},Lay}) where Lay<:BroadcastLayout{typeof($op)} = copy(Mul{UnknownLayout,Lay}(M.A, M.B))
simplifiable(M::Mul{BroadcastLayout{typeof($op)},<:Union{PaddedColumns,PaddedLayout}}) = Val(true)
simplifiable(M::Mul{<:DualLayout{<:PaddedRows},BroadcastLayout{typeof($op)}}) = Val(true)
copy(M::Mul{BroadcastLayout{typeof($op)},<:Union{PaddedColumns,PaddedLayout}}) = copy(Mul{BroadcastLayout{typeof($op)},UnknownLayout}(M.A, M.B))
copy(M::Mul{<:DualLayout{<:PaddedRows},BroadcastLayout{typeof($op)}}) = copy(Mul{UnknownLayout,BroadcastLayout{typeof($op)}}(M.A, M.B))
end
end

Expand Down
13 changes: 13 additions & 0 deletions test/addtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,19 @@ import LazyArrays: Add, AddArray, MulAdd, materialize!, MemoryLayout, ApplyLayou
@test B*D simplify(Mul(B,D)) Matrix(B)*D
@test D*B simplify(Mul(D,B)) D*Matrix(B)
end

@testset "Add * Mul" begin
A = randn(5,5)
B = BroadcastArray(+, A, 2A)
C = BroadcastArray(-, A, 2A)
M = ApplyArray(*, A, A)
@test B*M Matrix(B)*M
@test C*M Matrix(C)*M
@test M*B M*Matrix(B)
@test M*C M*Matrix(C)
@test simplifiable(*,B,M) == simplifiable(*,C,M) == Val(true)
@test simplifiable(*,M,B) == simplifiable(*,M,C) == Val(true)
end
end
end # testset

Expand Down

2 comments on commit 8ee041b

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/110734

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.1.6 -m "<description of version>" 8ee041b18fc30bd723403e7fee08f1bb32129d94
git push origin v2.1.6

Please sign in to comment.