Skip to content

Commit

Permalink
Replace A[ct]_(mul|ldiv|rdiv)_B[ct][!] defs in base/linalg/rowvector.…
Browse files Browse the repository at this point in the history
…jl with de-jazzed passthroughs.
  • Loading branch information
Sacha0 committed Dec 7, 2017
1 parent 3a506bc commit e3fc8e8
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 47 deletions.
43 changes: 43 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2769,6 +2769,49 @@ end
A_mul_B!(y::StridedVector{Ty}, A::StridedMatrix, x::AbstractSparseVector{Tx}) where {Tx,Ty} = mul!(y, A, x)
end

# A[ct]_(mul|ldiv|rdiv)_B[ct][!] methods from base/linalg/rowvector.jl, to deprecate
@eval Base.LinAlg begin
A_rdiv_Bt(rowvec::RowVector, mat::AbstractMatrix) = /(rowvec, Transpose(mat))
A_rdiv_Bc(rowvec::RowVector, mat::AbstractMatrix) = /(rowvec, Adjoint(mat))
At_ldiv_B(mat::AbstractMatrix, rowvec::RowVector) = \(Transpose(mat), rowvec)
Ac_ldiv_B(mat::AbstractMatrix, rowvec::RowVector) = \(Adjoint(mat), rowvec)
Ac_mul_B(u::RowVector, v::AbstractVector) = *(Adjoint(u), v)
Ac_mul_B(vec::AbstractVector, mat::AbstractMatrix) = *(Adjoint(vec), mat)
Ac_mul_B(rowvec1::RowVector, rowvec2::RowVector) = *(Adjoint(rowvec1), rowvec2)
Ac_mul_B(vec::AbstractVector, rowvec::RowVector) = *(Adjoint(vec), rowvec)
Ac_mul_B(vec1::AbstractVector, vec2::AbstractVector) = *(Adjoint(vec1), vec2)
Ac_mul_Bc(rowvec::RowVector, vec::AbstractVector) = *(Adjoint(rowvec), Adjoint(vec))
Ac_mul_Bc(vec::AbstractVector, mat::AbstractMatrix) = *(Adjoint(vec), Adjoint(mat))
Ac_mul_Bc(rowvec1::RowVector, rowvec2::RowVector) = *(Adjoint(rowvec1), Adjoint(rowvec2))
Ac_mul_Bc(vec::AbstractVector, rowvec::RowVector) = *(Adjoint(vec), Adjoint(rowvec))
Ac_mul_Bc(vec::AbstractVector, rowvec::AbstractVector) = *(Adjoint(vec), Adjoint(rowvec))
Ac_mul_Bc(mat::AbstractMatrix, rowvec::RowVector) = *(Adjoint(mat), Adjoint(rowvec))
A_mul_Bc(u::RowVector, v::AbstractVector) = *(u, Adjoint(v))
A_mul_Bc(rowvec::RowVector, mat::AbstractMatrix) = *(rowvec, Adjoint(mat))
A_mul_Bc(rowvec1::RowVector, rowvec2::RowVector) = *(rowvec1, Adjoint(rowvec2))
A_mul_Bc(vec::AbstractVector, rowvec::RowVector) = *(vec, Adjoint(rowvec))
A_mul_Bc(vec1::AbstractVector, vec2::AbstractVector) = *(vec1, Adjoint(vec2))
A_mul_Bc(mat::AbstractMatrix, rowvec::RowVector) = *(mat, Adjoint(rowvec))
At_mul_B(v::RowVector, u::AbstractVector) = *(Transpose(v), u)
At_mul_B(vec::AbstractVector, mat::AbstractMatrix) = *(Transpose(vec), mat)
At_mul_B(rowvec1::RowVector, rowvec2::RowVector) = *(Transpose(rowvec1), rowvec2)
At_mul_B(vec::AbstractVector, rowvec::RowVector) = *(Transpose(vec), rowvec)
At_mul_B(vec1::AbstractVector{T}, vec2::AbstractVector{T}) where {T<:Real} = *(Transpose(vec1), vec2)
At_mul_B(vec1::AbstractVector, vec2::AbstractVector) = *(Transpose(vec1), vec2)
At_mul_Bt(rowvec::RowVector, vec::AbstractVector) = *(Transpose(rowvec), Transpose(vec))
At_mul_Bt(vec::AbstractVector, mat::AbstractMatrix) = *(Transpose(vec), Transpose(mat))
At_mul_Bt(rowvec1::RowVector, rowvec2::RowVector) = *(Transpose(rowvec1), Transpose(rowvec2))
At_mul_Bt(vec::AbstractVector, rowvec::RowVector) = *(Transpose(vec), Transpose(rowvec))
At_mul_Bt(vec::AbstractVector, rowvec::AbstractVector) = *(Transpose(vec), Transpose(rowvec))
At_mul_Bt(mat::AbstractMatrix, rowvec::RowVector) = *(Transpose(mat), Transpose(rowvec))
A_mul_Bt(v::RowVector, A::AbstractVector) = *(v, Transpose(A))
A_mul_Bt(rowvec::RowVector, mat::AbstractMatrix) = *(rowvec, Transpose(mat))
A_mul_Bt(rowvec1::RowVector, rowvec2::RowVector) = *(rowvec1, Transpose(rowvec2))
A_mul_Bt(vec::AbstractVector, rowvec::RowVector) = *(vec, Transpose(rowvec))
A_mul_Bt(vec1::AbstractVector, vec2::AbstractVector) = *(vec1, Transpose(vec2))
A_mul_Bt(mat::AbstractMatrix, rowvec::RowVector) = *(mat, Transpose(rowvec))
end

