|
90 | 90 | @test_throws DivideError mod(3, 1:0)
|
91 | 91 | end
|
92 | 92 |
|
| 93 | +using LinearAlgebra |
| 94 | + |
| 95 | +@testset "generalized dot #32739" begin |
| 96 | + # stdlib/LinearAlgebra/test/generic.jl |
| 97 | + for elty in (Int, Float32, Float64, BigFloat, Complex{Float32}, Complex{Float64}, Complex{BigFloat}) |
| 98 | + n = 10 |
| 99 | + if elty <: Int |
| 100 | + A = rand(-n:n, n, n) |
| 101 | + x = rand(-n:n, n) |
| 102 | + y = rand(-n:n, n) |
| 103 | + elseif elty <: Real |
| 104 | + A = convert(Matrix{elty}, randn(n,n)) |
| 105 | + x = rand(elty, n) |
| 106 | + y = rand(elty, n) |
| 107 | + else |
| 108 | + A = convert(Matrix{elty}, complex.(randn(n,n), randn(n,n))) |
| 109 | + x = rand(elty, n) |
| 110 | + y = rand(elty, n) |
| 111 | + end |
| 112 | + @test dot(x, A, y) ≈ dot(A'x, y) ≈ *(x', A, y) ≈ (x'A)*y |
| 113 | + @test dot(x, A', y) ≈ dot(A*x, y) ≈ *(x', A', y) ≈ (x'A')*y |
| 114 | + elty <: Real && @test dot(x, transpose(A), y) ≈ dot(x, transpose(A)*y) ≈ *(x', transpose(A), y) ≈ (x'*transpose(A))*y |
| 115 | + B = reshape([A], 1, 1) |
| 116 | + x = [x] |
| 117 | + y = [y] |
| 118 | + @test dot(x, B, y) ≈ dot(B'x, y) |
| 119 | + @test dot(x, B', y) ≈ dot(B*x, y) |
| 120 | + elty <: Real && @test dot(x, transpose(B), y) ≈ dot(x, transpose(B)*y) |
| 121 | + end |
| 122 | + |
| 123 | + # stdlib/LinearAlgebra/test/symmetric.jl |
| 124 | + n = 10 |
| 125 | + areal = randn(n,n)/2 |
| 126 | + aimg = randn(n,n)/2 |
| 127 | + @testset for eltya in (Float32, Float64, ComplexF32, ComplexF64, BigFloat, Int) |
| 128 | + a = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(areal, aimg) : areal) |
| 129 | + asym = transpose(a) + a # symmetric indefinite |
| 130 | + aherm = a' + a # Hermitian indefinite |
| 131 | + apos = a' * a # Hermitian positive definite |
| 132 | + aposs = apos + transpose(apos) # Symmetric positive definite |
| 133 | + ε = εa = eps(abs(float(one(eltya)))) |
| 134 | + x = randn(n) |
| 135 | + y = randn(n) |
| 136 | + b = randn(n,n)/2 |
| 137 | + x = eltya == Int ? rand(1:7, n) : convert(Vector{eltya}, eltya <: Complex ? complex.(x, zeros(n)) : x) |
| 138 | + y = eltya == Int ? rand(1:7, n) : convert(Vector{eltya}, eltya <: Complex ? complex.(y, zeros(n)) : y) |
| 139 | + b = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(b, zeros(n,n)) : b) |
| 140 | + |
| 141 | + @testset "generalized dot product" begin |
| 142 | + for uplo in (:U, :L) |
| 143 | + @test dot(x, Hermitian(aherm, uplo), y) ≈ dot(x, Hermitian(aherm, uplo)*y) ≈ dot(x, Matrix(Hermitian(aherm, uplo)), y) |
| 144 | + @test dot(x, Hermitian(aherm, uplo), x) ≈ dot(x, Hermitian(aherm, uplo)*x) ≈ dot(x, Matrix(Hermitian(aherm, uplo)), x) |
| 145 | + end |
| 146 | + if eltya <: Real |
| 147 | + for uplo in (:U, :L) |
| 148 | + @test dot(x, Symmetric(aherm, uplo), y) ≈ dot(x, Symmetric(aherm, uplo)*y) ≈ dot(x, Matrix(Symmetric(aherm, uplo)), y) |
| 149 | + @test dot(x, Symmetric(aherm, uplo), x) ≈ dot(x, Symmetric(aherm, uplo)*x) ≈ dot(x, Matrix(Symmetric(aherm, uplo)), x) |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + # stdlib/LinearAlgebra/test/uniformscaling.jl |
| 156 | + @testset "generalized dot" begin |
| 157 | + x = rand(-10:10, 3) |
| 158 | + y = rand(-10:10, 3) |
| 159 | + λ = rand(-10:10) |
| 160 | + J = UniformScaling(λ) |
| 161 | + @test dot(x, J, y) == λ*dot(x, y) |
| 162 | + end |
| 163 | + |
| 164 | + # stdlib/LinearAlgebra/test/bidiag.jl |
| 165 | + # The special method for this is not in Compat #683, so this tests the generic fallback |
| 166 | + @testset "generalized dot" begin |
| 167 | + for elty in (Float64, ComplexF64) |
| 168 | + dv = randn(elty, 5) |
| 169 | + ev = randn(elty, 4) |
| 170 | + x = randn(elty, 5) |
| 171 | + y = randn(elty, 5) |
| 172 | + for uplo in (:U, :L) |
| 173 | + B = Bidiagonal(dv, ev, uplo) |
| 174 | + @test dot(x, B, y) ≈ dot(B'x, y) ≈ dot(x, Matrix(B), y) |
| 175 | + end |
| 176 | + end |
| 177 | + end |
| 178 | + |
| 179 | + # Diagonal -- no such test in Base. |
| 180 | + @testset "diagonal" begin |
| 181 | + x = rand(-10:10, 3) .+ im |
| 182 | + y = rand(-10:10, 3) .+ im |
| 183 | + d = Diagonal(rand(-10:10, 3) .+ im) |
| 184 | + @test dot(x,d,y) == dot(x,collect(d),y) == dot(x, d*y) |
| 185 | + end |
| 186 | +end |
| 187 | + |
93 | 188 | # https://github.com/JuliaLang/julia/pull/33568
|
94 | 189 | @testset "function composition" begin
|
95 | 190 | @test ∘(x -> x-2, x -> x-3, x -> x+5)(7) == 7
|
|
0 commit comments