Skip to content

Commit

Permalink
Moved things out of nested loop that don't need to be there
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt committed Aug 17, 2016
1 parent 329cfa9 commit e745d7f
Showing 1 changed file with 92 additions and 92 deletions.
184 changes: 92 additions & 92 deletions test/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
debug && println("Linear solve")
@test_approx_eq_eps D*v DM*v n*eps(relty)*(elty<:Complex ? 2:1)
@test_approx_eq_eps D*U DM*U n^2*eps(relty)*(elty<:Complex ? 2:1)

@test U.'*D U.'*full(D)
@test U'*D U'*full(D)

if relty != BigFloat
@test_approx_eq_eps D\v DM\v 2n^2*eps(relty)*(elty<:Complex ? 2:1)
@test_approx_eq_eps D\U DM\U 2n^3*eps(relty)*(elty<:Complex ? 2:1)
Expand All @@ -102,104 +106,100 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
b = view(rand(elty,n+1),collect(1:n+1))
@test_throws DimensionMismatch A_ldiv_B!(D,b)
end
end
end
debug && println("Binary operations")
d = convert(Vector{elty}, randn(n))
D2 = Diagonal(d)
DM2= diagm(d)
for op in (+, -, *)
@test full(op(D, D2)) op(DM, DM2)
end
# binary ops with plain numbers
a = rand()
@test full(a*D) a*DM
@test full(D*a) DM*a
@test full(D/a) DM/a
if relty <: BlasFloat
b = rand(elty,n,n)
b = sparse(b)
@test A_mul_B!(copy(D), copy(b)) full(D)*full(b)
@test At_mul_B!(copy(D), copy(b)) full(D).'*full(b)
@test Ac_mul_B!(copy(D), copy(b)) full(D)'*full(b)
end

debug && println("Binary operations")
d = convert(Vector{elty}, randn(n))
D2 = Diagonal(d)
DM2= diagm(d)
for op in (+, -, *)
@test full(op(D, D2)) op(DM, DM2)
end
# binary ops with plain numbers
a = rand()
@test full(a*D) a*DM
@test full(D*a) DM*a
@test full(D/a) DM/a
if relty <: BlasFloat
b = rand(elty,n,n)
b = sparse(b)
@test A_mul_B!(copy(D), copy(b)) full(D)*full(b)
@test At_mul_B!(copy(D), copy(b)) full(D).'*full(b)
@test Ac_mul_B!(copy(D), copy(b)) full(D)'*full(b)
end

#a few missing mults
bd = Bidiagonal(D2)
@test D*D2.' full(D)*full(D2).'
@test D2*D.' full(D2)*full(D).'
@test D2*D' full(D2)*full(D)'
#a few missing mults
bd = Bidiagonal(D2)
@test D*D2.' full(D)*full(D2).'
@test D2*D.' full(D2)*full(D).'
@test D2*D' full(D2)*full(D)'

@test U.'*D U.'*full(D)
@test U'*D U'*full(D)

#division of two Diagonals
@test D/D2 Diagonal(D.diag./D2.diag)
@test D\D2 Diagonal(D2.diag./D.diag)
# test triu/tril
@test istriu(D)
@test istril(D)
@test triu(D,1) == zeros(D)
@test triu(D,0) == D
@test triu(D,-1) == D
@test tril(D,1) == D
@test tril(D,-1) == zeros(D)
@test tril(D,0) == D
@test_throws ArgumentError tril(D,n+1)
@test_throws ArgumentError triu(D,n+1)

# factorize
@test factorize(D) == D

debug && println("Eigensystem")
eigD = eigfact(D)
@test Diagonal(eigD[:values]) D
@test eigD[:vectors] == eye(D)

debug && println("ldiv")
v = rand(n + 1)
@test_throws DimensionMismatch D\v
v = rand(n)
@test D\v DM\v
V = rand(n + 1, n)
@test_throws DimensionMismatch D\V
V = rand(n, n)
@test D\V DM\V

debug && println("conj and transpose")
@test transpose(D) == D
if elty <: BlasComplex
@test full(conj(D)) conj(DM)
@test ctranspose(D) == conj(D)
end
#division of two Diagonals
@test D/D2 Diagonal(D.diag./D2.diag)
@test D\D2 Diagonal(D2.diag./D.diag)
# test triu/tril
@test istriu(D)
@test istril(D)
@test triu(D,1) == zeros(D)
@test triu(D,0) == D
@test triu(D,-1) == D
@test tril(D,1) == D
@test tril(D,-1) == zeros(D)
@test tril(D,0) == D
@test_throws ArgumentError tril(D,n+1)
@test_throws ArgumentError triu(D,n+1)

# factorize
@test factorize(D) == D

debug && println("Eigensystem")
eigD = eigfact(D)
@test Diagonal(eigD[:values]) D
@test eigD[:vectors] == eye(D)

debug && println("ldiv")
v = rand(n + 1)
@test_throws DimensionMismatch D\v
v = rand(n)
@test D\v DM\v
V = rand(n + 1, n)
@test_throws DimensionMismatch D\V
V = rand(n, n)
@test D\V DM\V

debug && println("conj and transpose")
@test transpose(D) == D
if elty <: BlasComplex
@test full(conj(D)) conj(DM)
@test ctranspose(D) == conj(D)
end

#logdet
if relty <: Real
ld=convert(Vector{relty},rand(n))
@test logdet(Diagonal(ld)) logdet(diagm(ld))
end
#logdet
if relty <: Real
ld=convert(Vector{relty},rand(n))
@test logdet(Diagonal(ld)) logdet(diagm(ld))
end

#similar
@test isa(similar(D), Diagonal{elty})
@test isa(similar(D, Int), Diagonal{Int})
@test isa(similar(D, (3,2)), Matrix{elty})
@test isa(similar(D, Int, (3,2)), Matrix{Int})

#10036
@test issymmetric(D2)
@test ishermitian(D2)
if elty <: Complex
dc = d + im*convert(Vector{elty}, ones(n))
D3 = Diagonal(dc)
@test issymmetric(D3)
@test !ishermitian(D3)
end
#similar
@test isa(similar(D), Diagonal{elty})
@test isa(similar(D, Int), Diagonal{Int})
@test isa(similar(D, (3,2)), Matrix{elty})
@test isa(similar(D, Int, (3,2)), Matrix{Int})

U, s, V = svd(D)
@test (U*Diagonal(s))*V' D
@test svdvals(D) == s
@test svdfact(D)[:V] == V
end
#10036
@test issymmetric(D2)
@test ishermitian(D2)
if elty <: Complex
dc = d + im*convert(Vector{elty}, ones(n))
D3 = Diagonal(dc)
@test issymmetric(D3)
@test !ishermitian(D3)
end

U, s, V = svd(D)
@test (U*Diagonal(s))*V' D
@test svdvals(D) == s
@test svdfact(D)[:V] == V
end

D = Diagonal(Matrix{Float64}[randn(3,3), randn(2,2)])
Expand Down

0 comments on commit e745d7f

Please sign in to comment.