Skip to content

Commit

Permalink
Handle more multiplications with AbstractQs (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarrasch authored Dec 22, 2022
1 parent 679805a commit 449a3e5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ArrayLayouts"
uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
authors = ["Sheehan Olver <solver@mac.com>"]
version = "0.8.17"
version = "0.8.18"

[deps]
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
Expand Down
4 changes: 2 additions & 2 deletions src/ArrayLayouts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ using Base.Broadcast: Broadcasted

import Base.Broadcast: BroadcastStyle, broadcastable, instantiate, materialize, materialize!

using LinearAlgebra: AbstractTriangular, AbstractQ, QRCompactWYQ, QRPackedQ, checksquare,
pinv, tilebufsize, cholcopy,
using LinearAlgebra: AbstractQ, QRCompactWYQ, QRPackedQ, HessenbergQ,
AbstractTriangular, checksquare, pinv, tilebufsize, cholcopy,
norm2, norm1, normInf, normMinusInf,
AdjOrTrans, HermOrSym, RealHermSymComplexHerm, AdjointAbsVec, TransposeAbsVec,
checknonsingular, _apply_ipiv_rows!, ipiv2perm, chkfullrank
Expand Down
23 changes: 15 additions & 8 deletions src/mul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,26 @@ check_mul_axes(A) = nothing
_check_mul_axes(::Number, ::Number) = nothing
_check_mul_axes(::Number, _) = nothing
_check_mul_axes(_, ::Number) = nothing
_check_mul_axes(A, B) = axes(A,2) == axes(B,1) || throw(DimensionMismatch("Second axis of A, $(axes(A,2)), and first axis of B, $(axes(B,1)) must match"))
_check_mul_axes(A, B) = axes(A, 2) == axes(B, 1) || throw(DimensionMismatch("Second axis of A, $(axes(A,2)), and first axis of B, $(axes(B,1)) must match"))
# we need to special case AbstractQ as it allows non-compatiple multiplication
const FlexibleLeftQs = Union{QRCompactWYQ,QRPackedQ,HessenbergQ}
_check_mul_axes(::FlexibleLeftQs, ::Number) = nothing
_check_mul_axes(Q::FlexibleLeftQs, B) =
axes(Q.factors, 1) == axes(B, 1) || axes(Q.factors, 2) == axes(B, 1) ||
throw(DimensionMismatch("First axis of B, $(axes(B,1)) must match either axes of A, $(axes(Q.factors))"))
_check_mul_axes(::Number, ::AdjointQtype{<:Any,<:FlexibleLeftQs}) = nothing
function _check_mul_axes(A, adjQ::AdjointQtype{<:Any,<:FlexibleLeftQs})
Q = parent(adjQ)
axes(A, 2) == axes(Q.factors, 1) || axes(A, 2) == axes(Q.factors, 2) ||
throw(DimensionMismatch("Second axis of A, $(axes(A,2)) must match either axes of B, $(axes(Q.factors))"))
end
_check_mul_axes(Q::FlexibleLeftQs, adjQ::AdjointQtype{<:Any,<:FlexibleLeftQs}) =
invoke(_check_mul_axes, Tuple{Any,Any}, Q, adjQ)
function check_mul_axes(A, B, C...)
_check_mul_axes(A, B)
check_mul_axes(B, C...)
end

# we need to special case AbstractQ as it allows non-compatiple multiplication
function check_mul_axes(A::Union{QRCompactWYQ,QRPackedQ}, B, C...)
axes(A.factors, 1) == axes(B, 1) || axes(A.factors, 2) == axes(B, 1) ||
throw(DimensionMismatch("First axis of B, $(axes(B,1)) must match either axes of A, $(axes(A))"))
check_mul_axes(B, C...)
end

function instantiate(M::Mul)
@boundscheck check_mul_axes(M.A, M.B)
M
Expand Down
18 changes: 18 additions & 0 deletions test/test_muladd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,12 @@ Random.seed!(0)
Q = qr(randn(5,5)).Q
b = randn(5)
B = randn(5,5)
@test Q*1.0 == ArrayLayouts.lmul!(Q, Matrix{Float64}(I, 5, 5))
@test Q*b == ArrayLayouts.lmul!(Q, copy(b)) == mul(Q,b)
@test Q*B == ArrayLayouts.lmul!(Q, copy(B)) == mul(Q,B)
@test B*Q == ArrayLayouts.rmul!(copy(B), Q) == mul(B,Q)
@test 1.0*Q ArrayLayouts.rmul!(Matrix{Float64}(I, 5, 5), Q)
@test 1.0*Q' ArrayLayouts.rmul!(Matrix{Float64}(I, 5, 5), Q')
@test Q*Q mul(Q,Q)
@test Q'*b == ArrayLayouts.lmul!(Q', copy(b)) == mul(Q',b)
@test Q'*B == ArrayLayouts.lmul!(Q', copy(B)) == mul(Q',B)
Expand All @@ -627,6 +630,21 @@ Random.seed!(0)
@test Q'*Q mul(Q',Q)
@test Q*UpperTriangular(B) mul(Q, UpperTriangular(B))
@test UpperTriangular(B)*Q mul(UpperTriangular(B), Q)

Q = qr(randn(7,5)).Q
b = randn(5)
B = randn(5,5)
@test Q*1.0 == ArrayLayouts.lmul!(Q, Matrix{Float64}(I, 7, 7))
@test Q*b == mul(Q,b)
@test Q*B == mul(Q,B)
@test 1.0*Q ArrayLayouts.rmul!(Matrix{Float64}(I, 7, 7), Q)
@test Q*Q mul(Q,Q)
@test B*Q' == mul(B,Q')
@test Q*Q' mul(Q,Q')
@test Q'*Q' mul(Q',Q')
@test Q'*Q mul(Q',Q)
VERSION >= v"1.8-" && @test Q*UpperTriangular(B) mul(Q, UpperTriangular(B))
@test UpperTriangular(B)*Q' mul(UpperTriangular(B), Q')
end

@testset "Mul" begin
Expand Down

2 comments on commit 449a3e5

@dkarrasch
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/74521

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 v0.8.18 -m "<description of version>" 449a3e5966691feb67456a7d919620cac911f75b
git push origin v0.8.18

Please sign in to comment.