# issue #24822
@deprecate_binding Display AbstractDisplay

Expand Down
121 changes: 75 additions & 46 deletions base/linalg/rowvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,50 +208,77 @@ end
*(vec::AbstractVector, rowvec::AbstractVector) = throw(DimensionMismatch("Cannot multiply two vectors"))

# Transposed forms
A_mul_Bt(::RowVector, ::AbstractVector) = throw(DimensionMismatch("Cannot multiply two transposed vectors"))
@inline A_mul_Bt(rowvec::RowVector, mat::AbstractMatrix) = transpose(mat * transpose(rowvec))
@inline A_mul_Bt(rowvec1::RowVector, rowvec2::RowVector) = rowvec1*transpose(rowvec2)
A_mul_Bt(vec::AbstractVector, rowvec::RowVector) = throw(DimensionMismatch("Cannot multiply two vectors"))
@inline A_mul_Bt(vec1::AbstractVector, vec2::AbstractVector) = vec1 * transpose(vec2)
@inline A_mul_Bt(mat::AbstractMatrix, rowvec::RowVector) = mat * transpose(rowvec)

@inline At_mul_Bt(rowvec::RowVector, vec::AbstractVector) = transpose(rowvec) * transpose(vec)
@inline At_mul_Bt(vec::AbstractVector, mat::AbstractMatrix) = transpose(mat * vec)
At_mul_Bt(rowvec1::RowVector, rowvec2::RowVector) = throw(DimensionMismatch("Cannot multiply two vectors"))
@inline At_mul_Bt(vec::AbstractVector, rowvec::RowVector) = transpose(vec)*transpose(rowvec)
At_mul_Bt(vec::AbstractVector, rowvec::AbstractVector) = throw(DimensionMismatch(
"Cannot multiply two transposed vectors"))
@inline At_mul_Bt(mat::AbstractMatrix, rowvec::RowVector) = mat.' * transpose(rowvec)

