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

Code before:
end

@testset "issue #30814: Symmetric of Hermitian if diag is not real" begin
    A = [1 2; 3 4] * (1 + im)
    B = Hermitian(A)
    @test_throws ArgumentError Symmetric(B) == Symmetric(Matrix(B))
    A[1,1] = 1; A[2,2] = 4
    @test Symmetric(B) == Symmetric(Matrix(B))
end

@testset "symmetric()/hermitian() for Numbers" begin
    @test LinearAlgebra.symmetric(1, :U) == 1
    @test LinearAlgebra.symmetric_type(Int) == Int
    @test LinearAlgebra.hermitian(1, :U) == 1
    @test LinearAlgebra.hermitian_type(Int) == Int
end

@testset "symmetrize" begin
    @testset "in-place" begin
        for T in [Float32, Complex{Float32}], conj in [true, false], offset in [true, false]
            X = rand(T, 4, 4)
            offset && (X = OffsetArray(X, -2, -2))
            Y = copy(X)
            symmetrize!(X, conjugate=conj)
            if conj
                @test X ≈ (Y + Y') / 2
                @test ishermitian(X)
            else
                @test X ≈ (Y + transpose(Y)) / 2
                @test issymmetric(X)
            end
        end
    end
    @testset "out-of-place" begin
        for conj in [true, false]
            for T in [Float32, Complex{Float32}], offset in [true, false]
                X = randn(T, 4, 4)
                offset && (X = OffsetArray(X, -2, -2))
                Y = symmetrize(X, conjugate=conj)
                @test eltype(Y) === T
                if conj
                    @test Y ≈ (X + X') / 2
                    @test ishermitian(Y)
                else
                    @test Y ≈ (X + transpose(X)) / 2
                    @test issymmetric(Y)
                end
            end
            for T in [Int32, Rational{Int32}, Complex{Int32}, Complex{Rational{Int32}}]
                X = T[1 2 3; 4 5 6; 7 8 9]
                Y = symmetrize(X, conjugate=conj)

Review comment (bug):
Shouldn't this be `==`?   `x - x == 0` and `(x + x) / 2 == x` should always be true for finite `x` without overflow, right?

Code after:
    A = rand(Float32, 3, 3)
    @test_throws TypeError Symmetric{Float64,Matrix{Float32}}(A, 'U')
    @test_throws TypeError Hermitian{Float64,Matrix{Float32}}(A, 'U')
end

@testset "fill[stored]!" begin
    for uplo in (:U, :L)
        # Hermitian
        A = Hermitian(fill(1.0+0im, 2, 2), uplo)
        @test fill!(A, 2) == fill(2, 2, 2)
        @test A.data == (uplo === :U ? [2 2; 1.0+0im 2] : [2 1.0+0im; 2 2])
        @test_throws ArgumentError fill!(A, 2+im)

        # Symmetric
        A = Symmetric(fill(1.0+im, 2, 2), uplo)
        @test fill!(A, 2) == fill(2, 2, 2)
        @test A.data == (uplo === :U ? [2 2; 1.0+im 2] : [2 1.0+im; 2 2])
    end
end

@testset "#25625 recursive transposition" begin
    A = Matrix{Matrix{Int}}(undef, 2, 2)
    A[1,1] = [1 2; 2 3]
    A[1,2] = [4 5 6; 7 8 9]
    A[2,1] = [4 7; 5 8; 6 9]
    A[2,2] = [1 2; 3 4]
    for uplo in (:U, :L)
        S = Symmetric(A, uplo)
        @test S[1,1] == A[1,1]
        @test S[1,2] == transpose(S[2,1]) == A[1,2]
        @test S[2,2] == Symmetric(A[2,2], uplo)
        @test S == transpose(S) == Matrix(S) == Matrix(transpose(S)) == transpose(Matrix(S))
    end

    B = Matrix{Matrix{Complex{Int}}}(undef, 2, 2)
    B[1,1] = [1 2+im; 2-im 3]
    B[1,2] = [4 5+1im 6-2im; 7+3im 8-4im 9+5im]
    B[2,1] = [4 7-3im; 5-1im 8+4im; 6+2im 9-5im]
    B[2,2] = [1+1im 2+2im; 3-3im 4-2im]
    for uplo in (:U, :L)
        H = Hermitian(B, uplo)
        @test H[1,1] == Hermitian(B[1,1], uplo)
        @test H[1,2] == adjoint(H[2,1]) == B[1,2]
        @test H[2,1] == adjoint(H[1,2]) == B[2,1]
        @test H[2,2] == Hermitian(B[2,2], uplo)
        @test H == adjoint(H) == Matrix(H) == Matrix(adjoint(H)) == adjoint(Matrix(H))
    end
end

@testset "getindex of diagonal element (#25972)" begin
    A = rand(ComplexF64, 2, 2)