Skip to content

Commit

Permalink
Add mul!!(C, A, B, α, β)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Aug 31, 2019
1 parent f2f208c commit 4fd4993
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/NoBang/linearalgebra.jl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mul(::Any, x, y) = x * y
mul(C, A, B, α, β) = A * B * α + C * β
8 changes: 7 additions & 1 deletion src/linearalgebra.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
"""
mul!!(C, A, B) -> C′
mul!!(C, A, B, [α, β]) -> C′
"""
mul!!(C, A, B) = may(mul!, C, A, B)
mul!!(C, A, B, α, β) = may(mul!, C, A, B, α, β)

pure(::typeof(mul!)) = NoBang.mul
_asbb(::typeof(mul!)) = mul!!
possible(::typeof(mul!), C, A, B) =
ismutable(C) && _matmuleltype(A, B) <: eltype(C)
possible(::typeof(mul!), C, A, B, α, β) =
ismutable(C) && _matmuleltype(C, A, B, α, β) <: eltype(C)

# Estimate `eltype` of `C`. This is how it's done in LinearAlgebra.jl
# but maybe it's better to use the approach of
# https://github.com/tpapp/AlgebraResultTypes.jl ?
_matprod(x, y) = x * y + x * y
_matprod(c, a, b, α, β) = a * b * α + a * b * α + c * β
_matmuleltype(A, B) = Base.promote_op(_matprod, eltype(A), eltype(B))
_matmuleltype(C, A, B, α, β) =
Base.promote_op(_matprod, eltype(C), eltype(A), eltype(B), typeof(α), typeof(β))

"""
lmul!!(A, B) -> B′
Expand Down
17 changes: 17 additions & 0 deletions test/test_mul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,21 @@ include("preamble.jl")
@test_inferred mul!!(C, A, B)
end

if VERSION >= v"1.3"
@testset "mul!(C, A, B, α, β)" begin
C = zeros(2, 2)
A = reshape(1:4, 2, 2)
B = ones(2, 2)

desired = A * B * 3 + C * 4
@test mul!!(C, A, B, 3, 4) === C
@test C == desired
@test_inferred mul!!(C, A, B, 3, 4)

desired = A * B * 3 + C * 4
@test mul!!(C, A, B, 3, 4) :: Matrix{Float64} == desired
@test_inferred mul!!(C, A, B, 3, 4)
end
end

end # module

0 comments on commit 4fd4993

Please sign in to comment.