Skip to content

Commit 93b5458

Browse files
oxinaboxdkarrasch
andauthored
Poorman's 5-arg mul! (#723)
Co-authored-by: Daniel Karrasch <daniel.karrasch@posteo.de>
1 parent f03e138 commit 93b5458

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Compat"
22
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
3-
version = "3.18.0"
3+
version = "3.19.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ changes in `julia`.
5555

5656
## Supported features
5757

58+
* `mul!(C, A, B, alpha, beta)` now performs in-place multiply add ([#29634]). (since Compat 3.19).
59+
5860
* `reinterpret(reshape, T, a::AbstractArray{S})` reinterprets `a` to have eltype `T` while
5961
inserting or consuming the first dimension depending on the ratio of `sizeof(T)` and `sizeof(S)`.
6062
([#37559]). (since Compat 3.18)
@@ -208,3 +210,4 @@ Note that you should specify the correct minimum version for `Compat` in the
208210
[#37244]: https://github.com/JuliaLang/julia/pull/37244
209211
[#37517]: https://github.com/JuliaLang/julia/pull/37517
210212
[#37559]: https://github.com/JuliaLang/julia/pull/37559
213+
[#29634]: https://github.com/JuliaLang/julia/pull/29634

src/Compat.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,29 @@ if VERSION < v"1.6.0-DEV.1083"
739739
end
740740
end
741741

742+
if VERSION < v"1.3.0-alpha.115"
743+
# https://github.com/JuliaLang/julia/pull/29634
744+
# Note this is much less performant than real 5-arg mul!, but is provided so old versions of julia don't error at least
745+
746+
function _mul!(C, A, B, alpha, beta)
747+
Y = similar(C)
748+
LinearAlgebra.mul!(Y, A, B)
749+
C .= Y .* alpha .+ C .* beta
750+
return C
751+
end
752+
753+
# all combination of Number and AbstractArray for A and B except both being Number
754+
function LinearAlgebra.mul!(C::AbstractArray, A::Number, B::AbstractArray, alpha::Number, beta::Number)
755+
return _mul!(C, A, B, alpha, beta)
756+
end
757+
function LinearAlgebra.mul!(C::AbstractArray, A::AbstractArray, B::Number, alpha::Number, beta::Number)
758+
return _mul!(C, A, B, alpha, beta)
759+
end
760+
function LinearAlgebra.mul!(C::AbstractArray, A::AbstractArray, B::AbstractArray, alpha::Number, beta::Number)
761+
return _mul!(C, A, B, alpha, beta)
762+
end
763+
end
764+
742765
include("iterators.jl")
743766
include("deprecated.jl")
744767

test/runtests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,25 @@ end
679679
@test sum(ars) == 8010003
680680
end
681681

682+
# https://github.com/JuliaLang/julia/pull/29634
683+
@testset "5-arg mul!" begin
684+
A = [1.0 2.0; 3.0 4.0]
685+
B = [10.0, 2.0]
686+
C = [100.0, 1000.0]
687+
x = -20.0
688+
alpha = 0.1
689+
beta = 0.1
690+
691+
Cmut = copy(C)
692+
@test Cmut == mul!(Cmut, A, B, alpha, beta) ((A * B * alpha) + (C * beta))
693+
694+
Cmut = copy(C)
695+
@test Cmut == mul!(Cmut, x, B, alpha, beta) ((x * B * alpha) + (C * beta))
696+
697+
Cmut = copy(C)
698+
@test Cmut == mul!(Cmut, B, x, alpha, beta) ((B * x * alpha) + (C * beta))
699+
end
700+
682701
include("iterators.jl")
683702

684703
nothing

0 commit comments

Comments
 (0)