At_mul_B(::RowVector, ::AbstractVector) = throw(DimensionMismatch("Cannot multiply two vectors"))
@inline At_mul_B(vec::AbstractVector, mat::AbstractMatrix) = transpose(At_mul_B(mat,vec))
@inline At_mul_B(rowvec1::RowVector, rowvec2::RowVector) = transpose(rowvec1) * rowvec2
At_mul_B(vec::AbstractVector, rowvec::RowVector) = throw(DimensionMismatch(
"Cannot multiply two transposed vectors"))
@inline At_mul_B(vec1::AbstractVector{T}, vec2::AbstractVector{T}) where {T<:Real} =
reduce(+, map(At_mul_B, vec1, vec2)) # Seems to be overloaded...
@inline At_mul_B(vec1::AbstractVector, vec2::AbstractVector) = transpose(vec1) * vec2
*(::RowVector, ::Transpose{<:Any,<:AbstractVector}) =
throw(DimensionMismatch("Cannot multiply two transposed vectors"))
*(rowvec::RowVector, transmat::Transpose{<:Any,<:AbstractMatrix}) =
(mat = transmat.parent; transpose(mat * transpose(rowvec)))
*(rowvec1::RowVector, transrowvec2::Transpose{<:Any,<:RowVector}) =
(rowvec2 = transrowvec2.parent; rowvec1*transpose(rowvec2))
*(::AbstractVector, ::Transpose{<:Any,<:RowVector}) =
throw(DimensionMismatch("Cannot multiply two vectors"))
*(vec1::AbstractVector, transvec2::Transpose{<:Any,<:AbstractVector}) =
(vec2 = transvec2.parent; vec1 * transpose(vec2))
*(mat::AbstractMatrix, transrowvec::Transpose{<:Any,<:RowVector}) =
(rowvec = transrowvec.parent; mat * transpose(rowvec))

*(transrowvec::Transpose{<:Any,<:RowVector}, transvec::Transpose{<:Any,<:AbstractVector}) =
transpose(transrowvec.parent) * transpose(transvec.parent)
*(transvec::Transpose{<:Any,<:AbstractVector}, transmat::Transpose{<:Any,<:AbstractMatrix}) =
transpose(transmat.parent * transvec.parent)
*(transrowvec1::Transpose{<:Any,<:RowVector}, transrowvec2::Transpose{<:Any,<:RowVector}) =
throw(DimensionMismatch("Cannot multiply two vectors"))
*(transvec::Transpose{<:Any,<:AbstractVector}, transrowvec::Transpose{<:Any,<:RowVector}) =
transpose(transvec.parent)*transpose(transrowvec.parent)
*(transvec::Transpose{<:Any,<:AbstractVector}, transrowvec::Transpose{<:Any,<:AbstractVector}) =
throw(DimensionMismatch("Cannot multiply two transposed vectors"))
*(transmat::Transpose{<:Any,<:AbstractMatrix}, transrowvec::Transpose{<:Any,<:RowVector}) =
(transmat.parent).' * transpose(transrowvec.parent)

*(::Transpose{<:Any,<:RowVector}, ::AbstractVector) =
throw(DimensionMismatch("Cannot multiply two vectors"))
*(transvec::Transpose{<:Any,<:AbstractVector}, mat::AbstractMatrix) =
transpose(At_mul_B(mat,transvec.parent))
*(transrowvec1::Transpose{<:Any,<:RowVector}, rowvec2::RowVector) =
transpose(transrowvec1.parent) * rowvec2
*(transvec::Transpose{<:Any,<:AbstractVector}, rowvec::RowVector) =
throw(DimensionMismatch("Cannot multiply two transposed vectors"))
*(transvec1::Transpose{<:Any,<:AbstractVector{T}}, vec2::AbstractVector{T}) where {T<:Real} =
reduce(+, map(At_mul_B, transvec1.parent, vec2)) # Seems to be overloaded...
*(transvec1::Transpose{<:Any,<:AbstractVector}, vec2::AbstractVector) =
transpose(transvec1.parent) * vec2

# Conjugated forms
A_mul_Bc(::RowVector, ::AbstractVector) = throw(DimensionMismatch("Cannot multiply two transposed vectors"))
@inline A_mul_Bc(rowvec::RowVector, mat::AbstractMatrix) = adjoint(mat * adjoint(rowvec))
@inline A_mul_Bc(rowvec1::RowVector, rowvec2::RowVector) = rowvec1 * adjoint(rowvec2)
A_mul_Bc(vec::AbstractVector, rowvec::RowVector) = throw(DimensionMismatch("Cannot multiply two vectors"))
@inline A_mul_Bc(vec1::AbstractVector, vec2::AbstractVector) = vec1 * adjoint(vec2)
@inline A_mul_Bc(mat::AbstractMatrix, rowvec::RowVector) = mat * adjoint(rowvec)

