From 58c59a15382a85c3ea9979c272adb485d71aaca7 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Tue, 10 Nov 2026 14:47:10 +0100 Subject: [PATCH 1/9] Lower allocations in LQ/QR pushforwards --- src/MatrixAlgebraKit.jl | 1 + src/common/pushforwards.jl | 13 ++++++++++ src/pushforwards/lq.jl | 51 ++++++++++++++++++++++++++------------ src/pushforwards/qr.jl | 50 ++++++++++++++++++++++++++----------- 4 files changed, 85 insertions(+), 30 deletions(-) create mode 100644 src/common/pushforwards.jl diff --git a/src/MatrixAlgebraKit.jl b/src/MatrixAlgebraKit.jl index 3a9f4b413..42b3f82e2 100644 --- a/src/MatrixAlgebraKit.jl +++ b/src/MatrixAlgebraKit.jl @@ -93,6 +93,7 @@ include("common/defaults.jl") include("common/householder.jl") include("common/initialization.jl") include("common/pullbacks.jl") +include("common/pushforwards.jl") include("common/safemethods.jl") include("common/view.jl") include("common/regularinv.jl") diff --git a/src/common/pushforwards.jl b/src/common/pushforwards.jl new file mode 100644 index 000000000..dd7b1c3c6 --- /dev/null +++ b/src/common/pushforwards.jl @@ -0,0 +1,13 @@ +function _diagonal_qr_lq_pushforward!(Δa, q, l_or_r, Δq, Δl_or_r, p::Int) + Base.mightalias(Δa, Δl_or_r) && (Δa = copy(Δa)) + Δa₁ = view(Δa, 1:p) + q₁ = view(q, 1:p) + l_or_r₁ = view(l_or_r, 1:p) + Δq₁ = view(Δq, 1:p) + Δl_or_r₁ = view(Δl_or_r, 1:p) + @. Δl_or_r₁ = real(conj(q₁) * Δa₁) + @. Δq₁ = (Δa₁ - q₁ * Δl_or_r₁) / l_or_r₁ + zero!(view(Δq, (p + 1):length(Δq))) + zero!(view(Δl_or_r, (p + 1):length(Δl_or_r))) + return Δq, Δl_or_r +end diff --git a/src/pushforwards/lq.jl b/src/pushforwards/lq.jl index bed3261a2..4854d6689 100644 --- a/src/pushforwards/lq.jl +++ b/src/pushforwards/lq.jl @@ -13,6 +13,9 @@ remaining rows of `Q` and columns of `L` are set to zero. Similarly, for `lq_ful rows of `Q` are only determined up to a unitary rotation, and only their gauge-invariant tangent component along the first `r` rows of `Q` is computed. +The tangent `ΔQ` may be the same array as `ΔA` (as happens for an in-place `lq_compact!` +with `Q === A`), in which case `ΔA` is overwritten. + See also [`qr_pushforward!`](@ref). """ function lq_pushforward!( @@ -30,34 +33,35 @@ function lq_pushforward!( p = lq_rank(L; rank_atol) (m, n) == size(ΔA) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of L*Q ($m, $n)")) + if Base.mightalias(ΔA, ΔL) || (ΔQ !== ΔA && Base.mightalias(ΔA, ΔQ)) + ΔA = copy(ΔA) + end + Q₁ = view(Q, 1:p, :) # Julia 1.13 `ldiv!` checks `istriu` on the parent, which # falls back to scalar indexing for a view of a GPU array L₁₁ = LowerTriangular(L[1:p, 1:p]) L₂₁ = view(L, (p + 1):m, 1:p) - ΔA₁ = view(ΔA, 1:p, :) - ΔA₂ = view(ΔA, (p + 1):m, :) + ΔQ₁ = view(ΔQ, 1:p, :) + ΔL₁₁ = view(ΔL, 1:p, 1:p) + ΔL₂₁ = view(ΔL, (p + 1):m, 1:p) + + zero!(ΔL) + mul!(ΔL₂₁, view(ΔA, (p + 1):m, :), Q₁') + + ΔQ === ΔA || copyto!(ΔQ₁, view(ΔA, 1:p, :)) + ldiv!(L₁₁, ΔQ₁) - # compute everything from ΔA before writing to ΔL and ΔQ, which may alias it - # (e.g. `lq_compact!` of a `Diagonal` returns `Q === A`) - # TODO rework this into two versions, one for `Q === A` and one for - # `Q !== A`, to minimize allocations - ΔQ₁ = L₁₁ \ ΔA₁ ΔQ₁Q₁ᴴ = ΔQ₁ * Q₁' M = ΔQ₁Q₁ᴴ + ΔQ₁Q₁ᴴ' diagview(M) ./= 2 view(M, uppertriangularind(M)) .= zero(eltype(M)) - ΔL₁₁ = L₁₁ * M - ΔQ₁ = mul!(ΔQ₁, M, Q₁, -1, 1) - ΔL₂₁ = ΔA₂ * Q₁' - ΔL₂₁ = mul!(ΔL₂₁, L₂₁, ΔQ₁ * Q₁', -1, 1) + mul!(ΔL₁₁, L₁₁, M) + mul!(ΔQ₁, M, Q₁, -1, 1) + mul!(ΔL₂₁, L₂₁, mul!(ΔQ₁Q₁ᴴ, ΔQ₁, Q₁'), -1, 1) - zero!(ΔL) - zero!(ΔQ) - view(ΔQ, 1:p, :) .= ΔQ₁ - view(ΔL, 1:p, 1:p) .= ΔL₁₁ - view(ΔL, (p + 1):m, 1:p) .= ΔL₂₁ + zero!(view(ΔQ, (p + 1):size(ΔQ, 1), :)) if p == minmn && size(Q, 1) > minmn Q₃ = view(Q, (minmn + 1):size(Q, 1), :) ΔQ₃ = view(ΔQ, (minmn + 1):size(Q, 1), :) @@ -66,6 +70,21 @@ function lq_pushforward!( return ΔL, ΔQ end +function lq_pushforward!( + ΔA::Diagonal, A, LQ::Tuple{Diagonal, Diagonal}, ΔLQ::Tuple{Diagonal, Diagonal}; + rank_atol::Real = default_pullback_rank_atol(LQ[1]), kwargs... + ) + L, Q = LQ + isempty(Q) && !isempty(L) && + throw(ArgumentError("LQ differentiation with an omitted Q output is not supported; compute both L and Q.")) + ΔL, ΔQ = ΔLQ + size(ΔA) == size(Q) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of L*Q $(size(Q))")) + p = lq_rank(L; rank_atol) + _diagonal_qr_lq_pushforward!(diagview(ΔA), diagview(Q), diagview(L), diagview(ΔQ), diagview(ΔL), p) + return ΔL, ΔQ +end + + """ lq_null_pushforward!(ΔA, A, Nᴴ, ΔNᴴ; kwargs...) diff --git a/src/pushforwards/qr.jl b/src/pushforwards/qr.jl index 7c84ba0c2..68eb8a1bb 100644 --- a/src/pushforwards/qr.jl +++ b/src/pushforwards/qr.jl @@ -12,6 +12,8 @@ columns of `Q` and the first `r` rows of `R` are differentiable, and the tangent remaining columns of `Q` and rows of `R` are set to zero. Similarly, for `qr_full` the extra columns of `Q` are only determined up to a unitary rotation, and only their gauge-invariant tangent component along the first `r` columns of `Q` is computed. + +The tangent `ΔQ` may be the same underlying data as `ΔA`, in which case `ΔA` is overwritten. """ function qr_pushforward!( ΔA, A, QR, ΔQR; @@ -28,30 +30,36 @@ function qr_pushforward!( p = qr_rank(R; rank_atol) (m, n) == size(ΔA) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of Q*R ($m, $n)")) - # TODO rework this into two versions, one for `Q === A` and one for - # `Q !== A`, to minimize allocations + # `ΔQ === ΔA` is handled by ordering the operations so + # that every column of ΔA is used before the same column of ΔQ is overwritten; + # any other overlap between the tangents forces a copy of ΔA. + if Base.mightalias(ΔA, ΔR) || (ΔQ !== ΔA && Base.mightalias(ΔA, ΔQ)) + ΔA = copy(ΔA) + end + Q₁ = view(Q, :, 1:p) R₁₁ = UpperTriangular(view(R, 1:p, 1:p)) R₁₂ = view(R, 1:p, (p + 1):n) - ΔA₁ = view(ΔA, :, 1:p) - ΔA₂ = view(ΔA, :, (p + 1):n) + ΔQ₁ = view(ΔQ, :, 1:p) + ΔR₁₁ = view(ΔR, 1:p, 1:p) + ΔR₁₂ = view(ΔR, 1:p, (p + 1):n) + + zero!(ΔR) + mul!(ΔR₁₂, Q₁', view(ΔA, :, (p + 1):n)) + + ΔQ === ΔA || copyto!(ΔQ₁, view(ΔA, :, 1:p)) + rdiv!(ΔQ₁, R₁₁) - ΔQ₁ = ΔA₁ / R₁₁ Q₁ᴴΔQ₁ = Q₁' * ΔQ₁ M = Q₁ᴴΔQ₁ + Q₁ᴴΔQ₁' diagview(M) ./= 2 view(M, lowertriangularind(M)) .= zero(eltype(M)) - ΔR₁₁ = M * R₁₁ - ΔQ₁ = mul!(ΔQ₁, Q₁, M, -1, 1) - ΔR₁₂ = Q₁' * ΔA₂ - ΔR₁₂ = mul!(ΔR₁₂, Q₁' * ΔQ₁, R₁₂, -1, 1) + mul!(ΔR₁₁, M, R₁₁) + mul!(ΔQ₁, Q₁, M, -1, 1) + mul!(ΔR₁₂, mul!(Q₁ᴴΔQ₁, Q₁', ΔQ₁), R₁₂, -1, 1) - zero!(ΔQ) - zero!(ΔR) - view(ΔQ, :, 1:p) .= ΔQ₁ - view(ΔR, 1:p, 1:p) .= ΔR₁₁ - view(ΔR, 1:p, (p + 1):n) .= ΔR₁₂ + zero!(view(ΔQ, :, (p + 1):size(ΔQ, 2))) if p == minmn && size(Q, 2) > minmn # extra columns in the case of qr_full, orthogonality to Q₁ fixes their component along Q₁ Q₃ = view(Q, :, (minmn + 1):size(Q, 2)) @@ -61,6 +69,20 @@ function qr_pushforward!( return ΔQ, ΔR end +function qr_pushforward!( + ΔA::Diagonal, A, QR::Tuple{Diagonal, Diagonal}, ΔQR::Tuple{Diagonal, Diagonal}; + rank_atol::Real = default_pullback_rank_atol(QR[2]), kwargs... + ) + Q, R = QR + isempty(Q) && !isempty(R) && + throw(ArgumentError("QR differentiation with an omitted Q output is not supported; compute both Q and R.")) + ΔQ, ΔR = ΔQR + size(ΔA) == size(Q) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of Q*R $(size(Q))")) + p = qr_rank(R; rank_atol) + _diagonal_qr_lq_pushforward!(diagview(ΔA), diagview(Q), diagview(R), diagview(ΔQ), diagview(ΔR), p) + return ΔQ, ΔR +end + """ qr_null_pushforward!(ΔA, A, N, ΔN; kwargs...) From 35dc66bf90374c584e6e23f075c8bb0ce1d351ce Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Fri, 13 Nov 2026 07:03:00 +0100 Subject: [PATCH 2/9] Try in-place project hermitian --- src/pushforwards/qr.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pushforwards/qr.jl b/src/pushforwards/qr.jl index 68eb8a1bb..b4efd6f9a 100644 --- a/src/pushforwards/qr.jl +++ b/src/pushforwards/qr.jl @@ -52,8 +52,7 @@ function qr_pushforward!( rdiv!(ΔQ₁, R₁₁) Q₁ᴴΔQ₁ = Q₁' * ΔQ₁ - M = Q₁ᴴΔQ₁ + Q₁ᴴΔQ₁' - diagview(M) ./= 2 + M = project_hermitian!(Q₁ᴴΔQ₁) view(M, lowertriangularind(M)) .= zero(eltype(M)) mul!(ΔR₁₁, M, R₁₁) mul!(ΔQ₁, Q₁, M, -1, 1) From 9b20838d671d45f0cdbc4dcfe6d5f8b007b36b10 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Sun, 15 Nov 2026 01:52:03 -0500 Subject: [PATCH 3/9] Fix project_hermitian --- src/pushforwards/lq.jl | 2 +- src/pushforwards/qr.jl | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pushforwards/lq.jl b/src/pushforwards/lq.jl index 4854d6689..ca521610f 100644 --- a/src/pushforwards/lq.jl +++ b/src/pushforwards/lq.jl @@ -54,7 +54,7 @@ function lq_pushforward!( ldiv!(L₁₁, ΔQ₁) ΔQ₁Q₁ᴴ = ΔQ₁ * Q₁' - M = ΔQ₁Q₁ᴴ + ΔQ₁Q₁ᴴ' + M = rmul!(project_hermitian!(ΔQ₁Q₁ᴴ), 2) diagview(M) ./= 2 view(M, uppertriangularind(M)) .= zero(eltype(M)) mul!(ΔL₁₁, L₁₁, M) diff --git a/src/pushforwards/qr.jl b/src/pushforwards/qr.jl index b4efd6f9a..4c44e0915 100644 --- a/src/pushforwards/qr.jl +++ b/src/pushforwards/qr.jl @@ -52,7 +52,8 @@ function qr_pushforward!( rdiv!(ΔQ₁, R₁₁) Q₁ᴴΔQ₁ = Q₁' * ΔQ₁ - M = project_hermitian!(Q₁ᴴΔQ₁) + M = rmul!(project_hermitian!(Q₁ᴴΔQ₁), 2) + diagview(M) ./= 2 view(M, lowertriangularind(M)) .= zero(eltype(M)) mul!(ΔR₁₁, M, R₁₁) mul!(ΔQ₁, Q₁, M, -1, 1) From e1c6a5b30b0b791ddc38a897e981dfa4857caf63 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Tue, 17 Nov 2026 04:35:07 -0500 Subject: [PATCH 4/9] Drop the Q checks --- src/pushforwards/lq.jl | 2 +- src/pushforwards/qr.jl | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/pushforwards/lq.jl b/src/pushforwards/lq.jl index ca521610f..955d8d68b 100644 --- a/src/pushforwards/lq.jl +++ b/src/pushforwards/lq.jl @@ -33,7 +33,7 @@ function lq_pushforward!( p = lq_rank(L; rank_atol) (m, n) == size(ΔA) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of L*Q ($m, $n)")) - if Base.mightalias(ΔA, ΔL) || (ΔQ !== ΔA && Base.mightalias(ΔA, ΔQ)) + if Base.mightalias(ΔA, ΔL) ΔA = copy(ΔA) end diff --git a/src/pushforwards/qr.jl b/src/pushforwards/qr.jl index 4c44e0915..4fe03827f 100644 --- a/src/pushforwards/qr.jl +++ b/src/pushforwards/qr.jl @@ -30,10 +30,7 @@ function qr_pushforward!( p = qr_rank(R; rank_atol) (m, n) == size(ΔA) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of Q*R ($m, $n)")) - # `ΔQ === ΔA` is handled by ordering the operations so - # that every column of ΔA is used before the same column of ΔQ is overwritten; - # any other overlap between the tangents forces a copy of ΔA. - if Base.mightalias(ΔA, ΔR) || (ΔQ !== ΔA && Base.mightalias(ΔA, ΔQ)) + if Base.mightalias(ΔA, ΔR) ΔA = copy(ΔA) end From 65515609d5148966b5336498eccefdb2ba7b312b Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 19 Nov 2026 13:34:08 +0100 Subject: [PATCH 5/9] restore some views --- src/pushforwards/lq.jl | 6 ++++-- src/pushforwards/qr.jl | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/pushforwards/lq.jl b/src/pushforwards/lq.jl index 955d8d68b..b1420267b 100644 --- a/src/pushforwards/lq.jl +++ b/src/pushforwards/lq.jl @@ -37,6 +37,8 @@ function lq_pushforward!( ΔA = copy(ΔA) end + ΔA₁ = view(ΔA, 1:p, :) + ΔA₂ = view(ΔA, (p + 1):m, :) Q₁ = view(Q, 1:p, :) # Julia 1.13 `ldiv!` checks `istriu` on the parent, which # falls back to scalar indexing for a view of a GPU array @@ -48,9 +50,9 @@ function lq_pushforward!( ΔL₂₁ = view(ΔL, (p + 1):m, 1:p) zero!(ΔL) - mul!(ΔL₂₁, view(ΔA, (p + 1):m, :), Q₁') + mul!(ΔL₂₁, ΔA₂, Q₁') - ΔQ === ΔA || copyto!(ΔQ₁, view(ΔA, 1:p, :)) + ΔQ === ΔA || copyto!(ΔQ₁, ΔA₁) ldiv!(L₁₁, ΔQ₁) ΔQ₁Q₁ᴴ = ΔQ₁ * Q₁' diff --git a/src/pushforwards/qr.jl b/src/pushforwards/qr.jl index 4fe03827f..a8427b55f 100644 --- a/src/pushforwards/qr.jl +++ b/src/pushforwards/qr.jl @@ -34,6 +34,8 @@ function qr_pushforward!( ΔA = copy(ΔA) end + ΔA₁ = view(ΔA, 1:p, :) + ΔA₂ = view(ΔA, :, (p + 1):n) Q₁ = view(Q, :, 1:p) R₁₁ = UpperTriangular(view(R, 1:p, 1:p)) R₁₂ = view(R, 1:p, (p + 1):n) @@ -43,9 +45,9 @@ function qr_pushforward!( ΔR₁₂ = view(ΔR, 1:p, (p + 1):n) zero!(ΔR) - mul!(ΔR₁₂, Q₁', view(ΔA, :, (p + 1):n)) + mul!(ΔR₁₂, Q₁', ΔA₂) - ΔQ === ΔA || copyto!(ΔQ₁, view(ΔA, :, 1:p)) + ΔQ === ΔA || copyto!(ΔQ₁, ΔA₁) rdiv!(ΔQ₁, R₁₁) Q₁ᴴΔQ₁ = Q₁' * ΔQ₁ From faa4b8c44f424809131de29398dde91c61179955 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 19 Nov 2026 14:10:43 +0100 Subject: [PATCH 6/9] Use scale --- src/pushforwards/lq.jl | 5 +++-- src/pushforwards/qr.jl | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/pushforwards/lq.jl b/src/pushforwards/lq.jl index b1420267b..38419b173 100644 --- a/src/pushforwards/lq.jl +++ b/src/pushforwards/lq.jl @@ -56,8 +56,9 @@ function lq_pushforward!( ldiv!(L₁₁, ΔQ₁) ΔQ₁Q₁ᴴ = ΔQ₁ * Q₁' - M = rmul!(project_hermitian!(ΔQ₁Q₁ᴴ), 2) - diagview(M) ./= 2 + M = project_hermitian!(ΔQ₁Q₁ᴴ) + _scale(i,j) = ifelse(i == j, 1, ifelse(i < j, 0, 2)) + M .*= _scale.(1:size(M, 1), transpose(1:size(M, 1))) view(M, uppertriangularind(M)) .= zero(eltype(M)) mul!(ΔL₁₁, L₁₁, M) mul!(ΔQ₁, M, Q₁, -1, 1) diff --git a/src/pushforwards/qr.jl b/src/pushforwards/qr.jl index a8427b55f..5faee5273 100644 --- a/src/pushforwards/qr.jl +++ b/src/pushforwards/qr.jl @@ -34,7 +34,7 @@ function qr_pushforward!( ΔA = copy(ΔA) end - ΔA₁ = view(ΔA, 1:p, :) + ΔA₁ = view(ΔA, :, 1:p) ΔA₂ = view(ΔA, :, (p + 1):n) Q₁ = view(Q, :, 1:p) R₁₁ = UpperTriangular(view(R, 1:p, 1:p)) @@ -51,8 +51,9 @@ function qr_pushforward!( rdiv!(ΔQ₁, R₁₁) Q₁ᴴΔQ₁ = Q₁' * ΔQ₁ - M = rmul!(project_hermitian!(Q₁ᴴΔQ₁), 2) - diagview(M) ./= 2 + M = project_hermitian!(Q₁ᴴΔQ₁) + _scale(i,j) = ifelse(i == j, 1, ifelse(i < j, 0, 2)) + M .*= _scale.(1:size(M, 1), transpose(1:size(M, 1))) view(M, lowertriangularind(M)) .= zero(eltype(M)) mul!(ΔR₁₁, M, R₁₁) mul!(ΔQ₁, Q₁, M, -1, 1) From ee7be65dca2cc2c2a529d81513cd66e2bbbf1b6f Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 19 Nov 2026 14:12:09 +0100 Subject: [PATCH 7/9] Formatter --- src/pushforwards/lq.jl | 2 +- src/pushforwards/qr.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pushforwards/lq.jl b/src/pushforwards/lq.jl index 38419b173..c8f09d613 100644 --- a/src/pushforwards/lq.jl +++ b/src/pushforwards/lq.jl @@ -57,7 +57,7 @@ function lq_pushforward!( ΔQ₁Q₁ᴴ = ΔQ₁ * Q₁' M = project_hermitian!(ΔQ₁Q₁ᴴ) - _scale(i,j) = ifelse(i == j, 1, ifelse(i < j, 0, 2)) + _scale(i, j) = ifelse(i == j, 1, ifelse(i < j, 0, 2)) M .*= _scale.(1:size(M, 1), transpose(1:size(M, 1))) view(M, uppertriangularind(M)) .= zero(eltype(M)) mul!(ΔL₁₁, L₁₁, M) diff --git a/src/pushforwards/qr.jl b/src/pushforwards/qr.jl index 5faee5273..e5dc00a75 100644 --- a/src/pushforwards/qr.jl +++ b/src/pushforwards/qr.jl @@ -52,7 +52,7 @@ function qr_pushforward!( Q₁ᴴΔQ₁ = Q₁' * ΔQ₁ M = project_hermitian!(Q₁ᴴΔQ₁) - _scale(i,j) = ifelse(i == j, 1, ifelse(i < j, 0, 2)) + _scale(i, j) = ifelse(i == j, 1, ifelse(i < j, 0, 2)) M .*= _scale.(1:size(M, 1), transpose(1:size(M, 1))) view(M, lowertriangularind(M)) .= zero(eltype(M)) mul!(ΔR₁₁, M, R₁₁) From e5e90afb86734c9263386937b100ced5c2ef2ef8 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 19 Nov 2026 14:33:31 +0100 Subject: [PATCH 8/9] Fix tests --- src/pushforwards/lq.jl | 1 - src/pushforwards/qr.jl | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/pushforwards/lq.jl b/src/pushforwards/lq.jl index c8f09d613..448d69054 100644 --- a/src/pushforwards/lq.jl +++ b/src/pushforwards/lq.jl @@ -59,7 +59,6 @@ function lq_pushforward!( M = project_hermitian!(ΔQ₁Q₁ᴴ) _scale(i, j) = ifelse(i == j, 1, ifelse(i < j, 0, 2)) M .*= _scale.(1:size(M, 1), transpose(1:size(M, 1))) - view(M, uppertriangularind(M)) .= zero(eltype(M)) mul!(ΔL₁₁, L₁₁, M) mul!(ΔQ₁, M, Q₁, -1, 1) mul!(ΔL₂₁, L₂₁, mul!(ΔQ₁Q₁ᴴ, ΔQ₁, Q₁'), -1, 1) diff --git a/src/pushforwards/qr.jl b/src/pushforwards/qr.jl index e5dc00a75..b138e0b62 100644 --- a/src/pushforwards/qr.jl +++ b/src/pushforwards/qr.jl @@ -52,9 +52,8 @@ function qr_pushforward!( Q₁ᴴΔQ₁ = Q₁' * ΔQ₁ M = project_hermitian!(Q₁ᴴΔQ₁) - _scale(i, j) = ifelse(i == j, 1, ifelse(i < j, 0, 2)) + _scale(i, j) = ifelse(i == j, 1, ifelse(i > j, 0, 2)) M .*= _scale.(1:size(M, 1), transpose(1:size(M, 1))) - view(M, lowertriangularind(M)) .= zero(eltype(M)) mul!(ΔR₁₁, M, R₁₁) mul!(ΔQ₁, Q₁, M, -1, 1) mul!(ΔR₁₂, mul!(Q₁ᴴΔQ₁, Q₁', ΔQ₁), R₁₂, -1, 1) From a732325e982445980f94fdd48558a9359df6c134 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 19 Nov 2026 16:30:07 +0100 Subject: [PATCH 9/9] Comments --- src/pushforwards/lq.jl | 5 ++--- src/pushforwards/qr.jl | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/pushforwards/lq.jl b/src/pushforwards/lq.jl index 448d69054..881ddf942 100644 --- a/src/pushforwards/lq.jl +++ b/src/pushforwards/lq.jl @@ -50,7 +50,6 @@ function lq_pushforward!( ΔL₂₁ = view(ΔL, (p + 1):m, 1:p) zero!(ΔL) - mul!(ΔL₂₁, ΔA₂, Q₁') ΔQ === ΔA || copyto!(ΔQ₁, ΔA₁) ldiv!(L₁₁, ΔQ₁) @@ -58,10 +57,10 @@ function lq_pushforward!( ΔQ₁Q₁ᴴ = ΔQ₁ * Q₁' M = project_hermitian!(ΔQ₁Q₁ᴴ) _scale(i, j) = ifelse(i == j, 1, ifelse(i < j, 0, 2)) - M .*= _scale.(1:size(M, 1), transpose(1:size(M, 1))) + M .*= _scale.(1:p, transpose(1:p)) mul!(ΔL₁₁, L₁₁, M) mul!(ΔQ₁, M, Q₁, -1, 1) - mul!(ΔL₂₁, L₂₁, mul!(ΔQ₁Q₁ᴴ, ΔQ₁, Q₁'), -1, 1) + mul!(ΔL₂₁, ΔA₂ - L₂₁ * ΔQ₁, Q₁', 1, 1) zero!(view(ΔQ, (p + 1):size(ΔQ, 1), :)) if p == minmn && size(Q, 1) > minmn diff --git a/src/pushforwards/qr.jl b/src/pushforwards/qr.jl index b138e0b62..698256306 100644 --- a/src/pushforwards/qr.jl +++ b/src/pushforwards/qr.jl @@ -45,7 +45,6 @@ function qr_pushforward!( ΔR₁₂ = view(ΔR, 1:p, (p + 1):n) zero!(ΔR) - mul!(ΔR₁₂, Q₁', ΔA₂) ΔQ === ΔA || copyto!(ΔQ₁, ΔA₁) rdiv!(ΔQ₁, R₁₁) @@ -53,10 +52,10 @@ function qr_pushforward!( Q₁ᴴΔQ₁ = Q₁' * ΔQ₁ M = project_hermitian!(Q₁ᴴΔQ₁) _scale(i, j) = ifelse(i == j, 1, ifelse(i > j, 0, 2)) - M .*= _scale.(1:size(M, 1), transpose(1:size(M, 1))) + M .*= _scale.(1:p, transpose(1:p)) mul!(ΔR₁₁, M, R₁₁) mul!(ΔQ₁, Q₁, M, -1, 1) - mul!(ΔR₁₂, mul!(Q₁ᴴΔQ₁, Q₁', ΔQ₁), R₁₂, -1, 1) + mul!(ΔR₁₂, Q₁', ΔA₂ - ΔQ₁ * R₁₂, 1, 1) zero!(view(ΔQ, :, (p + 1):size(ΔQ, 2))) if p == minmn && size(Q, 2) > minmn