Skip to content

Commit c9583e7

Browse files
authored
Fix initialization of accumulators in _generic_matmatmul! (#27123)
Remove some redundant methods related to multiplication of Adjoint/Transpose Expand multi-line functions to multiple lines instead of using one-line syntax with ;s
1 parent e655148 commit c9583e7

File tree

3 files changed

+182
-112
lines changed

3 files changed

+182
-112
lines changed

stdlib/LinearAlgebra/src/adjtrans.jl

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,16 @@ end
204204
*(u::AdjointAbsVec, v::AdjointAbsVec) = throw(MethodError(*, (u, v)))
205205
*(u::TransposeAbsVec, v::TransposeAbsVec) = throw(MethodError(*, (u, v)))
206206

207-
# Adjoint/Transpose-vector * matrix
208-
*(u::AdjointAbsVec, A::AbstractMatrix) = adjoint(adjoint(A) * u.parent)
209-
*(u::TransposeAbsVec, A::AbstractMatrix) = transpose(transpose(A) * u.parent)
210-
# Adjoint/Transpose-vector * Adjoint/Transpose-matrix
211-
*(u::AdjointAbsVec, A::Adjoint{<:Any,<:AbstractMatrix}) = adjoint(A.parent * u.parent)
212-
*(u::TransposeAbsVec, A::Transpose{<:Any,<:AbstractMatrix}) = transpose(A.parent * u.parent)
213-
207+
# AdjOrTransAbsVec{<:Any,<:AdjOrTransAbsVec} is a lazy conj vectors
208+
# We need to expand the combinations to avoid ambiguities
209+
(*)(u::TransposeAbsVec, v::AdjointAbsVec{<:Any,<:TransposeAbsVec}) =
210+
sum(uu*vv for (uu, vv) in zip(u, v))
211+
(*)(u::AdjointAbsVec, v::AdjointAbsVec{<:Any,<:TransposeAbsVec}) =
212+
sum(uu*vv for (uu, vv) in zip(u, v))
213+
(*)(u::TransposeAbsVec, v::TransposeAbsVec{<:Any,<:AdjointAbsVec}) =
214+
sum(uu*vv for (uu, vv) in zip(u, v))
215+
(*)(u::AdjointAbsVec, v::TransposeAbsVec{<:Any,<:AdjointAbsVec}) =
216+
sum(uu*vv for (uu, vv) in zip(u, v))
214217

215218
## pseudoinversion
216219
pinv(v::AdjointAbsVec, tol::Real = 0) = pinv(v.parent, tol).parent
@@ -226,16 +229,3 @@ pinv(v::TransposeAbsVec, tol::Real = 0) = pinv(conj(v.parent)).parent
226229
/(u::TransposeAbsVec, A::AbstractMatrix) = transpose(transpose(A) \ u.parent)
227230
/(u::AdjointAbsVec, A::Transpose{<:Any,<:AbstractMatrix}) = adjoint(conj(A.parent) \ u.parent) # technically should be adjoint(copy(adjoint(copy(A))) \ u.parent)
228231
/(u::TransposeAbsVec, A::Adjoint{<:Any,<:AbstractMatrix}) = transpose(conj(A.parent) \ u.parent) # technically should be transpose(copy(transpose(copy(A))) \ u.parent)
229-
230-
# dismabiguation methods
231-
*(A::AdjointAbsVec, B::Transpose{<:Any,<:AbstractMatrix}) = A * copy(B)
232-
*(A::TransposeAbsVec, B::Adjoint{<:Any,<:AbstractMatrix}) = A * copy(B)
233-
*(A::Transpose{<:Any,<:AbstractMatrix}, B::Adjoint{<:Any,<:AbstractMatrix}) = copy(A) * B
234-
*(A::Adjoint{<:Any,<:AbstractMatrix}, B::Transpose{<:Any,<:AbstractMatrix}) = A * copy(B)
235-
# Adj/Trans-vector * Trans/Adj-vector, shouldn't exist, here for ambiguity resolution? TODO: test removal
236-
*(A::Adjoint{<:Any,<:AbstractVector}, B::Transpose{<:Any,<:AbstractVector}) = throw(MethodError(*, (A, B)))
237-
*(A::Transpose{<:Any,<:AbstractVector}, B::Adjoint{<:Any,<:AbstractVector}) = throw(MethodError(*, (A, B)))
238-
# Adj/Trans-matrix * Trans/Adj-vector, shouldn't exist, here for ambiguity resolution? TODO: test removal
239-
*(A::Adjoint{<:Any,<:AbstractMatrix}, B::Adjoint{<:Any,<:AbstractVector}) = throw(MethodError(*, (A, B)))
240-
*(A::Adjoint{<:Any,<:AbstractMatrix}, B::Transpose{<:Any,<:AbstractVector}) = throw(MethodError(*, (A, B)))
241-
*(A::Transpose{<:Any,<:AbstractMatrix}, B::Adjoint{<:Any,<:AbstractVector}) = throw(MethodError(*, (A, B)))

0 commit comments

Comments
 (0)