@inline Ac_mul_Bc(rowvec::RowVector, vec::AbstractVector) = adjoint(rowvec) * adjoint(vec)
@inline Ac_mul_Bc(vec::AbstractVector, mat::AbstractMatrix) = adjoint(mat * vec)
Ac_mul_Bc(rowvec1::RowVector, rowvec2::RowVector) = throw(DimensionMismatch("Cannot multiply two vectors"))
@inline Ac_mul_Bc(vec::AbstractVector, rowvec::RowVector) = adjoint(vec)*adjoint(rowvec)
Ac_mul_Bc(vec::AbstractVector, rowvec::AbstractVector) = throw(DimensionMismatch("Cannot multiply two transposed vectors"))
@inline Ac_mul_Bc(mat::AbstractMatrix, rowvec::RowVector) = mat' * adjoint(rowvec)

Ac_mul_B(::RowVector, ::AbstractVector) = throw(DimensionMismatch("Cannot multiply two vectors"))
@inline Ac_mul_B(vec::AbstractVector, mat::AbstractMatrix) = adjoint(Ac_mul_B(mat,vec))
@inline Ac_mul_B(rowvec1::RowVector, rowvec2::RowVector) = adjoint(rowvec1) * rowvec2
Ac_mul_B(vec::AbstractVector, rowvec::RowVector) = throw(DimensionMismatch("Cannot multiply two transposed vectors"))
@inline Ac_mul_B(vec1::AbstractVector, vec2::AbstractVector) = adjoint(vec1)*vec2
*(::RowVector, ::Adjoint{<:Any,<:AbstractVector}) =
throw(DimensionMismatch("Cannot multiply two transposed vectors"))
*(rowvec::RowVector, adjmat::Adjoint{<:Any,<:AbstractMatrix}) =
adjoint(adjmat.parent * adjoint(rowvec))
*(rowvec1::RowVector, adjrowvec2::Adjoint{<:Any,<:RowVector}) =
rowvec1 * adjoint(adjrowvec2.parent)
*(vec::AbstractVector, adjrowvec::Adjoint{<:Any,<:RowVector}) =
throw(DimensionMismatch("Cannot multiply two vectors"))
*(vec1::AbstractVector, adjvec2::Adjoint{<:Any,<:AbstractVector}) =
vec1 * adjoint(adjvec2.parent)
*(mat::AbstractMatrix, adjrowvec::Adjoint{<:Any,<:RowVector}) =
mat * adjoint(adjrowvec.parent)

*(adjrowvec::Adjoint{<:Any,<:RowVector}, adjvec::Adjoint{<:Any,<:AbstractVector}) =
adjoint(adjrowvec.parent) * adjoint(adjvec.parent)
*(adjvec::Adjoint{<:Any,<:AbstractVector}, adjmat::Adjoint{<:Any,<:AbstractMatrix}) =
adjoint(adjmat.parent * adjvec.parent)
*(adjrowvec1::Adjoint{<:Any,<:RowVector}, adjrowvec2::Adjoint{<:Any,<:RowVector}) =
throw(DimensionMismatch("Cannot multiply two vectors"))
*(adjvec::Adjoint{<:Any,<:AbstractVector}, adjrowvec::Adjoint{<:Any,<:RowVector}) =
adjoint(adjvec.parent)*adjoint(adjrowvec.parent)
*(adjvec::Adjoint{<:Any,<:AbstractVector}, adjrowvec::Adjoint{<:Any,<:AbstractVector}) =
throw(DimensionMismatch("Cannot multiply two transposed vectors"))
*(adjmat::Adjoint{<:Any,<:AbstractMatrix}, adjrowvec::Adjoint{<:Any,<:RowVector}) =
(adjmat.parent)' * adjoint(adjrowvec.parent)

*(::Adjoint{<:Any,<:RowVector}, ::AbstractVector) = throw(DimensionMismatch("Cannot multiply two vectors"))
*(adjvec::Adjoint{<:Any,<:AbstractVector}, mat::AbstractMatrix) = adjoint(Ac_mul_B(mat,adjvec.parent))
*(adjrowvec1::Adjoint{<:Any,<:RowVector}, rowvec2::RowVector) = adjoint(adjrowvec1.parent) * rowvec2
*(adjvec::Adjoint{<:Any,<:AbstractVector}, rowvec::RowVector) = throw(DimensionMismatch("Cannot multiply two transposed vectors"))
*(adjvec1::Adjoint{<:Any,<:AbstractVector}, vec2::AbstractVector) = adjoint(adjvec1.parent)*vec2

