From 7e88c20ebbd06bb84769c9034dca5e5ccc654c25 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Fri, 18 May 2018 13:52:43 -0700 Subject: [PATCH] Add rmul! (#546) --- README.md | 5 +++++ src/Compat.jl | 25 +++++++++++++++++++++++++ test/runtests.jl | 8 ++++++++ 3 files changed, 38 insertions(+) diff --git a/README.md b/README.md index 5cae0be35..67e9931cf 100644 --- a/README.md +++ b/README.md @@ -303,6 +303,9 @@ Currently, the `@compat` macro supports the following syntaxes: * `Compat.qr` takes `pivot` as a `Val` _instance_ and keyword argument `full` ([#22475], [#24279]). +* `Compat.rmul!` provides a subset of the functionality of `LinearAlgebra.rmul!` for + use with Julia 0.6 ([#25701], [#25812]). + ## Renaming * `Display` is now `AbstractDisplay` ([#24831]). @@ -610,10 +613,12 @@ includes this fix. Find the minimum version from there. [#25647]: https://github.com/JuliaLang/julia/issues/25647 [#25654]: https://github.com/JuliaLang/julia/issues/25654 [#25662]: https://github.com/JuliaLang/julia/issues/25662 +[#25701]: https://github.com/JuliaLang/julia/issues/25701 [#25705]: https://github.com/JuliaLang/julia/issues/25705 [#25706]: https://github.com/JuliaLang/julia/issues/25706 [#25738]: https://github.com/JuliaLang/julia/issues/25738 [#25780]: https://github.com/JuliaLang/julia/issues/25780 +[#25812]: https://github.com/JuliaLang/julia/issues/25812 [#25819]: https://github.com/JuliaLang/julia/issues/25819 [#25873]: https://github.com/JuliaLang/julia/issues/25873 [#25896]: https://github.com/JuliaLang/julia/issues/25896 diff --git a/src/Compat.jl b/src/Compat.jl index 105ff648c..9acef20b5 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -1860,6 +1860,31 @@ else using LinearAlgebra: qr end +# rmul! (NOTE: Purposefully not exported) +if VERSION < v"0.7.0-DEV.3563" # scale! not deprecated + if VERSION >= v"0.7.0-DEV.3449" # LinearAlgebra in the stdlib + using LinearAlgebra: UnitUpperTriangular, UnitLowerTriangular, scale! + else + using Base.LinAlg: UnitUpperTriangular, UnitLowerTriangular, scale! + end + const Triangle = Union{UpperTriangular, UnitUpperTriangular, + LowerTriangular, UnitLowerTriangular} + if VERSION < v"0.7.0-DEV.3204" # A_mul_B! not deprecated + rmul!(A::AbstractMatrix, B::Triangle) = A_mul_B!(A, A, B) + else + rmul!(A::AbstractMatrix, B::Triangle) = mul!(A, A, B) + end + rmul!(A::AbstractArray, s::Number) = scale!(A, s) + rmul!(A::AbstractMatrix, D::Diagonal) = scale!(A, D.diag) + rmul!(A::Diagonal, B::Diagonal) = Diagonal(A.diag .*= B.diag) + rmul!(A::Triangle, B::Diagonal) = typeof(A)(rmul!(A.data, B)) +elseif v"0.7.0-DEV.3563" <= VERSION < v"0.7.0-DEV.3665" # scale! -> mul1! + using LinearAlgebra: mul1! + const rmul! = mul1! +elseif VERSION >= v"0.7.0-DEV.3665" # mul1! -> rmul! + using LinearAlgebra: rmul! +end + # https://github.com/JuliaLang/julia/pull/27077 @static if VERSION < v"0.7.0-DEV.5087" export isletter diff --git a/test/runtests.jl b/test/runtests.jl index a737555e5..8222eff93 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1744,6 +1744,14 @@ let A = [1 2; 1 2; 1 2] @test f[1] * [f[2]; [0 0]] ≈ A[:,f[3]] end +let A = [1 2; 3 4] + @test Compat.rmul!(A, 2) == [2 4; 6 8] + @test Compat.rmul!(A, Diagonal([1, 2])) == [2 8; 6 16] + @test Compat.rmul!(A, UpperTriangular([2 2; 3 3])) == [4 28; 12 60] + @test Compat.rmul!(LowerTriangular(A), Diagonal([1, 2])) == LowerTriangular([4 0; 12 120]) + @test Compat.rmul!(Diagonal(A), Diagonal([2, 1])) == Diagonal([8, 120]) +end + # 0.7.0-DEV.5087 @test isletter('a') @test isletter('β')