Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let muladd accept arrays #37065

Merged
merged 7 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use ndims(number)==0, and add array-of-array tests
  • Loading branch information
Michael Abbott committed Oct 6, 2020
commit 69a3546a5253eb22819ac47df486581d537713d4
14 changes: 4 additions & 10 deletions stdlib/LinearAlgebra/src/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ end
"""
muladd(A, y, z)

Combined multiply-add, `A*y .+ z`, for either matrix-matrix multiplication or
matrix-vector multiplication. The result is always the size of `A*y`, although
`z` may have fewer dimensions.
Combined multiply-add, `A*y .+ z`, for matrix-matrix or matrix-vector multiplication.
The result is always the size of `A*y`, but `z` may be smaller, or a scalar.
mcabbott marked this conversation as resolved.
Show resolved Hide resolved

!!! compat "Julia 1.6"
These methods require Julia 1.6 or later.
Expand Down Expand Up @@ -220,18 +219,13 @@ Base.muladd(x::AdjointAbsVec, A::AbstractMatrix, z) = muladd(A', x', z')'
Base.muladd(x::TransposeAbsVec, A::AbstractMatrix, z) = transpose(muladd(transpose(A), transpose(x), transpose(z)))

function Base.muladd(u::AbstractVector, v::AdjOrTransAbsVec, z)
if z isa AbstractArray
ndims(z) > 2 && throw(DimensionMismatch("cannot broadcast array to have fewer dimensions"))
end
ndims(z) > 2 && throw(DimensionMismatch("cannot broadcast array to have fewer dimensions"))
(u .* v) .+ z
end

function Base.muladd(u::AdjOrTransAbsVec, v::AbstractVector, z)
uv = _dot_nonrecursive(u, v)
if z isa AbstractArray
uv isa AbstractArray && ndims(uv) <= ndims(z) ||
throw(DimensionMismatch("cannot broadcast array to have fewer dimensions"))
end
ndims(z) > ndims(uv) && throw(DimensionMismatch("cannot broadcast array to have fewer dimensions"))
mcabbott marked this conversation as resolved.
Show resolved Hide resolved
uv .+ z
end

Expand Down
9 changes: 9 additions & 0 deletions stdlib/LinearAlgebra/test/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,15 @@ end
@test muladd(u2', u2, 0) isa Number
@test muladd(v3', v3, im) == dot(v3,v3) + im
@test_throws DimensionMismatch muladd(v3', v3, [1])

vofm = [rand(1:9,2,2) for _ in 1:3]
Mofm = [rand(1:9,2,2) for _ in 1:3, _ in 1:3]

@test muladd(vofm', vofm, vofm[1]) == vofm' * vofm .+ vofm[1] # inner
@test muladd(vofm, vofm', Mofm) == vofm * vofm' .+ Mofm # outer
@test muladd(vofm', Mofm, vofm') == vofm' * Mofm .+ vofm' # bra-mat
@test muladd(Mofm, Mofm, vofm) == Mofm * Mofm .+ vofm # mat-mat
@test_broken muladd(Mofm, vofm, vofm) == Mofm * vofm .+ vofm # mat-vec
end

# issue #6450
Expand Down