# Pseudo-inverse
pinv(v::RowVector, tol::Real=0) = pinv(v', tol)'
Expand All @@ -260,11 +287,13 @@ pinv(v::RowVector, tol::Real=0) = pinv(v', tol)'

\(rowvec1::RowVector, rowvec2::RowVector) = pinv(rowvec1) * rowvec2
\(mat::AbstractMatrix, rowvec::RowVector) = throw(DimensionMismatch("Cannot left-divide transposed vector by matrix"))
At_ldiv_B(mat::AbstractMatrix, rowvec::RowVector) = throw(DimensionMismatch("Cannot left-divide transposed vector by matrix"))
Ac_ldiv_B(mat::AbstractMatrix, rowvec::RowVector) = throw(DimensionMismatch("Cannot left-divide transposed vector by matrix"))
\(transmat::Transpose{<:Any,<:AbstractMatrix}, rowvec::RowVector) =
throw(DimensionMismatch("Cannot left-divide transposed vector by matrix"))
\(adjmat::Adjoint{<:Any,<:AbstractMatrix}, rowvec::RowVector) =
throw(DimensionMismatch("Cannot left-divide transposed vector by matrix"))

# Right Division #

@inline /(rowvec::RowVector, mat::AbstractMatrix) = transpose(transpose(mat) \ transpose(rowvec))
@inline A_rdiv_Bt(rowvec::RowVector, mat::AbstractMatrix) = transpose(mat \ transpose(rowvec))
@inline A_rdiv_Bc(rowvec::RowVector, mat::AbstractMatrix) = adjoint(mat \ adjoint(rowvec))
/(rowvec::RowVector, transmat::Transpose{<:Any,<:AbstractMatrix}) = transpose(transmat.parent \ transpose(rowvec))
/(rowvec::RowVector, adjmat::Adjoint{<:Any,<:AbstractMatrix}) = adjoint(adjmat.parent \ adjoint(rowvec))
8 changes: 7 additions & 1 deletion base/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,12 @@ function *(A::AbstractMatrix, transB::Transpose{<:Any,<:AbstractTriangular})
copy!(AA, A)
A_mul_Bt!(AA, convert(AbstractArray{TAB}, B))
end
# ambiguity resolution with definitions in linalg/rowvector.jl
*(transA::Transpose{<:Any,<:AbstractVector}, B::AbstractTriangular) = *(transpose(transA.parent), B)
*(adjA::Adjoint{<:Any,<:AbstractVector}, B::AbstractTriangular) = *(adjoint(adjA.parent), B)
*(transA::Transpose{<:Any,<:AbstractVector}, transB::Transpose{<:Any,<:AbstractTriangular}) = *(transpose(transA.parent), transB)
*(adjA::Adjoint{<:Any,<:AbstractVector}, adjB::Adjoint{<:Any,<:AbstractTriangular}) = *(adjoint(adjA.parent), adjB)


# If these are not defined, they will fallback to the versions in matmul.jl
# and dispatch to generic_matmatmul! which is very costly to compile. The methods
Expand All @@ -1844,7 +1850,7 @@ end
*(transA::Transpose{<:Any,<:AbstractMatrix}, transB::Transpose{<:Any,<:AbstractTriangular}) = A_mul_Bt(transA.parent.', transB.parent)

# Specializations for RowVector
@inline *(rowvec::RowVector, A::AbstractTriangular) = transpose(A * transpose(rowvec))
*(rowvec::RowVector, A::AbstractTriangular) = transpose(transpose(A) * transpose(rowvec))
*(rowvec::RowVector, transA::Transpose{<:Any,<:AbstractTriangular}) = transpose(transA.parent * transpose(rowvec))
*(A::AbstractTriangular, transrowvec::Transpose{<:Any,<:RowVector}) = A * transpose(transrowvec.parent)
*(transA::Transpose{<:Any,<:AbstractTriangular}, transrowvec::Transpose{<:Any,<:RowVector}) = transA.parent.' * transpose(transrowvec.parent)
Expand Down

0 comments on commit e3fc8e8

Please sign in to comment.