From 1c9da4c2b1e64804f772ce13d5306761ea78bd2c Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Fri, 24 Aug 2018 20:21:28 +0800 Subject: [PATCH 01/23] =?UTF-8?q?=CE=B1=20.\=20I,=20and=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stdlib/LinearAlgebra/src/uniformscaling.jl | 2 ++ stdlib/LinearAlgebra/test/uniformscaling.jl | 1 + 2 files changed, 3 insertions(+) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index 54db4d8a6b893..e3d9ecc2b9005 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -168,6 +168,8 @@ Broadcast.broadcasted(::typeof(*), J::UniformScaling,x::Number) = UniformScaling Broadcast.broadcasted(::typeof(/), J::UniformScaling,x::Number) = UniformScaling(J.λ/x) +Broadcast.broadcasted(::typeof(\), x::Number,J::UniformScaling) = UniformScaling(x\J.λ) + ==(J1::UniformScaling,J2::UniformScaling) = (J1.λ == J2.λ) ## equality comparison with UniformScaling diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 2e2b33a08250e..3a2170c4cbaf1 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -55,6 +55,7 @@ end @test I - α == 1 - α @test α .* UniformScaling(1.0) == UniformScaling(1.0) .* α @test UniformScaling(α)./α == UniformScaling(1.0) + @test α.\UniformScaling(α) == UniformScaling(1.0) @test α * UniformScaling(1.0) == UniformScaling(1.0) * α @test UniformScaling(α)/α == UniformScaling(1.0) end From 721df9db4503bf2db41b2f3c3a8e58c3df4486f5 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Fri, 24 Aug 2018 20:47:36 +0800 Subject: [PATCH 02/23] =?UTF-8?q?I=20^=20=CE=B1,=20I=20.^=20=CE=B1,=20and?= =?UTF-8?q?=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stdlib/LinearAlgebra/src/uniformscaling.jl | 4 ++++ stdlib/LinearAlgebra/test/uniformscaling.jl | 1 + 2 files changed, 5 insertions(+) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index e3d9ecc2b9005..3cfe06d9780c0 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -84,6 +84,8 @@ ishermitian(J::UniformScaling) = isreal(J.λ) (-)(J::UniformScaling, x::Number) = J.λ - x (-)(x::Number, J::UniformScaling) = x - J.λ +(^)(J::UniformScaling, x::Number) = UniformScaling(J.λ ^ x) + (+)(J1::UniformScaling, J2::UniformScaling) = UniformScaling(J1.λ+J2.λ) (+)(B::BitArray{2}, J::UniformScaling) = Array(B) + J (+)(J::UniformScaling, B::BitArray{2}) = J + Array(B) @@ -170,6 +172,8 @@ Broadcast.broadcasted(::typeof(/), J::UniformScaling,x::Number) = UniformScaling Broadcast.broadcasted(::typeof(\), x::Number,J::UniformScaling) = UniformScaling(x\J.λ) +Broadcast.broadcasted(::typeof(^), J::UniformScaling,x::Number) = UniformScaling(J.λ^x) + ==(J1::UniformScaling,J2::UniformScaling) = (J1.λ == J2.λ) ## equality comparison with UniformScaling diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 3a2170c4cbaf1..8a4e19171819a 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -58,6 +58,7 @@ end @test α.\UniformScaling(α) == UniformScaling(1.0) @test α * UniformScaling(1.0) == UniformScaling(1.0) * α @test UniformScaling(α)/α == UniformScaling(1.0) + @test (2I)^α == (2I).^α == (2^α)I end @testset "det and logdet" begin From f51f5f68eae668b9cad9fdcfc13414b4ebf12b66 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Fri, 24 Aug 2018 21:21:17 +0800 Subject: [PATCH 03/23] exp/log, sqrt/cbrt, and trigonometric functions tests not yet written --- stdlib/LinearAlgebra/src/uniformscaling.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index 3cfe06d9780c0..52119446311f2 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -97,6 +97,20 @@ ishermitian(J::UniformScaling) = isreal(J.λ) (-)(J::UniformScaling, B::BitArray{2}) = J - Array(B) (-)(A::AbstractMatrix, J::UniformScaling) = A + (-J) +# matrix functions +for f in ( :exp, :log, + :sqrt, :cbrt, + :sin, :cos, :tan, + :asin, :acos, :atan, + :csc, :sec, :cot, + :acsc, :asec, :acot, + :sinh, :cosh, :tanh, + :asinh, :acosh, :atanh, + :csch, :sech, :coth, + :acsch, :asech, :acoth ) + @eval $f(J::UniformScaling) = UniformScaling($f(J.λ)) +end + # Unit{Lower/Upper}Triangular matrices become {Lower/Upper}Triangular under # addition with a UniformScaling for (t1, t2) in ((:UnitUpperTriangular, :UpperTriangular), From 27d344d5c5b2a7943f1fc0ba54b22dabceb2f83a Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Fri, 24 Aug 2018 23:45:57 +0800 Subject: [PATCH 04/23] cbrt(I) removed, since cube root of matrix is rarely needed In fact none of other matrices in LinearAlgebra specializes cbrt(), I saw sqrt() and then added cbrt() as well without ever thinking. --- stdlib/LinearAlgebra/src/uniformscaling.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index 52119446311f2..e0492568ebdc5 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -99,7 +99,7 @@ ishermitian(J::UniformScaling) = isreal(J.λ) # matrix functions for f in ( :exp, :log, - :sqrt, :cbrt, + :sqrt, :sin, :cos, :tan, :asin, :acos, :atan, :csc, :sec, :cot, From f8dc3b190d7234ab8945e73e35cce0749a9402b0 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Sat, 25 Aug 2018 20:41:36 +0800 Subject: [PATCH 05/23] tests for exp, log, trigonometric functions on \bbC --- stdlib/LinearAlgebra/test/uniformscaling.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 8a4e19171819a..3f14789a132fd 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -23,6 +23,24 @@ Random.seed!(123) @test opnorm(UniformScaling(1+im)) ≈ sqrt(2) end +@testset "exp, log, and trigonometric functions" begin + # on complex plane + J = UniformScaling(randn(ComplexF64)) + # convert to a dense matrix with random size + M = (N = rand(1:0x678); Matrix(J, N, N)) + for f in ( exp, log, + sin, cos, tan, + asin, acos, atan, + csc, sec, cot, + acsc, asec, acot, + sinh, cosh, tanh, + asinh, acosh, atanh, + csch, sech, coth, + acsch, asech, acoth ) + @test f(J) ≈ f(M) + end +end + @testset "conjugation of UniformScaling" begin @test conj(UniformScaling(1))::UniformScaling{Int} == UniformScaling(1) @test conj(UniformScaling(1.0))::UniformScaling{Float64} == UniformScaling(1.0) From 53f8a3b1c78187f2f3fd197caba356f2f1ff3149 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Sat, 25 Aug 2018 21:01:41 +0800 Subject: [PATCH 06/23] minor update changes: M = ... to: M(x) = ... in order to make such conversion reusable in following tests --- stdlib/LinearAlgebra/test/uniformscaling.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 3f14789a132fd..3e9e497798096 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -24,10 +24,11 @@ Random.seed!(123) end @testset "exp, log, and trigonometric functions" begin + # convert to a dense matrix with random size + M(J) = (N = rand(1:0x678); Matrix(J, N, N)) + # on complex plane J = UniformScaling(randn(ComplexF64)) - # convert to a dense matrix with random size - M = (N = rand(1:0x678); Matrix(J, N, N)) for f in ( exp, log, sin, cos, tan, asin, acos, atan, @@ -37,7 +38,7 @@ end asinh, acosh, atanh, csch, sech, coth, acsch, asech, acoth ) - @test f(J) ≈ f(M) + @test f(J) ≈ f(M(J)) end end From cd11f0457b0c6b5aa1e2ed6b06998639ef1a81e7 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Sat, 25 Aug 2018 21:39:55 +0800 Subject: [PATCH 07/23] tests for exp, log, trigonometric functions on \bbR --- stdlib/LinearAlgebra/test/uniformscaling.jl | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 3e9e497798096..54aed8fab2ce3 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -40,6 +40,44 @@ end acsch, asech, acoth ) @test f(J) ≈ f(M(J)) end + + # on real axis + # functions defined for x ∈ ℝ + J = UniformScaling(randn(Float64)) + for f in ( exp, + sin, cos, tan, + csc, sec, cot, + atan, acot, + sinh, cosh, tanh, + csch, sech, coth, + asinh, acsch ) + @test f(J) ≈ f(M(J)) + end + # functions defined for x ≥ 0 + J = UniformScaling(abs(randn(Float64))) + for f in ( log, ) + @test f(J) ≈ f(M(J)) + end + # functions defined for -1 ≤ x ≤ 1 + J = UniformScaling(2 * rand(Float64) - 1) + for f in ( asin, acos, atanh ) + @test f(J) ≈ f(M(J)) + end + # functions defined for x ≤ -1 or x ≥ 1 + J = UniformScaling(1 ./ (2 * rand(Float64) - 1)) + for f in ( acsc, asec, acoth ) + @test f(J) ≈ f(M(J)) + end + # functions defined for 0 ≤ x ≤ 1 + J = UniformScaling(rand(Float64)) + for f in ( asech, ) + @test f(J) ≈ f(M(J)) + end + # functions defined for x ≥ 1 + J = UniformScaling(1 ./ rand(Float64)) + for f in ( acosh, ) + @test f(J) ≈ f(M(J)) + end end @testset "conjugation of UniformScaling" begin From 16dcabeaf58529f5bcf16c084e1dc4e8a54b1129 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Sat, 25 Aug 2018 21:45:51 +0800 Subject: [PATCH 08/23] add tests for sqrt I forgot it... --- stdlib/LinearAlgebra/test/uniformscaling.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 54aed8fab2ce3..78c3f521e2815 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -23,13 +23,14 @@ Random.seed!(123) @test opnorm(UniformScaling(1+im)) ≈ sqrt(2) end -@testset "exp, log, and trigonometric functions" begin +@testset "sqrt, exp, log, and trigonometric functions" begin # convert to a dense matrix with random size M(J) = (N = rand(1:0x678); Matrix(J, N, N)) # on complex plane J = UniformScaling(randn(ComplexF64)) for f in ( exp, log, + sqrt, sin, cos, tan, asin, acos, atan, csc, sec, cot, @@ -55,7 +56,7 @@ end end # functions defined for x ≥ 0 J = UniformScaling(abs(randn(Float64))) - for f in ( log, ) + for f in ( log, sqrt ) @test f(J) ≈ f(M(J)) end # functions defined for -1 ≤ x ≤ 1 From 762b39b10831b8de5f306ddfcbabec696d8d2aec Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Mon, 27 Aug 2018 15:01:12 +0800 Subject: [PATCH 09/23] v * I, and tests --- stdlib/LinearAlgebra/src/uniformscaling.jl | 2 +- stdlib/LinearAlgebra/test/uniformscaling.jl | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index e0492568ebdc5..6bcbe17d5aba4 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -160,7 +160,7 @@ end *(J1::UniformScaling, J2::UniformScaling) = UniformScaling(J1.λ*J2.λ) *(B::BitArray{2}, J::UniformScaling) = *(Array(B), J::UniformScaling) *(J::UniformScaling, B::BitArray{2}) = *(J::UniformScaling, Array(B)) -*(A::AbstractMatrix, J::UniformScaling) = A*J.λ +*(A::AbstractVecOrMat, J::UniformScaling) = A*J.λ *(J::UniformScaling, A::AbstractVecOrMat) = J.λ*A *(x::Number, J::UniformScaling) = UniformScaling(x*J.λ) *(J::UniformScaling, x::Number) = UniformScaling(J.λ*x) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 78c3f521e2815..4ac5d275b3b43 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -153,6 +153,17 @@ let @test copyto!(B, J) == [λ zero(λ)] end + @testset "binary ops with vectors" begin + for T in (Float64, ComplexF64) + v = randn(T, 3) + J = UniformScaling(randn(T)) + @test v * J == v * J.λ + @test v' * J == v' * J.λ + @test J * v == J.λ * v + @test J * v' == J.λ * v' + end + end + @testset "binary ops with matrices" begin B = bitrand(2, 2) @test B + I == B + Matrix(I, size(B)) From 6dfa1849261ab414ed2d2b6a44cbbd29fcbd6995 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Mon, 27 Aug 2018 16:12:55 +0800 Subject: [PATCH 10/23] fix tests `J` is used by following tests, but I mistakenly changed its value --- stdlib/LinearAlgebra/test/uniformscaling.jl | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 4ac5d275b3b43..be457af891110 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -154,14 +154,11 @@ let end @testset "binary ops with vectors" begin - for T in (Float64, ComplexF64) - v = randn(T, 3) - J = UniformScaling(randn(T)) - @test v * J == v * J.λ - @test v' * J == v' * J.λ - @test J * v == J.λ * v - @test J * v' == J.λ * v' - end + v = complex(randn(), randn()) + @test v * J == v * λ + @test v' * J == v' * λ + @test J * v == λ * v + @test J * v' == λ * v' end @testset "binary ops with matrices" begin From 56025a606ff43fca3102e23258984d86c585c7b1 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Mon, 27 Aug 2018 16:20:18 +0800 Subject: [PATCH 11/23] v / I, and tests --- stdlib/LinearAlgebra/src/uniformscaling.jl | 2 +- stdlib/LinearAlgebra/test/uniformscaling.jl | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index 6bcbe17d5aba4..d411b3cdbdd82 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -167,7 +167,7 @@ end /(J1::UniformScaling, J2::UniformScaling) = J2.λ == 0 ? throw(SingularException(1)) : UniformScaling(J1.λ/J2.λ) /(J::UniformScaling, A::AbstractMatrix) = lmul!(J.λ, inv(A)) -/(A::AbstractMatrix, J::UniformScaling) = J.λ == 0 ? throw(SingularException(1)) : A/J.λ +/(A::AbstractVecOrMat, J::UniformScaling) = J.λ == 0 ? throw(SingularException(1)) : A/J.λ /(J::UniformScaling, x::Number) = UniformScaling(J.λ/x) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index be457af891110..823cbe731eea9 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -159,6 +159,10 @@ let @test v' * J == v' * λ @test J * v == λ * v @test J * v' == λ * v' + @test v / J == v / λ + @test v' / J == v' / λ + @test J \ v == λ \ v + @test J \ v' == λ \ v' end @testset "binary ops with matrices" begin From 8be120ab152b4fb5c77f133a0bd6d9a22eb4f02b Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Mon, 27 Aug 2018 17:26:44 +0800 Subject: [PATCH 12/23] fix tests, seriously I made v a scalar, forgive me --- stdlib/LinearAlgebra/test/uniformscaling.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 823cbe731eea9..901a184055586 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -154,7 +154,7 @@ let end @testset "binary ops with vectors" begin - v = complex(randn(), randn()) + v = complex.(randn(3), randn(3)) @test v * J == v * λ @test v' * J == v' * λ @test J * v == λ * v From c66ce05bbe8792fae84ce4453cc21f1accb4fa88 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Mon, 27 Aug 2018 21:01:37 +0800 Subject: [PATCH 13/23] remove possibly redundant code to see if tests fail `Union{Bidiagonal,AbstractTriangular} \ UniformScaling` seems redundant because there's another method `AbstractMatrix \ UniformScaling` --- stdlib/LinearAlgebra/src/uniformscaling.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index d411b3cdbdd82..c7e9fb1985fa4 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -172,8 +172,6 @@ end /(J::UniformScaling, x::Number) = UniformScaling(J.λ/x) \(J1::UniformScaling, J2::UniformScaling) = J1.λ == 0 ? throw(SingularException(1)) : UniformScaling(J1.λ\J2.λ) -\(A::Union{Bidiagonal{T},AbstractTriangular{T}}, J::UniformScaling) where {T<:Number} = - rmul!(inv(A), J.λ) \(J::UniformScaling, A::AbstractVecOrMat) = J.λ == 0 ? throw(SingularException(1)) : J.λ\A \(A::AbstractMatrix, J::UniformScaling) = rmul!(inv(A), J.λ) From c600d525ce517ff1eabf206c22bc0e1118a26b52 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Wed, 29 Aug 2018 09:49:16 +0800 Subject: [PATCH 14/23] real, imag, and tests --- stdlib/LinearAlgebra/src/uniformscaling.jl | 2 ++ stdlib/LinearAlgebra/test/uniformscaling.jl | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index c7e9fb1985fa4..05b3d3dc8b937 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -63,6 +63,8 @@ end copy(J::UniformScaling) = UniformScaling(J.λ) conj(J::UniformScaling) = UniformScaling(conj(J.λ)) +real(J::UniformScaling) = UniformScaling(real(J.λ)) +imag(J::UniformScaling) = UniformScaling(imag(J.λ)) transpose(J::UniformScaling) = J adjoint(J::UniformScaling) = UniformScaling(conj(J.λ)) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 901a184055586..301c825187c94 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -146,6 +146,12 @@ let @test cond(J) == (λ ≠ zero(λ) ? one(real(λ)) : oftype(real(λ), Inf)) end + @testset "real, imag, reim" begin + @test real(J) == UniformScaling(real(λ)) + @test imag(J) == UniformScaling(imag(λ)) + @test reim(J) == (UniformScaling(real(λ)), UniformScaling(imag(λ))) + end + @testset "copyto!" begin A = Matrix{Int}(undef, (3,3)) @test copyto!(A, I) == one(A) From dc2eeb33ce97ca4ccad0e1d05194303174961dc0 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Wed, 29 Aug 2018 10:37:52 +0800 Subject: [PATCH 15/23] isposdef, and tests also made `isapprox` a standalone testset, because it's irrelevant to `istriu`, `issymmetric`, etc --- stdlib/LinearAlgebra/src/uniformscaling.jl | 2 ++ stdlib/LinearAlgebra/test/uniformscaling.jl | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index 05b3d3dc8b937..a119695ac2eba 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -81,6 +81,8 @@ istril(::UniformScaling) = true issymmetric(::UniformScaling) = true ishermitian(J::UniformScaling) = isreal(J.λ) +isposdef(J::UniformScaling) = isreal(J.λ) && J.λ > 0 + (+)(J::UniformScaling, x::Number) = J.λ + x (+)(x::Number, J::UniformScaling) = x + J.λ (-)(J::UniformScaling, x::Number) = J.λ - x diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 301c825187c94..7b8ec23b4123c 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -88,13 +88,20 @@ end @test conj(UniformScaling(1.0+1.0im))::UniformScaling{Complex{Float64}} == UniformScaling(1.0-1.0im) end -@testset "istriu, istril, issymmetric, ishermitian, isapprox" begin +@testset "istriu, istril, issymmetric, ishermitian, isposdef" begin @test istriu(I) @test istril(I) @test issymmetric(I) @test issymmetric(UniformScaling(complex(1.0,1.0))) @test ishermitian(I) @test !ishermitian(UniformScaling(complex(1.0,1.0))) + @test isposdef(UniformScaling(rand())) + @test !isposdef(UniformScaling(-rand())) + @test !isposdef(UniformScaling(randn(ComplexF64))) + @test !isposdef(UniformScaling(NaN)) +end + +@testset "isapprox" begin @test UniformScaling(4.00000000000001) ≈ UniformScaling(4.0) @test UniformScaling(4.32) ≈ UniformScaling(4.3) rtol=0.1 atol=0.01 @test UniformScaling(4.32) ≈ 4.3 * [1 0; 0 1] rtol=0.1 atol=0.01 From 5991853438aef21a8fe0a02a7d262179c434ae1b Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Wed, 29 Aug 2018 11:03:07 +0800 Subject: [PATCH 16/23] tr, and tests --- stdlib/LinearAlgebra/src/uniformscaling.jl | 8 ++++++++ stdlib/LinearAlgebra/test/uniformscaling.jl | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index a119695ac2eba..bd8fb427bfc09 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -161,6 +161,14 @@ function det(J::UniformScaling{T}) where T end end +function tr(J::UniformScaling{T}) where T + if iszero(J.λ) + zero(T) + else + throw(ArgumentError("Trace of UniformScaling is only well-defined when λ = 0")) + end +end + *(J1::UniformScaling, J2::UniformScaling) = UniformScaling(J1.λ*J2.λ) *(B::BitArray{2}, J::UniformScaling) = *(Array(B), J::UniformScaling) *(J::UniformScaling, B::BitArray{2}) = *(J::UniformScaling, Array(B)) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 7b8ec23b4123c..ea9a4db805d00 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -126,7 +126,11 @@ end @test (2I)^α == (2I).^α == (2^α)I end -@testset "det and logdet" begin +@testset "tr, det and logdet" begin + for T in (Int, Float64, ComplexF64, Bool) + @test tr(UniformScaling(zero(T))) === zero(T) + end + @test_throws ArgumentError tr(UniformScaling(1)) @test det(I) === true @test det(1.0I) === 1.0 @test det(0I) === 0 From fb7fb255337ed48a7ef1301d2f2078e92d6f3deb Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Wed, 29 Aug 2018 11:32:54 +0800 Subject: [PATCH 17/23] rearrange test code to make it more concise --- stdlib/LinearAlgebra/test/uniformscaling.jl | 59 +++++++++------------ 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index ea9a4db805d00..51b00c995f20f 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -43,41 +43,30 @@ end end # on real axis - # functions defined for x ∈ ℝ - J = UniformScaling(randn(Float64)) - for f in ( exp, - sin, cos, tan, - csc, sec, cot, - atan, acot, - sinh, cosh, tanh, - csch, sech, coth, - asinh, acsch ) - @test f(J) ≈ f(M(J)) - end - # functions defined for x ≥ 0 - J = UniformScaling(abs(randn(Float64))) - for f in ( log, sqrt ) - @test f(J) ≈ f(M(J)) - end - # functions defined for -1 ≤ x ≤ 1 - J = UniformScaling(2 * rand(Float64) - 1) - for f in ( asin, acos, atanh ) - @test f(J) ≈ f(M(J)) - end - # functions defined for x ≤ -1 or x ≥ 1 - J = UniformScaling(1 ./ (2 * rand(Float64) - 1)) - for f in ( acsc, asec, acoth ) - @test f(J) ≈ f(M(J)) - end - # functions defined for 0 ≤ x ≤ 1 - J = UniformScaling(rand(Float64)) - for f in ( asech, ) - @test f(J) ≈ f(M(J)) - end - # functions defined for x ≥ 1 - J = UniformScaling(1 ./ rand(Float64)) - for f in ( acosh, ) - @test f(J) ≈ f(M(J)) + for (λ, fs) in ( + # functions defined for x ∈ ℝ + (()->randn(), (exp, + sin, cos, tan, + csc, sec, cot, + atan, acot, + sinh, cosh, tanh, + csch, sech, coth, + asinh, acsch)), + # functions defined for x ≥ 0 + (()->abs(randn()), (log, sqrt)), + # functions defined for -1 ≤ x ≤ 1 + (()->2rand()-1, (asin, acos, atanh)), + # functions defined for x ≤ -1 or x ≥ 1 + (()->1/(2rand()-1), (acsc, asec, acoth)), + # functions defined for 0 ≤ x ≤ 1 + (()->rand(), (asech,)), + # functions defined for x ≥ 1 + (()->1/rand(), (acosh,)) + ) + for f in fs + J = UniformScaling(λ()) + @test f(J) ≈ f(M(J)) + end end end From a642feb43bbbb7c52768556d27ec241997afcc04 Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Wed, 29 Aug 2018 11:33:55 +0800 Subject: [PATCH 18/23] reduce size of matrix used for testing to reduce test time --- stdlib/LinearAlgebra/test/uniformscaling.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 51b00c995f20f..76bcc1e92e58a 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -25,7 +25,7 @@ end @testset "sqrt, exp, log, and trigonometric functions" begin # convert to a dense matrix with random size - M(J) = (N = rand(1:0x678); Matrix(J, N, N)) + M(J) = (N = rand(1:10); Matrix(J, N, N)) # on complex plane J = UniformScaling(randn(ComplexF64)) From 2c63d67cb867f5638e63756d5b74495026f00fba Mon Sep 17 00:00:00 2001 From: ZeppLu Date: Wed, 29 Aug 2018 18:07:10 +0800 Subject: [PATCH 19/23] pinv, and tests --- stdlib/LinearAlgebra/src/uniformscaling.jl | 4 ++++ stdlib/LinearAlgebra/test/uniformscaling.jl | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index bd8fb427bfc09..3ae1a550de14e 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -151,6 +151,10 @@ end inv(J::UniformScaling) = UniformScaling(inv(J.λ)) opnorm(J::UniformScaling, p::Real=2) = opnorm(J.λ, p) +pinv(J::UniformScaling) = ifelse(iszero(J.λ), + UniformScaling(zero(inv(J.λ))), # type stability + UniformScaling(inv(J.λ))) + function det(J::UniformScaling{T}) where T if isone(J.λ) one(T) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 76bcc1e92e58a..d67c30f38c85a 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -135,13 +135,18 @@ end let λ = complex(randn(),randn()) J = UniformScaling(λ) - @testset "transpose, conj, inv" begin + @testset "transpose, conj, inv, pinv, cond" begin @test ndims(J) == 2 @test transpose(J) == J @test J * [1 0; 0 1] == conj(*(adjoint(J), [1 0; 0 1])) # ctranpose (and A(c)_mul_B) @test I + I === UniformScaling(2) # + @test inv(I) == I @test inv(J) == UniformScaling(inv(λ)) + @test pinv(J) == UniformScaling(inv(λ)) + @test @inferred(pinv(0.0I)) == 0.0I + @test @inferred(pinv(0I)) == 0.0I + @test @inferred(pinv(false*I)) == 0.0I + @test @inferred(pinv(0im*I)) == 0im*I @test cond(I) == 1 @test cond(J) == (λ ≠ zero(λ) ? one(real(λ)) : oftype(real(λ), Inf)) end From 48cfc4d5d1b9b7ba727e9004b155608ba7200ae7 Mon Sep 17 00:00:00 2001 From: Zepp Date: Fri, 29 Mar 2019 15:13:39 +0800 Subject: [PATCH 20/23] update according to reviewers' suggestions - *(v::AbstractVector, J::UniformScaling) - /(v::AbstractVector, J::UniformScaling) - isposdef(J::UniformScaling) --- stdlib/LinearAlgebra/src/uniformscaling.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index 3ae1a550de14e..ccf9437a2bc3a 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -81,7 +81,7 @@ istril(::UniformScaling) = true issymmetric(::UniformScaling) = true ishermitian(J::UniformScaling) = isreal(J.λ) -isposdef(J::UniformScaling) = isreal(J.λ) && J.λ > 0 +isposdef(J::UniformScaling) = isposdef(J.λ) (+)(J::UniformScaling, x::Number) = J.λ + x (+)(x::Number, J::UniformScaling) = x + J.λ @@ -176,14 +176,16 @@ end *(J1::UniformScaling, J2::UniformScaling) = UniformScaling(J1.λ*J2.λ) *(B::BitArray{2}, J::UniformScaling) = *(Array(B), J::UniformScaling) *(J::UniformScaling, B::BitArray{2}) = *(J::UniformScaling, Array(B)) -*(A::AbstractVecOrMat, J::UniformScaling) = A*J.λ +*(A::AbstractMatrix, J::UniformScaling) = A*J.λ +*(v::AbstractVector, J::UniformScaling) = reshape(v, length(v), 1) * J *(J::UniformScaling, A::AbstractVecOrMat) = J.λ*A *(x::Number, J::UniformScaling) = UniformScaling(x*J.λ) *(J::UniformScaling, x::Number) = UniformScaling(J.λ*x) /(J1::UniformScaling, J2::UniformScaling) = J2.λ == 0 ? throw(SingularException(1)) : UniformScaling(J1.λ/J2.λ) /(J::UniformScaling, A::AbstractMatrix) = lmul!(J.λ, inv(A)) -/(A::AbstractVecOrMat, J::UniformScaling) = J.λ == 0 ? throw(SingularException(1)) : A/J.λ +/(A::AbstractMatrix, J::UniformScaling) = J.λ == 0 ? throw(SingularException(1)) : A/J.λ +/(v::AbstractVector, J::UniformScaling) = reshape(v, length(v), 1) / J /(J::UniformScaling, x::Number) = UniformScaling(J.λ/x) From 17854926b48362d8cf8719f1b058a0c373236223 Mon Sep 17 00:00:00 2001 From: Zepp Date: Tue, 2 Apr 2019 16:40:32 +0800 Subject: [PATCH 21/23] fix test --- stdlib/LinearAlgebra/test/uniformscaling.jl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index ed3d8105850e5..d4f9049ef5c34 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -170,14 +170,15 @@ let @testset "binary ops with vectors" begin v = complex.(randn(3), randn(3)) - @test v * J == v * λ - @test v' * J == v' * λ - @test J * v == λ * v - @test J * v' == λ * v' - @test v / J == v / λ - @test v' / J == v' / λ - @test J \ v == λ \ v - @test J \ v' == λ \ v' + # As shown in #20423@GitHub, vector acts like x1 matrix when participating in linear algebra + @test all(v * J == v * λ) + @test all(v' * J == v' * λ) + @test all(J * v == λ * v ) + @test all(J * v' == λ * v') + @test all(v / J == v / λ) + @test all(v' / J == v' / λ) + @test all(J \ v == λ \ v ) + @test all(J \ v' == λ \ v') end @testset "binary ops with matrices" begin From d659b40e394f642586cb74602f2f05579f741b3c Mon Sep 17 00:00:00 2001 From: Zepp Date: Tue, 2 Apr 2019 16:41:16 +0800 Subject: [PATCH 22/23] fix test (element-wise euqality) --- stdlib/LinearAlgebra/test/uniformscaling.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index d4f9049ef5c34..26ac39c9d680c 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -171,14 +171,14 @@ let @testset "binary ops with vectors" begin v = complex.(randn(3), randn(3)) # As shown in #20423@GitHub, vector acts like x1 matrix when participating in linear algebra - @test all(v * J == v * λ) - @test all(v' * J == v' * λ) - @test all(J * v == λ * v ) - @test all(J * v' == λ * v') - @test all(v / J == v / λ) - @test all(v' / J == v' / λ) - @test all(J \ v == λ \ v ) - @test all(J \ v' == λ \ v') + @test all(v * J .== v * λ) + @test all(v' * J .== v' * λ) + @test all(J * v .== λ * v ) + @test all(J * v' .== λ * v') + @test all(v / J .== v / λ) + @test all(v' / J .== v' / λ) + @test all(J \ v .== λ \ v ) + @test all(J \ v' .== λ \ v') end @testset "binary ops with matrices" begin From 6cc47a721b76dbc19a93915a27339ec16ebd147c Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Wed, 19 Feb 2020 10:31:58 +0100 Subject: [PATCH 23/23] relax some multiplication/division tests --- stdlib/LinearAlgebra/test/uniformscaling.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 17cb9418d8a4a..1564198443f24 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -184,14 +184,14 @@ let @testset "binary ops with vectors" begin v = complex.(randn(3), randn(3)) # As shown in #20423@GitHub, vector acts like x1 matrix when participating in linear algebra - @test v * J == v * λ - @test v' * J == v' * λ - @test J * v == λ * v - @test J * v' == λ * v' - @test v / J == v / λ - @test v' / J == v' / λ - @test J \ v == λ \ v - @test J \ v' == λ \ v' + @test v * J ≈ v * λ + @test v' * J ≈ v' * λ + @test J * v ≈ λ * v + @test J * v' ≈ λ * v' + @test v / J ≈ v / λ + @test v' / J ≈ v' / λ + @test J \ v ≈ λ \ v + @test J \ v' ≈ λ \ v' end @testset "binary ops with matrices" begin