Repository: JuliaLang/julia
File: stdlib/LinearAlgebra/test/symmetric.jl (Julia)

Code before:
@testset "Addition/subtraction with SymTridiagonal" begin
    TR = SymTridiagonal(randn(Float64,5), randn(Float64,4))
    TC = SymTridiagonal(randn(ComplexF64,5), randn(ComplexF64,4))
    SR = Symmetric(randn(Float64,5,5))
    SC = Symmetric(randn(ComplexF64,5,5))
    HR = Hermitian(randn(Float64,5,5))
    HC = Hermitian(randn(ComplexF64,5,5))
    for op = (+,-)
        for T = (TR, TC), S = (SR, SC)
            @test op(T, S) == op(Array(T), S)
            @test op(S, T) == op(S, Array(T))
            @test op(T, S) isa Symmetric
            @test op(S, T) isa Symmetric
        end
        for H = (HR, HC)
            for T = (TR, TC)
                @test op(T, H) == op(Array(T), H)
                @test op(H, T) == op(H, Array(T))
            end
            @test op(TR, H) isa Hermitian
            @test op(H, TR) isa Hermitian
        end
    end
end

@testset "symmetric and hermitian part" begin
    for T in [Float32, Complex{Float32}, Int32, Rational{Int32},
              Complex{Int32}, Complex{Rational{Int32}}]
        f, f!, t = hermitianpart, hermitianpart!, T <: Real ? transpose : adjoint
        X = T[1 2 3; 4 5 6; 7 8 9]
        T <: Complex && (X .+= im .* X)
        Xc = copy(X)
        Y = (X + t(X)) / 2
        U = f(X)
        L = f(X, :L)
        @test U isa Hermitian
        @test L isa Hermitian
        @test U.uplo == 'U'
        @test L.uplo == 'L'
        @test U == L == Y
        if T <: AbstractFloat || real(T) <: AbstractFloat
            HU = f!(X)
            @test HU == Y
            @test triu(X) == triu(Y)
            HL = f!(Xc, :L)
            @test HL == Y
            @test tril(Xc) == tril(Y)
        end
    end
    @test_throws DimensionMismatch hermitianpart(ones(1,2))
    for T in (Float64, ComplexF64), uplo in (:U, :L)

Review comment (suggestion):
```suggestion
@testset "hermitian part" begin
```

Code after:
    end
    let Ahrs = adjoint(Hermitian(Symmetric(rand(T, 3, 3)))),
        Acs = adjoint(Symmetric(rand(S, 3, 3))),
        Ahcs = adjoint(Hermitian(Symmetric(rand(S, 3, 3))))

        @test Ahrs * Ahrs ≈ Ahrs * Matrix(Ahrs)
        @test Ahcs * Ahcs ≈ Matrix(Ahcs) * Matrix(Ahcs)
        @test Ahrs * Ahcs ≈ Ahrs * Matrix(Ahcs)
        @test Acs * Ahcs ≈ Acs * Matrix(Ahcs)
        @test Ahcs * Ahrs ≈ Matrix(Ahcs) * Ahrs
        @test Ahcs * Acs ≈ Matrix(Ahcs) * Acs
    end
end

@testset "Addition/subtraction with SymTridiagonal" begin
    TR = SymTridiagonal(randn(Float64,5), randn(Float64,4))
    TC = SymTridiagonal(randn(ComplexF64,5), randn(ComplexF64,4))
    SR = Symmetric(randn(Float64,5,5))
    SC = Symmetric(randn(ComplexF64,5,5))
    HR = Hermitian(randn(Float64,5,5))
    HC = Hermitian(randn(ComplexF64,5,5))
    for op = (+,-)
        for T = (TR, TC), S = (SR, SC)
            @test op(T, S) == op(Array(T), S)
            @test op(S, T) == op(S, Array(T))
            @test op(T, S) isa Symmetric
            @test op(S, T) isa Symmetric
        end
        for H = (HR, HC)
            for T = (TR, TC)
                @test op(T, H) == op(Array(T), H)
                @test op(H, T) == op(H, Array(T))
            end
            @test op(TR, H) isa Hermitian
            @test op(H, TR) isa Hermitian
        end
    end
end

@testset "hermitian part" begin
    for T in [Float32, Complex{Float32}, Int32, Rational{Int32},
              Complex{Int32}, Complex{Rational{Int32}}]
        f, f!, t = hermitianpart, hermitianpart!, T <: Real ? transpose : adjoint
        X = T[1 2 3; 4 5 6; 7 8 9]
        T <: Complex && (X .+= im .* X)
        Xc = copy(X)
        Y = (X + t(X)) / 2
        U = f(X)
        L = f(X, :L)
        @test U isa Hermitian
        @test L isa Hermitian