diff --git a/test/linalg/bunchkaufman.jl b/test/linalg/bunchkaufman.jl index d6879e68e5543..30847c6705f31 100644 --- a/test/linalg/bunchkaufman.jl +++ b/test/linalg/bunchkaufman.jl @@ -22,19 +22,14 @@ bimg = randn(n,2)/2 @testset for eltya in (Float32, Float64, Complex64, Complex128, Int) a = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(areal, aimg) : areal) a2 = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(a2real, a2img) : a2real) - @testset for atype in ("Array", "SubArray") - asym = a.'+ a # symmetric indefinite - aher = a' + a # Hermitian indefinite - apd = a' * a # Positive-definite - if atype == "Array" - a = a - a2 = a2 - else - a = view(a , 1:n, 1:n) - a2 = view(a2 , 1:n, 1:n) - aher = view(aher, 1:n, 1:n) - apd = view(apd , 1:n, 1:n) - end + asym = a.'+ a # symmetric indefinite + aher = a' + a # Hermitian indefinite + apd = a' * a # Positive-definite + for (a, a2, aher, apd) in ((a, a2, aher, apd), + (view(a, 1:n, 1:n), + view(a2, 1:n, 1:n), + view(aher, 1:n, 1:n), + view(apd , 1:n, 1:n))) ε = εa = eps(abs(float(one(eltya)))) @testset for eltyb in (Float32, Float64, Complex64, Complex128, Int) @@ -44,13 +39,7 @@ bimg = randn(n,2)/2 @test isa(factorize(asym), LinAlg.BunchKaufman) @test isa(factorize(aher), LinAlg.BunchKaufman) - @testset for btype in ("Array", "SubArray") - if btype == "Array" - b = b - else - b = view(b, 1:n, 1:2) - end - + for b in (b, view(b, 1:n, 1:2)) εb = eps(abs(float(one(eltyb)))) ε = max(εa,εb) @@ -113,13 +102,7 @@ end As3[1, end] -= im for As = (As1, As2, As3) - @testset for Astype in ("Array", "SubArray") - if Astype == "Array" - As = As - else - As = view(As, 1:n, 1:n) - end - + for As in (As, view(As, 1:n, 1:n)) @testset for rook in (false, true) @testset for uplo in (:L, :U) F = bkfact(issymmetric(As) ? Symmetric(As, uplo) : Hermitian(As, uplo), rook) diff --git a/test/linalg/dense.jl b/test/linalg/dense.jl index ea313bc8f7579..ad719ee0fb3cc 100644 --- a/test/linalg/dense.jl +++ b/test/linalg/dense.jl @@ -20,12 +20,7 @@ srand(1234321) ainit = rand(n,n) @testset "for $elty" for elty in (Float32, Float64, Complex64, Complex128) ainit = convert(Matrix{elty}, ainit) - for arraytype in ("Array", "SubArray") - if arraytype == "Array" - a = ainit - else - a = view(ainit, 1:n, 1:n) - end + for a in (copy(ainit), view(ainit, 1:n, 1:n)) @test cond(a,1) ≈ 4.837320054554436e+02 atol=0.01 @test cond(a,2) ≈ 1.960057871514615e+02 atol=0.01 @test cond(a,Inf) ≈ 3.757017682707787e+02 atol=0.01 @@ -60,16 +55,7 @@ bimg = randn(n,2)/2 binit = eltyb == Int ? rand(1:5, n, 2) : convert(Matrix{eltyb}, eltyb <: Complex ? complex.(breal, bimg) : breal) εb = eps(abs(float(one(eltyb)))) ε = max(εa,εb) - - for arraytype in ("Array", "SubArray") - if arraytype == "Array" - a = ainit - b = binit - else - a = view(ainit, 1:n, 1:n) - b = view(binit, 1:n, 1:2) - end - + for (a, b) in ((copy(ainit), copy(binit)), (view(ainit, 1:n, 1:n), view(binit, 1:n, 1:2))) @testset "Solve square general system of equations" begin κ = cond(a,1) x = a \ b @@ -90,15 +76,7 @@ bimg = randn(n,2)/2 end end # for eltyb - for arraytype in ("Array", "SubArray") - if arraytype == "Array" - a = ainit - a2 = ainit2 - else - a = view(ainit, 1:n, 1:n) - a2 = view(ainit2, 1:n, 1:n) - end - + for (a, a2) in ((copy(ainit), copy(ainit2)), (view(ainit, 1:n, 1:n), view(ainit2, 1:n, 1:n))) @testset "Test pinv" begin pinva15 = pinv(a[:,1:n1]) @test a[:,1:n1]*pinva15*a[:,1:n1] ≈ a[:,1:n1] @@ -169,14 +147,9 @@ end # for eltya @testset "test triu/tril bounds checking" begin ainit = rand(5,7) - for arraytype in ("Array", "SubArray") - if arraytype == "Array" - a = ainit - else - a = view(ainit, 1:size(ainit, 1), 1:size(ainit, 2)) - end - @test_throws(ArgumentError,triu(a,8)) + for a in (copy(ainit), view(ainit, 1:size(ainit, 1), 1:size(ainit, 2))) @test_throws(ArgumentError,triu(a,-6)) + @test_throws(ArgumentError,triu(a,8)) @test_throws(ArgumentError,tril(a,8)) @test_throws(ArgumentError,tril(a,-6)) end @@ -253,14 +226,7 @@ end α = elty <: Integer ? randn() : elty <: Complex ? convert(elty, complex(randn(),randn())) : convert(elty, randn()) - for arraytype in ("Array", "SubArray") - if arraytype == "Array" - x = xinit - y = yinit - else - x = view(xinit,1:2:nnorm) - y = view(yinit,1:2:nnorm) - end + for (x, y) in ((copy(xinit), copy(yinit)), (view(xinit,1:2:nnorm), view(yinit,1:2:nnorm))) # Absolute homogeneity @test norm(α*x,-Inf) ≈ abs(α)*norm(x,-Inf) @test norm(α*x,-1) ≈ abs(α)*norm(x,-1) @@ -309,16 +275,7 @@ end α = elty <: Integer ? randn() : elty <: Complex ? convert(elty, complex(randn(),randn())) : convert(elty, randn()) - - for arraytype in ("Array", "SubArray") - if arraytype == "Array" - A = Ainit - B = Binit - else - A = view(Ainit,1:nmat,1:nmat) - B = view(Binit,1:nmat,1:nmat) - end - + for (A, B) in ((copy(Ainit), copy(Binit)), (view(Ainit,1:nmat,1:nmat), view(Binit,1:nmat,1:nmat))) # Absolute homogeneity @test norm(α*A,1) ≈ abs(α)*norm(A,1) elty <: Union{BigFloat,Complex{BigFloat},BigInt} || @test norm(α*A) ≈ abs(α)*norm(A) # two is default diff --git a/test/linalg/diagonal.jl b/test/linalg/diagonal.jl index 4d490c2d6e281..ec7b4f9338a8e 100644 --- a/test/linalg/diagonal.jl +++ b/test/linalg/diagonal.jl @@ -8,19 +8,19 @@ n=12 #Size of matrix problem to test srand(1) @testset for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty}) - d=convert(Vector{elty}, randn(n)) - v=convert(Vector{elty}, randn(n)) - U=convert(Matrix{elty}, randn(n,n)) + dd=convert(Vector{elty}, randn(n)) + vv=convert(Vector{elty}, randn(n)) + UU=convert(Matrix{elty}, randn(n,n)) if elty <: Complex - d+=im*convert(Vector{elty}, randn(n)) - v+=im*convert(Vector{elty}, randn(n)) - U+=im*convert(Matrix{elty}, randn(n,n)) + dd+=im*convert(Vector{elty}, randn(n)) + vv+=im*convert(Vector{elty}, randn(n)) + UU+=im*convert(Matrix{elty}, randn(n,n)) end - D = Diagonal(d) - DM = diagm(d) + D = Diagonal(dd) + DM = diagm(dd) @testset "constructor" begin - for x in (d, GenericArray(d)) + for x in (dd, GenericArray(dd)) @test Diagonal(x)::Diagonal{elty,typeof(x)} == DM @test Diagonal(x).diag === x @test Diagonal{elty}(x)::Diagonal{elty,typeof(x)} == DM @@ -38,9 +38,9 @@ srand(1) @test Array(abs.(D)) == abs.(DM) @test Array(imag(D)) == imag(DM) - @test parent(D) == d - @test diag(D) == d - @test D[1,1] == d[1] + @test parent(D) == dd + @test diag(D) == dd + @test D[1,1] == dd[1] @test D[1,2] == 0 @test issymmetric(D) @@ -73,61 +73,49 @@ srand(1) end @testset "Linear solve" begin - let vv = v, UU = U - @testset for atype in ("Array", "SubArray") - if atype == "Array" - v = vv - U = UU - else - v = view(vv, 1:n) - U = view(UU, 1:n, 1:2) - end - - @test D*v ≈ DM*v atol=n*eps(relty)*(1+(elty<:Complex)) - @test D*U ≈ DM*U atol=n^2*eps(relty)*(1+(elty<:Complex)) - - @test U.'*D ≈ U.'*Array(D) - @test U'*D ≈ U'*Array(D) - - if relty != BigFloat - atol_two = 2n^2 * eps(relty) * (1 + (elty <: Complex)) - atol_three = 2n^3 * eps(relty) * (1 + (elty <: Complex)) - @test D\v ≈ DM\v atol=atol_two - @test D\U ≈ DM\U atol=atol_three - @test A_ldiv_B!(D, copy(v)) ≈ DM\v atol=atol_two - @test At_ldiv_B!(D, copy(v)) ≈ DM\v atol=atol_two - @test Ac_ldiv_B!(conj(D), copy(v)) ≈ DM\v atol=atol_two - @test A_ldiv_B!(D, copy(U)) ≈ DM\U atol=atol_three - @test At_ldiv_B!(D, copy(U)) ≈ DM\U atol=atol_three - @test Ac_ldiv_B!(conj(D), copy(U)) ≈ DM\U atol=atol_three - Uc = adjoint(U) - target = scale!(Uc, inv.(D.diag)) - @test A_rdiv_B!(Uc, D) ≈ target atol=atol_three - @test_throws DimensionMismatch A_rdiv_B!(eye(elty, n-1), D) - @test_throws SingularException A_rdiv_B!(Uc, zeros(D)) - @test A_rdiv_Bt!(Uc, D) ≈ target atol=atol_three - @test A_rdiv_Bc!(Uc, conj(D)) ≈ target atol=atol_three - @test A_ldiv_B!(D, eye(D)) ≈ D\eye(D) atol=atol_three - @test_throws DimensionMismatch A_ldiv_B!(D, ones(elty, n + 1)) - @test_throws SingularException A_ldiv_B!(Diagonal(zeros(relty, n)), copy(v)) - b = rand(elty, n, n) - b = sparse(b) - @test A_ldiv_B!(D, copy(b)) ≈ Array(D)\Array(b) - @test_throws SingularException A_ldiv_B!(Diagonal(zeros(elty, n)), copy(b)) - b = view(rand(elty, n), collect(1:n)) - b2 = copy(b) - c = A_ldiv_B!(D, b) - d = Array(D)\b2 - for i in 1:n - @test c[i] ≈ d[i] - end - @test_throws SingularException A_ldiv_B!(Diagonal(zeros(elty, n)), b) - b = rand(elty, n+1, n+1) - b = sparse(b) - @test_throws DimensionMismatch A_ldiv_B!(D, copy(b)) - b = view(rand(elty, n+1), collect(1:n+1)) - @test_throws DimensionMismatch A_ldiv_B!(D, b) - end + for (v, U) in ((vv, UU), (view(vv, 1:n), view(UU, 1:n, 1:2))) + @test D*v ≈ DM*v atol=n*eps(relty)*(1+(elty<:Complex)) + @test D*U ≈ DM*U atol=n^2*eps(relty)*(1+(elty<:Complex)) + + @test U.'*D ≈ U.'*Array(D) + @test U'*D ≈ U'*Array(D) + + if relty != BigFloat + atol_two = 2n^2 * eps(relty) * (1 + (elty <: Complex)) + atol_three = 2n^3 * eps(relty) * (1 + (elty <: Complex)) + @test D\v ≈ DM\v atol=atol_two + @test D\U ≈ DM\U atol=atol_three + @test A_ldiv_B!(D, copy(v)) ≈ DM\v atol=atol_two + @test At_ldiv_B!(D, copy(v)) ≈ DM\v atol=atol_two + @test Ac_ldiv_B!(conj(D), copy(v)) ≈ DM\v atol=atol_two + @test A_ldiv_B!(D, copy(U)) ≈ DM\U atol=atol_three + @test At_ldiv_B!(D, copy(U)) ≈ DM\U atol=atol_three + @test Ac_ldiv_B!(conj(D), copy(U)) ≈ DM\U atol=atol_three + Uc = adjoint(U) + target = scale!(Uc, inv.(D.diag)) + @test A_rdiv_B!(Uc, D) ≈ target atol=atol_three + @test_throws DimensionMismatch A_rdiv_B!(eye(elty, n-1), D) + @test_throws SingularException A_rdiv_B!(Uc, zeros(D)) + @test A_rdiv_Bt!(Uc, D) ≈ target atol=atol_three + @test A_rdiv_Bc!(Uc, conj(D)) ≈ target atol=atol_three + @test A_ldiv_B!(D, eye(D)) ≈ D\eye(D) atol=atol_three + @test_throws DimensionMismatch A_ldiv_B!(D, ones(elty, n + 1)) + @test_throws SingularException A_ldiv_B!(Diagonal(zeros(relty, n)), copy(v)) + b = rand(elty, n, n) + b = sparse(b) + @test A_ldiv_B!(D, copy(b)) ≈ Array(D)\Array(b) + @test_throws SingularException A_ldiv_B!(Diagonal(zeros(elty, n)), copy(b)) + b = view(rand(elty, n), collect(1:n)) + b2 = copy(b) + c = A_ldiv_B!(D, b) + d = Array(D)\b2 + @test c ≈ d + @test_throws SingularException A_ldiv_B!(Diagonal(zeros(elty, n)), b) + b = rand(elty, n+1, n+1) + b = sparse(b) + @test_throws DimensionMismatch A_ldiv_B!(D, copy(b)) + b = view(rand(elty, n+1), collect(1:n+1)) + @test_throws DimensionMismatch A_ldiv_B!(D, b) end end end @@ -163,15 +151,15 @@ srand(1) @test D\D2 ≈ Diagonal(D2.diag./D.diag) # Performance specialisations for A*_mul_B! - vv = similar(v) - @test (r = full(D) * v ; A_mul_B!(vv, D, v) ≈ r ≈ vv) - @test (r = full(D)' * v ; Ac_mul_B!(vv, D, v) ≈ r ≈ vv) - @test (r = full(D).' * v ; At_mul_B!(vv, D, v) ≈ r ≈ vv) + vvv = similar(vv) + @test (r = full(D) * vv ; A_mul_B!(vvv, D, vv) ≈ r ≈ vvv) + @test (r = full(D)' * vv ; Ac_mul_B!(vvv, D, vv) ≈ r ≈ vvv) + @test (r = full(D).' * vv ; At_mul_B!(vvv, D, vv) ≈ r ≈ vvv) - UU = similar(U) - @test (r = full(D) * U ; A_mul_B!(UU, D, U) ≈ r ≈ UU) - @test (r = full(D)' * U ; Ac_mul_B!(UU, D, U) ≈ r ≈ UU) - @test (r = full(D).' * U ; At_mul_B!(UU, D, U) ≈ r ≈ UU) + UUU = similar(UU) + @test (r = full(D) * UU ; A_mul_B!(UUU, D, UU) ≈ r ≈ UUU) + @test (r = full(D)' * UU ; Ac_mul_B!(UUU, D, UU) ≈ r ≈ UUU) + @test (r = full(D).' * UU ; At_mul_B!(UUU, D, UU) ≈ r ≈ UUU) # make sure that A_mul_B{c,t}! works with B as a Diagonal VV = Array(D) @@ -225,8 +213,8 @@ srand(1) @test adjoint(D) == conj(D) end # Translates to Ac/t_mul_B, which is specialized after issue 21286 - @test(D' * v == conj(D) * v) - @test(D.' * v == D * v) + @test(D' * vv == conj(D) * vv) + @test(D.' * vv == D * vv) end #logdet diff --git a/test/linalg/eigen.jl b/test/linalg/eigen.jl index 5e6ec48db16f2..edfa42c7e7991 100644 --- a/test/linalg/eigen.jl +++ b/test/linalg/eigen.jl @@ -19,14 +19,10 @@ aimg = randn(n,n)/2 aa = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(areal, aimg) : areal) asym = aa'+aa # symmetric indefinite apd = aa'*aa # symmetric positive-definite - @testset for atype in ("Array", "SubArray") - if atype == "Array" - a = aa - else - a = view(aa, 1:n, 1:n) - asym = view(asym, 1:n, 1:n) - apd = view(apd, 1:n, 1:n) - end + for (a, asym, apd) in ((aa, asym, apd), + (view(aa, 1:n, 1:n), + view(asym, 1:n, 1:n), + view(apd, 1:n, 1:n))) ε = εa = eps(abs(float(one(eltya)))) α = rand(eltya) @@ -56,7 +52,7 @@ aimg = randn(n,n)/2 @test_throws DomainError eigmax(a - a') end @testset "symmetric generalized eigenproblem" begin - if atype == "Array" + if isa(a, Array) asym_sg = asym[1:n1, 1:n1] a_sg = a[:,n1+1:n2] else @@ -77,7 +73,7 @@ aimg = randn(n,n)/2 @test v == f[:vectors] end @testset "Non-symmetric generalized eigenproblem" begin - if atype == "Array" + if isa(a, Array) a1_nsg = a[1:n1, 1:n1] a2_nsg = a[n1+1:n2, n1+1:n2] else @@ -110,12 +106,7 @@ end # test a matrix larger than 140-by-140 for #14174 let aa = rand(200, 200) - for atype in ("Array", "SubArray") - if atype == "Array" - a = aa - else - a = view(aa, 1:n, 1:n) - end + for a in (aa, view(aa, 1:n, 1:n)) f = eigfact(a) @test a ≈ f[:vectors] * Diagonal(f[:values]) / f[:vectors] end diff --git a/test/linalg/lu.jl b/test/linalg/lu.jl index dd5a84b7904d1..bcb09e2bf9ead 100644 --- a/test/linalg/lu.jl +++ b/test/linalg/lu.jl @@ -69,23 +69,16 @@ dimg = randn(n)/2 lstring = sprint(show,l) ustring = sprint(show,u) @test sprint(show,lua) == "$(typeof(lua)) with factors L and U:\n$lstring\n$ustring\nsuccessful: true" - let Bs = b, Cs = c - @testset for atype in ("Array", "SubArray") - if atype == "Array" - b = Bs - c = Cs - else - b = view(Bs, 1:n, 1) - c = view(Cs, 1:n) - end - @test norm(a*(lua\b) - b, 1) < ε*κ*n*2 # Two because the right hand side has two columns - @test norm(a'*(lua'\b) - b, 1) < ε*κ*n*2 # Two because the right hand side has two columns + let Bs = copy(b), Cs = copy(c) + for (bb, cc) in ((Bs, Cs), (view(Bs, 1:n, 1), view(Cs, 1:n))) + @test norm(a*(lua\bb) - bb, 1) < ε*κ*n*2 # Two because the right hand side has two columns + @test norm(a'*(lua'\bb) - bb, 1) < ε*κ*n*2 # Two because the right hand side has two columns @test norm(a'*(lua'\a') - a', 1) < ε*κ*n^2 - @test norm(a*(lua\c) - c, 1) < ε*κ*n # c is a vector - @test norm(a'*(lua'\c) - c, 1) < ε*κ*n # c is a vector + @test norm(a*(lua\cc) - cc, 1) < ε*κ*n # cc is a vector + @test norm(a'*(lua'\cc) - cc, 1) < ε*κ*n # cc is a vector @test AbstractArray(lua) ≈ a - @test norm(a.'*(lua.'\b) - b,1) < ε*κ*n*2 # Two because the right hand side has two columns - @test norm(a.'*(lua.'\c) - c,1) < ε*κ*n + @test norm(a.'*(lua.'\bb) - bb,1) < ε*κ*n*2 # Two because the right hand side has two columns + @test norm(a.'*(lua.'\cc) - cc,1) < ε*κ*n end # Test whether Ax_ldiv_B!(y, LU, x) indeed overwrites y @@ -128,23 +121,17 @@ dimg = randn(n)/2 @test_throws DimensionMismatch lud.'\f @test_throws DimensionMismatch lud'\f @test_throws DimensionMismatch Base.LinAlg.At_ldiv_B!(lud, f) - let Bs = b - @testset for atype in ("Array", "SubArray") - if atype == "Array" - b = Bs - else - b = view(Bs, 1:n, 1) - end - - @test norm(d*(lud\b) - b, 1) < ε*κd*n*2 # Two because the right hand side has two columns + let Bs = copy(b) + for bb in (Bs, view(Bs, 1:n, 1)) + @test norm(d*(lud\bb) - bb, 1) < ε*κd*n*2 # Two because the right hand side has two columns if eltya <: Real - @test norm((lud.'\b) - Array(d.')\b, 1) < ε*κd*n*2 # Two because the right hand side has two columns + @test norm((lud.'\bb) - Array(d.')\bb, 1) < ε*κd*n*2 # Two because the right hand side has two columns if eltya != Int && eltyb != Int - @test norm(Base.LinAlg.At_ldiv_B!(lud, copy(b)) - Array(d.')\b, 1) < ε*κd*n*2 + @test norm(Base.LinAlg.At_ldiv_B!(lud, copy(bb)) - Array(d.')\bb, 1) < ε*κd*n*2 end end if eltya <: Complex - @test norm((lud'\b) - Array(d')\b, 1) < ε*κd*n*2 # Two because the right hand side has two columns + @test norm((lud'\bb) - Array(d')\bb, 1) < ε*κd*n*2 # Two because the right hand side has two columns end end end @@ -201,12 +188,7 @@ end @test l[invperm(p),:]*u ≈ a @test a*inv(lua) ≈ eye(n) let Bs = b - for atype in ("Array", "SubArray") - if atype == "Array" - b = Bs - else - b = view(Bs, 1:n, 1) - end + for b in (Bs, view(Bs, 1:n, 1)) @test a*(lua\b) ≈ b end end diff --git a/test/linalg/matmul.jl b/test/linalg/matmul.jl index 5e27672df6a0d..3b8f6d18257e9 100644 --- a/test/linalg/matmul.jl +++ b/test/linalg/matmul.jl @@ -23,16 +23,13 @@ let BB = [5 6; 7 8] AAi = AA+(0.5*im).*BB BBi = BB+(2.5*im).*AA[[2,1],[2,1]] - for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : view(AA, 1:2, 1:2) - B = Btype == "Array" ? BB : view(BB, 1:2, 1:2) + for A in (copy(AA), view(AA, 1:2, 1:2)), B in (copy(BB), view(BB, 1:2, 1:2)) @test A*B == [19 22; 43 50] @test At_mul_B(A, B) == [26 30; 38 44] @test A_mul_Bt(A, B) == [17 23; 39 53] @test At_mul_Bt(A, B) == [23 31; 34 46] - - Ai = Atype == "Array" ? AAi : view(AAi, 1:2, 1:2) - Bi = Btype == "Array" ? BBi : view(BBi, 1:2, 1:2) + end + for Ai in (copy(AAi), view(AAi, 1:2, 1:2)), Bi in (copy(BBi), view(BBi, 1:2, 1:2)) @test Ai*Bi == [-21+53.5im -4.25+51.5im; -12+95.5im 13.75+85.5im] @test Ac_mul_B(Ai, Bi) == [68.5-12im 57.5-28im; 88-3im 76.5-25im] @test A_mul_Bc(Ai, Bi) == [64.5+5.5im 43+31.5im; 104-18.5im 80.5+31.5im] @@ -48,16 +45,13 @@ let BB = [1 0 5; 6 -10 3; 2 -4 -1] AAi = AA+(0.5*im).*BB BBi = BB+(2.5*im).*AA[[2,1,3],[2,3,1]] - for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : view(AA, 1:3, 1:3) - B = Btype == "Array" ? BB : view(BB, 1:3, 1:3) + for A in (copy(AA), view(AA, 1:3, 1:3)), B in (copy(BB), view(BB, 1:3, 1:3)) @test A*B == [-26 38 -27; 1 -4 -6; 28 -46 15] @test Ac_mul_B(A, B) == [-6 2 -25; 3 -12 -18; 12 -26 -11] @test A_mul_Bc(A, B) == [-14 0 6; 4 -3 -3; 22 -6 -12] @test Ac_mul_Bc(A, B) == [6 -8 -6; 12 -9 -9; 18 -10 -12] - - Ai = Atype == "Array" ? AAi : view(AAi, 1:3, 1:3) - Bi = Btype == "Array" ? BBi : view(BBi, 1:3, 1:3) + end + for Ai in (copy(AAi), view(AAi, 1:3, 1:3)), Bi in (copy(BBi), view(BBi, 1:3, 1:3)) @test Ai*Bi == [-44.75+13im 11.75-25im -38.25+30im; -47.75-16.5im -51.5+51.5im -56+6im; 16.75-4.5im -53.5+52im -15.5im] @test Ac_mul_B(Ai, Bi) == [-21+2im -1.75+49im -51.25+19.5im; 25.5+56.5im -7-35.5im 22+35.5im; -3+12im -32.25+43im -34.75-2.5im] @test A_mul_Bc(Ai, Bi) == [-20.25+15.5im -28.75-54.5im 22.25+68.5im; -12.25+13im -15.5+75im -23+27im; 18.25+im 1.5+94.5im -27-54.5im] @@ -86,26 +80,19 @@ end let AA = [1 2 3; 4 5 6] .- 3 BB = [2 -2; 3 -5; -4 7] - for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : view(AA, 1:2, 1:3) - B = Btype == "Array" ? BB : view(BB, 1:3, 1:2) + for A in (copy(AA), view(AA, 1:2, 1:3)), B in (copy(BB), view(BB, 1:3, 1:2)) @test A*B == [-7 9; -4 9] @test At_mul_Bt(A, B) == [-6 -11 15; -6 -13 18; -6 -15 21] end AA = ones(Int, 2, 100) BB = ones(Int, 100, 3) - for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : view(AA, 1:2, 1:100) - B = Btype == "Array" ? BB : view(BB, 1:100, 1:3) + for A in (copy(AA), view(AA, 1:2, 1:100)), B in (copy(BB), view(BB, 1:100, 1:3)) @test A*B == [100 100 100; 100 100 100] end AA = rand(1:20, 5, 5) .- 10 BB = rand(1:20, 5, 5) .- 10 CC = Array{Int}(size(AA, 1), size(BB, 2)) - for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"], Ctype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : view(AA, 1:5, 1:5) - B = Btype == "Array" ? BB : view(BB, 1:5, 1:5) - C = Btype == "Array" ? CC : view(CC, 1:5, 1:5) + for A in (copy(AA), view(AA, 1:5, 1:5)), B in (copy(BB), view(BB, 1:5, 1:5)), C in (copy(CC), view(CC, 1:5, 1:5)) @test At_mul_B(A, B) == A'*B @test A_mul_Bt(A, B) == A*B' # Preallocated @@ -121,9 +108,7 @@ let end vv = [1,2] CC = Array{Int}(2, 2) - for vtype = ["Array", "SubArray"], Ctype = ["Array", "SubArray"] - v = vtype == "Array" ? vv : view(vv, 1:2) - C = Ctype == "Array" ? CC : view(CC, 1:2, 1:2) + for v in (copy(vv), view(vv, 1:2)), C in (copy(CC), view(CC, 1:2, 1:2)) @test @inferred(A_mul_Bc!(C, v, v)) == [1 2; 2 4] end end @@ -132,24 +117,18 @@ end let AA = rand(5,5) BB = rand(5) - for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : view(AA, 1:5, 1:5) - B = Btype == "Array" ? BB : view(BB, 1:5) + for A in (copy(AA), view(AA, 1:5, 1:5)), B in (copy(BB), view(BB, 1:5)) @test_throws DimensionMismatch Base.LinAlg.generic_matvecmul!(zeros(6),'N',A,B) @test_throws DimensionMismatch Base.LinAlg.generic_matvecmul!(B,'N',A,zeros(6)) end vv = [1,2,3] CC = Array{Int}(3, 3) - for vtype = ["Array", "SubArray"], Ctype = ["Array", "SubArray"] - v = vtype == "Array" ? vv : view(vv, 1:3) - C = Ctype == "Array" ? CC : view(CC, 1:3, 1:3) + for v in (copy(vv), view(vv, 1:3)), C in (copy(CC), view(CC, 1:3, 1:3)) @test A_mul_Bt!(C, v, v) == v*v' end vvf = map(Float64,vv) CC = Array{Float64}(3, 3) - for vtype = ["Array", "SubArray"], Ctype = ["Array", "SubArray"] - vf = vtype == "Array" ? vvf : view(vvf, 1:3) - C = Ctype == "Array" ? CC : view(CC, 1:3, 1:3) + for vf in (copy(vvf), view(vvf, 1:3)), C in (copy(CC), view(CC, 1:3, 1:3)) @test A_mul_Bt!(C, vf, vf) == vf*vf' end end @@ -159,10 +138,7 @@ let AA = rand(Float64,6,6) BB = rand(Float64,6,6) CC = zeros(Float64,6,6) - for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"], Ctype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : view(AA, 1:6, 1:6) - B = Btype == "Array" ? BB : view(BB, 1:6, 1:6) - C = Ctype == "Array" ? CC : view(CC, 1:6, 1:6) + for A in (copy(AA), view(AA, 1:6, 1:6)), B in (copy(BB), view(BB, 1:6, 1:6)), C in (copy(CC), view(CC, 1:6, 1:6)) @test Base.LinAlg.At_mul_Bt!(C,A,B) == A.'*B.' @test Base.LinAlg.A_mul_Bc!(C,A,B) == A*B.' @test Base.LinAlg.Ac_mul_B!(C,A,B) == A.'*B @@ -202,8 +178,7 @@ end let AA = reshape(1:1503, 501, 3).-750.0 res = Float64[135228751 9979252 -115270247; 9979252 10481254 10983256; -115270247 10983256 137236759] - for Atype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : view(AA, 1:501, 1:3) + for A in (copy(AA), view(AA, 1:501, 1:3)) @test At_mul_B(A, A) == res @test A_mul_Bt(A',A') == res end @@ -224,8 +199,7 @@ end # matmul for types w/o sizeof (issue #1282) let AA = fill(complex(1,1), 10, 10) - for Atype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : view(AA, 1:10, 1:10) + for A in (copy(AA), view(AA, 1:10, 1:10)) A2 = A^2 @test A2[1,1] == 20im end @@ -235,14 +209,8 @@ let AA = zeros(5, 5) BB = ones(5) CC = rand(5, 6) - for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - for Ctype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : view(AA, 1:5, 1:5) - B = Btype == "Array" ? BB : view(BB, 1:5) - C = Ctype == "Array" ? CC : view(CC, 1:5, 1:6) - - @test_throws DimensionMismatch scale!(A, B, C) - end + for A in (copy(AA), view(AA, 1:5, 1:5)), B in (copy(BB), view(BB, 1:5)), C in (copy(CC), view(CC, 1:5, 1:6)) + @test_throws DimensionMismatch scale!(A, B, C) end end @@ -265,9 +233,7 @@ end vecdot_(x,y) = invoke(vecdot, Tuple{Any,Any}, x,y) # generic vecdot let AA = [1+2im 3+4im; 5+6im 7+8im], BB = [2+7im 4+1im; 3+8im 6+5im] - for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : view(AA, 1:2, 1:2) - B = Btype == "Array" ? BB : view(BB, 1:2, 1:2) + for A in (copy(AA), view(AA, 1:2, 1:2)), B in (copy(BB), view(BB, 1:2, 1:2)) @test vecdot(A,B) == dot(vec(A),vec(B)) == vecdot_(A,B) == vecdot(float.(A),float.(B)) @test vecdot(Int[], Int[]) == 0 == vecdot_(Int[], Int[]) @test_throws MethodError vecdot(Any[], Any[]) @@ -315,9 +281,7 @@ end let aa = rand(3,3) bb = rand(3,3) - for atype = ["Array", "SubArray"], btype = ["Array", "SubArray"] - a = atype == "Array" ? aa : view(aa, 1:3, 1:3) - b = btype == "Array" ? bb : view(bb, 1:3, 1:3) + for a in (copy(aa), view(aa, 1:3, 1:3)), b in (copy(bb), view(bb, 1:3, 1:3)) @test_throws ArgumentError A_mul_B!(a, a, b) @test_throws ArgumentError A_mul_B!(a, b, a) @test_throws ArgumentError A_mul_B!(a, a, a) diff --git a/test/linalg/schur.jl b/test/linalg/schur.jl index dd88bda0c0554..53350074e2245 100644 --- a/test/linalg/schur.jl +++ b/test/linalg/schur.jl @@ -19,14 +19,10 @@ aimg = randn(n,n)/2 a = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(areal, aimg) : areal) asym = a'+a # symmetric indefinite apd = a'*a # symmetric positive-definite - @testset for atype in ("Array", "SubArray") - if atype == "Array" - a = a - else - a = view(a, 1:n, 1:n) - asym = view(asym, 1:n, 1:n) - apd = view(apd, 1:n, 1:n) - end + for (a, asym, apd) in ((a, asym, apd), + (view(a, 1:n, 1:n), + view(asym, 1:n, 1:n), + view(apd, 1:n, 1:n))) ε = εa = eps(abs(float(one(eltya)))) d,v = eig(a) @@ -67,7 +63,7 @@ aimg = randn(n,n)/2 @test O[:Schur] ≈ SchurNew[:Schur] end - if atype == "Array" + if isa(a, Array) a1_sf = a[1:n1, 1:n1] a2_sf = a[n1+1:n2, n1+1:n2] else diff --git a/test/linalg/svd.jl b/test/linalg/svd.jl index d85d4eae26f6e..aa135c8448793 100644 --- a/test/linalg/svd.jl +++ b/test/linalg/svd.jl @@ -47,14 +47,7 @@ a2img = randn(n,n)/2 aa2 = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(a2real, a2img) : a2real) asym = aa'+aa # symmetric indefinite apd = aa'*aa # symmetric positive-definite - @testset for atype in ("Array", "SubArray") - if atype == "Array" - a = aa - a2 = aa2 - else - a = view(aa, 1:n, 1:n) - a2 = view(aa2, 1:n, 1:n) - end + for (a, a2) in ((aa, aa2), (view(aa, 1:n, 1:n), view(aa2, 1:n, 1:n))) ε = εa = eps(abs(float(one(eltya)))) usv = svdfact(a) diff --git a/test/linalg/uniformscaling.jl b/test/linalg/uniformscaling.jl index e745e3ac1f441..88fb277b7b201 100644 --- a/test/linalg/uniformscaling.jl +++ b/test/linalg/uniformscaling.jl @@ -76,15 +76,7 @@ B = bitrand(2,2) @testset "binary ops with matrices" begin let AA = randn(2, 2) for SS in (sprandn(3,3, 0.5), speye(Int, 3)) - @testset for atype in ("Array", "SubArray") - if atype == "Array" - A = AA - S = SS - else - A = view(AA, 1:2, 1:2) - S = view(SS, 1:3, 1:3) - end - + for (A, S) in ((AA, SS), (view(AA, 1:2, 1:2), view(SS, 1:3, 1:3))) @test @inferred(A + I) == A + eye(A) @test @inferred(I + A) == A + eye(A) @test @inferred(I - I) === UniformScaling(0) @@ -113,7 +105,7 @@ B = bitrand(2,2) @test @inferred(I/λ) === UniformScaling(1/λ) @test @inferred(I\J) === J - if atype == "Array" + if isa(A, Array) T = LowerTriangular(randn(3,3)) else T = LowerTriangular(view(randn(3,3), 1:3, 1:3)) @@ -124,7 +116,7 @@ B = bitrand(2,2) @test @inferred(J - T) == J - full(T) @test @inferred(T\I) == inv(T) - if atype == "Array" + if isa(A, Array) T = LinAlg.UnitLowerTriangular(randn(3,3)) else T = LinAlg.UnitLowerTriangular(view(randn(3,3), 1:3, 1:3)) @@ -135,7 +127,7 @@ B = bitrand(2,2) @test @inferred(J - T) == J - full(T) @test @inferred(T\I) == inv(T) - if atype == "Array" + if isa(A, Array) T = UpperTriangular(randn(3,3)) else T = UpperTriangular(view(randn(3,3), 1:3, 1:3)) @@ -146,7 +138,7 @@ B = bitrand(2,2) @test @inferred(J - T) == J - full(T) @test @inferred(T\I) == inv(T) - if atype == "Array" + if isa(A, Array) T = LinAlg.UnitUpperTriangular(randn(3,3)) else T = LinAlg.UnitUpperTriangular(view(randn(3,3), 1:3, 1:3))