Skip to content

Commit 5cfe719

Browse files
authored
Add * overloads coming from v0.9 (#141)
* Add * overloads coming from v0.9 * Rmul with DualLayout and Diagonal * Add zeros mul tests * v1.0.7 * Update diagonal.jl * increase coverage
1 parent fc67490 commit 5cfe719

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

Project.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
name = "ArrayLayouts"
22
uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
33
authors = ["Sheehan Olver <solver@mac.com>"]
4-
version = "1.0.6"
4+
version = "1.0.7"
55

66
[deps]
77
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
88
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
99
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1010

1111
[compat]
12-
FillArrays = "1.0"
12+
FillArrays = "1.2.1"
1313
julia = "1.6"
1414

1515
[extras]

src/ArrayLayouts.jl

+3
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,11 @@ getindex(A::AdjOrTrans{<:Any,<:LayoutVector}, kr::Integer, jr::AbstractVector) =
184184

185185
*(a::Zeros{<:Any,2}, b::LayoutMatrix) = FillArrays.mult_zeros(a, b)
186186
*(a::LayoutMatrix, b::Zeros{<:Any,2}) = FillArrays.mult_zeros(a, b)
187+
*(a::LayoutMatrix, b::Zeros{<:Any,1}) = FillArrays.mult_zeros(a, b)
187188
*(a::Transpose{T, <:LayoutMatrix{T}} where T, b::Zeros{<:Any, 2}) = FillArrays.mult_zeros(a, b)
188189
*(a::Adjoint{T, <:LayoutMatrix{T}} where T, b::Zeros{<:Any, 2}) = FillArrays.mult_zeros(a, b)
190+
*(A::Adjoint{<:Any, <:Zeros{<:Any,1}}, B::Diagonal{<:Any,<:LayoutVector}) = (B' * A')'
191+
*(A::Transpose{<:Any, <:Zeros{<:Any,1}}, B::Diagonal{<:Any,<:LayoutVector}) = transpose(transpose(B) * transpose(A))
189192

190193
*(A::Diagonal{<:Any,<:LayoutVector}, B::Diagonal{<:Any,<:LayoutVector}) = mul(A, B)
191194
*(A::Diagonal{<:Any,<:LayoutVector}, B::AbstractMatrix) = mul(A, B)

src/diagonal.jl

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ copy(M::Lmul{<:DiagonalLayout}) = diagonaldata(M.A) .* M.B
2020
copy(M::Rmul{<:Any,<:DiagonalLayout}) = M.A .* permutedims(diagonaldata(M.B))
2121

2222

23+
dualadjoint(_) = adjoint
24+
dualadjoint(::Transpose) = transpose
25+
dualadjoint(V::SubArray) = dualadjoint(parent(V))
26+
function copy(M::Rmul{<:DualLayout,<:DiagonalLayout})
27+
adj = dualadjoint(M.A)
28+
adj(adj(M.B) * adj(M.A))
29+
end
30+
31+
2332

2433
# Diagonal multiplication never changes structure
2534
similar(M::Rmul{<:Any,<:DiagonalLayout}, ::Type{T}, axes) where T = similar(M.A, T, axes)
@@ -60,10 +69,12 @@ copy(M::Lmul{DiagonalLayout{OnesLayout},<:DiagonalLayout}) = Diagonal(_copy_ofty
6069
copy(M::Lmul{<:DiagonalLayout,DiagonalLayout{OnesLayout}}) = Diagonal(_copy_oftype(diagonaldata(M.A), eltype(M)))
6170
copy(M::Lmul{DiagonalLayout{OnesLayout},DiagonalLayout{OnesLayout}}) = _copy_oftype(M.B, eltype(M))
6271
copy(M::Rmul{<:Any,DiagonalLayout{OnesLayout}}) = _copy_oftype(M.A, eltype(M))
72+
copy(M::Rmul{<:DualLayout,DiagonalLayout{OnesLayout}}) = _copy_oftype(M.A, eltype(M))
6373

6474
copy(M::Lmul{<:DiagonalLayout{<:AbstractFillLayout}}) = getindex_value(diagonaldata(M.A)) * M.B
6575
copy(M::Lmul{<:DiagonalLayout{<:AbstractFillLayout},<:DiagonalLayout}) = getindex_value(diagonaldata(M.A)) * M.B
6676
copy(M::Rmul{<:Any,<:DiagonalLayout{<:AbstractFillLayout}}) = M.A * getindex_value(diagonaldata(M.B))
77+
copy(M::Rmul{<:DualLayout,<:DiagonalLayout{<:AbstractFillLayout}}) = M.A * getindex_value(diagonaldata(M.B))
6778

6879
copy(M::Rmul{<:BidiagonalLayout,<:DiagonalLayout{<:AbstractFillLayout}}) = M.A * getindex_value(diagonaldata(M.B))
6980
copy(M::Lmul{<:DiagonalLayout{<:AbstractFillLayout},<:BidiagonalLayout}) = getindex_value(diagonaldata(M.A)) * M.B

src/mul.jl

+10
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,16 @@ end
300300
*(A::Transpose{<:Any,<:LayoutVector}, B::Adjoint{<:Any,<:LayoutMatrix}) = mul(A,B)
301301
*(A::Transpose{<:Any,<:LayoutVector}, B::Transpose{<:Any,<:LayoutMatrix}) = mul(A,B)
302302

303+
## special routines introduced in v0.9. We need to avoid these to support ∞-arrays
304+
305+
*(x::Adjoint{<:Any,<:LayoutVector}, D::Diagonal{<:Any,<:LayoutVector}) = mul(x, D)
306+
*(x::Transpose{<:Any,<:LayoutVector}, D::Diagonal{<:Any,<:LayoutVector}) = mul(x, D)
307+
*(x::AdjointAbsVec, D::Diagonal, y::LayoutVector) = x * mul(D,y)
308+
*(x::TransposeAbsVec, D::Diagonal, y::LayoutVector) = x * mul(D,y)
309+
*(x::AdjointAbsVec{<:Any,<:Zeros{<:Any,1}}, D::Diagonal, y::LayoutVector) = FillArrays._triple_zeromul(x, D, y)
310+
*(x::TransposeAbsVec{<:Any,<:Zeros{<:Any,1}}, D::Diagonal, y::LayoutVector) = FillArrays._triple_zeromul(x, D, y)
311+
312+
303313

304314
###
305315
# Dot

test/test_layoutarray.jl

+47
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,53 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
395395
@test x'A transpose(x)A x.A'A.A
396396
@test x'A' x'transpose(A) transpose(x)A' transpose(x)transpose(A) x.A'A.A'
397397
end
398+
399+
@testset "Zeros mul" begin
400+
A = MyMatrix(randn(5,5))
401+
@test A*Zeros(5) Zeros(5)
402+
@test A*Zeros(5,2) Zeros(5,2)
403+
@test Zeros(1,5) * A Zeros(1,5)
404+
@test Zeros(5)' * A Zeros(5)'
405+
@test transpose(Zeros(5)) * A transpose(Zeros(5))
406+
407+
@test Zeros(5)' * A * (1:5) ==
408+
transpose(Zeros(5)) * A * (1:5) ==
409+
(1:5)' * A * Zeros(5) ==
410+
transpose(1:5) * A * Zeros(5) ==
411+
Zeros(5)' * A * Zeros(5) ==
412+
transpose(Zeros(5)) * A * Zeros(5) == 0.0
413+
end
414+
415+
@testset "triple *" begin
416+
D = Diagonal(1:5)
417+
y = MyVector(randn(5))
418+
@test (1:5)' * D * y transpose(1:5) * D * y (1:5)' * D * y.A
419+
@test y' * D * y transpose(y) * D * y y.A' * D * y.A
420+
@test y' * D * (1:5) y.A' * D * (1:5)
421+
@test y' * Diagonal(y) isa Adjoint
422+
@test transpose(y) * Diagonal(y) isa Transpose
423+
424+
@test y' * D isa Adjoint
425+
@test transpose(y) * D isa Transpose
426+
427+
@test Zeros(5)' * D * y == transpose(Zeros(5)) * D * y == 0.0
428+
@test y' * D * Zeros(5) == transpose(y) * D * Zeros(5) == 0.0
429+
@test Zeros(5)' * Diagonal(y) Zeros(5)'
430+
@test transpose(Zeros(5)) * Diagonal(y) transpose(Zeros(5))
431+
@test Zeros(5)' * Diagonal(y) * y == 0.0
432+
@test transpose(Zeros(5)) * Diagonal(y) * y == 0.0
433+
@test y' * Diagonal(y) * Zeros(5) == 0.0
434+
@test transpose(y) * Diagonal(y) * Zeros(5) == 0.0
435+
@test Zeros(5)' * Diagonal(y) * Zeros(5) == 0.0
436+
@test transpose(Zeros(5)) * Diagonal(y) * Zeros(5) == 0.0
437+
end
438+
439+
@testset "rmul with lazy and Diagonal" begin
440+
D = Diagonal(1:5)
441+
y = MyVector(randn(5))
442+
@test mul(view(y', :, 1:5), D) isa Adjoint
443+
@test mul(view(transpose(y), :, 1:5), D) isa Transpose
444+
end
398445
end
399446

400447
struct MyUpperTriangular{T} <: AbstractMatrix{T}

test/test_layouts.jl

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ struct FooNumber <: Number end
9898
@test layout_getindex(transpose(a .+ im),:,1:3) == transpose((a .+ im)[1:3])
9999

100100
@test ArrayLayouts._copyto!(similar(a'), a') == a'
101+
102+
@test mul(randn(5)', Diagonal(1:5)) isa Adjoint
103+
@test mul(transpose(randn(5)), Diagonal(1:5)) isa Transpose
101104
end
102105
end
103106

0 commit comments

Comments
 (0)